Example #1
0
        public string Serialize(object instance)
        {
            var wr = new SLJsonWriter();

            SerializeObject(wr, instance);
            return(wr.ToString());
        }
Example #2
0
        //------------------------------------------------------------------------

        public string Serialize(bool indented)
        {
            var wr = new SLJsonWriter(new StringBuilder(), indented);

            Serialize(wr);
            return(wr.ToString());
        }
        public void Serialize(SLJsonWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            switch (NodeType)
            {
            case SLJsonNodeType.Missing:
            case SLJsonNodeType.Null: writer.WriteValueNull(); break;

            case SLJsonNodeType.Array: SerializeArray(writer); break;

            case SLJsonNodeType.Object: SerializeObject(writer); break;

            case SLJsonNodeType.Boolean: writer.WriteValue(AsBoolean); break;

            case SLJsonNodeType.Number: writer.WriteValue(AsDouble); break;

            case SLJsonNodeType.String: writer.WriteValue(AsString); break;

            default: throw new NotImplementedException();
            }
        }
Example #4
0
        static string Serialize(object instance)
        {
            var wr = new SLJsonWriter();

            new SLJsonSerializer(wr).SerializeObject(instance);
            return(wr.ToString());
        }
Example #5
0
        //------------------------------------------------------------------------

        void SerializeArray(SLJsonWriter writer)
        {
            writer.BeginArray();
            foreach (SLJsonNode n in this)
            {
                n.Serialize(writer);
            }
            writer.EndArray();
        }
Example #6
0
        public SLJsonWriter SerializeObject(object instance)
        {
            if (m_Writer == null)
            {
                m_Writer = new SLJsonWriter();
            }

            SerializeObject(m_Writer, instance);
            return(m_Writer);
        }
Example #7
0
        void SerializeObject(SLJsonWriter writer)
        {
            writer.BeginObject();

            foreach (string n in Names)
            {
                writer.BeginField(n);
                this[n].Serialize(writer);
            }

            writer.EndObject();
        }
Example #8
0
        void SerializeProperty(SLJsonWriter writer, SLJsonMemberAttribute attribute, Type type, object value)
        {
            writer.BeginField(attribute.MemberName);
            switch (attribute.MemberType)
            {
            case SLJsonMemberType.Value: SerializeValue(writer, type, value); break;

            case SLJsonMemberType.Object: SerializeObject(writer, value); break;

            case SLJsonMemberType.ValueArray: SerializeArray(writer, type, value, false); break;

            case SLJsonMemberType.ObjectArray: SerializeArray(writer, type, value, true); break;

            default: throw new NotImplementedException();
            }
        }
Example #9
0
        void SerializeObject(SLJsonWriter writer, object instance)
        {
            if (instance == null)
            {
                writer.WriteValueNull();
            }
            else
            {
                writer.BeginObject();

                Type t = instance.GetType();

                foreach (FieldInfo fi in t.GetRuntimeFields())
                {
                    if (fi.IsPublic)
                    {
                        foreach (SLJsonMemberAttribute attr in fi.GetCustomAttributes(typeof(SLJsonMemberAttribute), false))
                        {
                            SerializeProperty(writer, attr, fi.FieldType, fi.GetValue(instance));
                        }
                    }
                }

                foreach (PropertyInfo pi in t.GetRuntimeProperties())
                {
                    if (pi.CanRead && pi.CanWrite)
                    {
                        MethodInfo mi1 = pi.GetGetMethod();
                        if (mi1 != null && mi1.IsPublic)
                        {
                            MethodInfo mi2 = pi.GetSetMethod();
                            if (mi2 != null && mi2.IsPublic)
                            {
                                foreach (SLJsonMemberAttribute attr in pi.GetCustomAttributes(typeof(SLJsonMemberAttribute), false))
                                {
                                    SerializeProperty(writer, attr, pi.PropertyType, pi.GetValue(instance, null));
                                }
                            }
                        }
                    }
                }

                writer.EndObject();
            }
        }
Example #10
0
        void SerializeArray(SLJsonWriter writer, Type type, object array, bool asObject)
        {
            if (type.IsArray && type.GetArrayRank() != 1)
            {
                throw new NotSupportedException("Multi-dimensional arrays are not supported");
            }

            if (!typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()))
            {
                throw new NotSupportedException("Type " + type.FullName + " does not implement IEnumerable");
            }

            if (array == null)
            {
                writer.WriteValue(null);
                return;
            }

            writer.BeginArray();

            var helper = (IEnumerable)array;

            foreach (object value in helper)
            {
                if (asObject)
                {
                    SerializeObject(writer, value);
                }
                else
                {
                    SerializeValue(writer, type, value);
                }
            }

            writer.EndArray();
        }
Example #11
0
 public void Serialize(object instance, SLJsonWriter writer)
 {
     SerializeObject(writer, instance);
 }
Example #12
0
 public SLJsonSerializer(SLJsonWriter writer)
 {
     m_Writer = writer;
 }
Example #13
0
        void SerializeValue(SLJsonWriter writer, Type type, object value)
        {
            SLJsonConverter <object, string> serialize;

            if (m_Converters.TryGetValue(type, out serialize))
            {
                string s = serialize(value);
                if (s == null)
                {
                    writer.WriteValueNull();
                }
                else
                {
                    writer.WriteValue(s);
                }
                return;
            }

            if (value == null)
            {
                writer.WriteValueNull();
            }
            else if (value is bool)
            {
                writer.WriteValue((bool)value);
            }
            else if (value is sbyte)
            {
                writer.WriteValue((sbyte)value);
            }
            else if (value is byte)
            {
                writer.WriteValue((byte)value);
            }
            else if (value is short)
            {
                writer.WriteValue((short)value);
            }
            else if (value is ushort)
            {
                writer.WriteValue((ushort)value);
            }
            else if (value is int)
            {
                writer.WriteValue((int)value);
            }
            else if (value is uint)
            {
                writer.WriteValue((uint)value);
            }
            else if (value is long)
            {
                writer.WriteValue((long)value);
            }
            else if (value is ulong)
            {
                writer.WriteValue((ulong)value);
            }
            else if (value is float)
            {
                writer.WriteValue((float)value);
            }
            else if (value is double)
            {
                writer.WriteValue((double)value);
            }
            else if (value is DateTime)
            {
                writer.WriteValue((DateTime)value);
            }
            else if (value is TimeSpan)
            {
                writer.WriteValue((TimeSpan)value);
            }
            else
            {
                string s = value as string;
                if (s == null)
                {
                    if (m_ThrowOnUnknownValueType)
                    {
                        throw new NotSupportedException("Unknown data type " + value.GetType().FullName + " is not supported");
                    }

                    s = ToStringInvariant(value);
                }

                writer.WriteValue(s);
            }
        }
 public SLJsonSerializer(SLJsonWriter writer)
 {
     m_Writer = writer ?? new SLJsonWriter();
 }
        //------------------------------------------------------------------------

        public SLJsonSerializer()
        {
            m_Writer = new SLJsonWriter();
        }