本文整理汇总了C#中TypeNameHandling类的典型用法代码示例。如果您正苦于以下问题:C# TypeNameHandling类的具体用法?C# TypeNameHandling怎么用?C# TypeNameHandling使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TypeNameHandling类属于命名空间,在下文中一共展示了TypeNameHandling类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SaveToFile
public void SaveToFile(object obj, string file, Formatting formatting = Formatting.Indented, TypeNameHandling nameHandling = TypeNameHandling.Auto)
{
var path = Util.Str.getFolder(file);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
File.WriteAllText(file, WriteObjectAsString(obj, formatting, nameHandling));
}
示例2: WriteObjectAsString
public string WriteObjectAsString(object obj, Formatting formatting = Formatting.None, TypeNameHandling nameHandling = TypeNameHandling.Auto)
{
return JsonConvert.SerializeObject(obj, formatting,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
TypeNameHandling = nameHandling
});
}
示例3: JsonSerializer
/// <summary>
/// Initializes a new instance of the <see cref="JsonSerializer"/> class.
/// </summary>
public JsonSerializer()
{
_referenceLoopHandling = JsonSerializerSettings.DefaultReferenceLoopHandling;
_missingMemberHandling = JsonSerializerSettings.DefaultMissingMemberHandling;
_nullValueHandling = JsonSerializerSettings.DefaultNullValueHandling;
_defaultValueHandling = JsonSerializerSettings.DefaultDefaultValueHandling;
_objectCreationHandling = JsonSerializerSettings.DefaultObjectCreationHandling;
_preserveReferencesHandling = JsonSerializerSettings.DefaultPreserveReferencesHandling;
_typeNameHandling = JsonSerializerSettings.DefaultTypeNameHandling;
_binder = DefaultSerializationBinder.Instance;
}
示例4: CreateSettings
/// <summary>
/// Creates the settings instance.
/// </summary>
/// <param name="nullValueHandling">The null value handling.</param>
/// <param name="typeNameHandling">The type name handling.</param>
/// <returns></returns>
private static JsonSerializerSettings CreateSettings(NullValueHandling nullValueHandling, TypeNameHandling typeNameHandling) {
var jsonSerializerSettings = new JsonSerializerSettings {
NullValueHandling = nullValueHandling,
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
TypeNameHandling = typeNameHandling,
TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
};
jsonSerializerSettings.Converters.Add(new BeetleDateTimeConverter { DateTimeFormat = "yyyy-MM-dd\\THH:mm:ss.fffK" });
jsonSerializerSettings.Converters.Add(new TimeSpanConverter());
jsonSerializerSettings.Converters.Add(new StringEnumConverter());
jsonSerializerSettings.Converters.Add(new ByteArrayConverter());
jsonSerializerSettings.Converters.Add(new DbGeometryConverter());
jsonSerializerSettings.Converters.Add(new DbGeographyConverter());
return jsonSerializerSettings;
}
示例5: ShouldWriteType
private bool ShouldWriteType(TypeNameHandling typeNameHandlingFlag, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
{
TypeNameHandling resolvedTypeNameHandling =
((member != null) ? member.TypeNameHandling : null)
?? ((containerProperty != null) ? containerProperty.ItemTypeNameHandling : null)
?? ((containerContract != null) ? containerContract.ItemTypeNameHandling : null)
?? Serializer.TypeNameHandling;
if (HasFlag(resolvedTypeNameHandling, typeNameHandlingFlag))
return true;
// instance type and the property's type's contract default type are different (no need to put the type in JSON because the type will be created by default)
if (HasFlag(resolvedTypeNameHandling, TypeNameHandling.Auto))
{
if (member != null)
{
if (contract.UnderlyingType != member.PropertyContract.CreatedType)
return true;
}
else if (containerContract != null && containerContract.ItemContract != null)
{
if (contract.UnderlyingType != containerContract.ItemContract.CreatedType)
return true;
}
}
return false;
}
示例6: ShouldWriteType
private bool ShouldWriteType(TypeNameHandling typeNameHandlingFlag, JsonContract contract, JsonProperty member, JsonContract collectionValueContract)
{
if (HasFlag(((member != null) ? member.TypeNameHandling : null) ?? Serializer.TypeNameHandling, typeNameHandlingFlag))
return true;
if (member != null)
{
if ((member.TypeNameHandling ?? Serializer.TypeNameHandling) == TypeNameHandling.Auto
// instance and property type are different
&& contract.UnderlyingType != member.PropertyType)
{
JsonContract memberTypeContract = Serializer.ContractResolver.ResolveContract(member.PropertyType);
// instance type and the property's type's contract default type are different (no need to put the type in JSON because the type will be created by default)
if (contract.UnderlyingType != memberTypeContract.CreatedType)
return true;
}
}
else if (collectionValueContract != null)
{
if (Serializer.TypeNameHandling == TypeNameHandling.Auto && contract.UnderlyingType != collectionValueContract.UnderlyingType)
return true;
}
return false;
}
示例7: ShouldWriteType
//private bool ShouldWriteTypeProperty(JsonProperty member, JsonContract contract, TypeNameHandling typeFlag)
//{
// if (HasFlag(((member != null) ? member.TypeNameHandling : null) ?? Serializer.TypeNameHandling, typeFlag))
// return true;
// if ((((member != null) ? member.TypeNameHandling : null) ?? Serializer.TypeNameHandling) == TypeNameHandling.Auto)
// || (member != null
// && (member.TypeNameHandling ?? Serializer.TypeNameHandling) == TypeNameHandling.Auto
// && contract.UnderlyingType != member.PropertyType)
// )
//}
private bool ShouldWriteType(TypeNameHandling typeNameHandlingFlag, JsonContract contract, JsonProperty member, JsonContract collectionValueContract)
{
if (HasFlag(((member != null) ? member.TypeNameHandling : null) ?? Serializer.TypeNameHandling, typeNameHandlingFlag))
return true;
if (member != null)
{
if ((member.TypeNameHandling ?? Serializer.TypeNameHandling) == TypeNameHandling.Auto && contract.UnderlyingType != member.PropertyType)
return true;
}
else if (collectionValueContract != null)
{
if (Serializer.TypeNameHandling == TypeNameHandling.Auto && contract.UnderlyingType != collectionValueContract.UnderlyingType)
return true;
}
return false;
}
示例8: ShouldWriteType
private bool ShouldWriteType(TypeNameHandling typeNameHandlingFlag, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
{
TypeNameHandling resolvedTypeNameHandling =
((member != null) ? member.TypeNameHandling : null)
?? ((containerProperty != null) ? containerProperty.ItemTypeNameHandling : null)
?? ((containerContract != null) ? containerContract.ItemTypeNameHandling : null)
?? Serializer.TypeNameHandling;
if (HasFlag(resolvedTypeNameHandling, typeNameHandlingFlag))
return true;
// instance type and the property's type's contract default type are different (no need to put the type in JSON because the type will be created by default)
if (HasFlag(resolvedTypeNameHandling, TypeNameHandling.Auto))
{
if (member != null)
{
if (contract.UnderlyingType != member.PropertyContract.CreatedType)
return true;
}
else if (containerContract != null && containerContract.ItemContract != null)
{
if (contract.UnderlyingType != containerContract.ItemContract.CreatedType)
return true;
}
}
#if !(NET35 || NET20 || WINDOWS_PHONE)
if (HasFlag(resolvedTypeNameHandling, TypeNameHandling.Auto))
{
if (contract.CreatedType == typeof(ExpandoObject))
{
//Add $type for ExpandoObject when called as root by default
return true;
}
if (member == null && containerContract != null && containerContract.UnderlyingType == typeof(object))
{
//We handling a dynamic ExpandoObject
if (containerProperty != null && contract.UnderlyingType != containerProperty.PropertyType)
{
//An IEnumerable of String (property name), Object (value) is a ExpandoObject
return true;
}
}
}
if (HasFlag(resolvedTypeNameHandling, TypeNameHandling.Objects))
{
if (member != null && member.TypeNameHandling != TypeNameHandling.None && contract.CreatedType == typeof(ExpandoObject))
{
//Add $type for ExpandoObject when called as a property by default
return true;
}
}
#endif
return false;
}
示例9: ShouldWriteType
private bool ShouldWriteType(TypeNameHandling typeNameHandlingFlag, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
{
TypeNameHandling resolvedTypeNameHandling =
((member != null) ? member.TypeNameHandling : null)
?? ((containerProperty != null) ? containerProperty.ItemTypeNameHandling : null)
?? ((containerContract != null) ? containerContract.ItemTypeNameHandling : null)
?? Serializer._typeNameHandling;
if (HasFlag(resolvedTypeNameHandling, typeNameHandlingFlag))
{
return true;
}
// instance type and the property's type's contract default type are different (no need to put the type in JSON because the type will be created by default)
if (HasFlag(resolvedTypeNameHandling, TypeNameHandling.Auto))
{
if (member != null)
{
if (contract.NonNullableUnderlyingType != member.PropertyContract.CreatedType)
{
return true;
}
}
else if (containerContract != null)
{
if (containerContract.ItemContract == null || contract.NonNullableUnderlyingType != containerContract.ItemContract.CreatedType)
{
return true;
}
}
else if (_rootType != null && _serializeStack.Count == _rootLevel)
{
JsonContract rootContract = Serializer._contractResolver.ResolveContract(_rootType);
if (contract.NonNullableUnderlyingType != rootContract.CreatedType)
{
return true;
}
}
}
return false;
}
示例10: BeetleConfig
/// <summary>
/// Initializes a new instance of the <see cref="BeetleConfig" /> class.
/// </summary>
/// <param name="nullValueHandling">The null value handling.</param>
/// <param name="typeNameHandling">The type name handling.</param>
public BeetleConfig(NullValueHandling nullValueHandling, TypeNameHandling typeNameHandling)
: this(CreateSettings(nullValueHandling, typeNameHandling)) {
}
示例11: WriteObject
public byte[] WriteObject(object obj, Formatting formatting = Formatting.None, TypeNameHandling nameHandling = TypeNameHandling.Auto)
{
return Util.Str.GetBytes(WriteObjectAsString(obj, formatting, nameHandling));
}
示例12: ReadObject
public object ReadObject(string str, Type objType, TypeNameHandling nameHandling = TypeNameHandling.Auto)
{
return JsonConvert.DeserializeObject(str, objType, new JsonSerializerSettings() { TypeNameHandling = nameHandling });
}
示例13: OpenFromFile
public object OpenFromFile(Type objType, string file, TypeNameHandling nameHandling = TypeNameHandling.Auto)
{
return ReadObject(File.ReadAllText(file), objType);
}
示例14: ShouldWriteType
private bool ShouldWriteType(TypeNameHandling typeNameHandlingFlag, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
{
TypeNameHandling? nullable1 = member != null ? member.TypeNameHandling : new TypeNameHandling?();
int num;
if (!nullable1.HasValue)
{
TypeNameHandling? nullable2 = containerProperty != null ? containerProperty.ItemTypeNameHandling : new TypeNameHandling?();
if (!nullable2.HasValue)
{
TypeNameHandling? nullable3 = containerContract != null ? containerContract.ItemTypeNameHandling : new TypeNameHandling?();
num = nullable3.HasValue ? (int) nullable3.GetValueOrDefault() : (int) this.Serializer.TypeNameHandling;
}
else
num = (int) nullable2.GetValueOrDefault();
}
else
num = (int) nullable1.GetValueOrDefault();
TypeNameHandling typeNameHandling = (TypeNameHandling) num;
if (this.HasFlag(typeNameHandling, typeNameHandlingFlag))
return true;
if (this.HasFlag(typeNameHandling, TypeNameHandling.Auto))
{
if (member != null)
{
if (contract.UnderlyingType != member.PropertyContract.CreatedType)
return true;
}
else if (containerContract != null && containerContract.ItemContract != null && contract.UnderlyingType != containerContract.ItemContract.CreatedType)
return true;
}
return false;
}
示例15: ShouldWriteType
// Token: 0x06000C09 RID: 3081
// RVA: 0x000478E4 File Offset: 0x00045AE4
private bool ShouldWriteType(TypeNameHandling typeNameHandlingFlag, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
{
TypeNameHandling value = ((member != null) ? member.TypeNameHandling : null) ?? (((containerProperty != null) ? containerProperty.ItemTypeNameHandling : null) ?? (((containerContract != null) ? containerContract.ItemTypeNameHandling : null) ?? this.Serializer._typeNameHandling));
if (this.HasFlag(value, typeNameHandlingFlag))
{
return true;
}
if (this.HasFlag(value, TypeNameHandling.Auto))
{
if (member != null)
{
if (contract.UnderlyingType != member.PropertyContract.CreatedType)
{
return true;
}
}
else if (containerContract != null)
{
if (containerContract.ItemContract == null || contract.UnderlyingType != containerContract.ItemContract.CreatedType)
{
return true;
}
}
else if (this._rootContract != null && this._serializeStack.Count == this._rootLevel && contract.UnderlyingType != this._rootContract.CreatedType)
{
return true;
}
}
return false;
}