/// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="output"></param> /// <param name="mode"></param> public static void ToJsonArray(UObj obj, TextWriter output, UObjSerializeMode mode) { output.Write("["); bool hasprop = false; foreach (var item in obj.Array) { if (item is UObj) { if (((UObj)item).UObjMode == UObjMode.Fake) { continue; } } if (hasprop) { output.Write(","); } else { hasprop = true; } ToJsonValue(output, mode, item); } output.Write("]"); }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="output"></param> /// <param name="mode"></param> public static void ToJsonArray(UObj obj, TextWriter output, UObjSerializeMode mode) { output.Write("["); bool hasprop = false; foreach (var item in obj.Array) { if (item is UObj) { if (((UObj)item).UObjMode == UObjMode.Fake) { continue; } } if (hasprop) { output.Write(","); } else { hasprop = true; } ToJsonValue(output, mode, item); } output.Write("]"); }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="output"></param> /// <param name="mode"></param> public static void ToJsonObject(UObj obj,TextWriter output, UObjSerializeMode mode) { output.Write("{"); bool hasprop = false; if (mode.HasFlag(UObjSerializeMode.KeepType) && null != obj._srctype) { output.Write("\"{0}\":\"{1}, {2}\"", "_srctype", obj._srctype.FullName, obj._srctype.Assembly.GetName().Name); hasprop = true; } foreach (var property in obj.Properties.OrderBy(_ => _.Key)) { if (property.Value is UObj) { if (((UObj) property.Value).UObjMode == UObjMode.Fake) { continue; } } if (hasprop) { output.Write(","); } else { hasprop = true; } output.Write("\"" + property.Key + "\""); output.Write(":"); ToJsonValue(output,mode,property.Value); } output.Write("}"); }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="output"></param> /// <param name="mode"></param> public static void ToJsonObject(UObj obj, TextWriter output, UObjSerializeMode mode) { output.Write("{"); bool hasprop = false; if (mode.HasFlag(UObjSerializeMode.KeepType) && null != obj._srctype) { output.Write("\"{0}\":\"{1}, {2}\"", "_srctype", obj._srctype.FullName, obj._srctype.Assembly.GetName().Name); hasprop = true; } foreach (var property in obj.Properties.OrderBy(_ => _.Key)) { if (property.Value is UObj) { if (((UObj)property.Value).UObjMode == UObjMode.Fake) { continue; } } if (hasprop) { output.Write(","); } else { hasprop = true; } output.Write("\"" + property.Key + "\""); output.Write(":"); ToJsonValue(output, mode, property.Value); } output.Write("}"); }
/// <summary> /// /// </summary> /// <returns></returns> public string ToJson(UObjSerializeMode mode = UObjSerializeMode.None) { var sw = new StringWriter(); UsonExtensions.ToJson(this, sw, mode); return(sw.ToString()); }
/// <summary> /// /// </summary> /// <param name="mode"></param> /// <returns></returns> public XElement ToXElement(UObjSerializeMode mode = UObjSerializeMode.None) { var sw = new StringWriter(); var xw = XmlWriter.Create(sw); WriteXml(xw, mode); xw.Flush(); return(XElement.Parse(sw.ToString())); }
/// <summary> /// /// </summary> /// <param name="output"></param> /// <param name="mode"></param> /// <param name="item"></param> public static void ToJsonValue(TextWriter output, UObjSerializeMode mode, object item) { if (null == item) { output.Write("null"); } else if (item is UObj) { ToJson(((UObj)item), output, mode); } else if (item is string) { var s = item as string; if (s.StartsWith("javascript://")) { s = s.Substring("javascript://".Length); if (mode.HasFlag(UObjSerializeMode.Javascript)) { output.Write(s); } return; } if (mode == UObjSerializeMode.Javascript && (s == "true" || s == "false")) { output.Write(s); } else { output.Write("\"" + s.Escape(EscapingType.JsonValue).Replace("\\'", "'") + "\""); } } else if (item is bool) { output.Write(item.ToString().ToLower()); } else if (item is decimal) { output.Write(((decimal)item).ToString("0.#####", CultureInfo.InvariantCulture)); } else if (item is float) { output.Write(((float)item).ToString("0.#####", CultureInfo.InvariantCulture)); } else if (item is int) { output.Write(item); } else if (item is DateTime) { output.Write("\"" + ((DateTime)item).ToString("yyyy-MM-ddTHH:mm:ss") + "\""); } else { output.Write("\"" + item.ToString().Escape(EscapingType.JsonValue).Replace("\\'", "'") + "\""); } }
/// <summary> /// /// </summary> /// <param name="mode"></param> /// <returns></returns> public string ToXmlString(UObjSerializeMode mode = UObjSerializeMode.None) { var sw = new StringWriter(); var xs = new XmlWriterSettings(); xs.OmitXmlDeclaration = true; var xw = XmlWriter.Create(sw, xs); WriteXml(xw, mode); xw.Flush(); return(sw.ToString()); }
/// <summary> /// /// </summary> /// <param name="output"></param> /// <param name="mode"></param> /// <param name="item"></param> public static void ToJsonValue(TextWriter output, UObjSerializeMode mode, object item) { if (null == item) { output.Write("null"); } else if (item is UObj) { ToJson(((UObj)item), output, mode); } else if (item is string) { output.Write("\"" + (item as string).Escape(EscapingType.JsonValue) + "\""); } else if (item is bool) { output.Write(item.ToString().ToLower()); } else if (item is decimal) { output.Write(((decimal)item).ToString("0.#####", CultureInfo.InvariantCulture)); } else if (item is float) { output.Write(((float)item).ToString("0.#####", CultureInfo.InvariantCulture)); } else if (item is int) { output.Write(item); } else if (item is DateTime) { output.Write("\"" + ((DateTime)item).ToString("yyyy-MM-ddThh:mm:ss") + "\""); } else { var str = item.ToString(); if (mode == UObjSerializeMode.Javascript && (str == "true" || str == "false")) { output.Write(str); } else { output.Write("\"" + str.Escape(EscapingType.JsonValue) + "\""); } } }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="output"></param> /// <param name="mode"></param> public static void ToJson(UObj obj, TextWriter output, UObjSerializeMode mode = UObjSerializeMode.None) { if (obj.UObjMode == UObjMode.Default) { ToJsonObject(obj, output, mode); } else if (obj.UObjMode == UObjMode.Array) { ToJsonArray(obj, output, mode); } else { throw new NotImplementedException(); } }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="output"></param> /// <param name="mode"></param> public static void ToJson(UObj obj, TextWriter output, UObjSerializeMode mode = UObjSerializeMode.None) { if (obj.UObjMode == UObjMode.Default) { ToJsonObject(obj,output, mode); } else if (obj.UObjMode == UObjMode.Array) { ToJsonArray(obj,output, mode); } else { throw new NotImplementedException(); } }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="output"></param> /// <param name="mode"></param> public static void ToJson(UObj obj, TextWriter output, UObjSerializeMode mode = UObjSerializeMode.None) { if (obj.UObjMode == UObjMode.Default) { ToJsonObject(obj, output, mode); } else if (obj.UObjMode == UObjMode.Array) { ToJsonArray(obj, output, mode); } else if (obj.UObjMode == UObjMode.Value) { var val = obj.Properties["__value"]; if (val != null) { ToJsonValue(output, mode, val); } } else { throw new NotImplementedException(); } }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="writer"></param> /// <param name="mode"></param> public static void WriteXml(UObj obj, XmlWriter writer, UObjSerializeMode mode) { WriteXml(obj, "result", null, writer, mode); }
/// <summary> /// /// </summary> /// <returns></returns> public void WriteXml(XmlWriter writer, UObjSerializeMode mode = UObjSerializeMode.None) { UsonExtensions.WriteXml(this, writer, mode); }
/// <summary> /// /// </summary> /// <param name="output"></param> /// <param name="mode"></param> /// <param name="item"></param> public static void ToJsonValue(TextWriter output, UObjSerializeMode mode, object item) { if (null == item) { output.Write("null"); } else if (item is UObj) { ToJson(((UObj) item),output, mode); } else if (item is string) { output.Write("\"" + (item as string).Escape(EscapingType.JsonValue) + "\""); } else if (item is bool) { output.Write(item.ToString().ToLower()); } else if (item is decimal) { output.Write(((decimal) item).ToString("0.#####", CultureInfo.InvariantCulture)); } else if (item is float) { output.Write(((float) item).ToString("0.#####", CultureInfo.InvariantCulture)); } else if (item is int) { output.Write(item); } else if (item is DateTime) { output.Write("\"" + ((DateTime) item).ToString("yyyy-MM-ddThh:mm:ss") + "\""); } else { var str = item.ToString(); if (mode == UObjSerializeMode.Javascript && (str == "true" || str == "false")) { output.Write(str); } else { output.Write("\"" + str.Escape(EscapingType.JsonValue) + "\""); } } }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="elementName"></param> /// <param name="advAttributes"></param> /// <param name="writer"></param> /// <param name="mode"></param> private static void WriteXml(UObj obj, string elementName, string advAttributes, XmlWriter writer, UObjSerializeMode mode) { if (obj.UObjMode == UObjMode.Fake) { return; } writer.WriteStartElement(elementName); if (!String.IsNullOrWhiteSpace(advAttributes)) { writer.WriteRaw(" " + advAttributes + " "); } if (mode.HasFlag(UObjSerializeMode.KeepType) && null != obj._srctype) { writer.WriteAttributeString("_srctype", String.Format("{0}, {1}", obj._srctype.FullName, obj._srctype.Assembly.GetName().Name)); } if (obj.UObjMode == UObjMode.Default) { foreach (var property in obj.Properties) { if (property.Value == null) { continue; } if (property.Value is string || property.Value.GetType().IsValueType) { writer.WriteAttributeString(property.Key, ToXmlValue(property.Value)); } } foreach (var property in obj.Properties) { if (property.Value == null) { continue; } if (!(property.Value is string || property.Value.GetType().IsValueType)) { WriteXml((UObj)property.Value, property.Key, null, writer, mode); } } } else if (obj.UObjMode == UObjMode.Array) { writer.WriteAttributeString("_array", "true"); foreach (var p in obj.Array) { if (null == p) { writer.WriteElementString("item", ""); } else if (p is string || p.GetType().IsValueType) { writer.WriteElementString("item", ToXmlValue(p)); } else { WriteXml(p as UObj, "item", null, writer, mode); } } } else { writer.WriteElementString("value", obj.Properties["__value"].ToString()); } writer.WriteEndElement(); }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="elementName"></param> /// <param name="advAttributes"></param> /// <param name="writer"></param> /// <param name="mode"></param> private static void WriteXml(UObj obj, string elementName, string advAttributes, XmlWriter writer, UObjSerializeMode mode) { if(obj.UObjMode==UObjMode.Fake)return; writer.WriteStartElement(elementName); if (!string.IsNullOrWhiteSpace(advAttributes)) { writer.WriteRaw(" "+advAttributes+" "); } if (mode.HasFlag(UObjSerializeMode.KeepType) && null != obj._srctype) { writer.WriteAttributeString("_srctype",string.Format("{0}, {1}",obj._srctype.FullName,obj._srctype.Assembly.GetName().Name)); } if (obj.UObjMode == UObjMode.Default) { foreach (var property in obj.Properties) { if (property.Value == null) continue; if (property.Value is string || property.Value.GetType().IsValueType) { writer.WriteAttributeString(property.Key, ToXmlValue(property.Value)); } } foreach (var property in obj.Properties) { if (property.Value == null) continue; if (!(property.Value is string || property.Value.GetType().IsValueType)) { WriteXml((UObj) property.Value, property.Key, null, writer, mode); } } } else { writer.WriteAttributeString("_array","true"); foreach (var p in obj.Array) { if (null == p) { writer.WriteElementString("item", ""); }else if (p is string || p.GetType().IsValueType) { writer.WriteElementString("item", ToXmlValue(p)); } else { WriteXml(p as UObj,"item",null,writer,mode); } } } writer.WriteEndElement(); }
/// <summary> /// /// </summary> /// <param name="e"></param> /// <param name="root"></param> /// <param name="mode"></param> /// <returns></returns> public static string ToJson(this XElement e, bool root = false, UObjSerializeMode mode = UObjSerializeMode.Javascript) { return(e.XmlToUson(root).ToJson(mode)); }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="writer"></param> /// <param name="mode"></param> public static void WriteXml(UObj obj, XmlWriter writer, UObjSerializeMode mode) { WriteXml(obj, "result",null, writer, mode); }