Exemplo n.º 1
0
 /// <summary>
 /// Writes a <see cref="Nullable{Double}"/> value.
 /// </summary>
 /// <param name="value">The <see cref="Nullable{Double}"/> value to write.</param>
 public override void WriteValue(double?value)
 {
     if (value == null)
     {
         WriteNull();
     }
     else
     {
         InternalWriteValue(JsonToken.Float);
         WriteValueInternal(BacktraceDataConverter.ToString(value.GetValueOrDefault(), FloatFormatHandling, QuoteChar, true), JsonToken.Float);
     }
 }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }

            var result = new List <T>();

            var obj = BacktraceJObject.Load(reader);

            for (var i = 0; i < obj.Count; i++)
            {
                result.Add(BacktraceDataConverter.DeserializeObject <T>(obj[i].ToString()));
            }

            return(result);
        }
        private void ValidateFloat(JsonSchemaModel schema)
        {
            if (schema == null)
            {
                return;
            }

            if (!TestType(schema, JsonSchemaType.Float))
            {
                return;
            }

            ValidateNotDisallowed(schema);

            double value = Convert.ToDouble(_reader.Value, CultureInfo.InvariantCulture);

            if (schema.Maximum != null)
            {
                if (value > schema.Maximum)
                {
                    RaiseError("Float {0} exceeds maximum value of {1}.".FormatWith(CultureInfo.InvariantCulture, BacktraceDataConverter.ToString(value), schema.Maximum), schema);
                }
                if (schema.ExclusiveMaximum && value == schema.Maximum)
                {
                    RaiseError("Float {0} equals maximum value of {1} and exclusive maximum is true.".FormatWith(CultureInfo.InvariantCulture, BacktraceDataConverter.ToString(value), schema.Maximum), schema);
                }
            }

            if (schema.Minimum != null)
            {
                if (value < schema.Minimum)
                {
                    RaiseError("Float {0} is less than minimum value of {1}.".FormatWith(CultureInfo.InvariantCulture, BacktraceDataConverter.ToString(value), schema.Minimum), schema);
                }
                if (schema.ExclusiveMinimum && value == schema.Minimum)
                {
                    RaiseError("Float {0} equals minimum value of {1} and exclusive minimum is true.".FormatWith(CultureInfo.InvariantCulture, BacktraceDataConverter.ToString(value), schema.Minimum), schema);
                }
            }

            if (schema.DivisibleBy != null)
            {
                double remainder = FloatingPointRemainder(value, schema.DivisibleBy.GetValueOrDefault());

                if (!IsZero(remainder))
                {
                    RaiseError("Float {0} is not evenly divisible by {1}.".FormatWith(CultureInfo.InvariantCulture, BacktraceDataConverter.ToString(value), schema.DivisibleBy), schema);
                }
            }
        }
        private void ValidateInteger(JsonSchemaModel schema)
        {
            if (schema == null)
            {
                return;
            }

            if (!TestType(schema, JsonSchemaType.Integer))
            {
                return;
            }

            ValidateNotDisallowed(schema);

            object value = _reader.Value;

            if (schema.Maximum != null)
            {
                if (JValue.Compare(JTokenType.Integer, value, schema.Maximum) > 0)
                {
                    RaiseError("Integer {0} exceeds maximum value of {1}.".FormatWith(CultureInfo.InvariantCulture, value, schema.Maximum), schema);
                }
                if (schema.ExclusiveMaximum && JValue.Compare(JTokenType.Integer, value, schema.Maximum) == 0)
                {
                    RaiseError("Integer {0} equals maximum value of {1} and exclusive maximum is true.".FormatWith(CultureInfo.InvariantCulture, value, schema.Maximum), schema);
                }
            }

            if (schema.Minimum != null)
            {
                if (JValue.Compare(JTokenType.Integer, value, schema.Minimum) < 0)
                {
                    RaiseError("Integer {0} is less than minimum value of {1}.".FormatWith(CultureInfo.InvariantCulture, value, schema.Minimum), schema);
                }
                if (schema.ExclusiveMinimum && JValue.Compare(JTokenType.Integer, value, schema.Minimum) == 0)
                {
                    RaiseError("Integer {0} equals minimum value of {1} and exclusive minimum is true.".FormatWith(CultureInfo.InvariantCulture, value, schema.Minimum), schema);
                }
            }

            if (schema.DivisibleBy != null)
            {
                bool notDivisible = !IsZero(Convert.ToInt64(value, CultureInfo.InvariantCulture) % schema.DivisibleBy.GetValueOrDefault());

                if (notDivisible)
                {
                    RaiseError("Integer {0} is not evenly divisible by {1}.".FormatWith(CultureInfo.InvariantCulture, BacktraceDataConverter.ToString(value), schema.DivisibleBy), schema);
                }
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Writes a <see cref="decimal"/> value.
 /// </summary>
 /// <param name="value">The <see cref="decimal"/> value to write.</param>
 public override void WriteValue(decimal value)
 {
     InternalWriteValue(JsonToken.Float);
     WriteValueInternal(BacktraceDataConverter.ToString(value), JsonToken.Float);
 }
Exemplo n.º 6
0
 /// <summary>
 /// Writes a <see cref="char"/> value.
 /// </summary>
 /// <param name="value">The <see cref="char"/> value to write.</param>
 public override void WriteValue(char value)
 {
     InternalWriteValue(JsonToken.String);
     WriteValueInternal(BacktraceDataConverter.ToString(value), JsonToken.String);
 }
Exemplo n.º 7
0
 /// <summary>
 /// Writes a <see cref="bool"/> value.
 /// </summary>
 /// <param name="value">The <see cref="bool"/> value to write.</param>
 public override void WriteValue(bool value)
 {
     InternalWriteValue(JsonToken.Boolean);
     WriteValueInternal(BacktraceDataConverter.ToString(value), JsonToken.Boolean);
 }
Exemplo n.º 8
0
 /// <summary>
 /// Writes a <see cref="double"/> value.
 /// </summary>
 /// <param name="value">The <see cref="double"/> value to write.</param>
 public override void WriteValue(double value)
 {
     InternalWriteValue(JsonToken.Float);
     WriteValueInternal(BacktraceDataConverter.ToString(value, FloatFormatHandling, QuoteChar, false), JsonToken.Float);
 }