Here goes Steve again with another piece of hare-brained code. I have
a use for it, maybe someone else could use it.
Several weeks ago I asked about a json parse routine - something that
would convert json to a collection. I wrote one several years ago, but
since I don't use json with javascript, I'm not sure it had a use
outside of mine. I was also not very happy with the complexity of the
converted code.
My use of json is to serialize some form data and I didn't need every
possible type(text!). I had done the serialization before with tagged
text(xml) and collection or object tools blobs, but was looking for
something more generic and better than xml.
jsonParseSimple is an Active4D method that will convert a json object
to a collection. It's big limitation is that it will only handle
objects/collections and text arrays. Again, for me, that is not a
limitation - that's all I need. Code below and more meaningless
comments below that;
method "new array"($list)
// convert semi-colon delimited list to a text array
$cnt := split string($list;";";$chunks)
return($chunks)
end method
method "jsonParseSimple"($json)
/*given a json object,convert it to an Active4D collection*/
array text($re;0) // regex array
array text($rp;0) // replace array
// set the reqex array
$re{} := "|:\\s*{|" // object => convert :{ to ;new collection(
$re{} := "|:\\s*\\[|" // text array => convert :[ to ;new array
( -- local method see above
$re{} := "|\\]|" // end text array => convert ] to )
$re{} := "|}|" // end object => convert } to )
$re{} := "|[:,]|" // replace comma and colon with semi-colon
$re{} := "|{|" // replace begining object with new collection()
$re{} := "|n\\(\\)|" // replace empty collection convert n() to
new n
//set the replacement array
$rp{} := ";new collection("
$rp{} := ";new array('"
$rp{} := "')"
$rp{} := ")"
$rp{} := ";"
$rp{} := "new collection("
$rp{} := "n"
regex replace($re; $json; $rp; $results)
$collection := execute("return ("+ $results+ ")")
return($collection)
end method
//test
$json := '{"post":{"answers":{"1":"2","10":"19","2":
["11","12"],"74":false,"75":["555","1212"],"76":
["277
","278
","279"],"77":"283","78":"285","79":"286","9":"18"},"answers_text":
{"10":"dfd","74":"","76":["one","two","three"],"78":"fox back
lazy","79":"3.1415"},"assessment_id":"1","failed":
[],"maxValue":"32","maxValueWeighted":"44","other_answers":
{},"totalScore":29,"totalScoreWeighted":33}}'
$myCollection := jsonParseSimple($json)
a4d.debug.dump collection($myCollection)
-----Meaningless comments ----
Several years ago I was pressured into developing an on-line job
application for our customers. This happened when our 4D structure was
in the middle of a major change and I could not get the structure
changed. I had a couple blob and text fields available to me, so I
wrote the application and stored the data in the blobs. This was
supposed to be temporary because someone else was going to develop a
RoR application.
The RoR application never made it. After I retired, I experiment with
the RoR application and got it working using some tables. It turns out
that the major part of the job application process is asking a bunch
of multiple choice question: "How many years of welding experience do
you have?". Since those questions were different for every job, I
still ended up using json to store the raw input data . Only the
Assessments(questions and answers) and the scoring results use a table
structure.
Since they are still not ready to convert to RoR, I replicated my
experiment in Active4D. The above json object is an example of what I
created using the form data and some results from processing the data
(how they scored on the assessment, etc). It is just a bunch of ids
pointing to the questions and answers. In case you have not noticed,
this will serialize nested collections, something "collection to blob"
will not do.
Steve Alex
_______________________________________________
Active4D-dev mailing list
[email protected]
http://mailman.aparajitaworld.com/mailman/listinfo/active4d-dev
Archives: http://mailman.aparajitaworld.com/archive/active4d-dev/