示例#1
0
 private static JsonWriterException CreateUnsupportedTypeException(JsonWriter writer, object value)
 {
     return JsonWriterException.Create(writer, "Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()), null);
 }
 internal static JsonWriterException Create(JsonWriter writer, string message, Exception ex)
 {
     return Create(writer.ContainerPath, message, ex);
 }
示例#3
0
 internal static void WriteValue(JsonWriter writer, PrimitiveTypeCode typeCode, object value)
 {
     switch (typeCode)
     {
         case PrimitiveTypeCode.Char:
             writer.WriteValue((char)value);
             break;
         case PrimitiveTypeCode.CharNullable:
             writer.WriteValue((value == null) ? (char?)null : (char)value);
             break;
         case PrimitiveTypeCode.Boolean:
             writer.WriteValue((bool)value);
             break;
         case PrimitiveTypeCode.BooleanNullable:
             writer.WriteValue((value == null) ? (bool?)null : (bool)value);
             break;
         case PrimitiveTypeCode.SByte:
             writer.WriteValue((sbyte)value);
             break;
         case PrimitiveTypeCode.SByteNullable:
             writer.WriteValue((value == null) ? (sbyte?)null : (sbyte)value);
             break;
         case PrimitiveTypeCode.Int16:
             writer.WriteValue((short)value);
             break;
         case PrimitiveTypeCode.Int16Nullable:
             writer.WriteValue((value == null) ? (short?)null : (short)value);
             break;
         case PrimitiveTypeCode.UInt16:
             writer.WriteValue((ushort)value);
             break;
         case PrimitiveTypeCode.UInt16Nullable:
             writer.WriteValue((value == null) ? (ushort?)null : (ushort)value);
             break;
         case PrimitiveTypeCode.Int32:
             writer.WriteValue((int)value);
             break;
         case PrimitiveTypeCode.Int32Nullable:
             writer.WriteValue((value == null) ? (int?)null : (int)value);
             break;
         case PrimitiveTypeCode.Byte:
             writer.WriteValue((byte)value);
             break;
         case PrimitiveTypeCode.ByteNullable:
             writer.WriteValue((value == null) ? (byte?)null : (byte)value);
             break;
         case PrimitiveTypeCode.UInt32:
             writer.WriteValue((uint)value);
             break;
         case PrimitiveTypeCode.UInt32Nullable:
             writer.WriteValue((value == null) ? (uint?)null : (uint)value);
             break;
         case PrimitiveTypeCode.Int64:
             writer.WriteValue((long)value);
             break;
         case PrimitiveTypeCode.Int64Nullable:
             writer.WriteValue((value == null) ? (long?)null : (long)value);
             break;
         case PrimitiveTypeCode.UInt64:
             writer.WriteValue((ulong)value);
             break;
         case PrimitiveTypeCode.UInt64Nullable:
             writer.WriteValue((value == null) ? (ulong?)null : (ulong)value);
             break;
         case PrimitiveTypeCode.Single:
             writer.WriteValue((float)value);
             break;
         case PrimitiveTypeCode.SingleNullable:
             writer.WriteValue((value == null) ? (float?)null : (float)value);
             break;
         case PrimitiveTypeCode.Double:
             writer.WriteValue((double)value);
             break;
         case PrimitiveTypeCode.DoubleNullable:
             writer.WriteValue((value == null) ? (double?)null : (double)value);
             break;
         case PrimitiveTypeCode.DateTime:
             writer.WriteValue((DateTime)value);
             break;
         case PrimitiveTypeCode.DateTimeNullable:
             writer.WriteValue((value == null) ? (DateTime?)null : (DateTime)value);
             break;
         case PrimitiveTypeCode.DateTimeOffset:
             writer.WriteValue((DateTimeOffset)value);
             break;
         case PrimitiveTypeCode.DateTimeOffsetNullable:
             writer.WriteValue((value == null) ? (DateTimeOffset?)null : (DateTimeOffset)value);
             break;
         case PrimitiveTypeCode.Decimal:
             writer.WriteValue((decimal)value);
             break;
         case PrimitiveTypeCode.DecimalNullable:
             writer.WriteValue((value == null) ? (decimal?)null : (decimal)value);
             break;
         case PrimitiveTypeCode.Guid:
             writer.WriteValue((Guid)value);
             break;
         case PrimitiveTypeCode.GuidNullable:
             writer.WriteValue((value == null) ? (Guid?)null : (Guid)value);
             break;
         case PrimitiveTypeCode.TimeSpan:
             writer.WriteValue((TimeSpan)value);
             break;
         case PrimitiveTypeCode.TimeSpanNullable:
             writer.WriteValue((value == null) ? (TimeSpan?)null : (TimeSpan)value);
             break;
         case PrimitiveTypeCode.BigInteger:
             // this will call to WriteValue(object)
             writer.WriteValue((BigInteger)value);
             break;
         case PrimitiveTypeCode.BigIntegerNullable:
             // this will call to WriteValue(object)
             writer.WriteValue((value == null) ? (BigInteger?)null : (BigInteger)value);
             break;
         case PrimitiveTypeCode.Uri:
             writer.WriteValue((Uri)value);
             break;
         case PrimitiveTypeCode.String:
             writer.WriteValue((string)value);
             break;
         case PrimitiveTypeCode.Bytes:
             writer.WriteValue((byte[])value);
             break;
         case PrimitiveTypeCode.DBNull:
             writer.WriteNull();
             break;
         default:
             var convertible = value as IConvertible;
             if (convertible == null) throw CreateUnsupportedTypeException(writer, value);
             // the value is a non-standard IConvertible
             // convert to the underlying value and retry
             var convertable = convertible;
             var typeInformation = ConvertUtils.GetTypeInformation(convertable);
             // if convertable has an underlying typecode of Object then attempt to convert it to a string
             var resolvedTypeCode = (typeInformation.TypeCode == PrimitiveTypeCode.Object) ? PrimitiveTypeCode.String : typeInformation.TypeCode;
             var resolvedType = (typeInformation.TypeCode == PrimitiveTypeCode.Object) ? typeof (string) : typeInformation.Type;
             var convertedValue = convertable.ToType(resolvedType, CultureInfo.InvariantCulture);
             WriteValue(writer, resolvedTypeCode, convertedValue);
             break;
     }
 }
示例#4
0
 /// <summary>
 /// Writes the JSON representation of the object.
 /// </summary>
 /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
 /// <param name="value">The value.</param>
 /// <param name="serializer">The calling serializer.</param>
 public abstract void WriteJson(JsonWriter writer, object value, JsonSerializer serializer);