Exemple #1
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);
Exemple #2
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;
#if !NET20
        case PrimitiveTypeCode.DateTimeOffset:
          writer.WriteValue((DateTimeOffset)value);
          break;
        case PrimitiveTypeCode.DateTimeOffsetNullable:
          writer.WriteValue((value == null) ? (DateTimeOffset?)null : (DateTimeOffset)value);
          break;
#endif
        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;
#if !(PORTABLE || PORTABLE40 || NET35 || NET20 || WINDOWS_PHONE || SILVERLIGHT)
        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;
#endif
        case PrimitiveTypeCode.Uri:
          writer.WriteValue((Uri)value);
          break;
        case PrimitiveTypeCode.String:
          writer.WriteValue((string)value);
          break;
        case PrimitiveTypeCode.Bytes:
          writer.WriteValue((byte[])value);
          break;
#if !(PORTABLE || NETFX_CORE)
        case PrimitiveTypeCode.DBNull:
          writer.WriteNull();
          break;
#endif
        default:
#if !(PORTABLE || NETFX_CORE)
          if (value is IConvertible)
          {
            // the value is a non-standard IConvertible
            // convert to the underlying value and retry
            IConvertible convertable = (IConvertible)value;
            TypeInformation typeInformation = ConvertUtils.GetTypeInformation(convertable);
            object convertedValue = convertable.ToType(typeInformation.Type, CultureInfo.InvariantCulture);

            WriteValue(writer, typeInformation.TypeCode, convertedValue);
          }
          else
#endif
          {
            throw CreateUnsupportedTypeException(writer, value);
          }
          break;
      }
    }
Exemple #3
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);
 }