private void SetExtensionData(JsonObjectContract contract, JsonProperty member, JsonReader reader, string memberName, object o) { if (contract.ExtensionDataSetter != null) { try { object value = CreateValueInternal(reader, null, null, null, contract, member, null); contract.ExtensionDataSetter(o, memberName, value); } catch (Exception ex) { throw JsonSerializationException.Create(reader, "Error setting value in extension data for type '{0}'.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType), ex); } } else { reader.Skip(); } }
private object CreateObjectUsingCreatorWithParameters(JsonReader reader, JsonObjectContract contract, JsonProperty containerProperty, ObjectConstructor<object> creator, string id) { ValidationUtils.ArgumentNotNull(creator, nameof(creator)); // only need to keep a track of properies presence if they are required or a value should be defaulted if missing bool trackPresence = (contract.HasRequiredOrDefaultValueProperties || HasFlag(Serializer._defaultValueHandling, DefaultValueHandling.Populate)); Type objectType = contract.UnderlyingType; if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info) { string parameters = string.Join(", ", contract.CreatorParameters.Select(p => p.PropertyName).ToArray()); TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Deserializing {0} using creator with parameters: {1}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType, parameters)), null); } List<CreatorPropertyContext> propertyContexts = ResolvePropertyAndCreatorValues(contract, containerProperty, reader, objectType); if (trackPresence) { foreach (JsonProperty property in contract.Properties) { if (propertyContexts.All(p => p.Property != property)) { propertyContexts.Add(new CreatorPropertyContext { Property = property, Name = property.PropertyName, Presence = PropertyPresence.None }); } } } object[] creatorParameterValues = new object[contract.CreatorParameters.Count]; foreach (CreatorPropertyContext context in propertyContexts) { // set presence of read values if (trackPresence) { if (context.Property != null && context.Presence == null) { object v = context.Value; PropertyPresence propertyPresence; if (v == null) { propertyPresence = PropertyPresence.Null; } else if (v is string) { propertyPresence = CoerceEmptyStringToNull(context.Property.PropertyType, context.Property.PropertyContract, (string)v) ? PropertyPresence.Null : PropertyPresence.Value; } else { propertyPresence = PropertyPresence.Value; } context.Presence = propertyPresence; } } JsonProperty constructorProperty = context.ConstructorProperty; if (constructorProperty == null && context.Property != null) { constructorProperty = contract.CreatorParameters.ForgivingCaseSensitiveFind(p => p.PropertyName, context.Property.UnderlyingName); } if (constructorProperty != null && !constructorProperty.Ignored) { // handle giving default values to creator parameters // this needs to happen before the call to creator if (trackPresence) { if (context.Presence == PropertyPresence.None || context.Presence == PropertyPresence.Null) { if (constructorProperty.PropertyContract == null) { constructorProperty.PropertyContract = GetContractSafe(constructorProperty.PropertyType); } if (HasFlag(constructorProperty.DefaultValueHandling.GetValueOrDefault(Serializer._defaultValueHandling), DefaultValueHandling.Populate)) { context.Value = EnsureType( reader, constructorProperty.GetResolvedDefaultValue(), CultureInfo.InvariantCulture, constructorProperty.PropertyContract, constructorProperty.PropertyType); } } } int i = contract.CreatorParameters.IndexOf(constructorProperty); creatorParameterValues[i] = context.Value; context.Used = true; } } object createdObject = creator(creatorParameterValues); if (id != null) { AddReference(reader, id, createdObject); } OnDeserializing(reader, contract, createdObject); // go through unused values and set the newly created object's properties foreach (CreatorPropertyContext context in propertyContexts) { if (context.Used || context.Property == null || context.Property.Ignored || context.Presence == PropertyPresence.None) { continue; } JsonProperty property = context.Property; object value = context.Value; if (ShouldSetPropertyValue(property, value)) { property.ValueProvider.SetValue(createdObject, value); context.Used = true; } else if (!property.Writable && value != null) { // handle readonly collection/dictionary properties JsonContract propertyContract = Serializer._contractResolver.ResolveContract(property.PropertyType); if (propertyContract.ContractType == JsonContractType.Array) { JsonArrayContract propertyArrayContract = (JsonArrayContract)propertyContract; object createdObjectCollection = property.ValueProvider.GetValue(createdObject); if (createdObjectCollection != null) { IWrappedCollection createdObjectCollectionWrapper = propertyArrayContract.CreateWrapper(createdObjectCollection); IWrappedCollection newValues = propertyArrayContract.CreateWrapper(value); foreach (object newValue in newValues) { createdObjectCollectionWrapper.Add(newValue); } } } else if (propertyContract.ContractType == JsonContractType.Dictionary) { JsonDictionaryContract dictionaryContract = (JsonDictionaryContract)propertyContract; object createdObjectDictionary = property.ValueProvider.GetValue(createdObject); if (createdObjectDictionary != null) { IDictionary targetDictionary = (dictionaryContract.ShouldCreateWrapper) ? dictionaryContract.CreateWrapper(createdObjectDictionary) : (IDictionary)createdObjectDictionary; IDictionary newValues = (dictionaryContract.ShouldCreateWrapper) ? dictionaryContract.CreateWrapper(value) : (IDictionary)value; // Manual use of IDictionaryEnumerator instead of foreach to avoid DictionaryEntry box allocations. IDictionaryEnumerator e = newValues.GetEnumerator(); try { while (e.MoveNext()) { DictionaryEntry entry = e.Entry; targetDictionary[entry.Key] = entry.Value; } } finally { (e as IDisposable)?.Dispose(); } } } context.Used = true; } } if (contract.ExtensionDataSetter != null) { foreach (CreatorPropertyContext propertyValue in propertyContexts) { if (!propertyValue.Used) { contract.ExtensionDataSetter(createdObject, propertyValue.Name, propertyValue.Value); } } } if (trackPresence) { foreach (CreatorPropertyContext context in propertyContexts) { if (context.Property == null) { continue; } EndProcessProperty( createdObject, reader, contract, reader.Depth, context.Property, context.Presence.GetValueOrDefault(), !context.Used); } } OnDeserialized(reader, contract, createdObject); return createdObject; }
private object CreateObjectUsingCreatorWithParameters(JsonReader reader, JsonObjectContract contract, JsonProperty containerProperty, ObjectConstructor<object> creator, string id) { ValidationUtils.ArgumentNotNull(creator, "creator"); // only need to keep a track of properies presence if they are required or a value should be defaulted if missing Dictionary<JsonProperty, PropertyPresence> propertiesPresence = (contract.HasRequiredOrDefaultValueProperties || HasFlag(Serializer._defaultValueHandling, DefaultValueHandling.Populate)) ? contract.Properties.ToDictionary(m => m, m => PropertyPresence.None) : null; Type objectType = contract.UnderlyingType; if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info) { string parameters = string.Join(", ", contract.CreatorParameters.Select(p => p.PropertyName).ToArray()); TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Deserializing {0} using creator with parameters: {1}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType, parameters)), null); } IDictionary<string, object> extensionData; IDictionary<JsonProperty, object> propertyValues = ResolvePropertyAndCreatorValues(contract, containerProperty, reader, objectType, out extensionData); object[] creatorParameterValues = new object[contract.CreatorParameters.Count]; IDictionary<JsonProperty, object> remainingPropertyValues = new Dictionary<JsonProperty, object>(); foreach (KeyValuePair<JsonProperty, object> propertyValue in propertyValues) { JsonProperty property = propertyValue.Key; JsonProperty matchingCreatorParameter; if (contract.CreatorParameters.Contains(property)) { matchingCreatorParameter = property; } else { // check to see if a parameter with the same name as the underlying property name exists and match to that matchingCreatorParameter = contract.CreatorParameters.ForgivingCaseSensitiveFind(p => p.PropertyName, property.UnderlyingName); } if (matchingCreatorParameter != null) { int i = contract.CreatorParameters.IndexOf(matchingCreatorParameter); creatorParameterValues[i] = propertyValue.Value; } else { remainingPropertyValues.Add(propertyValue); } if (propertiesPresence != null) { // map from creator property to normal property JsonProperty presenceProperty = propertiesPresence.Keys.FirstOrDefault(p => p.PropertyName == property.PropertyName); if (presenceProperty != null) propertiesPresence[presenceProperty] = (propertyValue.Value == null) ? PropertyPresence.Null : PropertyPresence.Value; } } object createdObject = creator(creatorParameterValues); if (id != null) AddReference(reader, id, createdObject); OnDeserializing(reader, contract, createdObject); // go through unused values and set the newly created object's properties foreach (KeyValuePair<JsonProperty, object> remainingPropertyValue in remainingPropertyValues) { JsonProperty property = remainingPropertyValue.Key; object value = remainingPropertyValue.Value; if (ShouldSetPropertyValue(property, value)) { property.ValueProvider.SetValue(createdObject, value); } else if (!property.Writable && value != null) { // handle readonly collection/dictionary properties JsonContract propertyContract = Serializer._contractResolver.ResolveContract(property.PropertyType); if (propertyContract.ContractType == JsonContractType.Array) { JsonArrayContract propertyArrayContract = (JsonArrayContract)propertyContract; object createdObjectCollection = property.ValueProvider.GetValue(createdObject); if (createdObjectCollection != null) { IWrappedCollection createdObjectCollectionWrapper = propertyArrayContract.CreateWrapper(createdObjectCollection); IWrappedCollection newValues = propertyArrayContract.CreateWrapper(value); foreach (object newValue in newValues) { createdObjectCollectionWrapper.Add(newValue); } } } else if (propertyContract.ContractType == JsonContractType.Dictionary) { JsonDictionaryContract dictionaryContract = (JsonDictionaryContract)propertyContract; object createdObjectDictionary = property.ValueProvider.GetValue(createdObject); if (createdObjectDictionary != null) { IDictionary targetDictionary = (dictionaryContract.ShouldCreateWrapper) ? dictionaryContract.CreateWrapper(createdObjectDictionary) : (IDictionary)createdObjectDictionary; IDictionary newValues = (dictionaryContract.ShouldCreateWrapper) ? dictionaryContract.CreateWrapper(value) : (IDictionary)value; foreach (DictionaryEntry newValue in newValues) { targetDictionary.Add(newValue.Key, newValue.Value); } } } } } if (extensionData != null) { foreach (KeyValuePair<string, object> e in extensionData) { contract.ExtensionDataSetter(createdObject, e.Key, e.Value); } } EndObject(createdObject, reader, contract, reader.Depth, propertiesPresence); OnDeserialized(reader, contract, createdObject); return createdObject; }
// Token: 0x06000BD7 RID: 3031 // RVA: 0x00045760 File Offset: 0x00043960 private object CreateObjectUsingCreatorWithParameters(JsonReader reader, JsonObjectContract contract, JsonProperty containerProperty, ObjectConstructor<object> creator, string id) { ValidationUtils.ArgumentNotNull(creator, "creator"); Dictionary<JsonProperty, JsonSerializerInternalReader.PropertyPresence> arg_70_0; if (!contract.HasRequiredOrDefaultValueProperties && !this.HasFlag(this.Serializer._defaultValueHandling, DefaultValueHandling.Populate)) { arg_70_0 = null; } else { arg_70_0 = Enumerable.ToDictionary<JsonProperty, JsonProperty, JsonSerializerInternalReader.PropertyPresence>(contract.Properties, (JsonProperty m) => m, (JsonProperty m) => JsonSerializerInternalReader.PropertyPresence.None); } Dictionary<JsonProperty, JsonSerializerInternalReader.PropertyPresence> dictionary = arg_70_0; Type underlyingType = contract.UnderlyingType; if (this.TraceWriter != null && this.TraceWriter.LevelFilter >= TraceLevel.Info) { string arg = string.Join(", ", Enumerable.ToArray<string>(Enumerable.Select<JsonProperty, string>(contract.CreatorParameters, (JsonProperty p) => p.PropertyName))); this.TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, StringUtils.FormatWith("Deserializing {0} using creator with parameters: {1}.", CultureInfo.InvariantCulture, contract.UnderlyingType, arg)), null); } IDictionary<string, object> dictionary3; IDictionary<JsonProperty, object> dictionary2 = this.ResolvePropertyAndCreatorValues(contract, containerProperty, reader, underlyingType, out dictionary3); object[] array = new object[contract.CreatorParameters.Count]; IDictionary<JsonProperty, object> dictionary4 = new Dictionary<JsonProperty, object>(); foreach (KeyValuePair<JsonProperty, object> current in dictionary2) { JsonProperty property = current.Key; JsonProperty jsonProperty; if (contract.CreatorParameters.Contains(property)) { jsonProperty = property; } else { jsonProperty = StringUtils.ForgivingCaseSensitiveFind<JsonProperty>(contract.CreatorParameters, (JsonProperty p) => p.PropertyName, property.UnderlyingName); } if (jsonProperty != null) { int num = contract.CreatorParameters.IndexOf(jsonProperty); array[num] = current.Value; } else { dictionary4.Add(current); } if (dictionary != null) { JsonProperty jsonProperty2 = Enumerable.FirstOrDefault<JsonProperty>(dictionary.Keys, (JsonProperty p) => p.PropertyName == property.PropertyName); if (jsonProperty2 != null) { dictionary[jsonProperty2] = ((current.Value == null) ? JsonSerializerInternalReader.PropertyPresence.Null : JsonSerializerInternalReader.PropertyPresence.Value); } } } object obj = creator(array); if (id != null) { this.AddReference(reader, id, obj); } this.OnDeserializing(reader, contract, obj); foreach (KeyValuePair<JsonProperty, object> current2 in dictionary4) { JsonProperty key = current2.Key; object value = current2.Value; if (this.ShouldSetPropertyValue(key, value)) { key.ValueProvider.SetValue(obj, value); } else if (!key.Writable && value != null) { JsonContract jsonContract = this.Serializer._contractResolver.ResolveContract(key.PropertyType); if (jsonContract.ContractType == JsonContractType.Array) { JsonArrayContract jsonArrayContract = (JsonArrayContract)jsonContract; object value2 = key.ValueProvider.GetValue(obj); if (value2 == null) { continue; } IWrappedCollection wrappedCollection = jsonArrayContract.CreateWrapper(value2); IWrappedCollection wrappedCollection2 = jsonArrayContract.CreateWrapper(value); IEnumerator enumerator3 = wrappedCollection2.GetEnumerator(); try { while (enumerator3.MoveNext()) { object current3 = enumerator3.Current; wrappedCollection.Add(current3); } continue; } finally { IDisposable disposable = enumerator3 as IDisposable; if (disposable != null) { disposable.Dispose(); } } } if (jsonContract.ContractType == JsonContractType.Dictionary) { JsonDictionaryContract jsonDictionaryContract = (JsonDictionaryContract)jsonContract; object value3 = key.ValueProvider.GetValue(obj); if (value3 != null) { IDictionary dictionary5 = jsonDictionaryContract.ShouldCreateWrapper ? jsonDictionaryContract.CreateWrapper(value3) : ((IDictionary)value3); IDictionary dictionary6 = jsonDictionaryContract.ShouldCreateWrapper ? jsonDictionaryContract.CreateWrapper(value) : ((IDictionary)value); foreach (DictionaryEntry dictionaryEntry in dictionary6) { dictionary5.Add(dictionaryEntry.Key, dictionaryEntry.Value); } } } } } if (dictionary3 != null) { foreach (KeyValuePair<string, object> current4 in dictionary3) { contract.ExtensionDataSetter(obj, current4.Key, current4.Value); } } this.EndObject(obj, reader, contract, reader.Depth, dictionary); this.OnDeserialized(reader, contract, obj); return obj; }