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 <stepharo.self@gmail.com> 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