public override object Deserialize(string value, Type t) { value = value.Trim(); if (value.StartsWith("[") && value.EndsWith("]")) { value = value.Substring(1, value.Length - 2).Trim(); } string[] items = SophiaMapper.SplitByComma(value); IList en = (IList)Activator.CreateInstance(t); foreach (string s in items) { if (KeyType != null) { en.Add(KeyType.Deserialize(s, t.GetGenericArguments()[0])); } else { SophiaType sp = SophiaMapper.GetSophiaPossibleTypeFromType(t.GetGenericArguments()[0]); en.Add(sp.Deserialize(s, t.GetGenericArguments()[0])); } } return(en); }
public override object Deserialize(string value, Type t) { if (!t.IsClass) { throw new ArgumentException("Record can be only classes"); } value = value.Trim(); int idx = value.IndexOf('{'); if (idx > -1) { value = value.Substring(idx); } if (value.StartsWith("{") && value.EndsWith("}")) { value = value.Substring(1, value.Length - 2).Trim(); } string[] dicsplits = SophiaMapper.SplitByComma(value); List <(string, PropertyInfo)> props = OrderProps(t.GetTypeInfo()); object o = Activator.CreateInstance(t); foreach (string s in dicsplits) { string[] spl = SophiaMapper.SplitByTwoPoints(s); if (spl.Length != 2) { throw new ArgumentException($"Unable to record item {s}"); } string name = spl[0].Trim(); if (name.StartsWith("\"") && name.EndsWith("\"")) { name = name.Substring(1, name.Length - 2); } (string, PropertyInfo)prop = props.FirstOrDefault(a => a.Item1 == name); if (prop.Item1 == null) { throw new ArgumentException($"Unable to deserialize record, missing property {name} in class {t.Name}"); } if (!FieldTypes.ContainsKey(name)) { throw new ArgumentException($"Unable to deserialize record, missing property {name} in record definition {SophiaName}"); } SophiaType tp = FieldTypes[name]; object ob = tp.Deserialize(spl[1].Trim(), prop.Item2.PropertyType); prop.Item2.SetValue(o, ob); } return(o); }
public override object Deserialize(string value, Type t) { string fullname = t.FullName; if (fullname != null && fullname.StartsWith("System.ValueTuple")) { value = value.Trim(); if (value.StartsWith("[") && value.EndsWith("]")) { value = value.Substring(1, value.Length - 2).Trim(); } string[] items = SophiaMapper.SplitByComma(value); if (TupleTypes.Count > 0 && TupleTypes.Count != items.Length) { throw new ArgumentException($"The object has {t.GetFields().Length} in the tuple, the sophia type has {items.Length}"); } List <object> objs = new List <object>(); for (int x = 0; x < items.Length; x++) { if (TupleTypes.Count > 0) { objs.Add(TupleTypes[x].Deserialize(items[x], t.GetGenericArguments()[x])); } else { SophiaType sp = SophiaMapper.GetSophiaPossibleTypeFromType(t.GetGenericArguments()[x]); objs.Add(sp.Deserialize(items[x], t.GetGenericArguments()[x])); } } MethodInfo method = typeof(ValueType).GetMethods(BindingFlags.Static | BindingFlags.Public).ToList().First(a => a.Name == "Create" && a.GetParameters().Length == objs.Count); return(method.Invoke(null, objs.ToArray())); } return(base.Deserialize(value, t)); }