-Fix passed in JsonConverter not overriding internally specified JsonConverter
This commit is contained in:
@@ -102,6 +102,40 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
}
|
||||
]", json);
|
||||
}
|
||||
|
||||
public class TestDataTableConverter : JsonConverter
|
||||
{
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
DataTable d = (DataTable) value;
|
||||
writer.WriteValue(d.TableName);
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
|
||||
{
|
||||
//reader.Read();
|
||||
DataTable d = new DataTable((string)reader.Value);
|
||||
|
||||
return d;
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return (objectType == typeof (DataTable));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PassedInJsonConverterOverridesInternalConverter()
|
||||
{
|
||||
DataTable t1 = new DataTable("Custom");
|
||||
|
||||
string json = JsonConvert.SerializeObject(t1, Formatting.Indented, new TestDataTableConverter());
|
||||
Assert.AreEqual(@"""Custom""", json);
|
||||
|
||||
DataTable t2 = JsonConvert.DeserializeObject<DataTable>(json, new TestDataTableConverter());
|
||||
Assert.AreEqual(t1.TableName, t2.TableName);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -181,14 +181,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
/// <returns></returns>
|
||||
protected virtual JsonConverter ResolveContractConverter(Type objectType)
|
||||
{
|
||||
// check for an attribute first
|
||||
JsonConverter converter = JsonTypeReflector.GetJsonConverter(objectType, objectType);
|
||||
if (converter != null)
|
||||
return converter;
|
||||
|
||||
// then see whether object is compadible with any of the built in converters
|
||||
JsonSerializer.HasMatchingConverter(BuiltInConverters, objectType, out converter);
|
||||
return converter;
|
||||
return JsonTypeReflector.GetJsonConverter(objectType, objectType);
|
||||
}
|
||||
|
||||
private void InitializeContract(JsonContract contract)
|
||||
@@ -210,6 +203,11 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
contract.Converter = ResolveContractConverter(contract.UnderlyingType);
|
||||
|
||||
// then see whether object is compadible with any of the built in converters
|
||||
JsonConverter internalConverter;
|
||||
if (JsonSerializer.HasMatchingConverter(BuiltInConverters, contract.UnderlyingType, out internalConverter))
|
||||
contract.InternalConverter = internalConverter;
|
||||
|
||||
if (ReflectionUtils.HasDefaultConstructor(contract.CreatedType, true)
|
||||
|| contract.CreatedType.IsValueType)
|
||||
{
|
||||
|
||||
@@ -59,6 +59,10 @@ namespace Newtonsoft.Json.Serialization
|
||||
/// <value>The converter.</value>
|
||||
public JsonConverter Converter { get; set; }
|
||||
|
||||
// internally specified JsonConverter's to override default behavour
|
||||
// checked for after passed in converters and attribute specified converters
|
||||
internal JsonConverter InternalConverter { get; set; }
|
||||
|
||||
#if !PocketPC
|
||||
/// <summary>
|
||||
/// Gets or sets the method called immediately after deserialization of the object.
|
||||
|
||||
@@ -168,15 +168,22 @@ namespace Newtonsoft.Json.Serialization
|
||||
{
|
||||
JsonConverter converter;
|
||||
|
||||
// member attribute converter
|
||||
if (memberConverter != null)
|
||||
return memberConverter.ReadJson(reader, objectType, GetInternalSerializer());
|
||||
|
||||
// class attribute converter
|
||||
if (contract != null && contract.Converter != null)
|
||||
return contract.Converter.ReadJson(reader, objectType, GetInternalSerializer());
|
||||
|
||||
// passed in converters
|
||||
if (objectType != null && Serializer.HasMatchingConverter(objectType, out converter))
|
||||
return converter.ReadJson(reader, objectType, GetInternalSerializer());
|
||||
|
||||
// internally specified converter
|
||||
if (contract != null && contract.InternalConverter != null)
|
||||
return contract.InternalConverter.ReadJson(reader, objectType, GetInternalSerializer());
|
||||
|
||||
if (contract is JsonLinqContract)
|
||||
return CreateJToken(reader, contract);
|
||||
|
||||
|
||||
@@ -92,7 +92,8 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
if (converter != null
|
||||
|| ((converter = contract.Converter) != null)
|
||||
|| Serializer.HasMatchingConverter(contract.UnderlyingType, out converter))
|
||||
|| Serializer.HasMatchingConverter(contract.UnderlyingType, out converter)
|
||||
|| ((converter = contract.InternalConverter) != null))
|
||||
{
|
||||
SerializeConvertable(writer, converter, value, contract);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user