当前位置: 首页>>代码示例>>C#>>正文


C# Serialization.JsonISerializableContract类代码示例

本文整理汇总了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);
    }
开发者ID:draptik,项目名称:RepoSync,代码行数:27,代码来源:JsonSerializerInternalWriter.cs

示例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;
    }
开发者ID:bladefist,项目名称:Newtonsoft.Json,代码行数:42,代码来源:JsonSerializerInternalReader.cs

示例3: GenerateISerializableContract

 private void GenerateISerializableContract(Type type, JsonISerializableContract contract)
 {
   CurrentSchema.AllowAdditionalProperties = true;
 }
开发者ID:AshD,项目名称:Newtonsoft.Json,代码行数:4,代码来源:JsonSchemaGenerator.cs

示例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;
        }
开发者ID:rv192,项目名称:Fussen,代码行数:64,代码来源:JsonSerializerInternalReader.cs

示例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;
    }
开发者ID:nathanpalmer,项目名称:ravendb,代码行数:39,代码来源:JsonSerializerInternalReader.cs

示例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;
        }
开发者ID:NTUST-PTL,项目名称:PTL-Project,代码行数:16,代码来源:JsonSerializerInternalReader.cs

示例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;
    }
开发者ID:Contatta,项目名称:Newtonsoft.Json,代码行数:49,代码来源:JsonSerializerInternalReader.cs

示例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);
		}
开发者ID:JungWon2,项目名称:memory_book,代码行数:21,代码来源:JsonSerializerInternalWriter.cs

示例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;
 }
开发者ID:Zeludon,项目名称:FEZ,代码行数:42,代码来源:JsonSerializerInternalReader.cs

示例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;
 }
开发者ID:newchild,项目名称:Project-DayZero,代码行数:74,代码来源:JsonSerializerInternalReader.cs

示例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;
 }
开发者ID:newchild,项目名称:Project-DayZero,代码行数:19,代码来源:JsonSerializerInternalReader.cs

示例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;
 }
开发者ID:Zeludon,项目名称:FEZ,代码行数:16,代码来源:DefaultContractResolver.cs

示例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);
 }
开发者ID:newchild,项目名称:Project-DayZero,代码行数:35,代码来源:JsonSerializerInternalWriter.cs


注:本文中的Newtonsoft.Json.Serialization.JsonISerializableContract类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。