public override void WriteJson( JsonWriter writer, object value, JsonSerializer serializer) { Type valueType = value.GetType(); StrongValueTypeAttribute valueTypeAttribute = GetStrongValueTypeAttribute(value.GetType()); if (valueTypeAttribute.ValueType == typeof(int)) { MethodInfo cast = valueType.GetMethod("op_Explicit", new Type[] { valueType }); int intValue = (int)cast.Invoke(null, new object[] { value }); writer.WriteValue(intValue); } else if (valueTypeAttribute.ValueType == typeof(long)) { MethodInfo cast = valueType.GetMethod("op_Explicit", new Type[] { valueType }); long longValue = (long)cast.Invoke(null, new object[] { value }); writer.WriteValue(longValue); } else { throw new NotImplementedException(); } }
public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { Type strongType = RemoveNullableWrapperIfPresent(objectType); StrongValueTypeAttribute valueTypeAttribute = GetStrongValueTypeAttribute(strongType); if (valueTypeAttribute.ValueType == typeof(int)) { long?longValue = (long?)reader.Value; return(CastToNullableStrongType(strongType, (int?)longValue)); } else if (valueTypeAttribute.ValueType == typeof(long)) { long?longValue = (long?)reader.Value; return(CastToNullableStrongType(strongType, longValue)); } throw new NotImplementedException(); }