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


C# Serialization.JsonProperty类代码示例

本文整理汇总了C#中Newtonsoft.Json.Serialization.JsonProperty的典型用法代码示例。如果您正苦于以下问题:C# JsonProperty类的具体用法?C# JsonProperty怎么用?C# JsonProperty使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


JsonProperty类属于Newtonsoft.Json.Serialization命名空间,在下文中一共展示了JsonProperty类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: WithValidationProperties

        public static Schema WithValidationProperties(this Schema schema, JsonProperty jsonProperty)
        {
            var propInfo = jsonProperty.PropertyInfo();
            if (propInfo == null)
                return schema;

            foreach (var attribute in propInfo.GetCustomAttributes(false))
            {
                var regex = attribute as RegularExpressionAttribute;
                if (regex != null)
                    schema.pattern = regex.Pattern;

                var range = attribute as RangeAttribute;
                if (range != null)
                {
                    int maximum;
                    if (Int32.TryParse(range.Maximum.ToString(), out maximum))
                        schema.maximum = maximum;

                    int minimum;
                    if (Int32.TryParse(range.Minimum.ToString(), out minimum))
                        schema.minimum = minimum;
                }
            }

            if (!jsonProperty.Writable)
                schema.readOnly = true;

            return schema;
        }
开发者ID:shaunparsloe,项目名称:Swashbuckle,代码行数:30,代码来源:SchemaExtensions.cs

示例2: WriteJson

    /// <summary>
    /// Writes the JSON representation of the object.
    /// </summary>
    /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
    /// <param name="value">The value.</param>
    /// <param name="serializer">The calling serializer.</param>
    public override void WriteJson(JsonWriter writer, JsonProperty property, object value, JsonSerializer serializer)
    {
      IEntityKeyMember entityKeyMember = DynamicWrapper.CreateWrapper<IEntityKeyMember>(value);
      Type keyType = (entityKeyMember.Value != null) ? entityKeyMember.Value.GetType() : null;

      writer.WriteStartObject();
      writer.WritePropertyName("Key");
      writer.WriteValue(entityKeyMember.Key);
      writer.WritePropertyName("Type");
      writer.WriteValue((keyType != null) ? keyType.FullName : null);

      writer.WritePropertyName("Value");

      if (keyType != null)
      {
        string valueJson;
        if (JsonSerializerInternalWriter.TryConvertToString(entityKeyMember.Value, keyType, out valueJson))
          writer.WriteValue(valueJson);
        else
          writer.WriteValue(entityKeyMember.Value);
      }
      else
      {
        writer.WriteNull();
      }

      writer.WriteEndObject();
    }
开发者ID:Rickasaurus,项目名称:Newtonsoft.Json-Fork,代码行数:34,代码来源:EntityKeyMemberConverter.cs

示例3: ReadJson

      public override object ReadJson(JsonReader reader, Type objectType, JsonProperty member, object existingValue, JsonSerializer serializer)
    {
      NameContainer nameContainer = new NameContainer();
      nameContainer.Value = (string)reader.Value;

      return nameContainer;
    }
开发者ID:Rickasaurus,项目名称:Newtonsoft.Json-Fork,代码行数:7,代码来源:PublicParametizedConstructorRequiringConverterTestClass.cs

