in src/Elastic.Apm/Libraries/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs [2128:2236]
private object PopulateObject(object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty? member, string? id)
{
OnDeserializing(reader, contract, newObject);
// only need to keep a track of properties' presence if they are required or a value should be defaulted if missing
var propertiesPresence = contract.HasRequiredOrDefaultValueProperties
|| HasFlag(Serializer._defaultValueHandling, DefaultValueHandling.Populate)
? contract.Properties.ToDictionary(m => m, m => PropertyPresence.None)
: null;
if (id != null) AddReference(reader, id, newObject);
var initialDepth = reader.Depth;
var finished = false;
do
switch (reader.TokenType)
{
case JsonToken.PropertyName:
{
var propertyName = reader.Value!.ToString();
if (CheckPropertyName(reader, propertyName)) continue;
try
{
// attempt exact case match first
// then try match ignoring case
var property = contract.Properties.GetClosestMatchProperty(propertyName);
if (property == null)
{
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Verbose)
TraceWriter.Trace(TraceLevel.Verbose,
JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path,
"Could not find member '{0}' on {1}".FormatWith(CultureInfo.InvariantCulture, propertyName,
contract.UnderlyingType)), null);
if ((contract.MissingMemberHandling ?? Serializer._missingMemberHandling) == MissingMemberHandling.Error)
throw JsonSerializationException.Create(reader,
"Could not find member '{0}' on object of type '{1}'".FormatWith(CultureInfo.InvariantCulture, propertyName,
contract.UnderlyingType.Name));
if (!reader.Read()) break;
SetExtensionData(contract, member, reader, propertyName, newObject);
continue;
}
if (property.Ignored || !ShouldDeserialize(reader, property, newObject))
{
if (!reader.Read()) break;
SetPropertyPresence(reader, property, propertiesPresence);
SetExtensionData(contract, member, reader, propertyName, newObject);
}
else
{
if (property.PropertyContract == null) property.PropertyContract = GetContractSafe(property.PropertyType);
var propertyConverter = GetConverter(property.PropertyContract, property.Converter, contract, member);
if (!reader.ReadForType(property.PropertyContract, propertyConverter != null))
throw JsonSerializationException.Create(reader,
"Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, propertyName));
SetPropertyPresence(reader, property, propertiesPresence);
// set extension data if property is ignored or readonly
if (!SetPropertyValue(property, propertyConverter, contract, member, reader, newObject))
SetExtensionData(contract, member, reader, propertyName, newObject);
}
}
catch (Exception ex)
{
if (IsErrorHandled(newObject, contract, propertyName, reader as IJsonLineInfo, reader.Path, ex))
HandleError(reader, true, initialDepth);
else
throw;
}
break;
}
case JsonToken.EndObject:
finished = true;
break;
case JsonToken.Comment:
// ignore
break;
default:
throw JsonSerializationException.Create(reader, "Unexpected token when deserializing object: " + reader.TokenType);
}
while (!finished && reader.Read());
if (!finished) ThrowUnexpectedEndException(reader, contract, newObject, "Unexpected end when deserializing object.");
if (propertiesPresence != null)
{
foreach (var propertyPresence in propertiesPresence)
{
var property = propertyPresence.Key;
var presence = propertyPresence.Value;
EndProcessProperty(newObject, reader, contract, initialDepth, property, presence, true);
}
}
OnDeserialized(reader, contract, newObject);
return newObject;
}