void SerializeObject(KJson obj) { bool first = true; builder.Append('{'); //foreach(string e in obj.Data.Keys) { // if(!first) { builder.Append(','); } // SerializeString(e); // builder.Append(':'); // SerializeValue(obj[e]); // first = false; //} //obj.Data.ForEachPairs((k, v)) foreach (var p in obj.Data) { if (!first) { builder.Append(','); } SerializeString(p.Key); builder.Append(':'); SerializeValue(p.Value); first = false; } builder.Append('}'); }
void SerializeValue(object value) { if (value == null) { builder.Append(def_null); } else if (value is string) { SerializeString(value as string); } else if (value is bool) { builder.Append((bool)value ? def_true : def_false); } else if (value is KJson) { KJson kJsonObject = value as KJson; if (kJsonObject.isList) { SerializeArray(kJsonObject); } else { SerializeObject(kJsonObject); } } else if (value is char) { SerializeStringObj(value); } else { SerializeOther(value); } }
KJson ParseArray() { KJson kJsonArray = new KJson(); kJsonArray.isList = true; // ditch opening bracket json.Read(); // [ var parsing = true; while (parsing) { TOKEN nextToken = NextToken(); switch (nextToken) { case TOKEN.NONE: return(null); case TOKEN.COMMA: continue; case TOKEN.SQUARED_CLOSE: parsing = false; break; default: object value = ParseByToken(nextToken); kJsonArray.Add(value); break; } } return(kJsonArray); }
public KJson CreateListChild() { KJson j = new KJson(); Add(j); return(j); }
public KJson CreateChild(string key, bool _isList = false) { if (HasChild(key)) { throw new KJsonException("[Json CreateChild] the key (" + key + ") is already exist. "); } KJson j = new KJson(); Add(key, j); j.isList = _isList; return(j); }
void SerializeArray(KJson anArray) { builder.Append('['); bool first = true; anArray.ListData.ForEach(obj => { if (!first) { builder.Append(','); } SerializeValue(obj); first = false; }); builder.Append(']'); }
KJson ParseObject() { KJson kJsonObj = new KJson(); // ditch opening brace json.Read(); while (true) { TOKEN nextToken = NextToken(); switch (nextToken) { case TOKEN.NONE: return(null); case TOKEN.COMMA: continue; case TOKEN.CURLY_CLOSE: return(kJsonObj); default: // name string name = ParseString(); if (name == null) { return(null); } // : nextToken = NextToken(); if (nextToken != TOKEN.COLON) { return(null); } // ditch the colon json.Read(); // value kJsonObj[name] = ParseValue(); break; } } }