sean May be we can add that to the JSOn chapter. Are you interested? Stef On 30/4/14 16:28, Sean P. DeNigris wrote:
Okay, here is version 0.1 - the most basic useful functionality to aid in writing mappings.
"Assuming Neo-JSON-Core is loaded" Gofer it smalltalkhubUser: 'SeanDeNigris' project: 'SeansPlayground'; package: 'Neo-JSON-Utilities'; load.
Now if you: reader := NeoJSONReader on: entity contents readStream. reader nextAs: MyClass "(that does not implement MyClass class>>#neoJsonMapping:)".
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, where: - 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, numbers
Some useful things to do: - For each NeoJSONObject that you want to map to a class, select it in the explorer and: self schema: MyOtherClass. self generateInstVars. "self explanatory?" self mappingSourceCode inspect. "see the neoJsonMapping: source that would be generated" 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: neoJsonMapping: mapper 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. Enjoy!