JsonPrimitiveContract
Contract details for a Type used by the JsonSerializer.
using Newtonsoft.Json.Utilities;
using System;
using System.Collections.Generic;
namespace Newtonsoft.Json.Serialization
{
public class JsonPrimitiveContract : JsonContract
{
private static readonly Dictionary<Type, ReadType> ReadTypeMap = new Dictionary<Type, ReadType> {
[typeof(byte[])] = ReadType.ReadAsBytes,
[typeof(byte)] = ReadType.ReadAsInt32,
[typeof(short)] = ReadType.ReadAsInt32,
[typeof(int)] = ReadType.ReadAsInt32,
[typeof(decimal)] = ReadType.ReadAsDecimal,
[typeof(bool)] = ReadType.ReadAsBoolean,
[typeof(string)] = ReadType.ReadAsString,
[typeof(DateTime)] = ReadType.ReadAsDateTime,
[typeof(DateTimeOffset)] = ReadType.ReadAsDateTimeOffset,
[typeof(float)] = ReadType.ReadAsDouble,
[typeof(double)] = ReadType.ReadAsDouble
};
internal PrimitiveTypeCode TypeCode { get; set; }
public JsonPrimitiveContract(Type underlyingType)
: base(underlyingType)
{
ContractType = JsonContractType.Primitive;
TypeCode = ConvertUtils.GetTypeCode(underlyingType);
IsReadOnlyOrFixedSize = true;
if (ReadTypeMap.TryGetValue(NonNullableUnderlyingType, out ReadType value))
InternalReadType = value;
}
}
}