OSDN Git Service

change the json function feature.
authorvisor <visor@users.sourceforge.jp>
Mon, 20 Feb 2012 15:23:17 +0000 (00:23 +0900)
committervisor <visor@users.sourceforge.jp>
Mon, 20 Feb 2012 15:23:17 +0000 (00:23 +0900)
ext/ml-json.cc

index 491f00c..4e3d64f 100644 (file)
 
 */
 
-/*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>()) {
@@ -31,18 +26,23 @@ static MNode*  picoj2ml (const picojson::value& v) {
        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);
@@ -54,27 +54,55 @@ static MNode*  picoj2ml (const picojson::value& v) {
            } 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, &params, 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 ())));
 }