Ejemplo n.º 1
0
        public void WriteValue(object obj, KnownTypeSerializer serializer)
        {
            WritePrefix();

            // write value
            if (obj == null)
            {
                writer.Write("null");
            }
            else if (obj is string)
            {
                WriteString((string)obj);
            }
            else if (obj is bool)
            {
                writer.Write(((bool)obj) ? "true" : "false");
            }
            else
            {
                var t = obj.GetType();
                if (t.IsEnum)
                {
                    var eValue = Convert.ChangeType(obj, Enum.GetUnderlyingType(t));
                    writer.Write(eValue); // Enum as WriteNumber
                    return;
                }

                switch (Type.GetTypeCode(t))
                {
                case TypeCode.SByte:
                    writer.Write((sbyte)obj);
                    break;

                case TypeCode.Byte:
                    writer.Write((byte)obj);
                    break;

                case TypeCode.Int16:
                    writer.Write((Int16)obj);
                    break;

                case TypeCode.UInt16:
                    writer.Write((UInt16)obj);
                    break;

                case TypeCode.Int32:
                    writer.Write((Int32)obj);
                    break;

                case TypeCode.UInt32:
                    writer.Write((UInt32)obj);
                    break;

                case TypeCode.Int64:
                    writer.Write((Int64)obj);
                    break;

                case TypeCode.UInt64:
                    writer.Write((UInt64)obj);
                    break;

                case TypeCode.Single:
                    writer.Write((Single)obj);
                    break;

                case TypeCode.Double:
                    writer.Write((Double)obj);
                    break;

                case TypeCode.Decimal:
                    writer.Write((Decimal)obj);
                    break;

                default:
                    string result;
                    if (serializer.TrySerialize(t, obj, out result))
                    {
                        WriteString(result);
                    }
                    else
                    {
                        WriteString(obj.ToString());
                    }
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        public void WriteValue(object obj, KnownTypeSerializer serializer)
        {
            WritePrefix();

            // write value
            if (obj == null)
            {
                writer.Write("null");
            }
            else if (obj is string)
            {
                WriteString((string)obj);
            }
            else if (obj is bool)
            {
                writer.Write(((bool)obj) ? "true" : "false");
            }
            else
            {
                var t = obj.GetType();
                if (t.IsEnum)
                {
                    var eValue = Convert.ChangeType(obj, Enum.GetUnderlyingType(t));
                    writer.Write(eValue); // Enum as WriteNumber
                    return;
                }

                switch (Type.GetTypeCode(t))
                {
                    case TypeCode.SByte:
                        writer.Write((sbyte)obj);
                        break;
                    case TypeCode.Byte:
                        writer.Write((byte)obj);
                        break;
                    case TypeCode.Int16:
                        writer.Write((Int16)obj);
                        break;
                    case TypeCode.UInt16:
                        writer.Write((UInt16)obj);
                        break;
                    case TypeCode.Int32:
                        writer.Write((Int32)obj);
                        break;
                    case TypeCode.UInt32:
                        writer.Write((UInt32)obj);
                        break;
                    case TypeCode.Int64:
                        writer.Write((Int64)obj);
                        break;
                    case TypeCode.UInt64:
                        writer.Write((UInt64)obj);
                        break;
                    case TypeCode.Single:
                        writer.Write((Single)obj);
                        break;
                    case TypeCode.Double:
                        writer.Write((Double)obj);
                        break;
                    case TypeCode.Decimal:
                        writer.Write((Decimal)obj);
                        break;
                    default:
                        string result;
                        if (serializer.TrySerialize(t, obj, out result))
                        {
                            WriteString(result);
                        }
                        else
                        {
                            WriteString(obj.ToString());
                        }
                        break;
                }
            }
        }