本文整理汇总了C#中Newtonsoft.Json.Serialization.JsonObjectContract类的典型用法代码示例。如果您正苦于以下问题:C# JsonObjectContract类的具体用法?C# JsonObjectContract怎么用?C# JsonObjectContract使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsonObjectContract类属于Newtonsoft.Json.Serialization命名空间,在下文中一共展示了JsonObjectContract类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateProperties
protected override System.Collections.Generic.IList<JsonProperty> CreateProperties(JsonObjectContract contract)
{
IList<JsonProperty> properties = base.CreateProperties(contract);
properties = properties.Where(p => !notSerilizingProperties.Contains(p.PropertyName) ).ToList();
return properties;
}
示例2: CreateNewObject
// Token: 0x06000BDB RID: 3035
// RVA: 0x00046004 File Offset: 0x00044204
public object CreateNewObject(JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMember, JsonProperty containerProperty, string id, out bool createdFromNonDefaultCreator)
{
object obj = null;
if (objectContract.OverrideCreator != null)
{
if (objectContract.CreatorParameters.Count > 0)
{
createdFromNonDefaultCreator = true;
return this.CreateObjectUsingCreatorWithParameters(reader, objectContract, containerMember, objectContract.OverrideCreator, id);
}
obj = objectContract.OverrideCreator(new object[0]);
}
else if (objectContract.DefaultCreator != null && (!objectContract.DefaultCreatorNonPublic || this.Serializer._constructorHandling == ConstructorHandling.AllowNonPublicDefaultConstructor || objectContract.ParametrizedCreator == null))
{
obj = objectContract.DefaultCreator();
}
else if (objectContract.ParametrizedCreator != null)
{
createdFromNonDefaultCreator = true;
return this.CreateObjectUsingCreatorWithParameters(reader, objectContract, containerMember, objectContract.ParametrizedCreator, id);
}
if (obj != null)
{
createdFromNonDefaultCreator = false;
return obj;
}
if (!objectContract.IsInstantiable)
{
throw JsonSerializationException.Create(reader, StringUtils.FormatWith("Could not create an instance of type {0}. Type is an interface or abstract class and cannot be instantiated.", CultureInfo.InvariantCulture, objectContract.UnderlyingType));
}
throw JsonSerializationException.Create(reader, StringUtils.FormatWith("Unable to find a constructor to use for type {0}. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute.", CultureInfo.InvariantCulture, objectContract.UnderlyingType));
}
示例3: CreateProperties
protected override System.Collections.Generic.IList<JsonProperty> CreateProperties(JsonObjectContract contract)
{
IList<JsonProperty> properties = base.CreateProperties(contract);
if(contract.UnderlyingType != typeof(MonoReports.Model.Thickness))
properties = properties.Where(p =>!notSerilizingProperties.Contains(p.PropertyName) ).ToList();
return properties;
}
示例4: ModelFilterContext
public ModelFilterContext(
Type systemType,
JsonObjectContract jsonObjectContract,
ISchemaRegistry schemaRegistry)
{
SystemType = systemType;
JsonObjectContract = jsonObjectContract;
SchemaRegistry = schemaRegistry;
}
示例5: CreateProperties
protected override IList<JsonProperty> CreateProperties(JsonObjectContract contract)
{
IList<JsonProperty> properties = base.CreateProperties(contract);
// only serializer properties that start with the specified character
properties =
properties.Where(p => p.PropertyName.StartsWith(_startingWithChar.ToString())).ToList();
return properties;
}
示例6: CreateProperties
protected override IList<JsonProperty> CreateProperties(JsonObjectContract contract)
{
var properties = base.CreateProperties(contract);
foreach (var property in properties)
{
property.PropertyName = PascalCaseToElement(property.PropertyName);
}
var result = properties;
return result;
}
示例7: SortUnsortedProperties
private void SortUnsortedProperties(JsonObjectContract objContract)
{
if (objContract != null)
{
var order = 1;
foreach (var property in objContract.Properties.Where(each => (each.Order == null) || (each.Order > 0)))
{
if (property.PropertyName == "name" || property.PropertyName == "#name")
{
property.Order = -50;
}
else
{
property.Order = order++;
}
}
}
}
示例8: ModifyContract
public JsonObjectContract ModifyContract(JsonObjectContract contract)
{
foreach (var property in contract.Properties)
{
foreach (var type in property.PropertyType.GetInterfaces().Union(new Type[] { property.PropertyType }))
{
if (type.IsGenericType)
{
if (type.GetGenericTypeDefinition() == typeof(NestedList<>))
{
property.PropertyName = property.PropertyName + "$$nested";
}
}
}
}
return contract;
}
开发者ID:danieldekock1979,项目名称:Nested2Find,代码行数:18,代码来源:IncludeTypeNameInNestedListFieldNamesInterceptor.cs
示例9: CreateNewObject
public object CreateNewObject(JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMember, JsonProperty containerProperty, string id, out bool createdFromNonDefaultCreator)
{
object newObject = null;
if (objectContract.OverrideCreator != null)
{
if (objectContract.CreatorParameters.Count > 0)
{
createdFromNonDefaultCreator = true;
return CreateObjectUsingCreatorWithParameters(reader, objectContract, containerMember, objectContract.OverrideCreator, id);
}
newObject = objectContract.OverrideCreator(new object[0]);
}
else if (objectContract.DefaultCreator != null &&
(!objectContract.DefaultCreatorNonPublic || Serializer._constructorHandling == ConstructorHandling.AllowNonPublicDefaultConstructor || objectContract.ParametrizedCreator == null))
{
// use the default constructor if it is...
// public
// non-public and the user has change constructor handling settings
// non-public and there is no other creator
newObject = objectContract.DefaultCreator();
}
else if (objectContract.ParametrizedCreator != null)
{
createdFromNonDefaultCreator = true;
return CreateObjectUsingCreatorWithParameters(reader, objectContract, containerMember, objectContract.ParametrizedCreator, id);
}
if (newObject == null)
{
if (!objectContract.IsInstantiable)
throw JsonSerializationException.Create(reader, "Could not create an instance of type {0}. Type is an interface or abstract class and cannot be instantiated.".FormatWith(CultureInfo.InvariantCulture, objectContract.UnderlyingType));
throw JsonSerializationException.Create(reader, "Unable to find a constructor to use for type {0}. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute.".FormatWith(CultureInfo.InvariantCulture, objectContract.UnderlyingType));
}
createdFromNonDefaultCreator = false;
return newObject;
}
示例10: GenerateObjectSchema
private void GenerateObjectSchema(JSchema schema, Type type, JsonObjectContract contract)
{
foreach (JsonProperty property in contract.Properties)
{
if (!property.Ignored)
{
bool optional = property.NullValueHandling == NullValueHandling.Ignore ||
HasFlag(property.DefaultValueHandling.GetValueOrDefault(), DefaultValueHandling.Ignore) ||
property.ShouldSerialize != null ||
property.GetIsSpecified != null;
Required required = property.Required;
if (DataAnnotationHelpers.GetRequired(property))
required = Required.Always;
JSchema propertySchema = GenerateInternal(property.PropertyType, required, property, contract);
if (property.DefaultValue != null)
propertySchema.Default = JToken.FromObject(property.DefaultValue);
schema.Properties.Add(property.PropertyName, propertySchema);
if (!optional)
schema.Required.Add(property.PropertyName);
}
}
if (type.IsSealed())
schema.AllowAdditionalProperties = false;
}
示例11: CreateObjectSchema
private Schema CreateObjectSchema(JsonObjectContract jsonContract)
{
var properties = jsonContract.Properties
.Where(p => !p.Ignored)
.Where(p => !(_ignoreObsoleteProperties && p.IsObsolete()))
.ToDictionary(
prop => prop.PropertyName,
prop => CreateInlineSchema(prop.PropertyType).WithValidationProperties(prop)
);
var required = jsonContract.Properties.Where(prop => prop.IsRequired())
.Select(propInfo => propInfo.PropertyName)
.ToList();
var schema = new Schema
{
required = required.Any() ? required : null, // required can be null but not empty
properties = properties,
type = "object"
};
foreach (var filter in _schemaFilters)
{
filter.Apply(schema, this, jsonContract.UnderlyingType);
}
// NOTE: In next major version, _modelFilters will completely replace _schemaFilters
var modelFilterContext = new ModelFilterContext(jsonContract.UnderlyingType, jsonContract, this);
foreach (var filter in _modelFilters)
{
filter.Apply(schema, modelFilterContext);
}
return schema;
}
示例12: CreateObjectFromNonDefaultConstructor
private object CreateObjectFromNonDefaultConstructor(JsonReader reader, JsonObjectContract contract, JsonProperty containerProperty, ConstructorInfo constructorInfo, string id)
{
ValidationUtils.ArgumentNotNull(constructorInfo, "constructorInfo");
// 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)
TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Deserializing {0} using a non-default constructor '{1}'.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType, constructorInfo)), null);
IDictionary<JsonProperty, object> propertyValues = ResolvePropertyAndConstructorValues(contract, containerProperty, reader, objectType);
IDictionary<ParameterInfo, object> constructorParameters = constructorInfo.GetParameters().ToDictionary(p => p, p => (object) null);
IDictionary<JsonProperty, object> remainingPropertyValues = new Dictionary<JsonProperty, object>();
foreach (KeyValuePair<JsonProperty, object> propertyValue in propertyValues)
{
ParameterInfo matchingConstructorParameter = constructorParameters.ForgivingCaseSensitiveFind(kv => kv.Key.Name, propertyValue.Key.UnderlyingName).Key;
if (matchingConstructorParameter != null)
constructorParameters[matchingConstructorParameter] = propertyValue.Value;
else
remainingPropertyValues.Add(propertyValue);
if (propertiesPresence != null)
{
// map from constructor property to normal property
var property = propertiesPresence.Keys.FirstOrDefault(p => p.PropertyName == propertyValue.Key.PropertyName);
if (property != null)
propertiesPresence[property] = (propertyValue.Value == null) ? PropertyPresence.Null : PropertyPresence.Value;
}
}
object createdObject = constructorInfo.Invoke(constructorParameters.Values.ToArray());
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(remainingPropertyValue.Key, remainingPropertyValue.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);
}
}
}
}
}
EndObject(createdObject, reader, contract, reader.Depth, propertiesPresence);
OnDeserialized(reader, contract, createdObject);
return createdObject;
}
示例13: EndObject
private void EndObject(object newObject, JsonReader reader, JsonObjectContract contract, int initialDepth, Dictionary<JsonProperty, PropertyPresence> propertiesPresence)
{
if (propertiesPresence != null)
{
foreach (KeyValuePair<JsonProperty, PropertyPresence> propertyPresence in propertiesPresence)
{
JsonProperty property = propertyPresence.Key;
PropertyPresence presence = propertyPresence.Value;
if (presence == PropertyPresence.None || presence == PropertyPresence.Null)
{
try
{
Required resolvedRequired = property._required ?? contract.ItemRequired ?? Required.Default;
switch (presence)
{
case PropertyPresence.None:
if (resolvedRequired == Required.AllowNull || resolvedRequired == Required.Always)
throw JsonSerializationException.Create(reader, "Required property '{0}' not found in JSON.".FormatWith(CultureInfo.InvariantCulture, property.PropertyName));
if (property.PropertyContract == null)
property.PropertyContract = GetContractSafe(property.PropertyType);
if (HasFlag(property.DefaultValueHandling.GetValueOrDefault(Serializer._defaultValueHandling), DefaultValueHandling.Populate) && property.Writable)
property.ValueProvider.SetValue(newObject, EnsureType(reader, property.GetResolvedDefaultValue(), CultureInfo.InvariantCulture, property.PropertyContract, property.PropertyType));
break;
case PropertyPresence.Null:
if (resolvedRequired == Required.Always)
throw JsonSerializationException.Create(reader, "Required property '{0}' expects a value but got null.".FormatWith(CultureInfo.InvariantCulture, property.PropertyName));
break;
}
}
catch (Exception ex)
{
if (IsErrorHandled(newObject, contract, property.PropertyName, reader as IJsonLineInfo, reader.Path, ex))
HandleError(reader, true, initialDepth);
else
throw;
}
}
}
}
}
示例14: ResolvePropertyAndConstructorValues
private IDictionary<JsonProperty, object> ResolvePropertyAndConstructorValues(JsonObjectContract contract, JsonProperty containerProperty, JsonReader reader, Type objectType)
{
IDictionary<JsonProperty, object> propertyValues = new Dictionary<JsonProperty, object>();
bool exit = false;
do
{
switch (reader.TokenType)
{
case JsonToken.PropertyName:
string memberName = reader.Value.ToString();
// attempt exact case match first
// then try match ignoring case
JsonProperty property = contract.ConstructorParameters.GetClosestMatchProperty(memberName) ??
contract.Properties.GetClosestMatchProperty(memberName);
if (property != null)
{
if (property.PropertyContract == null)
property.PropertyContract = GetContractSafe(property.PropertyType);
JsonConverter propertyConverter = GetConverter(property.PropertyContract, property.MemberConverter, contract, containerProperty);
if (!ReadForType(reader, property.PropertyContract, propertyConverter != null))
throw JsonSerializationException.Create(reader, "Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, memberName));
if (!property.Ignored)
{
if (property.PropertyContract == null)
property.PropertyContract = GetContractSafe(property.PropertyType);
object propertyValue;
if (propertyConverter != null && propertyConverter.CanRead)
propertyValue = DeserializeConvertable(propertyConverter, reader, property.PropertyType, null);
else
propertyValue = CreateValueInternal(reader, property.PropertyType, property.PropertyContract, property, contract, containerProperty, null);
propertyValues[property] = propertyValue;
}
else
{
reader.Skip();
}
}
else
{
if (!reader.Read())
throw JsonSerializationException.Create(reader, "Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, memberName));
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, memberName, contract.UnderlyingType)), null);
if (Serializer._missingMemberHandling == MissingMemberHandling.Error)
throw JsonSerializationException.Create(reader, "Could not find member '{0}' on object of type '{1}'".FormatWith(CultureInfo.InvariantCulture, memberName, objectType.Name));
reader.Skip();
}
break;
case JsonToken.Comment:
break;
case JsonToken.EndObject:
exit = true;
break;
default:
throw JsonSerializationException.Create(reader, "Unexpected token when deserializing object: " + reader.TokenType);
}
} while (!exit && reader.Read());
return propertyValues;
}
示例15: 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();
}
}