Ejemplo n.º 1
0
        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();
            }
        }
Ejemplo n.º 2
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();
            }
        }
Ejemplo n.º 3
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);
            }
        }