/// <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> /// Конвертирует XElement в UObj /// </summary> /// <param name="e"></param> /// <param name="root"></param> /// <returns></returns> public static UObj XmlToUson(this XElement e, bool root = false) { dynamic result = new UObj(); dynamic target = result; if (root) { result[e.Name.LocalName] = new object(); target = result[e.Name.LocalName]; } foreach (var attribute in e.Attributes()) { target[attribute.Name.LocalName] = attribute.Value; } var eldict = e.Elements().GroupBy(_ => _.Name.LocalName, _ => _); foreach (var grp in eldict) { foreach (var element in grp.OrderBy(_ => _.Attr("code"))) { var item = XmlToUson(element, false); target[grp.Key].push(item); } } return(result); }
/// <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 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> /// <returns></returns> public static UObj ToUson(this object obj) { if (obj is UObj) { return((UObj)obj); } if (obj is XElement) { return(XmlToUson((XElement)obj, true)); } var result = UObjSerializerSupport.ToUson(obj); if (result is UObj) { return((UObj)result); } var real = new UObj(); real.Properties["__value"] = obj; real.UObjMode = UObjMode.Value; return(real); }
/// <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="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="writer"></param> /// <param name="mode"></param> public static void WriteXml(UObj obj, XmlWriter writer, UObjSerializeMode mode) { WriteXml(obj, "result", null, writer, mode); }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="parent"></param> /// <returns></returns> public static object ToUson(object obj, UObj parent = null) { if (obj == null || obj is string || obj.GetType().IsValueType) { return(obj); } var result = new UObj { Parent = parent, _srctype = obj.GetType() }; if (obj is UObj) { result.UObjMode = (obj as UObj).UObjMode; foreach (var p in ((UObj)obj).Properties) { result.Properties[p.Key] = ToUson(p.Value); } return(result); } if (obj is JsonItem) { if (obj is JsonObject) { foreach (var p in ((JsonObject)obj).Properties) { result.Properties[p.Name.ToString()] = ToUson(p.Value); } } else if (obj is JsonArray) { foreach (var p in ((JsonObject)obj).Properties) { result.Properties[p.Name.ToString()] = ToUson(p.Value); } } else if (obj is JsonValue) { var jv = (JsonValue)obj; if (jv.Type == JsonTokenType.String) { return(jv.Value); } if (jv.Type == JsonTokenType.Number) { return(Decimal.Parse(jv.Value)); } if (jv.Type == JsonTokenType.Null) { return(null); } if (jv.Type == JsonTokenType.Bool) { return(Boolean.Parse(jv.Value)); } return(jv.Value); } } if (obj is Array) { result.UObjMode = UObjMode.Array; foreach (var item in ((IEnumerable)obj)) { result.Array.Add(ToUson(item)); } } else if (obj.GetType().Name.StartsWith("Dictionary")) { result.UObjMode = UObjMode.Default; foreach (dynamic item in ((IEnumerable)obj)) { result.Properties[item.Key.ToString()] = item.Value; } } else if (obj.GetType().Name.StartsWith("List")) { result.UObjMode = UObjMode.Array; foreach (var item in ((IEnumerable)obj)) { result.Array.Add(ToUson(item)); } } else { foreach (var p in SerializableItem.GetSerializableItems(obj)) { result.Properties[p.Name] = ToUson(p.Value); } } return(result); }
// static readonly object locker = new object(); /// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="parent"></param> /// <param name="noParseJson"></param> /// <returns></returns> public static object ToUson(object obj, UObj parent = null, bool noParseJson = false) { // lock (locker){ if (obj is string && !noParseJson) { var s = obj.ToString().Trim(); if (0 != s.Length) { if (s[0] == '{' && s[s.Length - 1] == '}') { return(new JsonParser().Parse(s).ToUson()); } } } if (obj is string) { var s = obj as string; if (s == "0") { return(0m); } var dec = s.ToDecimal(true); if (dec != 0 && dec.ToString(CultureInfo.InvariantCulture) == s) { return(dec); } return(s); } if (obj == null || obj.GetType().IsValueType) { return(obj); } var result = new UObj { Parent = parent, _srctype = obj.GetType() }; if (obj is UObj) { result.UObjMode = (obj as UObj).UObjMode; foreach (var p in ((UObj)obj).Properties) { result.Properties[p.Key] = ToUson(p.Value, null, noParseJson); } foreach (var p in ((UObj)obj).Array) { result.Array.Add(p); } return(result); } if (obj is JsonItem) { if (obj is JsonObject) { foreach (var p in ((JsonObject)obj).Properties) { result.Properties[p.Name.Value] = ToUson(p.Value, null, noParseJson); } return(result); } else if (obj is JsonArray) { foreach (var p in ((JsonArray)obj).Values) { result.Array.Add(ToUson(p.Value, null, noParseJson)); } return(result); } else if (obj is JsonValue) { var jv = (JsonValue)obj; if (jv.Type == JsonTokenType.String) { return(ToUson(jv.Value, null, noParseJson)); } if (jv.Type == JsonTokenType.Number) { return(decimal.Parse(jv.Value)); } if (jv.Type == JsonTokenType.Null) { return(null); } if (jv.Type == JsonTokenType.Bool) { return(Boolean.Parse(jv.Value)); } return(ToUson(jv.Value, null, noParseJson)); } } if (obj is Array) { result.UObjMode = UObjMode.Array; foreach (var item in ((IEnumerable)obj)) { result.Array.Add(ToUson(item, null, noParseJson)); } } else if (obj.GetType().Name.StartsWith("Dictionary")) { result.UObjMode = UObjMode.Default; foreach (dynamic item in ((IEnumerable)obj)) { result.Properties[item.Key.ToString()] = ToUson(item.Value); } } else if (obj.GetType().Name.StartsWith("List")) { result.UObjMode = UObjMode.Array; foreach (var item in ((IEnumerable)obj)) { result.Array.Add(ToUson(item, null, noParseJson)); } } else { foreach (var p in SerializableItem.GetSerializableItems(obj)) { result.Properties[p.Name] = ToUson(p.Value, null, noParseJson); } } return(result); // } }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="safe"></param> /// <param name="deep"></param> /// <returns></returns> private UObj extend(object obj, bool safe, bool deep) { UObj other = (obj as UObj) ?? (UObjSerializerSupport.ToUson(obj) as UObj); if (null == other) { return(this); } if (other.UObjMode != UObjMode.Fake) { if (UObjMode == UObjMode.Fake || IsFakeAble()) { this.UObjMode = other.UObjMode; } if (this.UObjMode != other.UObjMode) { throw new Exception("cannot extend distinct USON object types (arrays and objects)"); } if (this.UObjMode == UObjMode.Default) { foreach (var property in other.Properties) { if (this.Properties.ContainsKey(property.Key)) { if (deep && this.Properties[property.Key] is UObj && property.Value is UObj) { (this.Properties[property.Key] as UObj).extend(property.Value as UObj, safe, deep); continue; } else if (safe) { continue; } } this.Properties[property.Key] = UObjSerializerSupport.ToUson(property.Value); } } else { for (var i = 0; i < other.Array.Count; i++) { if (Array.Count <= i) { Array.Add(null); } if (null != this.Array[i]) { if (deep && this.Array[i] is UObj && other.Array[i] is UObj) { (this.Array[i] as UObj).extend(other.Array[i] as UObj, safe, deep); continue; } else if (safe) { continue; } } this.Array[i] = UObjSerializerSupport.ToUson(other.Array[i]); } } } return(this); }