Beispiel #1
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));
        }
Beispiel #2
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));
        }