private static void WriteJson(IJsonWrapper obj, JsonWriter writer) { if (obj.IsString) { writer.Write(obj.GetString()); } else if (obj.IsBoolean) { writer.Write(obj.GetBoolean()); } else if (obj.IsDouble) { writer.Write(obj.GetDouble()); } else if (obj.IsInt) { writer.Write(obj.GetInt()); } else if (obj.IsUint) { writer.Write(obj.GetUint()); } else if (obj.IsLong) { writer.Write(obj.GetLong()); } else if (obj.IsArray) { writer.WriteArrayStart(); foreach (object current in (IList)obj) { JsonData.WriteJson((JsonData)current, writer); } writer.WriteArrayEnd(); } else if (obj.IsObject) { writer.WriteObjectStart(); foreach (DictionaryEntry dictionaryEntry in (IDictionary)obj) { writer.WritePropertyName((string)dictionaryEntry.Key); JsonData.WriteJson((JsonData)dictionaryEntry.Value, writer); } writer.WriteObjectEnd(); } else if (obj.GetJsonType() == JsonType.None) { //为空的情况下,也要能写一对花括号,避免格式出错。 writer.WriteObjectStart(); writer.WriteObjectEnd(); } }
private static void WriteJson(IJsonWrapper obj, JsonWriter writer) { if (obj == null) { writer.Write(null); return; } if (obj.IsString) { writer.Write(obj.GetString()); return; } if (obj.IsBoolean) { writer.Write(obj.GetBoolean()); return; } if (obj.IsDouble) { writer.Write(obj.GetDouble()); return; } if (obj.IsInt) { writer.Write(obj.GetInt()); return; } if (obj.IsUint) { writer.Write(obj.GetUint()); return; } if (obj.IsLong) { writer.Write(obj.GetLong()); return; } if (obj.IsUlong) { writer.Write(obj.GetUlong()); return; } if (obj.IsArray) { writer.WriteArrayStart(); foreach (object elem in (IList)obj) { WriteJson((JsonData)elem, writer); } writer.WriteArrayEnd(); return; } if (obj.IsObject) { writer.WriteObjectStart(); foreach (DictionaryEntry entry in ((IDictionary)obj)) { writer.WritePropertyName((string)entry.Key); WriteJson((JsonData)entry.Value, writer); } writer.WriteObjectEnd(); return; } }