/// <summary> /// Reads the JSON representation of the object. /// </summary> /// <param name="reader">The <see cref="JsonReader"/> to read from.</param> /// <param name="objectType">Type of the object.</param> /// <param name="existingValue">The existing value of object being read.</param> /// <param name="serializer">The calling serializer.</param> /// <returns>The object value.</returns> public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { if (!ReflectionUtils.IsNullable(objectType)) { throw JsonSerializationException.Create(reader, "Cannot convert null value to {0}.".FormatWith(CultureInfo.InvariantCulture, objectType)); } return(null); } byte[] data; if (reader.TokenType == JsonToken.StartArray) { data = ReadByteArray(reader); } else if (reader.TokenType == JsonToken.String) { // current token is already at base64 string // unable to call ReadAsBytes so do it the old fashion way string encodedData = reader.Value.ToString(); data = Convert.FromBase64String(encodedData); } else { throw JsonSerializationException.Create(reader, "Unexpected token parsing binary. Expected String or StartArray, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType)); } Type t = (ReflectionUtils.IsNullableType(objectType)) ? Nullable.GetUnderlyingType(objectType) : objectType; #if !NET20 if (t.AssignableToTypeName(BinaryTypeName)) { EnsureReflectionObject(t); return(_reflectionObject.Creator(data)); } #endif if (t == typeof(SqlBinary)) { return(new SqlBinary(data)); } throw JsonSerializationException.Create(reader, "Unexpected object type when writing binary: {0}".FormatWith(CultureInfo.InvariantCulture, objectType)); }
/// <summary> /// Reads the JSON representation of the object. /// </summary> /// <param name="reader">The <see cref="JsonReader"/> to read from.</param> /// <param name="objectType">Type of the object.</param> /// <param name="existingValue">The existing value of object being read.</param> /// <param name="serializer">The calling serializer.</param> /// <returns>The object value.</returns> public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { if (!ReflectionUtils.IsNullableType(objectType)) { throw JsonSerializationException.Create(reader, "Cannot convert null value to KeyValuePair."); } return(null); } object key = null; object value = null; reader.ReadAndAssert(); Type t = ReflectionUtils.IsNullableType(objectType) ? Nullable.GetUnderlyingType(objectType) : objectType; ReflectionObject reflectionObject = ReflectionObjectPerType.Get(t); JsonContract keyContract = serializer.ContractResolver.ResolveContract(reflectionObject.GetType(KeyName)); JsonContract valueContract = serializer.ContractResolver.ResolveContract(reflectionObject.GetType(ValueName)); while (reader.TokenType == JsonToken.PropertyName) { string propertyName = reader.Value.ToString(); if (string.Equals(propertyName, KeyName, StringComparison.OrdinalIgnoreCase)) { reader.ReadForTypeAndAssert(keyContract, false); key = serializer.Deserialize(reader, keyContract.UnderlyingType); } else if (string.Equals(propertyName, ValueName, StringComparison.OrdinalIgnoreCase)) { reader.ReadForTypeAndAssert(valueContract, false); value = serializer.Deserialize(reader, valueContract.UnderlyingType); } else { reader.Skip(); } reader.ReadAndAssert(); } return(reflectionObject.Creator(key, value)); }
public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { if (!ReflectionUtils.IsNullableType(objectType)) { throw JsonSerializationException.Create(reader, "Cannot convert null value to KeyValuePair."); } return((object)null); } object obj1 = (object)null; object obj2 = (object)null; reader.ReadAndAssert(); Type key = ReflectionUtils.IsNullableType(objectType) ? Nullable.GetUnderlyingType(objectType) : objectType; ReflectionObject reflectionObject = KeyValuePairConverter.ReflectionObjectPerType.Get(key); while (reader.TokenType == JsonToken.PropertyName) { string a = reader.Value.ToString(); if (string.Equals(a, "Key", StringComparison.OrdinalIgnoreCase)) { reader.ReadAndAssert(); obj1 = serializer.Deserialize(reader, reflectionObject.GetType("Key")); } else if (string.Equals(a, "Value", StringComparison.OrdinalIgnoreCase)) { reader.ReadAndAssert(); obj2 = serializer.Deserialize(reader, reflectionObject.GetType("Value")); } else { reader.Skip(); } reader.ReadAndAssert(); } return(reflectionObject.Creator(new object[2] { obj1, obj2 })); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { EnsureReflectionObject(objectType); object target = _reflectionObject.Creator(new object[0]); ReadAndAssertProperty(reader, "Key"); reader.ReadAndAssert(); _reflectionObject.SetValue(target, "Key", reader.Value.ToString()); ReadAndAssertProperty(reader, "Type"); reader.ReadAndAssert(); Type type = Type.GetType(reader.Value.ToString()); ReadAndAssertProperty(reader, "Value"); reader.ReadAndAssert(); _reflectionObject.SetValue(target, "Value", serializer.Deserialize(reader, type)); reader.ReadAndAssert(); return(target); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { if (!ReflectionUtils.IsNullableType(objectType)) { throw JsonSerializationException.Create(reader, "Cannot convert null value to KeyValuePair."); } return(null); } object obj = null; object obj1 = null; reader.ReadAndAssert(); ReflectionObject reflectionObject = KeyValuePairConverter.ReflectionObjectPerType.Get((ReflectionUtils.IsNullableType(objectType) ? Nullable.GetUnderlyingType(objectType) : objectType)); JsonContract jsonContract = serializer.ContractResolver.ResolveContract(reflectionObject.GetType("Key")); JsonContract jsonContract1 = serializer.ContractResolver.ResolveContract(reflectionObject.GetType("Value")); while (reader.TokenType == JsonToken.PropertyName) { string str = reader.Value.ToString(); if (string.Equals(str, "Key", StringComparison.OrdinalIgnoreCase)) { reader.ReadForTypeAndAssert(jsonContract, false); obj = serializer.Deserialize(reader, jsonContract.UnderlyingType); } else if (!string.Equals(str, "Value", StringComparison.OrdinalIgnoreCase)) { reader.Skip(); } else { reader.ReadForTypeAndAssert(jsonContract1, false); obj1 = serializer.Deserialize(reader, jsonContract1.UnderlyingType); } reader.ReadAndAssert(); } return(reflectionObject.Creator(new object[] { obj, obj1 })); }
/// <summary> /// Reads the JSON representation of the object. /// </summary> /// <param name="reader">The <see cref="JsonReader"/> to read from.</param> /// <param name="objectType">Type of the object.</param> /// <param name="existingValue">The existing value of object being read.</param> /// <param name="serializer">The calling serializer.</param> /// <returns>The object value.</returns> public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { EnsureReflectionObject(objectType); object entityKeyMember = _reflectionObject.Creator(); ReadAndAssertProperty(reader, KeyPropertyName); ReadAndAssert(reader); _reflectionObject.SetValue(entityKeyMember, KeyPropertyName, reader.Value.ToString()); ReadAndAssertProperty(reader, TypePropertyName); ReadAndAssert(reader); string type = reader.Value.ToString(); Type t = Type.GetType(type); ReadAndAssertProperty(reader, ValuePropertyName); ReadAndAssert(reader); _reflectionObject.SetValue(entityKeyMember, ValuePropertyName, serializer.Deserialize(reader, t)); ReadAndAssert(reader); return(entityKeyMember); }