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; 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 void SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContract collectionValueContract) { contract.InvokeOnSerializing(values.UnderlyingCollection, Serializer.Context); SerializeStack.Add(values.UnderlyingCollection); bool isReference = contract.IsReference ?? HasFlag(Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Arrays); bool includeTypeDetails = ShouldWriteType(TypeNameHandling.Arrays, contract, member, collectionValueContract); if (isReference || includeTypeDetails) { writer.WriteStartObject(); if (isReference) { writer.WritePropertyName(JsonTypeReflector.IdPropertyName); writer.WriteValue(Serializer.ReferenceResolver.GetReference(this, values.UnderlyingCollection)); } if (includeTypeDetails) { WriteTypeProperty(writer, values.UnderlyingCollection.GetType()); } writer.WritePropertyName(JsonTypeReflector.ArrayValuesPropertyName); } JsonContract childValuesContract = Serializer.ContractResolver.ResolveContract(contract.CollectionItemType ?? typeof(object)); writer.WriteStartArray(); int initialDepth = writer.Top; int index = 0; // note that an error in the IEnumerable won't be caught foreach (object value in values) { try { JsonContract valueContract = GetContractSafe(value); if (ShouldWriteReference(value, null, valueContract)) { WriteReference(writer, value); } else { if (CheckForCircularReference(value, null, contract)) { SerializeValue(writer, value, valueContract, null, childValuesContract); } } } catch (Exception ex) { if (IsErrorHandled(values.UnderlyingCollection, contract, index, ex)) HandleError(writer, initialDepth); else throw; } finally { index++; } } writer.WriteEndArray(); if (isReference || includeTypeDetails) { writer.WriteEndObject(); } SerializeStack.RemoveAt(SerializeStack.Count - 1); contract.InvokeOnSerialized(values.UnderlyingCollection, Serializer.Context); }
private object CreateAndPopulateList(JsonReader reader, string reference, JsonArrayContract contract) { return CollectionUtils.CreateAndPopulateList(contract.CreatedType, (l, isTemporaryListReference) => { if (reference != null && isTemporaryListReference) throw new JsonSerializationException("Cannot preserve reference to array or readonly list: {0}".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)); #if !PocketPC if (contract.OnSerializing != null && isTemporaryListReference) throw new JsonSerializationException("Cannot call OnSerializing on an array or readonly list: {0}".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)); #endif if (contract.OnError != null && isTemporaryListReference) throw new JsonSerializationException("Cannot call OnError on an array or readonly list: {0}".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)); PopulateList(contract.CreateWrapper(l), reader, reference, contract); }); }
private void SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContract collectionValueContract) { contract.InvokeOnSerializing(values.UnderlyingCollection, Serializer.Context); SerializeStack.Add(values.UnderlyingCollection); bool isReference = contract.IsReference ?? HasFlag(Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Arrays); bool includeTypeDetails = ShouldWriteType(TypeNameHandling.Arrays, contract, member, collectionValueContract); if (isReference || includeTypeDetails) { writer.WriteStartObject(); if (isReference) { writer.WritePropertyName(JsonTypeReflector.IdPropertyName); writer.WriteValue(Serializer.ReferenceResolver.GetReference(this, values.UnderlyingCollection)); } if (includeTypeDetails) { WriteTypeProperty(writer, values.UnderlyingCollection.GetType()); } writer.WritePropertyName(JsonTypeReflector.ArrayValuesPropertyName); } JsonContract childValuesContract = Serializer.ContractResolver.ResolveContract(contract.CollectionItemType ?? typeof(object)); writer.WriteStartArray(); int initialDepth = writer.Top; int index = 0; // note that an error in the IEnumerable won't be caught foreach (object value in values) { try { JsonContract valueContract = GetContractSafe(value); if (ShouldWriteReference(value, null, valueContract)) { WriteReference(writer, value); } else { if (CheckForCircularReference(value, null, contract)) { SerializeValue(writer, value, valueContract, null, childValuesContract); } } } catch (Exception ex) { if (IsErrorHandled(values.UnderlyingCollection, contract, index, ex)) { HandleError(writer, initialDepth); } else { throw; } } finally { index++; } } writer.WriteEndArray(); if (isReference || includeTypeDetails) { writer.WriteEndObject(); } SerializeStack.RemoveAt(SerializeStack.Count - 1); contract.InvokeOnSerialized(values.UnderlyingCollection, Serializer.Context); }