/// <summary> /// /// </summary> /// <param name="binder"></param> /// <param name="value"></param> /// <returns></returns> public override bool TrySetMember(SetMemberBinder binder, object value) { var res = (Properties[binder.Name] = UObjSerializerSupport.ToUson(value)) as UObj; if (null != res) { if (res.UObjMode == UObjMode.Fake) { res.UObjMode = UObjMode.Default; } } else { this.UObjMode = UObjMode.Default; } return(true); }
/// <summary> /// /// </summary> /// <param name="args"></param> /// <returns></returns> public UObj push(params object[] args) { if (0 == args.Length) { throw new Exception("require at least one parameter"); } if (this.UObjMode == UObjMode.Fake || (this.UObjMode == UObjMode.Default && IsFakeAble())) { this.UObjMode = UObjMode.Array; } if (this.UObjMode != UObjMode.Array) { throw new Exception("this is not array to call push"); } foreach (var o in args) { this.Array.Add(UObjSerializerSupport.ToUson(o)); } return(this); }
/// <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="binder"></param> /// <param name="indexes"></param> /// <param name="value"></param> /// <returns></returns> public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value) { if (this.UObjMode == UObjMode.Fake) { this.UObjMode = UObjMode.Default; } var idx = indexes[0]; if (this.UObjMode == UObjMode.Default) { if (idx is int && IsFakeAble()) { this.UObjMode = UObjMode.Array; } else { this.Properties[idx.ToString()] = UObjSerializerSupport.ToUson(value); } } if (this.UObjMode == UObjMode.Array) { if (!(idx is int)) { throw new Exception("cannot index arrays with not ints"); } if (((int)idx) < 0) { throw new Exception("index of array cannot be negative"); } while (((int)idx) >= Array.Count) { Array.Add(null); } Array[(int)idx] = UObjSerializerSupport.ToUson(value); } return(true); }
/// <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); }