本文整理汇总了C#中Newtonsoft.Json.Serialization.JsonObjectContract.ExtensionDataSetter方法的典型用法代码示例。如果您正苦于以下问题:C# JsonObjectContract.ExtensionDataSetter方法的具体用法?C# JsonObjectContract.ExtensionDataSetter怎么用?C# JsonObjectContract.ExtensionDataSetter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Newtonsoft.Json.Serialization.JsonObjectContract
的用法示例。
在下文中一共展示了JsonObjectContract.ExtensionDataSetter方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetExtensionData
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();
}
}
示例2: CreateObjectUsingCreatorWithParameters
//.........这里部分代码省略.........
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;
}
示例3: CreateObjectUsingCreatorWithParameters
//.........这里部分代码省略.........
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;
}
示例4: CreateObjectUsingCreatorWithParameters
//.........这里部分代码省略.........
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;
}