Chan my apologies. I got lost in finding a suitable website to post a snippet then didn't get around to using any of them!
There is barely 100 lines, but I didn't want to post it here. In the end I just used GitHub. You can find it here https://github.com/graeme-defty/JSONerl Suggestions welcome, of course. g On 9 December 2013 10:00, chan sisowath <[email protected]> wrote: > hi Graeme, > > if you could make it available, i could be interested. > > regards > chan. > > > > 2011/12/5 Graeme Defty <[email protected]> > >> I also had fun getting JSON to do what I wanted. >> >> After digging around in the code for a while, I realised that to create >> the JSON that I needed I had to walk through my data to set up the correct >> structure to pass into the JSON libraries. >> >> It seemed to me that the complexity of the functions I had to write to >> walk the data was getting harder than what would be needed to generate the >> JSON directly! (Well, maybe not harder, but certainly up there.) >> >> I looked around at other Erlang/JSON implementations and it seemed to me >> that everyone was trying to solve the wrong problem. >> >> The common goal (with all the libraries I found anyway) was to try and >> JSONise some arbitrary Erlang form in such a manner that the original form >> could be re-created without prior knowledge at the other end. >> >> Now this is a laudable goal, butunless I am writing some goofy >> applications that is not the most common use case. Normally, JSON is used >> for interchange/transmission of data. The message structure is specified >> and understood, and may or may not be under your control, but it is at >> least well-known, and the task is to build it from the data you have >> available in your app. >> >> In a functional language like Erlang, I feel the 'builder' style works >> really well, and so I put together a short library (and it really is short >> - about 100 lines, i think) which lets me use such a builder style. >> >> As an example, lets say the target is a JSON structure containing details >> of a competition. The message contents include details of the club holding >> the comp followed by an array of players names and score . >> >> S0 the output would look something like this: >> >> { >> "details" : { >> "club" : "Graemes Testing Club", >> "course" : "North Course", >> "name" : "February Monthly Event", >> "date" : 2011/2/26, >> >> >> "type" : "Endurance" >> }, >> "results" : [ >> { >> "fname" : "Powers", >> "gname" : "Jib", >> "score" : 69 >> }, >> { >> "fname" : "Moroney", >> >> >> "gname" : "Jim", >> "score" : 73 >> }, >> >> <<snip>> >> >> { >> "fname" : "Defty", >> "gname" : "Graeme", >> "score" : 86 >> >> >> } >> ] >> } >> >> >> To create that, my code is as follows : >> >> lb_format(1, Cred, Comp, Entries, Overall, Others, Divisions) -> >> json:format( >> json:object([ >> {json:val("details"), >> json:object([ >> {json:val("club"), json:val((Comp:club()):title())}, >> {json:val("course"), >> json:val(((hd(Comp:comp_rounds())):course()):name())}, >> {json:val("name"), json:val(Comp:name())}, >> {json:val("date"), json:val(Comp:date())}, >> {json:val("type"), >> json:val((hd(Comp:comp_rounds())):comp_type_disp())} >> ]) >> }, >> {json:val("results"), >> json:array([ >> json:object([ >> {json:val("fname"), >> json:val(((((hd(E:entry_players())):player()):usr()):fname()))}, >> {json:val("gname"), >> json:val(((((hd(E:entry_players())):player()):usr()):gname()))}, >> {json:val("score"), json:val(trunc(E:event_score()))} >> ]) || E <- Entries],$\n)} >> ]),2 >> ); >> >> Not to everyone's taste, perhaps, but the code structure mirrors closely >> the JSON structure and the signal to noise ratio is fairly good. >> >> The 2 on the second last line is the increment for indenting so that the >> JSON looks nice. The message can be shortened a bit at the cost of >> readability by setting this to zero so there is no indentation. >> >> Happy to make this available if anyone is interested. >> >> Cheers, >> >> g >> ________________________________________ >> >> On 5 December 2011 06:46, Drew <[email protected]> wrote: >> >>> I am trying to take some internal data objects, and return them as >>> JSON. >>> >>> My model looks like this: >>> -module(parse, [Content,Body]). >>> -compile(export_all). >>> >>> >>> My controller looks like this: >>> index('GET',[]) -> >>> {json, [{ >>> content, "Some Text" >>> }]}. >>> >>> This works great, but if I return any complex dataset like an array or >>> tuple, it errors out with JSON errors. >>> >>> >>> content, [1,2,3] //Outputs unicode (okay I guess) >>> >>> content, [{1},2,3] //Error, function_clause >>> >>> content, {1,2,3} //Outputs time >>> content, {1,2,3,4} //Error, bad_term >>> >>> I have been slightly more successful using mochijson2, but I like the >>> automagic part of json returns. Can you help explain or document how >>> I can return complex data structures via JSON action? >>> >> >> > -- > You received this message because you are subscribed to the Google Groups > "ChicagoBoss" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/groups/opt_out. > -- You received this message because you are subscribed to the Google Groups "ChicagoBoss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/groups/opt_out.
