[Pharo-dev] NeoJSONCustomMapping>>#mapWithValueSchema:

Sven Van Caekenberghe sven at stfx.eu
Fri Aug 30 05:27:38 EDT 2013


Hi,

Sebastian asked me for a solution to the following problem: reading a JSON map where the values should be instances of a specific domain object. This was already possible for lists.

Using the newly added NeoJSONCustomMapping>>#mapWithValueSchema: this is now possible, as the following unit test shows:

testDictionaryOfObject2
  | data json reader |
  data := Dictionary new.
  data 
    at: #one put: NeoJSONTestObject2 example1;
    at: #two put: NeoJSONTestObject2 example1.
  json := NeoJSONWriter toString: data.
  reader := NeoJSONReader on: json readStream.
  reader for: #DictionaryOfObject2 customDo: [ :mapping |
    mapping mapWithValueSchema: NeoJSONTestObject2 ].  
  self 
    assert: (reader nextAs: #DictionaryOfObject2)
    equals: data

Note that the mapping mechanism in NeoJSON is optional. Without mapping you get Arrays and Dictionaries back, that you can then convert to your domain objects in the a next step. With mapping however, these intermediary representations are not used and maps or lists are parsed while creating and populating your domain objects directly, which should be more efficient. This does of course introduce the necessity of adding some form of static typing.

http://www.smalltalkhub.com/#!/~SvenVanCaekenberghe/Neo

Sven





More information about the Pharo-dev mailing list