private void SerializeValue(JsonWriter writer, object value, JsonContract valueContract, JsonProperty member, JsonContainerContract collectionContract, JsonContract collectionValueContract) { if (value == null) { writer.WriteNull(); return; } JsonConverter converter; if ((((converter = (member != null) ? member.Converter : null) != null) || ((converter = (collectionContract != null) ? collectionContract.ItemConverter : null) != null) || ((converter = valueContract.Converter) != null) || ((converter = Serializer.GetMatchingConverter(valueContract.UnderlyingType)) != null) || ((converter = valueContract.InternalConverter) != null)) && converter.CanWrite) { SerializeConvertable(writer, converter, value, valueContract, collectionContract, collectionValueContract); return; } switch (valueContract.ContractType) { case JsonContractType.Object: SerializeObject(writer, value, (JsonObjectContract)valueContract, member, collectionContract, collectionValueContract); break; case JsonContractType.Array: JsonArrayContract arrayContract = (JsonArrayContract)valueContract; SerializeList(writer, arrayContract.CreateWrapper(value), arrayContract, member, collectionContract, collectionValueContract); break; case JsonContractType.Primitive: SerializePrimitive(writer, value, (JsonPrimitiveContract)valueContract, member, collectionContract, collectionValueContract); break; case JsonContractType.String: SerializeString(writer, value, (JsonStringContract)valueContract); break; case JsonContractType.Dictionary: JsonDictionaryContract dictionaryContract = (JsonDictionaryContract)valueContract; SerializeDictionary(writer, dictionaryContract.CreateWrapper(value), dictionaryContract, member, collectionContract, collectionValueContract); break; #if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE) case JsonContractType.Dynamic: SerializeDynamic(writer, (IDynamicMetaObjectProvider)value, (JsonDynamicContract)valueContract, member, collectionContract, collectionValueContract); break; #endif #if !(SILVERLIGHT || NETFX_CORE || PORTABLE) case JsonContractType.Serializable: SerializeISerializable(writer, (ISerializable)value, (JsonISerializableContract)valueContract, member, collectionContract, collectionValueContract); break; #endif case JsonContractType.Linq: ((JToken)value).WriteTo(writer, (Serializer.Converters != null) ? Serializer.Converters.ToArray() : null); break; } }
private object CreateAndPopulateDictionary(JsonReader reader, JsonDictionaryContract contract, string id) { IWrappedDictionary dictionary = contract.CreateWrapper(contract.DefaultCreator()); PopulateDictionary(dictionary, reader, contract, id); return(dictionary.UnderlyingDictionary); }
private void SerializeValue(JsonWriter writer, object value, JsonContract valueContract, JsonProperty member, JsonContract collectionValueContract) { JsonConverter converter = (member != null) ? member.Converter : null; if (value == null) { writer.WriteNull(); return; } if ((converter != null || ((converter = valueContract.Converter) != null) || ((converter = Serializer.GetMatchingConverter(valueContract.UnderlyingType)) != null) || ((converter = valueContract.InternalConverter) != null)) && converter.CanWrite) { SerializeConvertable(writer, converter, value, valueContract); } else if (valueContract is JsonPrimitiveContract) { SerializePrimitive(writer, value, (JsonPrimitiveContract)valueContract, member, collectionValueContract); } else if (valueContract is JsonStringContract) { SerializeString(writer, value, (JsonStringContract)valueContract); } else if (valueContract is JsonObjectContract) { SerializeObject(writer, value, (JsonObjectContract)valueContract, member, collectionValueContract); } else if (valueContract is JsonDictionaryContract) { JsonDictionaryContract dictionaryContract = (JsonDictionaryContract)valueContract; SerializeDictionary(writer, dictionaryContract.CreateWrapper(value), dictionaryContract, member, collectionValueContract); } else if (valueContract is JsonArrayContract) { JsonArrayContract arrayContract = (JsonArrayContract)valueContract; if (!arrayContract.IsMultidimensionalArray) { SerializeList(writer, arrayContract.CreateWrapper(value), arrayContract, member, collectionValueContract); } else { SerializeMultidimensionalArray(writer, (Array)value, arrayContract, member, collectionValueContract); } } else if (valueContract is JsonLinqContract) { ((JToken)value).WriteTo(writer, (Serializer.Converters != null) ? Serializer.Converters.ToArray() : null); } #if !((UNITY_WINRT && !UNITY_EDITOR) || (UNITY_WP8 || UNITY_WP_8_1)) else if (valueContract is JsonISerializableContract) { SerializeISerializable(writer, (ISerializable)value, (JsonISerializableContract)valueContract); } #endif }
private void SerializeValue(JsonWriter writer, object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) { if (value == null) { writer.WriteNull(); } else { JsonConverter converter; if (((converter = member != null ? member.Converter : (JsonConverter)null) != null || (converter = containerProperty != null ? containerProperty.ItemConverter : (JsonConverter)null) != null || ((converter = containerContract != null ? containerContract.ItemConverter : (JsonConverter)null) != null || (converter = valueContract.Converter) != null || ((converter = this.Serializer.GetMatchingConverter(valueContract.UnderlyingType)) != null || (converter = valueContract.InternalConverter) != null))) && converter.CanWrite) { this.SerializeConvertable(writer, converter, value, valueContract, containerContract, containerProperty); } else { switch (valueContract.ContractType) { case JsonContractType.Object: this.SerializeObject(writer, value, (JsonObjectContract)valueContract, member, containerContract, containerProperty); break; case JsonContractType.Array: JsonArrayContract contract1 = (JsonArrayContract)valueContract; if (!contract1.IsMultidimensionalArray) { this.SerializeList(writer, contract1.CreateWrapper(value), contract1, member, containerContract, containerProperty); break; } else { this.SerializeMultidimensionalArray(writer, (Array)value, contract1, member, containerContract, containerProperty); break; } case JsonContractType.Primitive: this.SerializePrimitive(writer, value, (JsonPrimitiveContract)valueContract, member, containerContract, containerProperty); break; case JsonContractType.String: this.SerializeString(writer, value, (JsonStringContract)valueContract); break; case JsonContractType.Dictionary: JsonDictionaryContract contract2 = (JsonDictionaryContract)valueContract; this.SerializeDictionary(writer, contract2.CreateWrapper(value), contract2, member, containerContract, containerProperty); break; case JsonContractType.Serializable: this.SerializeISerializable(writer, (ISerializable)value, (JsonISerializableContract)valueContract, member, containerContract, containerProperty); break; case JsonContractType.Linq: ((JToken)value).WriteTo(writer, this.Serializer.Converters != null ? Enumerable.ToArray <JsonConverter>((IEnumerable <JsonConverter>) this.Serializer.Converters) : (JsonConverter[])null); break; } } } }
private void SerializeValue(JsonWriter writer, object value, JsonContract valueContract, JsonProperty member, JsonContract collectionValueContract) { JsonConverter converter = (member != null) ? member.Converter : null; if (value == null) { writer.WriteNull(); return; } if ((converter != null || ((converter = valueContract.Converter) != null) || ((converter = Serializer.GetMatchingConverter(valueContract.UnderlyingType)) != null) || ((converter = valueContract.InternalConverter) != null)) && converter.CanWrite) { SerializeConvertable(writer, converter, value, valueContract); } else if (valueContract is JsonPrimitiveContract) { writer.WriteValue(value); } else if (valueContract is JsonStringContract) { SerializeString(writer, value, (JsonStringContract)valueContract); } else if (valueContract is JsonObjectContract) { SerializeObject(writer, value, (JsonObjectContract)valueContract, member, collectionValueContract); } else if (valueContract is JsonDictionaryContract) { JsonDictionaryContract dictionaryContract = (JsonDictionaryContract)valueContract; SerializeDictionary(writer, dictionaryContract.CreateWrapper(value), dictionaryContract, member, collectionValueContract); } else if (valueContract is JsonArrayContract) { JsonArrayContract arrayContract = (JsonArrayContract)valueContract; SerializeList(writer, arrayContract.CreateWrapper(value), arrayContract, member, collectionValueContract); } else if (valueContract is JsonLinqContract) { ((JToken)value).WriteTo(writer, (Serializer.Converters != null) ? Serializer.Converters.ToArray() : null); } #if !SILVERLIGHT && !PocketPC else if (valueContract is JsonISerializableContract) { SerializeISerializable(writer, (ISerializable)value, (JsonISerializableContract)valueContract); } #endif #if !(NET35 || NET20 || WINDOWS_PHONE) else if (valueContract is JsonDynamicContract) { SerializeDynamic(writer, (IDynamicMetaObjectProvider)value, (JsonDynamicContract)valueContract); } #endif }
private object CreateAndPopulateDictionary(JsonReader reader, JsonDictionaryContract contract, string id) { if (contract.DefaultCreator != null && (!contract.DefaultCreatorNonPublic || base.Serializer.ConstructorHandling == ConstructorHandling.AllowNonPublicDefaultConstructor)) { object dictionary = contract.DefaultCreator(); IWrappedDictionary wrappedDictionary = contract.CreateWrapper(dictionary); PopulateDictionary(wrappedDictionary, reader, contract, id); return(wrappedDictionary.UnderlyingDictionary); } throw new JsonSerializationException("Unable to find a default constructor to use for type {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)); }
private void SerializeValue(JsonWriter writer, object value, JsonContract valueContract, JsonProperty member, JsonContract collectionValueContract) { JsonConverter converter = (member != null) ? member.Converter : null; if (converter == null && JsonSerializerSettings.DefaultEnumSerializationHandling == EnumSerializationHandling.Name && value is Enum) { converter = new StringEnumConverter(); } if (value == null) { writer.WriteNull(); return; } if ((converter != null || ((converter = valueContract.Converter) != null) || ((converter = Serializer.GetMatchingConverter(valueContract.UnderlyingType)) != null) || ((converter = valueContract.InternalConverter) != null)) && converter.CanWrite) { SerializeConvertable(writer, converter, value, valueContract); } else if (valueContract is JsonPrimitiveContract) { SerializePrimitive(writer, value, (JsonPrimitiveContract)valueContract, member, collectionValueContract); } else if (valueContract is JsonStringContract) { SerializeString(writer, value, (JsonStringContract)valueContract); } else if (valueContract is JsonObjectContract) { SerializeObject(writer, value, (JsonObjectContract)valueContract, member, collectionValueContract); } else if (valueContract is JsonDictionaryContract) { JsonDictionaryContract dictionaryContract = (JsonDictionaryContract)valueContract; SerializeDictionary(writer, dictionaryContract.CreateWrapper(value), dictionaryContract, member, collectionValueContract); } else if (valueContract is JsonArrayContract) { JsonArrayContract arrayContract = (JsonArrayContract)valueContract; SerializeList(writer, arrayContract.CreateWrapper(value), arrayContract, member, collectionValueContract); } else if (valueContract is JsonLinqContract) { ((JToken)value).WriteTo(writer, (Serializer.Converters != null) ? Serializer.Converters.ToArray() : null); } else if (valueContract is JsonISerializableContract) { SerializeISerializable(writer, (ISerializable)value, (JsonISerializableContract)valueContract); } }
private void SerializeValue(JsonWriter writer, object value, JsonContract valueContract, JsonProperty member, JsonContract collectionValueContract) { JsonConverter jsonConverter = (member == null) ? null : member.Converter; if (value == null) { writer.WriteNull(); return; } if ((jsonConverter != null || (jsonConverter = valueContract.Converter) != null || (jsonConverter = base.Serializer.GetMatchingConverter(valueContract.UnderlyingType)) != null || (jsonConverter = valueContract.InternalConverter) != null) && jsonConverter.CanWrite) { this.SerializeConvertable(writer, jsonConverter, value, valueContract); } else if (valueContract is JsonPrimitiveContract) { this.SerializePrimitive(writer, value, (JsonPrimitiveContract)valueContract, member, collectionValueContract); } else if (valueContract is JsonStringContract) { this.SerializeString(writer, value, (JsonStringContract)valueContract); } else if (valueContract is JsonObjectContract) { this.SerializeObject(writer, value, (JsonObjectContract)valueContract, member, collectionValueContract); } else if (valueContract is JsonDictionaryContract) { JsonDictionaryContract jsonDictionaryContract = (JsonDictionaryContract)valueContract; this.SerializeDictionary(writer, jsonDictionaryContract.CreateWrapper(value), jsonDictionaryContract, member, collectionValueContract); } else if (valueContract is JsonArrayContract) { JsonArrayContract jsonArrayContract = (JsonArrayContract)valueContract; if (!jsonArrayContract.IsMultidimensionalArray) { this.SerializeList(writer, jsonArrayContract.CreateWrapper(value), jsonArrayContract, member, collectionValueContract); } else { this.SerializeMultidimensionalArray(writer, (Array)value, jsonArrayContract, member, collectionValueContract); } } else if (valueContract is JsonLinqContract) { ((JToken)value).WriteTo(writer, (base.Serializer.Converters == null) ? null : base.Serializer.Converters.ToArray <JsonConverter>()); } else if (valueContract is JsonISerializableContract) { this.SerializeISerializable(writer, (ISerializable)value, (JsonISerializableContract)valueContract); } }
private void SerializeValue(JsonWriter writer, object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) { object converter; IDictionary dictionaries; if (value == null) { writer.WriteNull(); return; } if (member != null) { converter = member.Converter; } else { converter = null; } if (converter == null) { if (containerProperty != null) { converter = containerProperty.ItemConverter; } else { converter = null; } if (converter == null) { if (containerContract != null) { converter = containerContract.ItemConverter; } else { converter = null; } if (converter == null) { converter = valueContract.Converter ?? (this.Serializer.GetMatchingConverter(valueContract.UnderlyingType) ?? valueContract.InternalConverter); } } } JsonConverter jsonConverter = (JsonConverter)converter; if (jsonConverter != null && jsonConverter.CanWrite) { this.SerializeConvertable(writer, jsonConverter, value, valueContract, containerContract, containerProperty); return; } switch (valueContract.ContractType) { case JsonContractType.Object: { this.SerializeObject(writer, value, (JsonObjectContract)valueContract, member, containerContract, containerProperty); return; } case JsonContractType.Array: { JsonArrayContract jsonArrayContract = (JsonArrayContract)valueContract; if (!jsonArrayContract.IsMultidimensionalArray) { this.SerializeList(writer, (IEnumerable)value, jsonArrayContract, member, containerContract, containerProperty); return; } this.SerializeMultidimensionalArray(writer, (Array)value, jsonArrayContract, member, containerContract, containerProperty); return; } case JsonContractType.Primitive: { this.SerializePrimitive(writer, value, (JsonPrimitiveContract)valueContract, member, containerContract, containerProperty); return; } case JsonContractType.String: { this.SerializeString(writer, value, (JsonStringContract)valueContract); return; } case JsonContractType.Dictionary: { JsonDictionaryContract jsonDictionaryContract = (JsonDictionaryContract)valueContract; JsonWriter jsonWriter = writer; if (value is IDictionary) { dictionaries = (IDictionary)value; } else { dictionaries = jsonDictionaryContract.CreateWrapper(value); } this.SerializeDictionary(jsonWriter, dictionaries, jsonDictionaryContract, member, containerContract, containerProperty); return; } case JsonContractType.Dynamic: { return; } case JsonContractType.Serializable: { this.SerializeISerializable(writer, (ISerializable)value, (JsonISerializableContract)valueContract, member, containerContract, containerProperty); return; } case JsonContractType.Linq: { ((JToken)value).WriteTo(writer, this.Serializer.Converters.ToArray <JsonConverter>()); return; } default: { return; } } }
private void SerializeValue(JsonWriter writer, object value, JsonContract valueContract, JsonProperty member, JsonContract collectionValueContract) { JsonConverter converter; JsonConverter[] array; if (member == null) { converter = null; } else { converter = member.Converter; } JsonConverter jsonConverter = converter; if (value == null) { writer.WriteNull(); return; } if (jsonConverter == null) { JsonConverter converter1 = valueContract.Converter; jsonConverter = converter1; if (converter1 == null) { JsonConverter matchingConverter = base.Serializer.GetMatchingConverter(valueContract.UnderlyingType); jsonConverter = matchingConverter; if (matchingConverter == null) { JsonConverter internalConverter = valueContract.InternalConverter; jsonConverter = internalConverter; if (internalConverter == null) { goto Label0; } } } } if (jsonConverter.CanWrite) { this.SerializeConvertable(writer, jsonConverter, value, valueContract); return; } if (valueContract is JsonPrimitiveContract) { this.SerializePrimitive(writer, value, (JsonPrimitiveContract)valueContract, member, collectionValueContract); } else if (valueContract is JsonStringContract) { this.SerializeString(writer, value, (JsonStringContract)valueContract); } else if (valueContract is JsonObjectContract) { this.SerializeObject(writer, value, (JsonObjectContract)valueContract, member, collectionValueContract); } else if (valueContract is JsonDictionaryContract) { JsonDictionaryContract jsonDictionaryContract = (JsonDictionaryContract)valueContract; this.SerializeDictionary(writer, jsonDictionaryContract.CreateWrapper(value), jsonDictionaryContract, member, collectionValueContract); } else if (valueContract is JsonArrayContract) { JsonArrayContract jsonArrayContract = (JsonArrayContract)valueContract; if (jsonArrayContract.IsMultidimensionalArray) { this.SerializeMultidimensionalArray(writer, (Array)value, jsonArrayContract, member, collectionValueContract); } else { this.SerializeList(writer, jsonArrayContract.CreateWrapper(value), jsonArrayContract, member, collectionValueContract); } } else if (valueContract is JsonLinqContract) { JToken jTokens = (JToken)value; JsonWriter jsonWriter = writer; if (base.Serializer.Converters == null) { array = null; } else { array = base.Serializer.Converters.ToArray <JsonConverter>(); } jTokens.WriteTo(jsonWriter, array); } else if (valueContract is JsonISerializableContract) { this.SerializeISerializable(writer, (ISerializable)value, (JsonISerializableContract)valueContract); } }
private IDictionary CreateNewDictionary(JsonReader reader, JsonDictionaryContract contract, out bool createdFromNonDefaultCreator) { if (contract.OverrideCreator != null) { if (contract.HasParameterizedCreator) { createdFromNonDefaultCreator = true; return contract.CreateTemporaryDictionary(); } else { createdFromNonDefaultCreator = false; return (IDictionary)contract.OverrideCreator(); } } else if (contract.IsReadOnlyOrFixedSize) { createdFromNonDefaultCreator = true; return contract.CreateTemporaryDictionary(); } else if (contract.DefaultCreator != null && (!contract.DefaultCreatorNonPublic || Serializer._constructorHandling == ConstructorHandling.AllowNonPublicDefaultConstructor)) { object dictionary = contract.DefaultCreator(); if (contract.ShouldCreateWrapper) { dictionary = contract.CreateWrapper(dictionary); } createdFromNonDefaultCreator = false; return (IDictionary)dictionary; } else if (contract.HasParameterizedCreatorInternal) { createdFromNonDefaultCreator = true; return contract.CreateTemporaryDictionary(); } else { if (!contract.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, contract.UnderlyingType)); } throw JsonSerializationException.Create(reader, "Unable to find a default constructor to use for type {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)); } }
private void SerializeValue(JsonWriter writer, object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) { if (value == null) { writer.WriteNull(); return; } JsonConverter jsonConverter; if (((jsonConverter = ((member != null) ? member.Converter : null)) != null || (jsonConverter = ((containerProperty != null) ? containerProperty.ItemConverter : null)) != null || (jsonConverter = ((containerContract != null) ? containerContract.ItemConverter : null)) != null || (jsonConverter = valueContract.Converter) != null || (jsonConverter = this.Serializer.GetMatchingConverter(valueContract.UnderlyingType)) != null || (jsonConverter = valueContract.InternalConverter) != null) && jsonConverter.CanWrite) { this.SerializeConvertable(writer, jsonConverter, value, valueContract, containerContract, containerProperty); return; } switch (valueContract.ContractType) { case JsonContractType.Object: this.SerializeObject(writer, value, (JsonObjectContract)valueContract, member, containerContract, containerProperty); return; case JsonContractType.Array: { JsonArrayContract jsonArrayContract = (JsonArrayContract)valueContract; if (!jsonArrayContract.IsMultidimensionalArray) { this.SerializeList(writer, (IEnumerable)value, jsonArrayContract, member, containerContract, containerProperty); return; } this.SerializeMultidimensionalArray(writer, (Array)value, jsonArrayContract, member, containerContract, containerProperty); return; } case JsonContractType.Primitive: this.SerializePrimitive(writer, value, (JsonPrimitiveContract)valueContract, member, containerContract, containerProperty); return; case JsonContractType.String: this.SerializeString(writer, value, (JsonStringContract)valueContract); return; case JsonContractType.Dictionary: { JsonDictionaryContract jsonDictionaryContract = (JsonDictionaryContract)valueContract; this.SerializeDictionary(writer, (value is IDictionary) ? ((IDictionary)value) : jsonDictionaryContract.CreateWrapper(value), jsonDictionaryContract, member, containerContract, containerProperty); return; } case JsonContractType.Dynamic: this.SerializeDynamic(writer, (IDynamicMetaObjectProvider)value, (JsonDynamicContract)valueContract, member, containerContract, containerProperty); return; case JsonContractType.Serializable: this.SerializeISerializable(writer, (ISerializable)value, (JsonISerializableContract)valueContract, member, containerContract, containerProperty); return; case JsonContractType.Linq: ((JToken)value).WriteTo(writer, this.Serializer.Converters.ToArray <JsonConverter>()); return; default: return; } }
private object CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, object existingValue) { CheckedRead(reader); string text = null; if (reader.TokenType == JsonToken.PropertyName) { bool flag; do { string a = reader.Value.ToString(); if (string.Equals(a, "$ref", StringComparison.Ordinal)) { CheckedRead(reader); if (reader.TokenType != JsonToken.String && reader.TokenType != JsonToken.Null) { throw new JsonSerializationException("JSON reference {0} property must have a string or null value.".FormatWith(CultureInfo.InvariantCulture, "$ref")); } string text2 = ((reader.Value != null) ? reader.Value.ToString() : null); CheckedRead(reader); if (text2 != null) { if (reader.TokenType == JsonToken.PropertyName) { throw new JsonSerializationException("Additional content found in JSON reference object. A JSON reference object should only have a {0} property.".FormatWith(CultureInfo.InvariantCulture, "$ref")); } return(base.Serializer.ReferenceResolver.ResolveReference(this, text2)); } flag = true; } else if (string.Equals(a, "$type", StringComparison.Ordinal)) { CheckedRead(reader); string text3 = reader.Value.ToString(); CheckedRead(reader); if ((member?.TypeNameHandling ?? base.Serializer.TypeNameHandling) != 0) { ReflectionUtils.SplitFullyQualifiedTypeName(text3, out var typeName, out var assemblyName); Type type; try { type = base.Serializer.Binder.BindToType(assemblyName, typeName); } catch (Exception innerException) { throw new JsonSerializationException("Error resolving type specified in JSON '{0}'.".FormatWith(CultureInfo.InvariantCulture, text3), innerException); } if (type == null) { throw new JsonSerializationException("Type specified in JSON '{0}' was not resolved.".FormatWith(CultureInfo.InvariantCulture, text3)); } if (!(objectType?.IsAssignableFrom(type) ?? true)) { throw new JsonSerializationException("Type specified in JSON '{0}' is not compatible with '{1}'.".FormatWith(CultureInfo.InvariantCulture, type.AssemblyQualifiedName, objectType.AssemblyQualifiedName)); } objectType = type; contract = GetContractSafe(type); } flag = true; } else if (string.Equals(a, "$id", StringComparison.Ordinal)) { CheckedRead(reader); text = ((reader.Value != null) ? reader.Value.ToString() : null); CheckedRead(reader); flag = true; } else { if (string.Equals(a, "$values", StringComparison.Ordinal)) { CheckedRead(reader); object result = CreateList(reader, objectType, contract, member, existingValue, text); CheckedRead(reader); return(result); } flag = false; } }while (flag && reader.TokenType == JsonToken.PropertyName); } if (!HasDefinedType(objectType)) { return(CreateJObject(reader)); } if (contract == null) { throw new JsonSerializationException("Could not resolve type '{0}' to a JsonContract.".FormatWith(CultureInfo.InvariantCulture, objectType)); } JsonDictionaryContract jsonDictionaryContract = contract as JsonDictionaryContract; if (jsonDictionaryContract != null) { if (existingValue == null) { return(CreateAndPopulateDictionary(reader, jsonDictionaryContract, text)); } return(PopulateDictionary(jsonDictionaryContract.CreateWrapper(existingValue), reader, jsonDictionaryContract, text)); } JsonObjectContract jsonObjectContract = contract as JsonObjectContract; if (jsonObjectContract != null) { if (existingValue == null) { return(CreateAndPopulateObject(reader, jsonObjectContract, text)); } return(PopulateObject(existingValue, reader, jsonObjectContract, text)); } JsonPrimitiveContract jsonPrimitiveContract = contract as JsonPrimitiveContract; if (jsonPrimitiveContract != null && reader.TokenType == JsonToken.PropertyName && string.Equals(reader.Value.ToString(), "$value", StringComparison.Ordinal)) { CheckedRead(reader); object result2 = CreateValueInternal(reader, objectType, jsonPrimitiveContract, member, existingValue); CheckedRead(reader); return(result2); } JsonISerializableContract jsonISerializableContract = contract as JsonISerializableContract; if (jsonISerializableContract != null) { return(CreateISerializable(reader, jsonISerializableContract, text)); } throw new JsonSerializationException("Cannot deserialize JSON object into type '{0}'.".FormatWith(CultureInfo.InvariantCulture, objectType)); }
private IDictionary CreateNewDictionary(JsonReader reader, JsonDictionaryContract contract, out bool createdFromNonDefaultConstructor) { if (contract.IsReadOnlyOrFixedSize) { createdFromNonDefaultConstructor = true; return contract.CreateTemporaryDictionary(); } else if (contract.DefaultCreator != null && (!contract.DefaultCreatorNonPublic || Serializer._constructorHandling == ConstructorHandling.AllowNonPublicDefaultConstructor)) { object dictionary = contract.DefaultCreator(); if (contract.ShouldCreateWrapper) dictionary = contract.CreateWrapper(dictionary); createdFromNonDefaultConstructor = false; return (IDictionary) dictionary; } else if (contract.ParametrizedConstructor != null) { createdFromNonDefaultConstructor = true; return contract.CreateTemporaryDictionary(); } else { throw JsonSerializationException.Create(reader, "Unable to find a default constructor to use for type {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)); } }
private void SerializeValue(JsonWriter writer, object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) { if (value == null) { writer.WriteNull(); return; } JsonConverter converter = ((member != null) ? member.Converter : null) ?? ((containerProperty != null) ? containerProperty.ItemConverter : null) ?? ((containerContract != null) ? containerContract.ItemConverter : null) ?? valueContract.Converter ?? Serializer.GetMatchingConverter(valueContract.UnderlyingType) ?? valueContract.InternalConverter; if (converter != null && converter.CanWrite) { SerializeConvertable(writer, converter, value, valueContract, containerContract, containerProperty); return; } switch (valueContract.ContractType) { case JsonContractType.Object: SerializeObject(writer, value, (JsonObjectContract)valueContract, member, containerContract, containerProperty); break; case JsonContractType.Array: JsonArrayContract arrayContract = (JsonArrayContract)valueContract; if (!arrayContract.IsMultidimensionalArray) { SerializeList(writer, (IEnumerable)value, arrayContract, member, containerContract, containerProperty); } else { SerializeMultidimensionalArray(writer, (Array)value, arrayContract, member, containerContract, containerProperty); } break; case JsonContractType.Primitive: SerializePrimitive(writer, value, (JsonPrimitiveContract)valueContract, member, containerContract, containerProperty); break; case JsonContractType.String: SerializeString(writer, value, (JsonStringContract)valueContract); break; case JsonContractType.Dictionary: JsonDictionaryContract dictionaryContract = (JsonDictionaryContract)valueContract; SerializeDictionary(writer, (value is IDictionary) ? (IDictionary)value : dictionaryContract.CreateWrapper(value), dictionaryContract, member, containerContract, containerProperty); break; #if !(NET35 || NET20 || PORTABLE40) case JsonContractType.Dynamic: SerializeDynamic(writer, (IDynamicMetaObjectProvider)value, (JsonDynamicContract)valueContract, member, containerContract, containerProperty); break; #endif #if !(NETFX_CORE || PORTABLE40 || PORTABLE) case JsonContractType.Serializable: SerializeISerializable(writer, (ISerializable)value, (JsonISerializableContract)valueContract, member, containerContract, containerProperty); break; #endif case JsonContractType.Linq: ((JToken)value).WriteTo(writer, Serializer.Converters.ToArray()); break; } }
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; }
private object CreateObjectFromNonDefaultConstructor(JsonReader reader, JsonObjectContract contract, ConstructorInfo constructorInfo, string id) { ValidationUtils.ArgumentNotNull(constructorInfo, "constructorInfo"); Type underlyingType = contract.UnderlyingType; IDictionary <JsonProperty, object> dictionary = ResolvePropertyAndConstructorValues(contract, reader, underlyingType); IDictionary <ParameterInfo, object> dictionary2 = ((IEnumerable <ParameterInfo>)constructorInfo.GetParameters()).ToDictionary((Func <ParameterInfo, ParameterInfo>)((ParameterInfo p) => p), (Func <ParameterInfo, object>)((ParameterInfo p) => null)); IDictionary <JsonProperty, object> dictionary3 = new Dictionary <JsonProperty, object>(); foreach (KeyValuePair <JsonProperty, object> item in dictionary) { ParameterInfo key = dictionary2.ForgivingCaseSensitiveFind((KeyValuePair <ParameterInfo, object> kv) => kv.Key.Name, item.Key.UnderlyingName).Key; if (key != null) { dictionary2[key] = item.Value; } else { dictionary3.Add(item); } } object obj = constructorInfo.Invoke(dictionary2.Values.ToArray()); if (id != null) { base.Serializer.ReferenceResolver.AddReference(this, id, obj); } contract.InvokeOnDeserializing(obj, base.Serializer.Context); foreach (KeyValuePair <JsonProperty, object> item2 in dictionary3) { JsonProperty key2 = item2.Key; object value = item2.Value; if (ShouldSetPropertyValue(item2.Key, item2.Value)) { key2.ValueProvider.SetValue(obj, value); } else { if (key2.Writable || value == null) { continue; } JsonContract jsonContract = base.Serializer.ContractResolver.ResolveContract(key2.PropertyType); if (jsonContract is JsonArrayContract) { JsonArrayContract jsonArrayContract = jsonContract as JsonArrayContract; object value2 = key2.ValueProvider.GetValue(obj); if (value2 == null) { continue; } IWrappedCollection wrappedCollection = jsonArrayContract.CreateWrapper(value2); IWrappedCollection wrappedCollection2 = jsonArrayContract.CreateWrapper(value); foreach (object item3 in wrappedCollection2) { wrappedCollection.Add(item3); } } else { if (!(jsonContract is JsonDictionaryContract)) { continue; } JsonDictionaryContract jsonDictionaryContract = jsonContract as JsonDictionaryContract; object value3 = key2.ValueProvider.GetValue(obj); if (value3 == null) { continue; } IWrappedDictionary wrappedDictionary = jsonDictionaryContract.CreateWrapper(value3); IWrappedDictionary wrappedDictionary2 = jsonDictionaryContract.CreateWrapper(value); foreach (DictionaryEntry item4 in wrappedDictionary2) { wrappedDictionary.Add(item4.Key, item4.Value); } } } } contract.InvokeOnDeserialized(obj, base.Serializer.Context); return(obj); }
private object CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, object existingValue) { CheckedRead(reader); string id = null; if (reader.TokenType == JsonToken.PropertyName) { bool specialProperty; do { string propertyName = reader.Value.ToString(); if (string.Equals(propertyName, JsonTypeReflector.RefPropertyName, StringComparison.Ordinal)) { CheckedRead(reader); if (reader.TokenType != JsonToken.String) { throw new JsonSerializationException("JSON reference {0} property must have a string value.".FormatWith(CultureInfo.InvariantCulture, JsonTypeReflector.RefPropertyName)); } string reference = reader.Value.ToString(); CheckedRead(reader); if (reader.TokenType == JsonToken.PropertyName) { throw new JsonSerializationException("Additional content found in JSON reference object. A JSON reference object should only have a {0} property.".FormatWith(CultureInfo.InvariantCulture, JsonTypeReflector.RefPropertyName)); } return(Serializer.ReferenceResolver.ResolveReference(reference)); } else if (string.Equals(propertyName, JsonTypeReflector.TypePropertyName, StringComparison.Ordinal)) { CheckedRead(reader); string qualifiedTypeName = reader.Value.ToString(); CheckedRead(reader); if ((((member != null) ? member.TypeNameHandling : null) ?? Serializer.TypeNameHandling) != TypeNameHandling.None) { string typeName; string assemblyName; ReflectionUtils.SplitFullyQualifiedTypeName(qualifiedTypeName, out typeName, out assemblyName); Type specifiedType; try { specifiedType = Serializer.Binder.BindToType(assemblyName, typeName); } catch (Exception ex) { throw new JsonSerializationException("Error resolving type specified in JSON '{0}'.".FormatWith(CultureInfo.InvariantCulture, qualifiedTypeName), ex); } if (specifiedType == null) { throw new JsonSerializationException("Type specified in JSON '{0}' was not resolved.".FormatWith(CultureInfo.InvariantCulture, qualifiedTypeName)); } if (objectType != null && !objectType.IsAssignableFrom(specifiedType)) { throw new JsonSerializationException("Type specified in JSON '{0}' is not compatible with '{1}'.".FormatWith(CultureInfo.InvariantCulture, specifiedType.AssemblyQualifiedName, objectType.AssemblyQualifiedName)); } objectType = specifiedType; contract = GetContractSafe(specifiedType); } specialProperty = true; } else if (string.Equals(propertyName, JsonTypeReflector.IdPropertyName, StringComparison.Ordinal)) { CheckedRead(reader); id = reader.Value.ToString(); CheckedRead(reader); specialProperty = true; } else if (string.Equals(propertyName, JsonTypeReflector.ArrayValuesPropertyName, StringComparison.Ordinal)) { CheckedRead(reader); object list = CreateList(reader, objectType, contract, member, existingValue, id); CheckedRead(reader); return(list); } else { specialProperty = false; } } while (specialProperty && reader.TokenType == JsonToken.PropertyName); } if (!HasDefinedType(objectType)) { return(CreateJObject(reader)); } if (contract == null) { throw new JsonSerializationException("Could not resolve type '{0}' to a JsonContract.".FormatWith(CultureInfo.InvariantCulture, objectType)); } JsonDictionaryContract dictionaryContract = contract as JsonDictionaryContract; if (dictionaryContract != null) { if (existingValue == null) { return(CreateAndPopulateDictionary(reader, dictionaryContract, id)); } return(PopulateDictionary(dictionaryContract.CreateWrapper(existingValue), reader, dictionaryContract, id)); } JsonObjectContract objectContract = contract as JsonObjectContract; if (objectContract != null) { if (existingValue == null) { return(CreateAndPopulateObject(reader, objectContract, id)); } return(PopulateObject(existingValue, reader, objectContract, id)); } #if !SILVERLIGHT && !PocketPC JsonISerializableContract serializableContract = contract as JsonISerializableContract; if (serializableContract != null) { return(CreateISerializable(reader, serializableContract, id)); } #endif #if !(NET35 || NET20 || SILVERLIGHT) JsonDynamicContract dynamicContract = contract as JsonDynamicContract; if (dynamicContract != null) { return(CreateDynamic(reader, dynamicContract, id)); } #endif throw new JsonSerializationException("Cannot deserialize JSON object into type '{0}'.".FormatWith(CultureInfo.InvariantCulture, objectType)); }
private void SerializeValue(JsonWriter writer, object value, JsonContract valueContract, JsonProperty member, JsonContract collectionValueContract) { JsonConverter converter = (member != null) ? member.Converter : null; if (value == null) { writer.WriteNull(); return; } if ((converter != null || ((converter = valueContract.Converter) != null) || ((converter = Serializer.GetMatchingConverter(valueContract.UnderlyingType)) != null) || ((converter = valueContract.InternalConverter) != null)) && converter.CanWrite) { SerializeConvertable(writer, converter, value, valueContract); } else if (valueContract is JsonPrimitiveContract) { writer.WriteValue(value); } else if (valueContract is JsonStringContract) { SerializeString(writer, value, (JsonStringContract)valueContract); } else if (valueContract is JsonObjectContract) { SerializeObject(writer, value, (JsonObjectContract)valueContract, member, collectionValueContract); } else if (valueContract is JsonDictionaryContract) { JsonDictionaryContract dictionaryContract = (JsonDictionaryContract)valueContract; SerializeDictionary(writer, dictionaryContract.CreateWrapper(value), dictionaryContract, member, collectionValueContract); } else if (valueContract is JsonArrayContract) { if (value is IList) { SerializeList(writer, (IList)value, (JsonArrayContract)valueContract, member, collectionValueContract); } else if (value is IEnumerable) { SerializeList(writer, ((IEnumerable)value).Cast <object>().ToList(), (JsonArrayContract)valueContract, member, collectionValueContract); } else { throw new Exception( "Cannot serialize '{0}' into a JSON array. Type does not implement IEnumerable.".FormatWith( CultureInfo.InvariantCulture, value.GetType())); } } else if (valueContract is JsonLinqContract) { ((JToken)value).WriteTo(writer, (Serializer.Converters != null) ? Serializer.Converters.ToArray() : null); } #if !SILVERLIGHT && !PocketPC else if (valueContract is JsonISerializableContract) { SerializeISerializable(writer, (ISerializable)value, (JsonISerializableContract)valueContract); } #endif }
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; }
private object CreateObjectFromNonDefaultConstructor(JsonReader reader, JsonObjectContract contract, ConstructorInfo constructorInfo, string id) { ValidationUtils.ArgumentNotNull(constructorInfo, "constructorInfo"); Type underlyingType = contract.UnderlyingType; IDictionary <JsonProperty, object> dictionary = ResolvePropertyAndConstructorValues(contract, reader, underlyingType); IDictionary <ParameterInfo, object> dictionary2 = ((IEnumerable <ParameterInfo>)constructorInfo.GetParameters()).ToDictionary((Func <ParameterInfo, ParameterInfo>)((ParameterInfo p) => p), (Func <ParameterInfo, object>)((ParameterInfo p) => null)); IDictionary <JsonProperty, object> dictionary3 = new Dictionary <JsonProperty, object>(); foreach (KeyValuePair <JsonProperty, object> item in dictionary) { ParameterInfo key = dictionary2.ForgivingCaseSensitiveFind((KeyValuePair <ParameterInfo, object> kv) => kv.Key.Name, item.Key.UnderlyingName).Key; if (key != null) { dictionary2[key] = item.Value; } else { dictionary3.Add(item); } } object obj = constructorInfo.Invoke(dictionary2.Values.ToArray()); if (id != null) { base.Serializer.ReferenceResolver.AddReference(this, id, obj); } contract.InvokeOnDeserializing(obj, base.Serializer.Context); foreach (KeyValuePair <JsonProperty, object> item2 in dictionary3) { JsonProperty key2 = item2.Key; object value = item2.Value; if (ShouldSetPropertyValue(item2.Key, item2.Value)) { key2.ValueProvider.SetValue(obj, value); } else if (!key2.Writable && value != null) { JsonContract jsonContract = base.Serializer.ContractResolver.ResolveContract(key2.PropertyType); if (jsonContract is JsonArrayContract) { JsonArrayContract jsonArrayContract = jsonContract as JsonArrayContract; object value2 = key2.ValueProvider.GetValue(obj); if (value2 != null) { 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); } } finally { IDisposable disposable; if ((disposable = (enumerator3 as IDisposable)) != null) { disposable.Dispose(); } } } } else if (jsonContract is JsonDictionaryContract) { JsonDictionaryContract jsonDictionaryContract = jsonContract as JsonDictionaryContract; object value3 = key2.ValueProvider.GetValue(obj); if (value3 != null) { IWrappedDictionary wrappedDictionary = jsonDictionaryContract.CreateWrapper(value3); IWrappedDictionary wrappedDictionary2 = jsonDictionaryContract.CreateWrapper(value); IDictionaryEnumerator enumerator4 = wrappedDictionary2.GetEnumerator(); try { while (enumerator4.MoveNext()) { DictionaryEntry dictionaryEntry = (DictionaryEntry)enumerator4.Current; wrappedDictionary.Add(dictionaryEntry.Key, dictionaryEntry.Value); } } finally { IDisposable disposable2; if ((disposable2 = (enumerator4 as IDisposable)) != null) { disposable2.Dispose(); } } } } } } contract.InvokeOnDeserialized(obj, base.Serializer.Context); return(obj); }