/// <summary> /// Use JsonValue as JsonObject and get JsonValue item by key. /// return null if not found key. /// </summary> public JsonValue AsObjectGet(string key) { DebugTool.Assert(this.type == JsonType.Object, "JsonValue type is not Object !"); var dict = this.objectValue as Dictionary <string, JsonValue>; JsonValue jsonValue; if (dict.TryGetValue(key, out jsonValue)) { return(jsonValue); } else { return(null); } }
/// <summary> /// Parse JsonArray. /// </summary> private static JsonValue ParseArray(Data data) { var jsonArray = new List <JsonValue>(JsonArrayInitCapacity); // skip '[' data.index++; do { SkipWhiteSpace(data); if (data.json[data.index] == ']') { break; } // add JsonArray item jsonArray.Add(ParseValue(data)); SkipWhiteSpace(data); if (data.json[data.index] == ',') { data.index++; } else { DebugTool.Assert ( data.json[data.index] == ']', "Json ParseArray error, char '{0}' should be ']' ", data.json[data.index] ); break; } }while (true); // skip ']' data.index++; return(new JsonValue(JsonType.Array, jsonArray)); }
/// <summary> /// Get JsonValue as bool. /// </summary> public bool AsBool() { DebugTool.Assert(this.type == JsonType.Bool, "JsonValue type is not Bool !"); return(this.numberValue > 0.0f); }
/// <summary> /// Get JsonValue as int. /// </summary> public int AsInt() { DebugTool.Assert(this.type == JsonType.Number, "JsonValue type is not Number !"); return((int)this.numberValue); }
/// <summary> /// Get JsonValue as float. /// </summary> public float AsFloat() { DebugTool.Assert(this.type == JsonType.Number, "JsonValue type is not Number !"); return(this.numberValue); }
//---------------------------------------------------------------------------------------------------------------------- #region Other Json value API /// <summary> /// Get JsonValue as string. /// </summary> public string AsString() { DebugTool.Assert(this.type == JsonType.String, "JsonValue type is not String !"); return(this.objectValue as string); }
/// <summary> /// Use JsonValue as JsonArray and get JsonValue item by index. /// </summary> public JsonValue AsArrayGet(int index) { DebugTool.Assert(this.type == JsonType.Array, "JsonValue type is not Array !"); return((this.objectValue as List <JsonValue>)[index]); }
//---------------------------------------------------------------------------------------------------------------------- #region JsonArray API /// <summary> /// Use JsonValue as JsonArray. /// </summary> public List <JsonValue> AsArray() { DebugTool.Assert(this.type == JsonType.Array, "JsonValue type is not Array !"); return(this.objectValue as List <JsonValue>); }
//---------------------------------------------------------------------------------------------------------------------- #region JsonObject API /// <summary> /// Use JsonValue as JsonObject. /// </summary> public Dictionary <string, JsonValue> AsObject() { DebugTool.Assert(this.type == JsonType.Object, "JsonValue type is not Object !"); return(this.objectValue as Dictionary <string, JsonValue>); }
/// <summary> /// Parse JsonObject. /// </summary> private static JsonValue ParseObject(Data data) { var jsonObject = new Dictionary <string, JsonValue>(JsonObjectInitCapacity); // skip '{' data.index++; do { SkipWhiteSpace(data); if (data.json[data.index] == '}') { break; } DebugTool.Assert ( data.json[data.index] == '"', "Json ParseObject error, char '{0}' should be '\"' ", data.json[data.index] ); // skip '"' data.index++; var start = data.index; while (true) { switch (data.json[data.index++]) { // check end '"' case '"': break; case '\\': // skip escaped quotes data.index++; continue; default: continue; } // already skip the end '"' break; } // get object key string var key = data.json.Substring(start, data.index - start - 1); SkipWhiteSpace(data); DebugTool.Assert ( data.json[data.index] == ':', "Json ParseObject error, after key = {0}, char '{1}' should be ':' ", key, data.json[data.index] ); // skip ':' data.index++; // set JsonObject key and value jsonObject.Add(key, ParseValue(data)); SkipWhiteSpace(data); if (data.json[data.index] == ',') { data.index++; } else { DebugTool.Assert ( data.json[data.index] == '}', "Json ParseObject error, after key = {0}, char '{1}' should be '}' ", key, data.json[data.index] ); break; } }while (true); // skip '}' and return after '}' data.index++; return(new JsonValue(JsonType.Object, jsonObject)); }