Support reference preservation for ISerializable objects.

This commit is contained in:
Inverness
2014-11-09 17:31:10 -06:00
parent 7b4995523b
commit 45d5e90da1
5 changed files with 157 additions and 31 deletions
@@ -58,9 +58,6 @@ namespace Newtonsoft.Json.Serialization
}
private JsonSerializerProxy _internalSerializer;
#if !(NETFX_CORE || PORTABLE40 || PORTABLE)
private JsonFormatterConverter _formatterConverter;
#endif
public JsonSerializerInternalReader(JsonSerializer serializer)
: base(serializer)
@@ -193,16 +190,6 @@ namespace Newtonsoft.Json.Serialization
return _internalSerializer;
}
#if !(NETFX_CORE || PORTABLE40 || PORTABLE)
private JsonFormatterConverter GetFormatterConverter()
{
if (_formatterConverter == null)
_formatterConverter = new JsonFormatterConverter(GetInternalSerializer());
return _formatterConverter;
}
#endif
private JToken CreateJToken(JsonReader reader, JsonContract contract)
{
ValidationUtils.ArgumentNotNull(reader, "reader");
@@ -1468,7 +1455,7 @@ namespace Newtonsoft.Json.Serialization
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Deserializing {0} using ISerializable constructor.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)), null);
SerializationInfo serializationInfo = new SerializationInfo(contract.UnderlyingType, GetFormatterConverter());
SerializationInfo serializationInfo = new SerializationInfo(contract.UnderlyingType, new JsonFormatterConverter(this, contract, member));
bool finished = false;
do
@@ -1479,17 +1466,7 @@ namespace Newtonsoft.Json.Serialization
string memberName = reader.Value.ToString();
if (!reader.Read())
throw JsonSerializationException.Create(reader, "Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, memberName));
if (reader.TokenType == JsonToken.StartObject)
{
// this will read any potential type names embedded in json
object o = CreateObject(reader, null, null, null, contract, member, null);
serializationInfo.AddValue(memberName, o);
}
else
{
serializationInfo.AddValue(memberName, JToken.ReadFrom(reader));
}
serializationInfo.AddValue(memberName, JToken.ReadFrom(reader));
break;
case JsonToken.Comment:
break;
@@ -1516,8 +1493,33 @@ namespace Newtonsoft.Json.Serialization
OnDeserializing(reader, contract, createdObject);
OnDeserialized(reader, contract, createdObject);
// According to documentation this method is called after the entire object graph is deserialized,
// however doing this isn't compatible with the type of serialization model used by JSON.
// The creation of an object with $id would end up postponed, causing references to it in the object graph
// to fail. The .NET Framework shows that it's valid to call this on-demand before that point if another
// object depends on this one, which is what will be done here in every case.
if (contract.HasDeserializationCallback)
((IDeserializationCallback) createdObject).OnDeserialization(null);
return createdObject;
}
internal object CreateISerializableItem(JToken token, Type type, JsonISerializableContract contract, JsonProperty member)
{
JsonContract itemContract = GetContractSafe(type);
JsonConverter itemConverter = GetConverter(itemContract, null, contract, member);
JsonReader tokenReader = token.CreateReader();
CheckedRead(tokenReader); // Move to first token
object result;
if (itemConverter != null && itemConverter.CanRead)
result = DeserializeConvertable(itemConverter, tokenReader, type, null);
else
result = CreateValueInternal(tokenReader, type, itemContract, null, contract, member, null);
return result;
}
#endif
#if !(NET35 || NET20 || PORTABLE40)