Allow readonly properties to serialize as references if they have corresponding creator parameters

This commit is contained in:
TylerBrinkley
2016-09-15 09:33:44 -05:00
parent fac72ad50c
commit 954057ad17
2 changed files with 64 additions and 4 deletions
@@ -577,8 +577,8 @@ namespace Newtonsoft.Json.Serialization
writer.WriteStartObject();
bool isReference = ResolveIsReference(contract, member, collectionContract, containerProperty) ?? HasFlag(Serializer._preserveReferencesHandling, PreserveReferencesHandling.Objects);
// don't make readonly fields the referenced value because they can't be deserialized to
if (isReference && (member == null || member.Writable))
// don't make readonly fields that aren't creator parameters the referenced value because they can't be deserialized to
if (isReference && (member == null || member.Writable || ((collectionContract as JsonObjectContract)?.CreatorParameters.Contains(member.PropertyName) ?? false)))
{
WriteReferenceIdProperty(writer, contract.UnderlyingType, value);
}
@@ -804,8 +804,8 @@ namespace Newtonsoft.Json.Serialization
private bool WriteStartArray(JsonWriter writer, object values, JsonArrayContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
{
bool isReference = ResolveIsReference(contract, member, containerContract, containerProperty) ?? HasFlag(Serializer._preserveReferencesHandling, PreserveReferencesHandling.Arrays);
// don't make readonly fields the referenced value because they can't be deserialized to
isReference = (isReference && (member == null || member.Writable));
// don't make readonly fields that aren't creator parameters the referenced value because they can't be deserialized to
isReference = (isReference && (member == null || member.Writable || ((containerContract as JsonObjectContract)?.CreatorParameters.Contains(member.PropertyName) ?? false)));
bool includeTypeDetails = ShouldWriteType(TypeNameHandling.Arrays, contract, member, containerContract, containerProperty);
bool writeMetadataObject = isReference || includeTypeDetails;