[Pharo-dev] [ANN]: Little NeoJSON Helper
Sean P. DeNigris
sean at clipperadams.com
Wed Apr 30 10:28:50 EDT 2014
Okay, here is version 0.1 - the most basic useful functionality to aid in
"Assuming Neo-JSON-Core is loaded"
smalltalkhubUser: 'SeanDeNigris' project: 'SeansPlayground';
Now if you:
reader := NeoJSONReader on: entity contents readStream.
reader nextAs: MyClass "(that does not implement MyClass
You will get a debugger (which you probably want to close because the new
mappings we create will not be activated as the helper hook is cached in the
mappings) and a tree inspector on a NeoJSON model that you can customize,
- a NeoJSONObject represents each JSON dictionary; for the enclosing object,
the schema is already set to MyClass (i.e. the argument to #nextAs:)
- a NeoJSONList represents each JSON list
- a NeoJSONValue represents all other values e.g. bools, nil, strings,
Some useful things to do:
- For each NeoJSONObject that you want to map to a class, select it in the
self schema: MyOtherClass.
self generateInstVars. "self explanatory?"
self mappingSourceCode inspect. "see the neoJsonMapping: source that would
self generateMapping. "compile class-side neoJsonMapping:"
- Select any field inside a NeoJSONObject that you want to map to an inst
var with a different name and:
self instVarName: #isSaved "(e.g. to map JSON fields_with_underscores to
camelCase var names)"
- Specify the element type of a list by selecting it and:
self elementSchema: MyListReturnType. "This will automatically set the
schema to #ArrayOfMyListReturnType, but you can change that manually if you
want by sending #schema:)
After customizing, sending #generateMapping to a NeoJSONObject should
generate something like:
mapper for: self do: [ :mapping |
mapping mapInstVars: #(notes id).
(mapping mapInstVar: #day) valueSchema: Date.
(mapping mapInstVar: #events) valueSchema: #ArrayOfMyEvent.
mapping mapInstVar: #isSaved to: #is_saved.
(mapping mapInstVar: #timeSaved) valueSchema: DateAndTime ].
mapper for: Date customDo: [ :mapping | ]. "now you specify how to convert
the string to a Date"
mapper for: #ArrayOfMyEvent customDo: [ :mapping |
mapping listOfElementSchema: MyEvent ].
mapper for: DateAndTime customDo: [ :mapping | ]. "same as Date above"
This started as a tiny little personal experiment that got away from me, ha
ha, so there is no UI, tests, or doc (except for this post). MIT of course.
View this message in context: http://forum.world.st/ANN-Little-NeoJSON-Helper-tp4756763p4757243.html
Sent from the Pharo Smalltalk Developers mailing list archive at Nabble.com.
More information about the Pharo-dev