本文整理汇总了C#中Newtonsoft.Json.Serialization.JsonDictionaryContract类的典型用法代码示例。如果您正苦于以下问题:C# JsonDictionaryContract类的具体用法?C# JsonDictionaryContract怎么用?C# JsonDictionaryContract使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsonDictionaryContract类属于Newtonsoft.Json.Serialization命名空间,在下文中一共展示了JsonDictionaryContract类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SerializeDictionary
private void SerializeDictionary(JsonWriter writer, IDictionary values, JsonDictionaryContract contract)
{
contract.InvokeOnSerializing(values);
SerializeStack.Add(values);
writer.WriteStartObject();
bool isReference = contract.IsReference ?? HasFlag(_serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects);
if (isReference)
{
writer.WritePropertyName(JsonTypeReflector.IdPropertyName);
writer.WriteValue(_serializer.ReferenceResolver.GetReference(values));
}
if (HasFlag(_serializer.TypeNameHandling, TypeNameHandling.Objects))
{
WriteTypeProperty(writer, values.GetType());
}
foreach (DictionaryEntry entry in values)
{
string propertyName = entry.Key.ToString();
object value = entry.Value;
if (ShouldWriteReference(value, null))
{
writer.WritePropertyName(propertyName);
WriteReference(writer, value);
}
else
{
if (!CheckForCircularReference(value, null))
continue;
writer.WritePropertyName(propertyName);
SerializeValue(writer, value, null);
}
}
writer.WriteEndObject();
SerializeStack.RemoveAt(SerializeStack.Count - 1);
contract.InvokeOnSerialized(values);
}
示例2: CreateAndPopulateDictionary
private object CreateAndPopulateDictionary(JsonReader reader, JsonDictionaryContract contract, string id)
{
object dictionary;
if (contract.DefaultCreator != null &&
(!contract.DefaultCreatorNonPublic || Serializer.ConstructorHandling == ConstructorHandling.AllowNonPublicDefaultConstructor))
dictionary = contract.DefaultCreator();
else
throw CreateSerializationException(reader, "Unable to find a default constructor to use for type {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));
IWrappedDictionary dictionaryWrapper = contract.CreateWrapper(dictionary);
PopulateDictionary(dictionaryWrapper, reader, contract, id);
return dictionaryWrapper.UnderlyingDictionary;
}
示例3: CreateDictionaryContract
protected virtual JsonDictionaryContract CreateDictionaryContract(Type objectType)
{
JsonDictionaryContract dictionaryContract = new JsonDictionaryContract(objectType);
this.InitializeContract((JsonContract) dictionaryContract);
dictionaryContract.PropertyNameResolver = new Func<string, string>(this.ResolvePropertyName);
return dictionaryContract;
}
示例4: PopulateDictionary
private object PopulateDictionary(IDictionary dictionary, JsonReader reader, JsonDictionaryContract contract, JsonProperty containerProperty, string id)
{
IWrappedDictionary wrappedDictionary = dictionary as IWrappedDictionary;
object underlyingDictionary = wrappedDictionary != null ? wrappedDictionary.UnderlyingDictionary : dictionary;
if (id != null)
AddReference(reader, id, underlyingDictionary);
OnDeserializing(reader, contract, underlyingDictionary);
int initialDepth = reader.Depth;
if (contract.KeyContract == null)
contract.KeyContract = GetContractSafe(contract.DictionaryKeyType);
if (contract.ItemContract == null)
contract.ItemContract = GetContractSafe(contract.DictionaryValueType);
JsonConverter dictionaryValueConverter = contract.ItemConverter ?? GetConverter(contract.ItemContract, null, contract, containerProperty);
PrimitiveTypeCode keyTypeCode = (contract.KeyContract is JsonPrimitiveContract) ? ((JsonPrimitiveContract)contract.KeyContract).TypeCode : PrimitiveTypeCode.Empty;
bool finished = false;
do
{
switch (reader.TokenType)
{
case JsonToken.PropertyName:
object keyValue = reader.Value;
try
{
try
{
object dt;
// this is for correctly reading ISO and MS formatted dictionary keys
if ((keyTypeCode == PrimitiveTypeCode.DateTime || keyTypeCode == PrimitiveTypeCode.DateTimeNullable)
&& DateTimeUtils.TryParseDateTime(keyValue.ToString(), DateParseHandling.DateTime, reader.DateTimeZoneHandling, out dt))
{
keyValue = dt;
}
#if !NET20
else if ((keyTypeCode == PrimitiveTypeCode.DateTimeOffset || keyTypeCode == PrimitiveTypeCode.DateTimeOffsetNullable)
&& DateTimeUtils.TryParseDateTime(keyValue.ToString(), DateParseHandling.DateTimeOffset, reader.DateTimeZoneHandling, out dt))
{
keyValue = dt;
}
#endif
else
{
keyValue = EnsureType(reader, keyValue, CultureInfo.InvariantCulture, contract.KeyContract, contract.DictionaryKeyType);
}
}
catch (Exception ex)
{
throw JsonSerializationException.Create(reader, "Could not convert string '{0}' to dictionary key type '{1}'. Create a TypeConverter to convert from the string to the key type object.".FormatWith(CultureInfo.InvariantCulture, reader.Value, contract.DictionaryKeyType), ex);
}
if (!ReadForType(reader, contract.ItemContract, dictionaryValueConverter != null))
throw JsonSerializationException.Create(reader, "Unexpected end when deserializing object.");
object itemValue;
if (dictionaryValueConverter != null && dictionaryValueConverter.CanRead)
itemValue = DeserializeConvertable(dictionaryValueConverter, reader, contract.DictionaryValueType, null);
else
itemValue = CreateValueInternal(reader, contract.DictionaryValueType, contract.ItemContract, null, contract, containerProperty, null);
dictionary[keyValue] = itemValue;
}
catch (Exception ex)
{
if (IsErrorHandled(underlyingDictionary, contract, keyValue, reader as IJsonLineInfo, reader.Path, ex))
HandleError(reader, true, initialDepth);
else
throw;
}
break;
case JsonToken.Comment:
break;
case JsonToken.EndObject:
finished = true;
break;
default:
throw JsonSerializationException.Create(reader, "Unexpected token when deserializing object: " + reader.TokenType);
}
} while (!finished && reader.Read());
if (!finished)
ThrowUnexpectedEndException(reader, contract, underlyingDictionary, "Unexpected end when deserializing object.");
OnDeserialized(reader, contract, underlyingDictionary);
return underlyingDictionary;
}
示例5: CreateDictionarySchema
private Schema CreateDictionarySchema(JsonDictionaryContract dictionaryContract)
{
var valueType = dictionaryContract.DictionaryValueType ?? typeof(object);
return new Schema
{
type = "object",
additionalProperties = CreateInlineSchema(valueType)
};
}
示例6: CreateNewDictionary
public object CreateNewDictionary(JsonReader reader, JsonDictionaryContract contract)
{
object dictionary;
if (contract.DefaultCreator != null &&
(!contract.DefaultCreatorNonPublic || Serializer.ConstructorHandling == ConstructorHandling.AllowNonPublicDefaultConstructor))
dictionary = contract.DefaultCreator();
else
throw JsonSerializationException.Create(reader, "Unable to find a default constructor to use for type {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));
return dictionary;
}
示例7: SerializeDictionary
private void SerializeDictionary(JsonWriter writer, IWrappedDictionary values, JsonDictionaryContract contract, JsonProperty member, JsonContract collectionValueContract)
{
contract.InvokeOnSerializing(values.UnderlyingDictionary, Serializer.Context);
SerializeStack.Add(values.UnderlyingDictionary);
writer.WriteStartObject();
bool isReference = contract.IsReference ?? HasFlag(Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects);
if (isReference)
{
writer.WritePropertyName(JsonTypeReflector.IdPropertyName);
writer.WriteValue(Serializer.ReferenceResolver.GetReference(values.UnderlyingDictionary));
}
if (ShouldWriteType(TypeNameHandling.Objects, contract, member, collectionValueContract))
{
WriteTypeProperty(writer, values.UnderlyingDictionary.GetType());
}
JsonContract childValuesContract = Serializer.ContractResolver.ResolveContract(contract.DictionaryValueType ?? typeof(object));
int initialDepth = writer.Top;
// Mono Unity 3.0 fix
IDictionary d = values;
foreach (DictionaryEntry entry in d)
{
string propertyName = GetPropertyName(entry);
try
{
object value = entry.Value;
JsonContract valueContract = GetContractSafe(value);
if (ShouldWriteReference(value, null, valueContract))
{
writer.WritePropertyName(propertyName);
WriteReference(writer, value);
}
else
{
if (!CheckForCircularReference(value, null, contract))
continue;
writer.WritePropertyName(propertyName);
SerializeValue(writer, value, valueContract, null, childValuesContract);
}
}
catch (Exception ex)
{
if (IsErrorHandled(values.UnderlyingDictionary, contract, propertyName, ex))
HandleError(writer, initialDepth);
else
throw;
}
}
writer.WriteEndObject();
SerializeStack.RemoveAt(SerializeStack.Count - 1);
contract.InvokeOnSerialized(values.UnderlyingDictionary, Serializer.Context);
}
示例8: CreateDictionarySchema
private Schema CreateDictionarySchema(JsonDictionaryContract dictionaryContract)
{
var keyType = dictionaryContract.DictionaryKeyType ?? typeof(object);
var valueType = dictionaryContract.DictionaryValueType ?? typeof(object);
if (keyType.IsEnum)
{
return new Schema
{
type = "object",
properties = Enum.GetNames(keyType).ToDictionary(
(name) => dictionaryContract.PropertyNameResolver(name),
(name) => CreateInlineSchema(valueType)
)
};
}
else
{
return new Schema
{
type = "object",
additionalProperties = CreateInlineSchema(valueType)
};
}
}
示例9: CreateNewDictionary
public object CreateNewDictionary(JsonReader reader, JsonDictionaryContract contract, out bool isTemporaryDictionary)
{
object dictionary;
if (contract.DefaultCreator != null && (!contract.DefaultCreatorNonPublic || Serializer.ConstructorHandling == ConstructorHandling.AllowNonPublicDefaultConstructor))
{
dictionary = contract.DefaultCreator();
isTemporaryDictionary = false;
}
else if (contract.IsReadOnlyDictionary)
{
dictionary = contract.CreateTemporaryDictionary();
isTemporaryDictionary = true;
}
else
{
throw JsonSerializationException.Create(reader, "Unable to find a default constructor to use for type {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));
}
return dictionary;
}
示例10: CreateAndPopulateDictionary
private object CreateAndPopulateDictionary(JsonReader reader, JsonDictionaryContract contract, string id)
{
IWrappedDictionary dictionary = CollectionUtils.CreateDictionaryWrapper(Activator.CreateInstance(contract.DictionaryTypeToCreate));
PopulateDictionary(dictionary, reader, contract, id);
return dictionary.UnderlyingDictionary;
}
示例11: PopulateDictionary
private IDictionary PopulateDictionary(IWrappedDictionary dictionary, JsonReader reader, JsonDictionaryContract contract, string id)
{
if (id != null)
_serializer.ReferenceResolver.AddReference(id, dictionary.UnderlyingDictionary);
contract.InvokeOnDeserializing(dictionary.UnderlyingDictionary);
do
{
switch (reader.TokenType)
{
case JsonToken.PropertyName:
object keyValue = EnsureType(reader.Value, contract.DictionaryKeyType);
CheckedRead(reader);
dictionary.Add(keyValue, CreateValue(reader, contract.DictionaryValueType, null, null));
break;
case JsonToken.EndObject:
contract.InvokeOnDeserialized(dictionary.UnderlyingDictionary);
return dictionary;
default:
throw new JsonSerializationException("Unexpected token when deserializing object: " + reader.TokenType);
}
} while (reader.Read());
throw new JsonSerializationException("Unexpected end when deserializing object.");
}
示例12: SerializeDictionary
private void SerializeDictionary(JsonWriter writer, IWrappedDictionary values, JsonDictionaryContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
contract.InvokeOnSerializing(values.UnderlyingDictionary, this.Serializer.Context);
this._serializeStack.Add(values.UnderlyingDictionary);
this.WriteObjectStart(writer, values.UnderlyingDictionary, (JsonContract) contract, member, collectionContract, containerProperty);
if (contract.ItemContract == null)
contract.ItemContract = this.Serializer.ContractResolver.ResolveContract(contract.DictionaryValueType ?? typeof (object));
int top = writer.Top;
foreach (DictionaryEntry entry in (IDictionary) values)
{
string propertyName = this.GetPropertyName(entry);
string name = contract.PropertyNameResolver != null ? contract.PropertyNameResolver(propertyName) : propertyName;
try
{
object obj = entry.Value;
JsonContract jsonContract = contract.FinalItemContract ?? this.GetContractSafe(obj);
if (this.ShouldWriteReference(obj, (JsonProperty) null, jsonContract, (JsonContainerContract) contract, member))
{
writer.WritePropertyName(name);
this.WriteReference(writer, obj);
}
else if (this.CheckForCircularReference(writer, obj, (JsonProperty) null, jsonContract, (JsonContainerContract) contract, member))
{
writer.WritePropertyName(name);
this.SerializeValue(writer, obj, jsonContract, (JsonProperty) null, (JsonContainerContract) contract, member);
}
}
catch (Exception ex)
{
if (this.IsErrorHandled(values.UnderlyingDictionary, (JsonContract) contract, (object) name, writer.ContainerPath, ex))
this.HandleError(writer, top);
else
throw;
}
}
writer.WriteEndObject();
this._serializeStack.RemoveAt(this._serializeStack.Count - 1);
contract.InvokeOnSerialized(values.UnderlyingDictionary, this.Serializer.Context);
}
示例13: PopulateDictionary
// Token: 0x06000BD1 RID: 3025
// RVA: 0x00044E70 File Offset: 0x00043070
private object PopulateDictionary(IDictionary dictionary, JsonReader reader, JsonDictionaryContract contract, JsonProperty containerProperty, string id)
{
IWrappedDictionary wrappedDictionary = dictionary as IWrappedDictionary;
object obj = (wrappedDictionary != null) ? wrappedDictionary.UnderlyingDictionary : dictionary;
if (id != null)
{
this.AddReference(reader, id, obj);
}
this.OnDeserializing(reader, contract, obj);
int depth = reader.Depth;
if (contract.KeyContract == null)
{
contract.KeyContract = this.GetContractSafe(contract.DictionaryKeyType);
}
if (contract.ItemContract == null)
{
contract.ItemContract = this.GetContractSafe(contract.DictionaryValueType);
}
JsonConverter jsonConverter = contract.ItemConverter ?? this.GetConverter(contract.ItemContract, null, contract, containerProperty);
PrimitiveTypeCode primitiveTypeCode = (contract.KeyContract is JsonPrimitiveContract) ? ((JsonPrimitiveContract)contract.KeyContract).TypeCode : PrimitiveTypeCode.Empty;
bool flag = false;
while (true)
{
JsonToken tokenType = reader.TokenType;
switch (tokenType)
{
case JsonToken.PropertyName:
{
object obj2 = reader.Value;
if (!this.CheckPropertyName(reader, obj2.ToString()))
{
try
{
try
{
DateParseHandling dateParseHandling;
switch (primitiveTypeCode)
{
case PrimitiveTypeCode.DateTime:
case PrimitiveTypeCode.DateTimeNullable:
dateParseHandling = DateParseHandling.DateTime;
break;
default:
dateParseHandling = DateParseHandling.None;
break;
}
object obj3;
if (dateParseHandling != DateParseHandling.None && DateTimeUtils.TryParseDateTime(obj2.ToString(), dateParseHandling, reader.DateTimeZoneHandling, reader.DateFormatString, reader.Culture, out obj3))
{
obj2 = obj3;
}
else
{
obj2 = this.EnsureType(reader, obj2, CultureInfo.InvariantCulture, contract.KeyContract, contract.DictionaryKeyType);
}
}
catch (Exception ex)
{
throw JsonSerializationException.Create(reader, StringUtils.FormatWith("Could not convert string '{0}' to dictionary key type '{1}'. Create a TypeConverter to convert from the string to the key type object.", CultureInfo.InvariantCulture, reader.Value, contract.DictionaryKeyType), ex);
}
if (!this.ReadForType(reader, contract.ItemContract, jsonConverter != null))
{
throw JsonSerializationException.Create(reader, "Unexpected end when deserializing object.");
}
object value;
if (jsonConverter != null && jsonConverter.CanRead)
{
value = this.DeserializeConvertable(jsonConverter, reader, contract.DictionaryValueType, null);
}
else
{
value = this.CreateValueInternal(reader, contract.DictionaryValueType, contract.ItemContract, null, contract, containerProperty, null);
}
dictionary[obj2] = value;
break;
}
catch (Exception ex2)
{
if (!base.IsErrorHandled(obj, contract, obj2, reader as IJsonLineInfo, reader.Path, ex2))
{
throw;
}
this.HandleError(reader, true, depth);
break;
}
goto IL_20B;
}
break;
}
case JsonToken.Comment:
break;
default:
goto IL_20B;
}
IL_216:
if (flag)
{
break;
//.........这里部分代码省略.........
示例14: CreateAndPopulateDictionary
private object CreateAndPopulateDictionary(JsonReader reader, JsonDictionaryContract contract, string id)
{
IWrappedDictionary dictionary = contract.CreateWrapper(contract.DefaultContstructor.Invoke(null));
PopulateDictionary(dictionary, reader, contract, id);
return dictionary.UnderlyingDictionary;
}
示例15: SerializeDictionary
private void SerializeDictionary(JsonWriter writer, IDictionary values, JsonDictionaryContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
IWrappedDictionary wrappedDictionary = values as IWrappedDictionary;
object underlyingDictionary = wrappedDictionary != null ? wrappedDictionary.UnderlyingDictionary : values;
OnSerializing(writer, contract, underlyingDictionary);
_serializeStack.Add(underlyingDictionary);
WriteObjectStart(writer, underlyingDictionary, contract, member, collectionContract, containerProperty);
if (contract.ItemContract == null)
contract.ItemContract = Serializer._contractResolver.ResolveContract(contract.DictionaryValueType ?? typeof(object));
if (contract.KeyContract == null)
contract.KeyContract = Serializer._contractResolver.ResolveContract(contract.DictionaryKeyType ?? typeof(object));
int initialDepth = writer.Top;
foreach (DictionaryEntry entry in values)
{
bool escape;
string propertyName = GetPropertyName(writer, entry.Key, contract.KeyContract, out escape);
propertyName = (contract.PropertyNameResolver != null)
? contract.PropertyNameResolver(propertyName)
: propertyName;
try
{
object value = entry.Value;
JsonContract valueContract = contract.FinalItemContract ?? GetContractSafe(value);
if (ShouldWriteReference(value, null, valueContract, contract, member))
{
writer.WritePropertyName(propertyName, escape);
WriteReference(writer, value);
}
else
{
if (!CheckForCircularReference(writer, value, null, valueContract, contract, member))
continue;
writer.WritePropertyName(propertyName, escape);
SerializeValue(writer, value, valueContract, null, contract, member);
}
}
catch (Exception ex)
{
if (IsErrorHandled(underlyingDictionary, contract, propertyName, null, writer.ContainerPath, ex))
HandleError(writer, initialDepth);
else
throw;
}
}
writer.WriteEndObject();
_serializeStack.RemoveAt(_serializeStack.Count - 1);
OnSerialized(writer, contract, underlyingDictionary);
}