示例4: WriteJson

    /// <summary>
    /// Writes the JSON representation of the object.
    /// </summary>
    /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
    /// <param name="value">The value.</param>
    /// <param name="serializer">The calling serializer.</param>
    public override void WriteJson(JsonWriter writer, JsonProperty property, object value, JsonSerializer serializer)
    {
      string text;

      if (value is DateTime)
      {
        DateTime dateTime = (DateTime)value;

        if ((_dateTimeStyles & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal
          || (_dateTimeStyles & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal)
          dateTime = dateTime.ToUniversalTime();

        text = dateTime.ToString(_dateTimeFormat ?? DefaultDateTimeFormat, Culture);
      }
#if !PocketPC && !NET20
      else if (value is DateTimeOffset)
      {
        DateTimeOffset dateTimeOffset = (DateTimeOffset)value;
        if ((_dateTimeStyles & DateTimeStyles.AdjustToUniversal) == DateTimeStyles.AdjustToUniversal
          || (_dateTimeStyles & DateTimeStyles.AssumeUniversal) == DateTimeStyles.AssumeUniversal)
          dateTimeOffset = dateTimeOffset.ToUniversalTime();

        text = dateTimeOffset.ToString(_dateTimeFormat ?? DefaultDateTimeFormat, Culture);
      }
#endif
      else
      {
        throw new Exception("Unexpected value when converting date. Expected DateTime or DateTimeOffset, got {0}.".FormatWith(CultureInfo.InvariantCulture, ReflectionUtils.GetObjectType(value)));
      }

      writer.WriteValue(text);
    }
开发者ID:Rickasaurus,项目名称:Newtonsoft.Json-Fork,代码行数:38,代码来源:IsoDateTimeConverter.cs

示例5: SerializePrimitive

    private void SerializePrimitive(JsonWriter writer, object value, JsonPrimitiveContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
    {
      if (contract.UnderlyingType == typeof (byte[]))
      {
        bool includeTypeDetails = ShouldWriteType(TypeNameHandling.Objects, contract, member, containerContract, containerProperty);
        if (includeTypeDetails)
        {
          writer.WriteStartObject();
          WriteTypeProperty(writer, contract.CreatedType);
          writer.WritePropertyName(JsonTypeReflector.ValuePropertyName);

          if (contract.IsNullable)
            writer.WriteValue(value, true);
          else
            writer.WriteValue(value);

          writer.WriteEndObject();
          return;
        }
      }

      if (contract.IsNullable)
        writer.WriteValue(value, true);
      else
        writer.WriteValue(value);
    }
开发者ID:medcat,项目名称:Newtonsoft.Json,代码行数:26,代码来源:JsonSerializerInternalWriter.cs

示例6: CanConvert

    /// <summary>
    /// Determines whether this instance can convert the specified object type.
    /// </summary>
    /// <param name="objectType">Type of the object.</param>
    /// <returns>
    /// 	<c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
    /// </returns>
      public override bool CanConvert(Type objectType, JsonProperty property)
    {
      if (objectType.IsValueType && objectType.IsGenericType)
        return (objectType.GetGenericTypeDefinition() == typeof (KeyValuePair<,>));

      return false;
    }
开发者ID:Rickasaurus,项目名称:Newtonsoft.Json-Fork,代码行数:14,代码来源:KeyValuePairConverter.cs

示例7: CamelCaseEnumValues

        /// <summary>
        /// Ensures that enum values are serialized as camel-cased strings.
        /// </summary>
        private static JsonProperty CamelCaseEnumValues(JsonProperty property)
        {
            if (property.PropertyType.GetTypeInfo().IsEnum)
                property.Converter = new StringEnumConverter(camelCaseText: true);

            return property;
        }
开发者ID:CSClassroom,项目名称:CSClassroom,代码行数:10,代码来源:ApiContractResolver.cs

示例8: CreateNewObject

 // Token: 0x06000BDB RID: 3035
 // RVA: 0x00046004 File Offset: 0x00044204
 public object CreateNewObject(JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMember, JsonProperty containerProperty, string id, out bool createdFromNonDefaultCreator)
 {
     object obj = null;
     if (objectContract.OverrideCreator != null)
     {
         if (objectContract.CreatorParameters.Count > 0)
         {
             createdFromNonDefaultCreator = true;
             return this.CreateObjectUsingCreatorWithParameters(reader, objectContract, containerMember, objectContract.OverrideCreator, id);
         }
         obj = objectContract.OverrideCreator(new object[0]);
     }
     else if (objectContract.DefaultCreator != null && (!objectContract.DefaultCreatorNonPublic || this.Serializer._constructorHandling == ConstructorHandling.AllowNonPublicDefaultConstructor || objectContract.ParametrizedCreator == null))
     {
         obj = objectContract.DefaultCreator();
     }
     else if (objectContract.ParametrizedCreator != null)
     {
         createdFromNonDefaultCreator = true;
         return this.CreateObjectUsingCreatorWithParameters(reader, objectContract, containerMember, objectContract.ParametrizedCreator, id);
     }
     if (obj != null)
     {
         createdFromNonDefaultCreator = false;
         return obj;
     }
     if (!objectContract.IsInstantiable)
     {
         throw JsonSerializationException.Create(reader, StringUtils.FormatWith("Could not create an instance of type {0}. Type is an interface or abstract class and cannot be instantiated.", CultureInfo.InvariantCulture, objectContract.UnderlyingType));
     }
     throw JsonSerializationException.Create(reader, StringUtils.FormatWith("Unable to find a constructor to use for type {0}. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute.", CultureInfo.InvariantCulture, objectContract.UnderlyingType));
 }
开发者ID:newchild,项目名称:Project-DayZero,代码行数:34,代码来源:JsonSerializerInternalReader.cs

示例9: WriteJson

    public override void WriteJson(JsonWriter writer, JsonProperty member, object value, JsonSerializer serializer)
    {
      ConverterPrecedenceClass c = (ConverterPrecedenceClass)value;

      JToken j = new JArray(ConverterType, c.TestValue);

      j.WriteTo(writer);
    }
开发者ID:Rickasaurus,项目名称:Newtonsoft.Json-Fork,代码行数:8,代码来源:ConverterPrecedenceClassConverter.cs

示例10: IsNotEmpty

        private bool IsNotEmpty(JsonProperty property, object target)
        {
            var value = property.ValueProvider.GetValue(target) as IEnumerable<object>;
            if (value != null)
                return value.Count() != 0;

            return true;
        }
开发者ID:MaxDeg,项目名称:NJose,代码行数:8,代码来源:IgnoreEmptyCollectionContractResolver.cs

示例11: JSchemaTypeGenerationContext

 /// <summary>
 /// Initializes a new instance of the <see cref="JSchemaTypeGenerationContext"/> class.
 /// </summary>
 /// <param name="objectType">The object type.</param>
 /// <param name="required">The required state.</param>
 /// <param name="memberProperty">The member property.</param>
 /// <param name="parentContract">The parent contract.</param>
 /// <param name="generator">The current <see cref="JSchemaGenerator"/>.</param>
 public JSchemaTypeGenerationContext(Type objectType, Required required, JsonProperty memberProperty, JsonContainerContract parentContract, JSchemaGenerator generator)
 {
     _objectType = objectType;
     _required = required;
     _memberProperty = memberProperty;
     _parentContract = parentContract;
     _generator = generator;
 }
开发者ID:Pondidum,项目名称:Newtonsoft.Json.Schema,代码行数:16,代码来源:JSchemaTypeGenerationContext.cs

示例12: IsEnumerable

        /// <summary>
        /// Check if a property implements IEnumerable and IEnumerable&lt;&gt;
        /// </summary>
        private static bool IsEnumerable(JsonProperty property)
        {
            if (property.PropertyType == typeof (string))
                return false;

            var interfaces = property.PropertyType.GetInterfaces();
            return interfaces.Any(i => i == typeof(IEnumerable));
        }
开发者ID:charlyraffellini,项目名称:openstack.net,代码行数:11,代码来源:OpenStackContractResolver.cs

示例13: ReadJson

    /// <summary>
    /// Reads the JSON representation of the object.
    /// </summary>
    /// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
    /// <param name="objectType">Type of the object.</param>
    /// <param name="existingValue">The existing value of object being read.</param>
    /// <param name="serializer">The calling serializer.</param>
    /// <returns>The object value.</returns>
      public override object ReadJson(JsonReader reader, Type objectType, JsonProperty property, object existingValue, JsonSerializer serializer)
    {
      if (reader.TokenType != JsonToken.Bytes)
        throw new JsonSerializationException("Expected Bytes but got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));

      byte[] value = (byte[])reader.Value;

      return new BsonObjectId(value);
    }
开发者ID:Rickasaurus,项目名称:Newtonsoft.Json-Fork,代码行数:17,代码来源:BsonObjectIdConverter.cs

示例14: WriteJson

      public override void WriteJson(JsonWriter writer, JsonProperty member, object value, JsonSerializer serializer)
    {
      NameContainer nameContainer = value as NameContainer;

      if (nameContainer != null)
        writer.WriteValue(nameContainer.Value);
      else
        writer.WriteNull();
    }
开发者ID:Rickasaurus,项目名称:Newtonsoft.Json-Fork,代码行数:9,代码来源:PublicParametizedConstructorRequiringConverterTestClass.cs

示例15: WriteJson

      public override void WriteJson(JsonWriter writer, JsonProperty member, object value, JsonSerializer serializer)
    {
      Department department = (Department)value;

      JObject o = new JObject();
      o["DepartmentId"] = new JValue(department.DepartmentId.ToString());
      o["Name"] = new JValue(new string(department.Name.Reverse().ToArray()));

      o.WriteTo(writer);
    }
开发者ID:Rickasaurus,项目名称:Newtonsoft.Json-Fork,代码行数:10,代码来源:DepartmentConverter.cs


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