Hi,

By studying the NeoJSON book chapter (Pharo Enterprise), I do not understand 
how to modify the following example:

-=-=-=-=-=-=-=-
"Let us say that we have an Attachment class..."

Object subclass: #Attachment
        instanceVariableNames: 'url fileName'
        classVariableNames: ''
        package: 'NeoJSON-Use-Case'.

"...with url: and fileName: methods."
Attachment compile: 'url: anObject', String cr, String tab, 'url := anObject' 
classified: 'accessing'.
Attachment compile: 'fileName: anObject', String cr, String tab, 'fileName := 
anObject' classified: 'accessing'.
        
"Let's create a collection of two instances:"
collectionOne := { 
        Attachment new 
                url: 'http://example.com/random-name.txt' asZnUrl; 
                fileName: 'chapter-one.txt'
                yourself.
        Attachment new 
                url: 'http://example.com/random-name.png' asZnUrl; 
                fileName: 'image.png';
                yourself.
}.

"And let's map it to a JSON structure:"
String streamContents: [ :aStream |
        (NeoJSONWriter on: aStream)
                for: #CollectionOfAttachments customDo: [ :mapping | 
                        mapping listOfElementSchema: Attachment ];
                mapAllInstVarsFor: Attachment;
                for: ZnUrl customDo: [ :mapping |
                        mapping encoder: [ :aZnUrl |
                                aZnUrl asString ] ];
                nextPut: collectionOne as: #CollectionOfAttachments.
].

"And read the JSON structure:"
(NeoJSONReader on: 
'[{"url":"http://example.com/random-name.txt","fileName":"chapter-one.txt"},{"url":"http://example.com/random-name.png","fileName":"image.png"}]'
 readStream)
        for: #CollectionOfAttachments customDo: [ :mapping | 
                        mapping listOfElementSchema: Attachment ];
        for: Attachment do: [ :mapping | 
                mapping mapInstVar: 'fileName'.
                (mapping mapInstVar: 'url') valueSchema: ZnUrl ];
        for: ZnUrl customDo: [ :mapping |
                mapping decoder: [ :string |
                        string asZnUrl ] ];
        nextAs: #CollectionOfAttachments.

"======================="
“The previous example works perfectly, including the ZnUrl mapping (notice that 
url variables have ZnUrl instances).

Now, let's say that we want to distinguish PNG and TXT attachments.
For that reason we will create two Attachment subclasses..."

Attachment subclass: #PngAttachment
        instanceVariableNames: ''
        classVariableNames: ''
        package: 'NeoJSON-Use-Case'.
        
Attachment subclass: #TxtAttachment
        instanceVariableNames: ''
        classVariableNames: ''
        package: 'NeoJSON-Use-Case'.

"...with type methods that might be used in a new JSON structure:"
PngAttachment compile: 'type', String cr, String tab, '^ ''png''' classified: 
'accessing'.
TxtAttachment compile: 'type', String cr, String tab, '^ ''txt''' classified: 
'accessing'.

"Let's create a collection with the PNG and TXT instances:"
collectionTwo := { 
        TxtAttachment new 
                url: 'http://example.com/random-name.txt' asZnUrl; 
                fileName: 'chapter-one.txt'
                yourself.
        PngAttachment new 
                url: 'http://example.com/random-name.png' asZnUrl; 
                fileName: 'image.png';
                yourself.
}.

"How can I modify NeoJSONWriter and NeoJSONReader mappings?

The JSON structure should (ideally) looks like this:"

'[      
        
{“type”:”txt”,"url":"http://example.com/random-name.txt","fileName":"chapter-one.txt”},
        
{“type”:”png”,"url":"http://example.com/random-name.png","fileName":"image.png”}
]'
-=-=-=-=-=-=-=-

I would like to keep the mapping isolated (without defining neoJsonOn: methods).

Thank you!
Juraj

Reply via email to