STON question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

STON question

Stephane Ducasse-3
Hi sven

I'm coding with my son a game collector application :)
We want to save our collection with STON

So far we have

GameCollector asSTON >>

     String streamContents: [ :s | STON put: self onStreamPretty: s ]

and we get

GameCollector {
#items : OrderedCollection [
GameItem {
#name : 'Final Fantasy X',
#console : #PS2,
#hasDocumentation : false,
#hasBox : true,
#finished : true,
#condition : 'Good',
#quotation : 10,
#critics : 18,
#comments : 'quite cool in fact'
},
GameItem {
#name : 'Final Fantasy XII',
#console : '',
#hasDocumentation : true,
#hasBox : false,
#finished : false,
#condition : 'Good',
#quotation : 10,
#critics : 15,
#comments : ''
}
]
}

And this is good.
Now I wanted to use STON to save the items without the class structure

to get something like

OrderedCollection [
GameItem {
#name : 'Final Fantasy X',
#console : #PS2,
#hasDocumentation : false,
#hasBox : true,
#finished : true,
#condition : 'Good',
#quotation : 10,
#critics : 18,
#comments : 'quite cool in fact'
},
GameItem {
#name : 'Final Fantasy XII',
#console : '',
#hasDocumentation : true,
#hasBox : false,
#finished : false,
#condition : 'Good',
#quotation : 10,
#critics : 15,
#comments : ''
}
]


I coded as

GameCollector >> asSTON

     ^ String streamContents: [ :s | STON put: items onStreamPretty: s ]

and STON is not happy on reload.

I know that we are bending the rules.
Is there a way to do simply what I want?

I could leave also with

GameItem {
#name : 'Final Fantasy X',
#console : #PS2,
#hasDocumentation : false,
#hasBox : true,
#finished : true,
#condition : 'Good',
#quotation : 10,
#critics : 18,
#comments : 'quite cool in fact'
},
GameItem {
#name : 'Final Fantasy XII',
#console : '',
#hasDocumentation : true,
#hasBox : false,
#finished : false,
#condition : 'Good',
#quotation : 10,
#critics : 15,
#comments : ''
}

Stef

Reply | Threaded
Open this post in threaded view
|

Re: STON question

CyrilFerlicot
Le 30/07/2017 à 17:39, Stephane Ducasse a écrit :

> Hi sven
>
> I'm coding with my son a game collector application :)
> We want to save our collection with STON
>
> So far we have
>
> GameCollector asSTON >>
>
>      String streamContents: [ :s | STON put: self onStreamPretty: s ]
>
> and we get
>
> GameCollector {
> #items : OrderedCollection [
> GameItem {
> #name : 'Final Fantasy X',
> #console : #PS2,
> #hasDocumentation : false,
> #hasBox : true,
> #finished : true,
> #condition : 'Good',
> #quotation : 10,
> #critics : 18,
> #comments : 'quite cool in fact'
> },
> GameItem {
> #name : 'Final Fantasy XII',
> #console : '',
> #hasDocumentation : true,
> #hasBox : false,
> #finished : false,
> #condition : 'Good',
> #quotation : 10,
> #critics : 15,
> #comments : ''
> }
> ]
> }
>
> And this is good.
> Now I wanted to use STON to save the items without the class structure
>
> to get something like
>
> OrderedCollection [
> GameItem {
> #name : 'Final Fantasy X',
> #console : #PS2,
> #hasDocumentation : false,
> #hasBox : true,
> #finished : true,
> #condition : 'Good',
> #quotation : 10,
> #critics : 18,
> #comments : 'quite cool in fact'
> },
> GameItem {
> #name : 'Final Fantasy XII',
> #console : '',
> #hasDocumentation : true,
> #hasBox : false,
> #finished : false,
> #condition : 'Good',
> #quotation : 10,
> #critics : 15,
> #comments : ''
> }
> ]
>
>
> I coded as
>
> GameCollector >> asSTON
>
>      ^ String streamContents: [ :s | STON put: items onStreamPretty: s ]
>
> and STON is not happy on reload.
>
> I know that we are bending the rules.
> Is there a way to do simply what I want?
>
> I could leave also with
>
> GameItem {
> #name : 'Final Fantasy X',
> #console : #PS2,
> #hasDocumentation : false,
> #hasBox : true,
> #finished : true,
> #condition : 'Good',
> #quotation : 10,
> #critics : 18,
> #comments : 'quite cool in fact'
> },
> GameItem {
> #name : 'Final Fantasy XII',
> #console : '',
> #hasDocumentation : true,
> #hasBox : false,
> #finished : false,
> #condition : 'Good',
> #quotation : 10,
> #critics : 15,
> #comments : ''
> }
>
> Stef
>
Hi,

Maybe you can just create a new GameCollector on the reading.

"protocol: instance-creation"
GameCollector class>>importFromFile: aFile

  ^ self new
    items: (STON fromString: aFile contents);
    yourself



--
Cyril Ferlicot
https://ferlicot.fr

http://www.synectique.eu
2 rue Jacques Prévert 01,
59650 Villeneuve d'ascq France


