本文整理汇总了C#中Newtonsoft.Json.Serialization.JsonArrayContract类的典型用法代码示例。如果您正苦于以下问题:C# JsonArrayContract类的具体用法?C# JsonArrayContract怎么用?C# JsonArrayContract使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsonArrayContract类属于Newtonsoft.Json.Serialization命名空间,在下文中一共展示了JsonArrayContract类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateArraySchema
private Schema CreateArraySchema(JsonArrayContract arrayContract)
{
var itemType = arrayContract.CollectionItemType ?? typeof(object);
return new Schema
{
Type = "array",
Items = CreateSchema(itemType, true)
};
}
示例2: SerializeList
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
{
// Note: So apparently the type of the items in the list are being retrieved already in the childValuesContract. Check to see
// if the type here is correct, and check to see if the contract retrieved here is the same as the childValuesContract.
JsonContract valueContract = childValuesContract;
if(value != null)
valueContract = Serializer.ContractResolver.ResolveContract(value.GetType());
//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);
}
示例3: CreateAndPopulateList
private object CreateAndPopulateList(JsonReader reader, string reference, JsonArrayContract contract)
{
return CollectionUtils.CreateAndPopulateList(contract.CreatedType, (l, isTemporaryListReference) =>
{
if (reference != null && isTemporaryListReference)
throw CreateSerializationException(reader, "Cannot preserve reference to array or readonly list: {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));
#if !PocketPC
if (contract.OnSerializing != null && isTemporaryListReference)
throw CreateSerializationException(reader, "Cannot call OnSerializing on an array or readonly list: {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));
#endif
if (contract.OnError != null && isTemporaryListReference)
throw CreateSerializationException(reader, "Cannot call OnError on an array or readonly list: {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));
PopulateList(contract.CreateWrapper(l), reader, reference, contract);
});
}
示例4: SerializeMultidimensionalArray
private void SerializeMultidimensionalArray(JsonWriter writer, Array values, JsonArrayContract contract, JsonProperty member, int initialDepth, int[] indices)
{
int dimension = indices.Length;
int[] newIndices = new int[dimension + 1];
for (int i = 0; i < dimension; i++)
{
newIndices[i] = indices[i];
}
writer.WriteStartArray();
for (int i = 0; i < values.GetLength(dimension); i++)
{
newIndices[dimension] = i;
bool isTopLevel = (newIndices.Length == values.Rank);
if (isTopLevel)
{
object value = values.GetValue(newIndices);
try
{
JsonContract valueContract = contract.FinalItemContract ?? GetContractSafe(value);
if (ShouldWriteReference(value, null, valueContract, contract, member))
{
WriteReference(writer, value);
}
else
{
if (CheckForCircularReference(writer, value, null, valueContract, contract, member))
{
SerializeValue(writer, value, valueContract, null, contract, member);
}
}
}
catch (Exception ex)
{
if (IsErrorHandled(values, contract, i, writer.ContainerPath, ex))
HandleError(writer, initialDepth + 1);
else
throw;
}
}
else
{
SerializeMultidimensionalArray(writer, values, contract, member, initialDepth + 1, newIndices);
}
}
writer.WriteEndArray();
}
示例5: SerializeList
private void SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
contract.InvokeOnSerializing(values.UnderlyingCollection, Serializer.Context);
_serializeStack.Add(values.UnderlyingCollection);
bool hasWrittenMetadataObject = WriteStartArray(writer, values.UnderlyingCollection, contract, member, collectionContract, containerProperty);
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 = contract.FinalItemContract ?? GetContractSafe(value);
if (ShouldWriteReference(value, null, valueContract, contract, member))
{
WriteReference(writer, value);
}
else
{
if (CheckForCircularReference(writer, value, null, valueContract, contract, member))
{
SerializeValue(writer, value, valueContract, null, contract, member);
}
}
}
catch (Exception ex)
{
if (IsErrorHandled(values.UnderlyingCollection, contract, index, writer.ContainerPath, ex))
HandleError(writer, initialDepth);
else
throw;
}
finally
{
index++;
}
}
writer.WriteEndArray();
if (hasWrittenMetadataObject)
writer.WriteEndObject();
_serializeStack.RemoveAt(_serializeStack.Count - 1);
contract.InvokeOnSerialized(values.UnderlyingCollection, Serializer.Context);
}
示例6: PopulateMultidimensionalArray
private object PopulateMultidimensionalArray(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, string id)
{
int rank = contract.UnderlyingType.GetArrayRank();
if (id != null)
AddReference(reader, id, list);
OnDeserializing(reader, contract, list);
JsonContract collectionItemContract = GetContractSafe(contract.CollectionItemType);
JsonConverter collectionItemConverter = GetConverter(collectionItemContract, null, contract, containerProperty);
int? previousErrorIndex = null;
Stack<IList> listStack = new Stack<IList>();
listStack.Push(list);
IList currentList = list;
bool finished = false;
do
{
int initialDepth = reader.Depth;
if (listStack.Count == rank)
{
try
{
if (ReadForType(reader, collectionItemContract, collectionItemConverter != null))
{
switch (reader.TokenType)
{
case JsonToken.EndArray:
listStack.Pop();
currentList = listStack.Peek();
previousErrorIndex = null;
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);
currentList.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;
}
}
}
else
{
if (reader.Read())
{
switch (reader.TokenType)
{
case JsonToken.StartArray:
IList newList = new List<object>();
currentList.Add(newList);
listStack.Push(newList);
currentList = newList;
break;
case JsonToken.EndArray:
listStack.Pop();
if (listStack.Count > 0)
{
currentList = listStack.Peek();
}
else
{
//.........这里部分代码省略.........
示例7: PopulateList
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.");
}
示例8: SerializeList
private void SerializeList(JsonWriter writer, IList values, JsonArrayContract contract, JsonProperty member, JsonContract collectionValueContract)
{
contract.InvokeOnSerializing(values, Serializer.Context);
SerializeStack.Add(values);
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(values));
}
if (includeTypeDetails)
{
WriteTypeProperty(writer, values.GetType());
}
writer.WritePropertyName(JsonTypeReflector.ArrayValuesPropertyName);
}
JsonContract childValuesContract = Serializer.ContractResolver.ResolveContract(contract.CollectionItemType ?? typeof(object));
writer.WriteStartArray();
int initialDepth = writer.Top;
for (int i = 0; i < values.Count; i++)
{
try
{
object value = values[i];
JsonContract valueContract = GetContractSafe(value);
if (ShouldWriteReference(value, null, valueContract))
{
WriteReference(writer, value);
}
else
{
if (!CheckForCircularReference(value, null, contract))
continue;
SerializeValue(writer, value, valueContract, null, childValuesContract);
}
}
catch (Exception ex)
{
if (IsErrorHandled(values, contract, i, ex))
HandleError(writer, initialDepth);
else
throw;
}
}
writer.WriteEndArray();
if (isReference || includeTypeDetails)
{
writer.WriteEndObject();
}
SerializeStack.RemoveAt(SerializeStack.Count - 1);
contract.InvokeOnSerialized(values, Serializer.Context);
}
示例9: SerializeList
private void SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) {
OnSerializing(writer, contract, values.UnderlyingCollection);
_serializeStack.Add(values.UnderlyingCollection);
bool hasWrittenMetadataObject = WriteStartArray(writer, values.UnderlyingCollection, contract, member, collectionContract, containerProperty);
writer.WriteStartArray();
int initialDepth = writer.Top;
int index = 0;
object valuePrevious = new object();
try {
// note that an error in the IEnumerable won't be caught
foreach (object value in values) {
try {
JsonContract valueContract = contract.FinalItemContract ?? GetContractSafe(value);
if (ShouldWriteReference(value, null, valueContract, contract, member)) {
WriteReference(writer, value);
} else {
if (CheckForCircularReference(writer, value, null, valueContract, contract, member)) {
SerializeValue(writer, value, valueContract, null, contract, member);
}
}
} catch (Exception ex) {
if (IsErrorHandled(values.UnderlyingCollection, contract, index, null, writer.ContainerPath, ex))
HandleError(writer, initialDepth);
else
throw;
} finally {
index++;
valuePrevious = value;
}
}
} catch (Exception e) {
string msg = "valuePrevious[" + valuePrevious.ToString() + "] trying to catch COLLECTION_CHANGED";
throw new Exception(msg, e);
}
writer.WriteEndArray();
if (hasWrittenMetadataObject)
writer.WriteEndObject();
_serializeStack.RemoveAt(_serializeStack.Count - 1);
OnSerialized(writer, contract, values.UnderlyingCollection);
}
示例10: PopulateList
private IList PopulateList(IList list, JsonReader reader, string reference, JsonArrayContract contract)
{
if (reference != null)
_serializer.ReferenceResolver.AddReference(reference, list);
contract.InvokeOnDeserializing(list);
while (reader.Read())
{
switch (reader.TokenType)
{
case JsonToken.EndArray:
contract.InvokeOnDeserialized(list);
return list;
case JsonToken.Comment:
break;
default:
object value = CreateValue(reader, contract.CollectionItemType, null, null);
list.Add(value);
break;
}
}
throw new JsonSerializationException("Unexpected end when deserializing array.");
}
示例11: CreateAndPopulateList
private object CreateAndPopulateList(JsonReader reader, string reference, JsonArrayContract contract)
{
return CollectionUtils.CreateAndPopulateList(contract.CollectionTypeToCreate, (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 && !SILVERLIGHT
if (contract.OnSerializing != null && isTemporaryListReference)
throw new JsonSerializationException("Cannot call OnSerializing on an array or readonly list: {0}".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));
#endif
PopulateList(l, reader, reference, contract);
});
}
示例12: WriteStartArray
private bool WriteStartArray(JsonWriter writer, object values, JsonArrayContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
{
bool? nullable = this.ResolveIsReference((JsonContract) contract, member, containerContract, containerProperty);
bool flag1 = nullable.HasValue ? nullable.GetValueOrDefault() : this.HasFlag(this.Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Arrays);
bool flag2 = this.ShouldWriteType(TypeNameHandling.Arrays, (JsonContract) contract, member, containerContract, containerProperty);
bool flag3 = flag1 || flag2;
if (flag3)
{
writer.WriteStartObject();
if (flag1)
{
writer.WritePropertyName("$id");
writer.WriteValue(this.GetReference(writer, values));
}
if (flag2)
this.WriteTypeProperty(writer, values.GetType());
writer.WritePropertyName("$values");
}
if (contract.ItemContract == null)
contract.ItemContract = this.Serializer.ContractResolver.ResolveContract(contract.CollectionItemType ?? typeof (object));
return flag3;
}
示例13: SerializeMultidimensionalArray
private void SerializeMultidimensionalArray(JsonWriter writer, Array values, JsonArrayContract contract, JsonProperty member, int initialDepth, int[] indices)
{
int length = indices.Length;
int[] indices1 = new int[length + 1];
for (int index = 0; index < length; ++index)
indices1[index] = indices[index];
writer.WriteStartArray();
for (int index = 0; index < values.GetLength(length); ++index)
{
indices1[length] = index;
if (indices1.Length == values.Rank)
{
object obj = values.GetValue(indices1);
try
{
JsonContract jsonContract = contract.FinalItemContract ?? this.GetContractSafe(obj);
if (this.ShouldWriteReference(obj, (JsonProperty) null, jsonContract, (JsonContainerContract) contract, member))
this.WriteReference(writer, obj);
else if (this.CheckForCircularReference(writer, obj, (JsonProperty) null, jsonContract, (JsonContainerContract) contract, member))
this.SerializeValue(writer, obj, jsonContract, (JsonProperty) null, (JsonContainerContract) contract, member);
}
catch (Exception ex)
{
if (this.IsErrorHandled((object) values, (JsonContract) contract, (object) index, writer.ContainerPath, ex))
this.HandleError(writer, initialDepth + 1);
else
throw;
}
}
else
this.SerializeMultidimensionalArray(writer, values, contract, member, initialDepth + 1, indices1);
}
writer.WriteEndArray();
}
示例14: SerializeList
private void SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
contract.InvokeOnSerializing(values.UnderlyingCollection, this.Serializer.Context);
this._serializeStack.Add(values.UnderlyingCollection);
bool flag = this.WriteStartArray(writer, values.UnderlyingCollection, contract, member, collectionContract, containerProperty);
writer.WriteStartArray();
int top = writer.Top;
int num = 0;
foreach (object obj in (IEnumerable) values)
{
try
{
JsonContract jsonContract = contract.FinalItemContract ?? this.GetContractSafe(obj);
if (this.ShouldWriteReference(obj, (JsonProperty) null, jsonContract, (JsonContainerContract) contract, member))
this.WriteReference(writer, obj);
else if (this.CheckForCircularReference(writer, obj, (JsonProperty) null, jsonContract, (JsonContainerContract) contract, member))
this.SerializeValue(writer, obj, jsonContract, (JsonProperty) null, (JsonContainerContract) contract, member);
}
catch (Exception ex)
{
if (this.IsErrorHandled(values.UnderlyingCollection, (JsonContract) contract, (object) num, writer.ContainerPath, ex))
this.HandleError(writer, top);
else
throw;
}
finally
{
++num;
}
}
writer.WriteEndArray();
if (flag)
writer.WriteEndObject();
this._serializeStack.RemoveAt(this._serializeStack.Count - 1);
contract.InvokeOnSerialized(values.UnderlyingCollection, this.Serializer.Context);
}
示例15: SerializeEnumerable
private void SerializeEnumerable(JsonWriter writer, IEnumerable values, JsonArrayContract contract)
{
SerializeList(writer, values.Cast<object>().ToList(), contract);
}