private static Document FromObject(IJsonWrapper jsonData) { switch (jsonData.GetJsonType()) { case JsonType.None: return(new Document()); case JsonType.Boolean: return(new Document(jsonData.GetBoolean())); case JsonType.Double: return(new Document(jsonData.GetDouble())); case JsonType.Int: return(new Document(jsonData.GetInt())); case JsonType.Long: return(new Document(jsonData.GetLong())); case JsonType.String: return(new Document(jsonData.GetString())); case JsonType.Array: return(new Document(jsonData.Values.OfType <object>().Select(FromObject).ToArray())); case JsonType.Object: var dictionary = new Dictionary <string, Document>(); Copy(jsonData, dictionary); return(new Document(dictionary)); } throw new NotSupportedException($"Couldn't convert {jsonData.GetJsonType()}"); }
public static int AsInt32(this IJsonWrapper source, int defaultValue = 0) { if (source == null) { return(defaultValue); } switch (source.GetJsonType()) { case JsonType.Int: return(source.GetInt()); case JsonType.Long: return((int)source.GetLong()); case JsonType.Double: return((int)source.GetDouble()); case JsonType.Boolean: return(source.GetBoolean() ? 1 : 0); case JsonType.String: int value; if (int.TryParse(source.GetString(), out value)) { return(value); } break; case JsonType.Array: case JsonType.Object: return(source.Count); } return(defaultValue); }
public static bool AsBoolean(this IJsonWrapper source, bool defaultValue = false) { if (source == null) { return(defaultValue); } switch (source.GetJsonType()) { case JsonType.Boolean: return(source.GetBoolean()); case JsonType.Int: return(source.GetInt() != 0); case JsonType.Long: return(source.GetLong() != 0); case JsonType.Double: return(source.GetDouble() != 0); case JsonType.String: bool value; if (bool.TryParse(source.GetString(), out value)) { return(value); } break; case JsonType.Array: case JsonType.Object: return(source.Count > 0); } return(defaultValue); }
public static sbyte AsInt8(this IJsonWrapper source, sbyte defaultValue = 0) { if (source == null) { return(defaultValue); } switch (source.GetJsonType()) { case JsonType.Int: return((sbyte)source.GetInt()); case JsonType.Long: return((sbyte)source.GetLong()); case JsonType.Double: return((sbyte)source.GetDouble()); case JsonType.Boolean: return(source.GetBoolean() ? (sbyte)1 : (sbyte)0); case JsonType.String: sbyte value; if (sbyte.TryParse(source.GetString(), out value)) { return(value); } break; case JsonType.Array: case JsonType.Object: return((sbyte)source.Count); } return(defaultValue); }
public static double AsDouble(this IJsonWrapper source, double defaultValue = 0) { if (source == null) { return(defaultValue); } switch (source.GetJsonType()) { case JsonType.Double: return(source.GetDouble()); case JsonType.Int: return(source.GetInt()); case JsonType.Long: return(source.GetLong()); case JsonType.Boolean: return(source.GetBoolean() ? 1D : 0D); case JsonType.String: double value; if (double.TryParse(source.GetString(), out value)) { return(value); } break; case JsonType.Array: case JsonType.Object: return(source.Count); } return(defaultValue); }
public static ulong AsUInt64(this IJsonWrapper source, ulong defaultValue = 0) { if (source == null) { return(defaultValue); } switch (source.GetJsonType()) { case JsonType.Int: return((ulong)source.GetInt()); case JsonType.Long: return((ulong)source.GetLong()); case JsonType.Double: return((ulong)source.GetDouble()); case JsonType.Boolean: return(source.GetBoolean() ? 1UL : 0UL); case JsonType.String: ulong value; if (ulong.TryParse(source.GetString(), out value)) { return(value); } break; case JsonType.Array: case JsonType.Object: return((ulong)source.Count); } return(defaultValue); }
public static ushort AsUInt16(this IJsonWrapper source, ushort defaultValue = 0) { if (source == null) { return(defaultValue); } switch (source.GetJsonType()) { case JsonType.Int: return((ushort)source.GetInt()); case JsonType.Long: return((ushort)source.GetLong()); case JsonType.Double: return((ushort)source.GetDouble()); case JsonType.Boolean: return(source.GetBoolean() ? (ushort)1 : (ushort)0); case JsonType.String: ushort value; if (ushort.TryParse(source.GetString(), out value)) { return(value); } break; case JsonType.Array: case JsonType.Object: return((ushort)source.Count); } return(defaultValue); }
public static float AsSingle(this IJsonWrapper source, float defaultValue = 0) { if (source == null) { return(defaultValue); } switch (source.GetJsonType()) { case JsonType.Double: return((float)source.GetDouble()); case JsonType.Int: return(source.GetInt()); case JsonType.Long: return(source.GetLong()); case JsonType.Boolean: return(source.GetBoolean() ? 1F : 0F); case JsonType.String: if (float.TryParse(source.GetString(), out float value)) { return(value); } break; case JsonType.Array: case JsonType.Object: return(source.Count); } return(defaultValue); }
private void CloneFromJsonWrapper(IJsonWrapper jsonWrapper) { JsonData jsonData = jsonWrapper as JsonData; if (jsonData != null) { type = jsonData.type; json = jsonData.json; inst_boolean = jsonData.inst_boolean; inst_double = jsonData.inst_double; inst_int = jsonData.inst_int; inst_long = jsonData.inst_long; inst_string = jsonData.inst_string; if (jsonData.inst_array != null) { inst_array = new List <JsonData>(jsonData.inst_array); } if (jsonData.inst_object != null) { inst_object = new Dictionary <string, JsonData>(jsonData.inst_object); } return; } type = jsonWrapper.GetJsonType(); switch (type) { case JsonType.Boolean: inst_boolean = jsonWrapper.GetBoolean(); break; case JsonType.Int: inst_int = jsonWrapper.GetInt(); break; case JsonType.Long: inst_long = jsonWrapper.GetLong(); break; case JsonType.Double: inst_double = jsonWrapper.GetDouble(); break; case JsonType.String: inst_string = jsonWrapper.GetString(); break; case JsonType.Array: SetJsonType(JsonType.Array); foreach (object item in jsonWrapper as IList) { Add(item as JsonData ?? new JsonData(item)); } break; case JsonType.Object: SetJsonType(JsonType.Object); foreach (DictionaryEntry item in jsonWrapper as IDictionary) { this[item.Key.ToString()] = item.Value as JsonData ?? new JsonData(item.Value); } break; } }
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 Document MapFromJsonObject(IJsonWrapper jsonWrapper) { if (null == jsonWrapper) { return(new Document()); } switch (jsonWrapper.GetJsonType()) { case JsonType.None: return(new Document()); case JsonType.Boolean: return(new Document(jsonWrapper.GetBoolean())); case JsonType.Double: return(new Document(jsonWrapper.GetDouble())); case JsonType.Int: return(new Document(jsonWrapper.GetInt())); case JsonType.Long: return(new Document(jsonWrapper.GetLong())); case JsonType.String: return(new Document(jsonWrapper.GetString())); case JsonType.Array: return(new Document((jsonWrapper as IList).Cast <IJsonWrapper>().Select(MapFromJsonObject).ToList())); case JsonType.Object: return(new Document(MapDictionary(jsonWrapper))); default: throw new ArgumentException($"Unknown JSON type: {jsonWrapper.GetJsonType()}"); } }
public static string AsString(this IJsonWrapper source, string defaultValue = "") { if (source == null) { return(defaultValue); } switch (source.GetJsonType()) { case JsonType.String: return(source.GetString()); case JsonType.Double: return(source.GetDouble().ToString()); case JsonType.Int: return(source.GetInt().ToString()); case JsonType.Long: return(source.GetLong().ToString()); case JsonType.Boolean: return(source.GetBoolean().ToString()); case JsonType.Array: case JsonType.Object: return(source.Count.ToString()); } return(defaultValue); }
private static void WriteJson(IJsonWrapper wrapper, JsonWriter writer) { if (wrapper == null && !JsonWriter.SkipNullMember) { writer.Write(null); return; } if (wrapper.IsString) { writer.Write(wrapper.GetString()); return; } if (wrapper.IsBoolean) { writer.Write(wrapper.GetBoolean()); return; } if (wrapper.IsDouble) { writer.Write(wrapper.GetDouble()); return; } if (wrapper.IsInt) { writer.Write(wrapper.GetInt()); return; } if (wrapper.IsFloat) { writer.Write(wrapper.GetFloat()); return; } if (wrapper.IsLong) { writer.Write(wrapper.GetLong()); return; } if (wrapper.IsArray) { writer.WriteArrayStart(); foreach (var elem in (IList)wrapper) { WriteJson((JsonData)elem, writer); } writer.WriteArrayEnd(); return; } if (wrapper.IsObject) { if (JsonWriter.SkipNullMember && wrapper.Count <= 0) { return; } writer.WriteObjectStart(); foreach (DictionaryEntry entry in (IDictionary)wrapper) { if (JsonWriter.SkipNullMember) { var type = ((JsonData)entry.Value).GetJsonType(); if (type == JsonType.None) { continue; } if (type == JsonType.Array && ((IList)entry.Value).Count <= 0) { continue; } } writer.WritePropertyName((string)entry.Key); WriteJson((JsonData)entry.Value, writer); } writer.WriteObjectEnd(); return; } if (wrapper.GetJsonType() == JsonType.None) { writer.WriteObjectStart(); writer.WriteObjectEnd(); } }
private static void WriteJson(IJsonWrapper wrapper, JsonWriter writer) { if (wrapper == null) { writer.Write(null); return; } if (wrapper.IsString) { writer.Write(wrapper.GetString()); return; } if (wrapper.IsBoolean) { writer.Write(wrapper.GetBoolean()); return; } if (wrapper.IsDouble) { writer.Write(wrapper.GetDouble()); return; } if (wrapper.IsInt) { writer.Write(wrapper.GetInt()); return; } if (wrapper.IsLong) { writer.Write(wrapper.GetLong()); return; } if (wrapper.IsArray) { writer.WriteArrayStart(); foreach (object elem in (IList)wrapper) { WriteJson((JsonData)elem, writer); } writer.WriteArrayEnd(); return; } if (wrapper.IsObject) { if (((IDictionary)wrapper).Count <= 0) { return; } writer.WriteObjectStart(); foreach (DictionaryEntry entry in ((IDictionary)wrapper)) { //TODO: ignore null entry var data = (JsonData)entry.Value; var type = data.GetJsonType(); if (type == JsonType.None) { continue; } if (type == JsonType.Array && ((IList)entry.Value).Count <= 0) { continue; } writer.WritePropertyName((string)entry.Key); WriteJson(data, writer); } writer.WriteObjectEnd(); return; } if (wrapper.GetJsonType() == JsonType.None) { writer.WriteObjectStart(); writer.WriteObjectEnd(); return; } }