signature.asc (836 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: STON question

Sven Van Caekenberghe-2
In reply to this post by Stephane Ducasse-3
First question is, why do anything at all ?

  STON toStringPretty: <your game collector object>

Then loading is as simple as

  STON fromString: '...'

Next step is to customise how STON output is generated (but again, why ?).

You do that by writing both an #stonOn: and a #fromSton: method (look for implementors for examples).

If you really want, you could get something like:

GameCollector [
GameItem {
#name : 'Final Fantasy X',
#console : #PS2,
#hasDocumentation : false,
#hasBox : true,
#finished : true,
#condition : 'Good',
#quotation : 10,
#critics : 18,
#comments : 'quite cool in fact'
},
GameItem {
#name : 'Final Fantasy XII',
#console : '',
#hasDocumentation : true,
#hasBox : false,
#finished : false,
#condition : 'Good',
#quotation : 10,
#critics : 15,
#comments : ''
}
]

to work.

Something like (untested):

GameCollector>>stonOn: stonWriter
        stonWriter writeObject: self do: [
                stonWriter encodeList: self items ]

GameCollector class>>fromSton: stonReader
        | collector |
        collector := self new.
        stonReader parseListDo: [ :each |
                collector addGameItem: each ].
        ^ collector

> On 30 Jul 2017, at 17:39, Stephane Ducasse <[hidden email]> wrote:
>
> Hi sven
>
> I'm coding with my son a game collector application :)
> We want to save our collection with STON
>
> So far we have
>
> GameCollector asSTON >>
>
>     String streamContents: [ :s | STON put: self onStreamPretty: s ]
>
> and we get
>
> GameCollector {
> #items : OrderedCollection [
> GameItem {
> #name : 'Final Fantasy X',
> #console : #PS2,
> #hasDocumentation : false,
> #hasBox : true,
> #finished : true,
> #condition : 'Good',
> #quotation : 10,
> #critics : 18,
> #comments : 'quite cool in fact'
> },
> GameItem {
> #name : 'Final Fantasy XII',
> #console : '',
> #hasDocumentation : true,
> #hasBox : false,
> #finished : false,
> #condition : 'Good',
> #quotation : 10,
> #critics : 15,
> #comments : ''
> }
> ]
> }
>
> And this is good.
> Now I wanted to use STON to save the items without the class structure
>
> to get something like
>
> OrderedCollection [
> GameItem {
> #name : 'Final Fantasy X',
> #console : #PS2,
> #hasDocumentation : false,
> #hasBox : true,
> #finished : true,
> #condition : 'Good',
> #quotation : 10,
> #critics : 18,
> #comments : 'quite cool in fact'
> },
> GameItem {
> #name : 'Final Fantasy XII',
> #console : '',
> #hasDocumentation : true,
> #hasBox : false,
> #finished : false,
> #condition : 'Good',
> #quotation : 10,
> #critics : 15,
> #comments : ''
> }
> ]
>
>
> I coded as
>
> GameCollector >> asSTON
>
>     ^ String streamContents: [ :s | STON put: items onStreamPretty: s ]
>
> and STON is not happy on reload.
>
> I know that we are bending the rules.
> Is there a way to do simply what I want?
>
> I could leave also with
>
> GameItem {
> #name : 'Final Fantasy X',
> #console : #PS2,
> #hasDocumentation : false,
> #hasBox : true,
> #finished : true,
> #condition : 'Good',
> #quotation : 10,
> #critics : 18,
> #comments : 'quite cool in fact'
> },
> GameItem {
> #name : 'Final Fantasy XII',
> #console : '',
> #hasDocumentation : true,
> #hasBox : false,
> #finished : false,
> #condition : 'Good',
> #quotation : 10,
> #critics : 15,
> #comments : ''
> }
>
> Stef
>


Reply | Threaded
Open this post in threaded view
|

Re: STON question

Stephane Ducasse-3
> First question is, why do anything at all ?
>
>   STON toStringPretty: <your game collector object>
>
> Then loading is as simple as
>
>   STON fromString: '...'

I know :)
Now I wanted to overuse STON to be able to edit manually the file too.


> Next step is to customise how STON output is generated (but again, why ?).
>
> You do that by writing both an #stonOn: and a #fromSton: method (look for implementors for examples).
>
> If you really want, you could get something like:
>
> GameCollector [
> GameItem {
> #name : 'Final Fantasy X',
> #console : #PS2,
> #hasDocumentation : false,
> #hasBox : true,
> #finished : true,
> #condition : 'Good',
> #quotation : 10,
> #critics : 18,
> #comments : 'quite cool in fact'
> },
> GameItem {
> #name : 'Final Fantasy XII',
> #console : '',
> #hasDocumentation : true,
> #hasBox : false,
> #finished : false,
> #condition : 'Good',
> #quotation : 10,
> #critics : 15,
> #comments : ''
> }
> ]
>
> to work.
>
> Something like (untested):
>
> GameCollector>>stonOn: stonWriter
>         stonWriter writeObject: self do: [
>                 stonWriter encodeList: self items ]
>
> GameCollector class>>fromSton: stonReader
>         | collector |
>         collector := self new.
>         stonReader parseListDo: [ :each |
>                 collector addGameItem: each ].
>         ^ collector

Thanks this is what I was looking for. :)