*/
-/*DOC:
-===json-read===
- (json-read TEXT) -> LIST
-
-*/
-static MNode* picoj2ml (const picojson::value& v) {
+static MNode* picoj2ml (const picojson::value& v, MNode* arylabel) {
if (v.is<picojson::null>()) {
return NULL;
} else if (v.is<bool>()) {
return newMNode_str (new ustring (ans));
} else if (v.is<picojson::array>()) {
MNodeList ans;
- const picojson::array& a = v.get<picojson::array>();
- for (picojson::array::const_iterator i = a.begin(); i != a.end(); ++i) {
- ans.append (picoj2ml (*i));
+ const picojson::array& a = v.get<picojson::array> ();
+ for (picojson::array::const_iterator i = a.begin (); i != a.end (); ++i) {
+// ans.append (picoj2ml (*i), arylabel);
+ if (arylabel)
+ ans.append (newMNode_cons (arylabel, newMNode_cons (picoj2ml (*i, arylabel))));
+ else
+ ans.append (picoj2ml (*i, arylabel));
}
return ans.release ();
- } else if (v.is<picojson::object>()) {
+ } else if (v.is<picojson::object> ()) {
MNodeList ans;
- const picojson::object& o = v.get<picojson::object>();
- for (picojson::object::const_iterator i = o.begin(); i != o.end(); ++i) {
+ const picojson::object& o = v.get<picojson::object> ();
+ for (picojson::object::const_iterator i = o.begin (); i != o.end (); ++i) {
ustring name = fixUTF8 (i->first);
MNode* label = newMNode_sym (new ustring (name));
- if (i->second.is<picojson::array>()) {
+#if 0
+ if (i->second.is<picojson::array> ()) {
MNodePtr l;
MNode* t;
l = picoj2ml (i->second);
} else {
ans.append (newMNode_cons (label, newMNode_cons (picoj2ml (i->second))));
}
+#endif
+ ans.append (newMNode_cons (label, newMNode_cons (picoj2ml (i->second, arylabel))));
}
return ans.release ();
}
return NULL; // error
}
+/*DOC:
+===json-read===
+ (json-read TEXT [#array-is-list | :array-is-cons SYMBOL]) -> LIST
+*list mode
+ scalar_value → Atom
+ array → List
+ object → ((name1 value1) (name2 value2) ...)
+*cons mode
+ scalar_value → Atom
+ array → ((SYMBOL value1) (SYMBOL value2) ...)
+ object → ((name1 value1) (name2 value2) ...)
+
+*/
//#AFUNC json-read ml_json_read
MNode* ml_json_read (MNode* cell, MlEnv* mlenv) {
MNode* arg = cell->cdr ();
ustring text;
+ MNodePtr label;
picojson::value val;
ustring err;
+ std::vector<MNode*> params;
+ std::vector<MNode*> keywords;
+ static paramList kwlist[] = {
+ {CharConst ("array-is-list"), true},
+ {CharConst ("array-is-cons"), false},
+ {NULL, 0, 0}
+ };
- if (! arg)
- throw (uErrorWrongNumber);
- text = eval_str (arg->car (), mlenv);
- nextNode (arg);
- if (arg)
- throw (uErrorWrongNumber);
+ setParams (arg, 1, ¶ms, kwlist, &keywords, NULL);
+ text = eval_str (params[0], mlenv);
+ if (keywords[0]) {
+ } else if (keywords[1]) {
+ label = eval (keywords[1], mlenv);
+ if (to_bool (label ())) {
+ label = newMNode_sym (new ustring (to_string (label ())));
+ } else {
+ label = NULL;
+ }
+ }
picojson::parse (val, text.begin (), text.end (), &err);
- return newMNode_cons (newMNode_sym (new ustring (CharConst ("json"))), newMNode_cons (picoj2ml (val)));
+ return newMNode_cons (newMNode_sym (new ustring (CharConst ("json"))), newMNode_cons (picoj2ml (val, label ())));
}