Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        public override string Serialize(object o, Type t)
        {
            StringBuilder bld = new StringBuilder();

            if (o is IEnumerable en)
            {
                bld.Append("[");
                bool add = false;
                foreach (object on in en)
                {
                    add = true;
                    if (KeyType != null)
                    {
                        bld.Append(KeyType.Serialize(on, t.GetGenericArguments()[0]));
                    }
                    else
                    {
                        SophiaType sp = SophiaMapper.GetSophiaPossibleTypeFromType(t.GetGenericArguments()[0]);
                        bld.Append(sp.Serialize(on, t.GetGenericArguments()[0]));
                    }

                    bld.Append(", ");
                }

                if (add)
                {
                    bld.Remove(bld.Length - 2, 2);
                }
                bld.Append("]");
                return(bld.ToString());
            }

            return(base.Serialize(o, t));
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        public override string Serialize(object o, Type t)
        {
            string fullname = t.FullName;

            if (fullname != null && fullname.StartsWith("System.ValueTuple"))
            {
                StringBuilder bld = new StringBuilder();
                bld.Append("(");
                bool add = false;
                int  x   = 0;
                if (TupleTypes.Count > 0 && TupleTypes.Count != t.GetFields().Length)
                {
                    throw new ArgumentException($"The object has {t.GetFields().Length} in the tuple, the sophia type has {TupleTypes.Count}");
                }
                foreach (FieldInfo f in t.GetFields())
                {
                    add = true;
                    if (TupleTypes.Count > 0)
                    {
                        bld.Append(TupleTypes[x].Serialize(f.GetValue(o), f.FieldType));
                    }
                    else
                    {
                        SophiaType sp = SophiaMapper.GetSophiaPossibleTypeFromType(f.FieldType);
                        bld.Append(sp.Serialize(f.GetValue(o), f.FieldType));
                    }

                    bld.Append(", ");
                }

                if (add)
                {
                    bld.Remove(bld.Length - 2, 2);
                }
                bld.Append(")");
                return(bld.ToString());
            }

            return(base.Serialize(o, t));
        }
Exemplo n.º 5
0
        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));
        }
Exemplo n.º 6
0
 public ListType(string mapname, SophiaType keytype) : base(mapname)
 {
     KeyType = keytype;
 }
Exemplo n.º 7
0
 public MapType(string mapname, SophiaType keytype, SophiaType valuetype) : base(mapname)
 {
     KeyType   = keytype;
     ValueType = valuetype;
 }