本文整理汇总了C#中Newtonsoft.Json.Serialization.JsonISerializableContract类的典型用法代码示例。如果您正苦于以下问题:C# JsonISerializableContract类的具体用法?C# JsonISerializableContract怎么用?C# JsonISerializableContract使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsonISerializableContract类属于Newtonsoft.Json.Serialization命名空间,在下文中一共展示了JsonISerializableContract类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SerializeISerializable
private void SerializeISerializable(JsonWriter writer, ISerializable value, JsonISerializableContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
if (!JsonTypeReflector.FullyTrusted)
{
throw JsonSerializationException.Create(null, writer.ContainerPath, @"Type '{0}' implements ISerializable but cannot be serialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data.
To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true.".FormatWith(CultureInfo.InvariantCulture, value.GetType()), null);
}
contract.InvokeOnSerializing(value, Serializer.Context);
_serializeStack.Add(value);
WriteObjectStart(writer, value, contract, member, collectionContract, containerProperty);
SerializationInfo serializationInfo = new SerializationInfo(contract.UnderlyingType, new FormatterConverter());
value.GetObjectData(serializationInfo, Serializer.Context);
foreach (SerializationEntry serializationEntry in serializationInfo)
{
writer.WritePropertyName(serializationEntry.Name);
SerializeValue(writer, serializationEntry.Value, GetContractSafe(serializationEntry.Value), null, null, member);
}
writer.WriteEndObject();
_serializeStack.RemoveAt(_serializeStack.Count - 1);
contract.InvokeOnSerialized(value, Serializer.Context);
}
示例2: CreateISerializable
private object CreateISerializable(JsonReader reader, JsonISerializableContract contract, string id)
{
Type objectType = contract.UnderlyingType;
SerializationInfo serializationInfo = new SerializationInfo(contract.UnderlyingType, GetFormatterConverter());
bool exit = false;
do
{
switch (reader.TokenType)
{
case JsonToken.PropertyName:
string memberName = reader.Value.ToString();
if (!reader.Read())
throw CreateSerializationException(reader, "Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, memberName));
serializationInfo.AddValue(memberName, JToken.ReadFrom(reader));
break;
case JsonToken.Comment:
break;
case JsonToken.EndObject:
exit = true;
break;
default:
throw CreateSerializationException(reader, "Unexpected token when deserializing object: " + reader.TokenType);
}
} while (!exit && reader.Read());
if (contract.ISerializableCreator == null)
throw CreateSerializationException(reader, "ISerializable type '{0}' does not have a valid constructor. To correctly implement ISerializable a constructor that takes SerializationInfo and StreamingContext parameters should be present.".FormatWith(CultureInfo.InvariantCulture, objectType));
object createdObject = contract.ISerializableCreator(serializationInfo, Serializer.Context);
if (id != null)
Serializer.ReferenceResolver.AddReference(this, id, createdObject);
// these are together because OnDeserializing takes an object but for an ISerializable the object is full created in the constructor
contract.InvokeOnDeserializing(createdObject, Serializer.Context);
contract.InvokeOnDeserialized(createdObject, Serializer.Context);
return createdObject;
}
示例3: GenerateISerializableContract
private void GenerateISerializableContract(Type type, JsonISerializableContract contract)
{
CurrentSchema.AllowAdditionalProperties = true;
}
示例4: CreateISerializable
private object CreateISerializable(JsonReader reader, JsonISerializableContract contract, JsonProperty member, string id)
{
Type objectType = contract.UnderlyingType;
if (!JsonTypeReflector.FullyTrusted)
{
throw JsonSerializationException.Create(reader, @"Type '{0}' implements ISerializable but cannot be deserialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data.
To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true.
".FormatWith(CultureInfo.InvariantCulture, objectType));
}
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Deserializing {0} using ISerializable constructor.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)), null);
SerializationInfo serializationInfo = new SerializationInfo(contract.UnderlyingType, GetFormatterConverter());
bool finished = false;
do
{
switch (reader.TokenType)
{
case JsonToken.PropertyName:
string memberName = reader.Value.ToString();
if (!reader.Read())
throw JsonSerializationException.Create(reader, "Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, memberName));
if (reader.TokenType == JsonToken.StartObject)
{
// this will read any potential type names embedded in json
object o = CreateObject(reader, null, null, null, contract, member, null);
serializationInfo.AddValue(memberName, o);
}
else
{
serializationInfo.AddValue(memberName, JToken.ReadFrom(reader));
}
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, serializationInfo, "Unexpected end when deserializing object.");
if (contract.ISerializableCreator == null)
throw JsonSerializationException.Create(reader, "ISerializable type '{0}' does not have a valid constructor. To correctly implement ISerializable a constructor that takes SerializationInfo and StreamingContext parameters should be present.".FormatWith(CultureInfo.InvariantCulture, objectType));
object createdObject = contract.ISerializableCreator(serializationInfo, Serializer._context);
if (id != null)
AddReference(reader, id, createdObject);
// these are together because OnDeserializing takes an object but for an ISerializable the object is fully created in the constructor
OnDeserializing(reader, contract, createdObject);
OnDeserialized(reader, contract, createdObject);
return createdObject;
}
示例5: CreateISerializable
private object CreateISerializable(JsonReader reader, JsonISerializableContract contract, string id)
{
Type objectType = contract.UnderlyingType;
SerializationInfo serializationInfo = new SerializationInfo(contract.UnderlyingType, GetFormatterConverter());
bool exit = false;
do
{
switch (reader.TokenType)
{
case JsonToken.PropertyName:
string memberName = reader.Value.ToString();
if (!reader.Read())
throw new JsonSerializationException("Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, memberName));
serializationInfo.AddValue(memberName, JToken.ReadFrom(reader));
break;
case JsonToken.EndObject:
exit = true;
break;
default:
throw new JsonSerializationException("Unexpected token when deserializing object: " + reader.TokenType);
}
} while (!exit && reader.Read());
if (contract.ISerializableCreator == null)
throw new JsonSerializationException("ISerializable type '{0}' does not have a valid constructor.".FormatWith(CultureInfo.InvariantCulture, objectType));
object createdObject = contract.ISerializableCreator(serializationInfo, Serializer.Context);
if (id != null)
Serializer.ReferenceResolver.AddReference(id, createdObject);
contract.InvokeOnDeserializing(createdObject, Serializer.Context);
contract.InvokeOnDeserialized(createdObject, Serializer.Context);
return createdObject;
}
示例6: CreateISerializableItem
internal object CreateISerializableItem(JToken token, Type type, JsonISerializableContract contract, JsonProperty member)
{
JsonContract itemContract = GetContractSafe(type);
JsonConverter itemConverter = GetConverter(itemContract, null, contract, member);
JsonReader tokenReader = token.CreateReader();
CheckedRead(tokenReader); // Move to first token
object result;
if (itemConverter != null && itemConverter.CanRead)
result = DeserializeConvertable(itemConverter, tokenReader, type, null);
else
result = CreateValueInternal(tokenReader, type, itemContract, null, contract, member, null);
return result;
}
示例7: CreateISerializable
private object CreateISerializable(JsonReader reader, JsonISerializableContract contract, string id)
{
Type objectType = contract.UnderlyingType;
if (!JsonTypeReflector.FullyTrusted)
{
throw JsonSerializationException.Create(reader, @"Type '{0}' implements ISerializable but cannot be deserialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data.
To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true.
".FormatWith(CultureInfo.InvariantCulture, objectType));
}
SerializationInfo serializationInfo = new SerializationInfo(contract.UnderlyingType, GetFormatterConverter());
bool exit = false;
do
{
switch (reader.TokenType)
{
case JsonToken.PropertyName:
string memberName = reader.Value.ToString();
if (!reader.Read())
throw JsonSerializationException.Create(reader, "Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, memberName));
serializationInfo.AddValue(memberName, JToken.ReadFrom(reader));
break;
case JsonToken.Comment:
break;
case JsonToken.EndObject:
exit = true;
break;
default:
throw JsonSerializationException.Create(reader, "Unexpected token when deserializing object: " + reader.TokenType);
}
} while (!exit && reader.Read());
if (contract.ISerializableCreator == null)
throw JsonSerializationException.Create(reader, "ISerializable type '{0}' does not have a valid constructor. To correctly implement ISerializable a constructor that takes SerializationInfo and StreamingContext parameters should be present.".FormatWith(CultureInfo.InvariantCulture, objectType));
object createdObject = contract.ISerializableCreator(serializationInfo, Serializer.Context);
if (id != null)
Serializer.ReferenceResolver.AddReference(this, id, createdObject);
// these are together because OnDeserializing takes an object but for an ISerializable the object is fully created in the constructor
contract.InvokeOnDeserializing(createdObject, Serializer.Context);
contract.InvokeOnDeserialized(createdObject, Serializer.Context);
return createdObject;
}
示例8: SerializeISerializable
private void SerializeISerializable(JsonWriter writer, ISerializable value, JsonISerializableContract contract)
{
contract.InvokeOnSerializing(value, Serializer.Context);
SerializeStack.Add(value);
writer.WriteStartObject();
SerializationInfo serializationInfo = new SerializationInfo(contract.UnderlyingType, new FormatterConverter());
value.GetObjectData(serializationInfo, Serializer.Context);
foreach (SerializationEntry serializationEntry in serializationInfo)
{
writer.WritePropertyName(serializationEntry.Name);
SerializeValue(writer, serializationEntry.Value, GetContractSafe(serializationEntry.Value), null, null);
}
writer.WriteEndObject();
SerializeStack.RemoveAt(SerializeStack.Count - 1);
contract.InvokeOnSerialized(value, Serializer.Context);
}
示例9: CreateISerializable
private object CreateISerializable(JsonReader reader, JsonISerializableContract contract, string id)
{
Type underlyingType = contract.UnderlyingType;
if (!JsonTypeReflector.FullyTrusted)
throw JsonSerializationException.Create(reader, StringUtils.FormatWith("Type '{0}' implements ISerializable but cannot be deserialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data.\r\nTo fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true.\r\n", (IFormatProvider) CultureInfo.InvariantCulture, (object) underlyingType));
SerializationInfo serializationInfo = new SerializationInfo(contract.UnderlyingType, (IFormatterConverter) this.GetFormatterConverter());
bool flag = false;
do
{
switch (reader.TokenType)
{
case JsonToken.PropertyName:
string name = reader.Value.ToString();
if (!reader.Read())
throw JsonSerializationException.Create(reader, StringUtils.FormatWith("Unexpected end when setting {0}'s value.", (IFormatProvider) CultureInfo.InvariantCulture, (object) name));
serializationInfo.AddValue(name, (object) JToken.ReadFrom(reader));
goto case JsonToken.Comment;
case JsonToken.Comment:
continue;
case JsonToken.EndObject:
flag = true;
goto case JsonToken.Comment;
default:
throw JsonSerializationException.Create(reader, "Unexpected token when deserializing object: " + (object) reader.TokenType);
}
}
while (!flag && reader.Read());
if (!flag)
this.ThrowUnexpectedEndException(reader, (JsonContract) contract, (object) serializationInfo, "Unexpected end when deserializing object.");
if (contract.ISerializableCreator == null)
throw JsonSerializationException.Create(reader, StringUtils.FormatWith("ISerializable type '{0}' does not have a valid constructor. To correctly implement ISerializable a constructor that takes SerializationInfo and StreamingContext parameters should be present.", (IFormatProvider) CultureInfo.InvariantCulture, (object) underlyingType));
object o = contract.ISerializableCreator(new object[2]
{
(object) serializationInfo,
(object) this.Serializer.Context
});
if (id != null)
this.AddReference(reader, id, o);
contract.InvokeOnDeserializing(o, this.Serializer.Context);
contract.InvokeOnDeserialized(o, this.Serializer.Context);
return o;
}
示例10: CreateISerializable
// Token: 0x06000BD5 RID: 3029
// RVA: 0x00045544 File Offset: 0x00043744
private object CreateISerializable(JsonReader reader, JsonISerializableContract contract, JsonProperty member, string id)
{
Type underlyingType = contract.UnderlyingType;
if (!JsonTypeReflector.FullyTrusted)
{
string text = "Type '{0}' implements ISerializable but cannot be deserialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data." + Environment.NewLine + "To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true." + Environment.NewLine;
text = StringUtils.FormatWith(text, CultureInfo.InvariantCulture, underlyingType);
throw JsonSerializationException.Create(reader, text);
}
if (this.TraceWriter != null && this.TraceWriter.LevelFilter >= TraceLevel.Info)
{
this.TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, StringUtils.FormatWith("Deserializing {0} using ISerializable constructor.", CultureInfo.InvariantCulture, contract.UnderlyingType)), null);
}
SerializationInfo serializationInfo = new SerializationInfo(contract.UnderlyingType, new JsonFormatterConverter(this, contract, member));
bool flag = false;
string text2;
do
{
JsonToken tokenType = reader.TokenType;
switch (tokenType)
{
case JsonToken.PropertyName:
text2 = reader.Value.ToString();
if (!reader.Read())
{
goto IL_119;
}
serializationInfo.AddValue(text2, JToken.ReadFrom(reader));
break;
case JsonToken.Comment:
break;
default:
if (tokenType != JsonToken.EndObject)
{
goto Block_7;
}
flag = true;
break;
}
if (flag)
{
break;
}
}
while (reader.Read());
goto IL_131;
Block_7:
throw JsonSerializationException.Create(reader, "Unexpected token when deserializing object: " + reader.TokenType);
IL_119:
throw JsonSerializationException.Create(reader, StringUtils.FormatWith("Unexpected end when setting {0}'s value.", CultureInfo.InvariantCulture, text2));
IL_131:
if (!flag)
{
this.ThrowUnexpectedEndException(reader, contract, serializationInfo, "Unexpected end when deserializing object.");
}
if (contract.ISerializableCreator == null)
{
throw JsonSerializationException.Create(reader, StringUtils.FormatWith("ISerializable type '{0}' does not have a valid constructor. To correctly implement ISerializable a constructor that takes SerializationInfo and StreamingContext parameters should be present.", CultureInfo.InvariantCulture, underlyingType));
}
object obj = contract.ISerializableCreator(new object[]
{
serializationInfo,
this.Serializer._context
});
if (id != null)
{
this.AddReference(reader, id, obj);
}
this.OnDeserializing(reader, contract, obj);
this.OnDeserialized(reader, contract, obj);
return obj;
}
示例11: CreateISerializableItem
// Token: 0x06000BD6 RID: 3030
// RVA: 0x00045708 File Offset: 0x00043908
internal object CreateISerializableItem(JToken token, Type type, JsonISerializableContract contract, JsonProperty member)
{
JsonContract contractSafe = this.GetContractSafe(type);
JsonConverter converter = this.GetConverter(contractSafe, null, contract, member);
JsonReader reader = token.CreateReader();
this.CheckedRead(reader);
object result;
if (converter != null && converter.CanRead)
{
result = this.DeserializeConvertable(converter, reader, type, null);
}
else
{
result = this.CreateValueInternal(reader, type, contractSafe, null, contract, member, null);
}
return result;
}
示例12: CreateISerializableContract
protected virtual JsonISerializableContract CreateISerializableContract(Type objectType)
{
JsonISerializableContract iserializableContract = new JsonISerializableContract(objectType);
this.InitializeContract((JsonContract) iserializableContract);
ConstructorInfo constructor = iserializableContract.NonNullableUnderlyingType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, (Binder) null, new Type[2]
{
typeof (SerializationInfo),
typeof (StreamingContext)
}, (ParameterModifier[]) null);
if (constructor != null)
{
MethodCall<object, object> methodCall = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall<object>((MethodBase) constructor);
iserializableContract.ISerializableCreator = (ObjectConstructor<object>) (args => methodCall((object) null, args));
}
return iserializableContract;
}
示例13: SerializeISerializable
// Token: 0x06000C07 RID: 3079
// RVA: 0x00047744 File Offset: 0x00045944
private void SerializeISerializable(JsonWriter writer, ISerializable value, JsonISerializableContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{
if (!JsonTypeReflector.FullyTrusted)
{
string text = "Type '{0}' implements ISerializable but cannot be serialized using the ISerializable interface because the current application is not fully trusted and ISerializable can expose secure data." + Environment.NewLine + "To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true." + Environment.NewLine;
text = StringUtils.FormatWith(text, CultureInfo.InvariantCulture, value.GetType());
throw JsonSerializationException.Create(null, writer.ContainerPath, text, null);
}
this.OnSerializing(writer, contract, value);
this._serializeStack.Add(value);
this.WriteObjectStart(writer, value, contract, member, collectionContract, containerProperty);
SerializationInfo serializationInfo = new SerializationInfo(contract.UnderlyingType, new FormatterConverter());
value.GetObjectData(serializationInfo, this.Serializer._context);
SerializationInfoEnumerator enumerator = serializationInfo.GetEnumerator();
while (enumerator.MoveNext())
{
SerializationEntry current = enumerator.Current;
JsonContract contractSafe = this.GetContractSafe(current.Value);
if (this.ShouldWriteReference(current.Value, null, contractSafe, contract, member))
{
writer.WritePropertyName(current.Name);
this.WriteReference(writer, current.Value);
}
else if (this.CheckForCircularReference(writer, current.Value, null, contractSafe, contract, member))
{
writer.WritePropertyName(current.Name);
this.SerializeValue(writer, current.Value, contractSafe, null, contract, member);
}
}
writer.WriteEndObject();
this._serializeStack.RemoveAt(this._serializeStack.Count - 1);
this.OnSerialized(writer, contract, value);
}