private object PopulateList(IWrappedCollection wrappedList, JsonReader reader, string reference, JsonArrayContract contract) { object list = wrappedList.UnderlyingCollection; if (reference != null) { Serializer.ReferenceResolver.AddReference(reference, list); } contract.InvokeOnDeserializing(list, Serializer.Context); int initialDepth = reader.Depth; while (ReadForTypeArrayHack(reader, contract.CollectionItemType)) { switch (reader.TokenType) { case JsonToken.EndArray: contract.InvokeOnDeserialized(list, Serializer.Context); return(wrappedList.UnderlyingCollection); case JsonToken.Comment: break; default: try { object value = CreateValueNonProperty(reader, contract.CollectionItemType, GetContractSafe(contract.CollectionItemType)); wrappedList.Add(value); } catch (Exception ex) { if (IsErrorHandled(list, contract, wrappedList.Count, ex)) { HandleError(reader, initialDepth); } else { throw; } } break; } } throw new JsonSerializationException("Unexpected end when deserializing array."); }
private object PopulateList(IWrappedCollection wrappedList, JsonReader reader, string reference, JsonArrayContract contract) { object underlyingCollection = wrappedList.UnderlyingCollection; if (wrappedList.IsFixedSize) { reader.Skip(); return(wrappedList.UnderlyingCollection); } if (reference != null) { base.Serializer.ReferenceResolver.AddReference(this, reference, underlyingCollection); } contract.InvokeOnDeserializing(underlyingCollection, base.Serializer.Context); int depth = reader.Depth; while (ReadForTypeArrayHack(reader, contract.CollectionItemType)) { switch (reader.TokenType) { case JsonToken.EndArray: contract.InvokeOnDeserialized(underlyingCollection, base.Serializer.Context); return(wrappedList.UnderlyingCollection); case JsonToken.Comment: continue; } try { object value = CreateValueNonProperty(reader, contract.CollectionItemType, GetContractSafe(contract.CollectionItemType)); wrappedList.Add(value); } catch (Exception ex) { if (IsErrorHandled(underlyingCollection, contract, wrappedList.Count, ex)) { HandleError(reader, depth); continue; } throw; } } throw new JsonSerializationException("Unexpected end when deserializing array."); }
private object PopulateList(IWrappedCollection wrappedList, JsonReader reader, string reference, JsonArrayContract contract) { object list = wrappedList.UnderlyingCollection; // can't populate an existing array if (wrappedList.IsFixedSize) { reader.Skip(); return wrappedList.UnderlyingCollection; } if (reference != null) Serializer.ReferenceResolver.AddReference(this, reference, list); contract.InvokeOnDeserializing(list, Serializer.Context); int initialDepth = reader.Depth; int index = 0; JsonContract collectionItemContract = GetContractSafe(contract.CollectionItemType); JsonConverter collectionItemConverter = GetConverter(collectionItemContract, null); while (true) { try { if (ReadForType(reader, collectionItemContract, collectionItemConverter != null, true)) { switch (reader.TokenType) { case JsonToken.EndArray: contract.InvokeOnDeserialized(list, Serializer.Context); return wrappedList.UnderlyingCollection; case JsonToken.Comment: break; default: object value = CreateValueNonProperty(reader, contract.CollectionItemType, collectionItemContract, collectionItemConverter); wrappedList.Add(value); break; } } else { break; } } catch (Exception ex) { if (IsErrorHandled(list, contract, index, reader.Path, ex)) HandleError(reader, initialDepth); else throw; } finally { index++; } } throw CreateSerializationException(reader, "Unexpected end when deserializing array."); }
private object PopulateList(IWrappedCollection wrappedList, JsonReader reader, string reference, JsonArrayContract contract) { object list = wrappedList.UnderlyingCollection; if (reference != null) Serializer.ReferenceResolver.AddReference(reference, list); contract.InvokeOnDeserializing(list, Serializer.Context); int initialDepth = reader.Depth; while (reader.Read()) { switch (reader.TokenType) { case JsonToken.EndArray: contract.InvokeOnDeserialized(list, Serializer.Context); return wrappedList.UnderlyingCollection; case JsonToken.Comment: break; default: try { object value = CreateValueNonProperty(reader, contract.CollectionItemType, GetContractSafe(contract.CollectionItemType)); wrappedList.Add(value); } catch (Exception ex) { if (IsErrorHandled(list, contract, wrappedList.Count, ex)) HandleError(reader, initialDepth); else throw; } break; } } throw new JsonSerializationException("Unexpected end when deserializing array."); }
private object PopulateList(IWrappedCollection wrappedList, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, string id) { object list = wrappedList.UnderlyingCollection; if (id != null) AddReference(reader, id, list); // can't populate an existing array if (wrappedList.IsFixedSize) { reader.Skip(); return list; } OnDeserializing(reader, contract, list); int initialDepth = reader.Depth; JsonContract collectionItemContract = GetContractSafe(contract.CollectionItemType); JsonConverter collectionItemConverter = GetConverter(collectionItemContract, null, contract, containerProperty); int? previousErrorIndex = null; bool finished = false; do { try { if (ReadForType(reader, collectionItemContract, collectionItemConverter != null)) { switch (reader.TokenType) { case JsonToken.EndArray: finished = true; break; case JsonToken.Comment: break; default: object value; if (collectionItemConverter != null && collectionItemConverter.CanRead) value = DeserializeConvertable(collectionItemConverter, reader, contract.CollectionItemType, null); else value = CreateValueInternal(reader, contract.CollectionItemType, collectionItemContract, null, contract, containerProperty, null); wrappedList.Add(value); break; } } else { break; } } catch (Exception ex) { JsonPosition errorPosition = reader.GetPosition(initialDepth); if (IsErrorHandled(list, contract, errorPosition.Position, reader as IJsonLineInfo, reader.Path, ex)) { HandleError(reader, true, initialDepth); if (previousErrorIndex != null && previousErrorIndex == errorPosition.Position) { // reader index has not moved since previous error handling // break out of reading array to prevent infinite loop throw JsonSerializationException.Create(reader, "Infinite loop detected from error handling.", ex); } else { previousErrorIndex = errorPosition.Position; } } else { throw; } } } while (!finished); if (!finished) ThrowUnexpectedEndException(reader, contract, list, "Unexpected end when deserializing array."); OnDeserialized(reader, contract, list); return list; }
private object PopulateList(IWrappedCollection wrappedList, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, string id) { object underlyingCollection = wrappedList.UnderlyingCollection; if (id != null) this.AddReference(reader, id, underlyingCollection); if (wrappedList.IsFixedSize) { reader.Skip(); return underlyingCollection; } else { contract.InvokeOnDeserializing(underlyingCollection, this.Serializer.Context); int depth = reader.Depth; JsonContract contractSafe = this.GetContractSafe(contract.CollectionItemType); JsonConverter converter = this.GetConverter(contractSafe, (JsonConverter) null, (JsonContainerContract) contract, containerProperty); int? nullable1 = new int?(); bool flag = false; do { try { if (this.ReadForType(reader, contractSafe, converter != null)) { switch (reader.TokenType) { case JsonToken.Comment: break; case JsonToken.EndArray: flag = true; break; default: object obj = converter == null || !converter.CanRead ? this.CreateValueInternal(reader, contract.CollectionItemType, contractSafe, (JsonProperty) null, (JsonContainerContract) contract, containerProperty, (object) null) : converter.ReadJson(reader, contract.CollectionItemType, (object) null, (JsonSerializer) this.GetInternalSerializer()); wrappedList.Add(obj); break; } } else break; } catch (Exception ex) { JsonPosition position = reader.GetPosition(depth); if (this.IsErrorHandled(underlyingCollection, (JsonContract) contract, (object) position.Position, reader.Path, ex)) { this.HandleError(reader, true, depth); if (nullable1.HasValue) { int? nullable2 = nullable1; int? nullable3 = position.Position; if ((nullable2.GetValueOrDefault() != nullable3.GetValueOrDefault() ? 0 : (nullable2.HasValue == nullable3.HasValue ? 1 : 0)) != 0) throw JsonSerializationException.Create(reader, "Infinite loop detected from error handling.", ex); } nullable1 = position.Position; } else throw; } } while (!flag); if (!flag) this.ThrowUnexpectedEndException(reader, (JsonContract) contract, underlyingCollection, "Unexpected end when deserializing array."); contract.InvokeOnDeserialized(underlyingCollection, this.Serializer.Context); return underlyingCollection; } }
private object PopulateList(IWrappedCollection wrappedList, JsonReader reader, string reference, JsonArrayContract contract, JsonProperty containerProperty) { object list = wrappedList.UnderlyingCollection; // can't populate an existing array if (wrappedList.IsFixedSize) { reader.Skip(); return wrappedList.UnderlyingCollection; } if (reference != null) Serializer.ReferenceResolver.AddReference(this, reference, list); contract.InvokeOnDeserializing(list, Serializer.Context); int initialDepth = reader.Depth; JsonContract collectionItemContract = GetContractSafe(contract.CollectionItemType); JsonConverter collectionItemConverter = GetConverter(collectionItemContract, null, contract, containerProperty); int? previousErrorIndex = null; while (true) { try { if (ReadForType(reader, collectionItemContract, collectionItemConverter != null, true)) { switch (reader.TokenType) { case JsonToken.EndArray: contract.InvokeOnDeserialized(list, Serializer.Context); return wrappedList.UnderlyingCollection; case JsonToken.Comment: break; default: object value; if (collectionItemConverter != null && collectionItemConverter.CanRead) value = collectionItemConverter.ReadJson(reader, contract.CollectionItemType, null, GetInternalSerializer()); else value = CreateValueInternal(reader, contract.CollectionItemType, collectionItemContract, null, contract, containerProperty, null); wrappedList.Add(value); break; } } else { break; } } catch (Exception ex) { JsonPosition errorPosition = reader.GetPosition(initialDepth); if (IsErrorHandled(list, contract, errorPosition.Position, reader.Path, ex)) { HandleError(reader, true, initialDepth); if (previousErrorIndex != null && previousErrorIndex == errorPosition.Position) { // reader index has not moved since previous error handling // break out of reading array to prevent infinite loop throw JsonSerializationException.Create(reader, "Infinite loop detected from error handling.", ex); } else { previousErrorIndex = errorPosition.Position; } } else { throw; } } } throw JsonSerializationException.Create(reader, "Unexpected end when deserializing array."); }
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 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); }