Пример #1
0
 internal static JsonReaderException Create(JsonReader reader, string message, Exception ex)
 {
   return Create(reader as IJsonLineInfo, reader.Path, message, ex);
 }
Пример #2
0
 /// <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 abstract object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer);
Пример #3
0
    private void WriteConstructorDate(JsonReader reader)
    {
      if (!reader.Read())
        throw JsonWriterException.Create(this, "Unexpected end when reading date constructor.", null);
      if (reader.TokenType != JsonToken.Integer)
        throw JsonWriterException.Create(this, "Unexpected token when reading date constructor. Expected Integer, got " + reader.TokenType, null);

      long ticks = (long)reader.Value;
      DateTime date = JsonConvert.ConvertJavaScriptTicksToDateTime(ticks);

      if (!reader.Read())
        throw JsonWriterException.Create(this, "Unexpected end when reading date constructor.", null);
      if (reader.TokenType != JsonToken.EndConstructor)
        throw JsonWriterException.Create(this, "Unexpected token when reading date constructor. Expected EndConstructor, got " + reader.TokenType, null);

      WriteValue(date);
    }
Пример #4
0
 internal static JsonReaderException Create(JsonReader reader, string message)
 {
   return Create(reader, message, null);
 }
Пример #5
0
    /// <summary>
    /// Writes the current <see cref="JsonReader"/> token.
    /// </summary>
    /// <param name="reader">The <see cref="JsonReader"/> to read the token from.</param>
    public void WriteToken(JsonReader reader)
    {
      ValidationUtils.ArgumentNotNull(reader, "reader");

      int initialDepth;

      if (reader.TokenType == JsonToken.None)
        initialDepth = -1;
      else if (!IsStartToken(reader.TokenType))
        initialDepth = reader.Depth + 1;
      else
        initialDepth = reader.Depth;

      WriteToken(reader, initialDepth);
    }
Пример #6
0
 internal void WriteToken(JsonReader reader, int initialDepth)
 {
   do
   {
     switch (reader.TokenType)
     {
       case JsonToken.None:
         // read to next
         break;
       case JsonToken.StartObject:
         WriteStartObject();
         break;
       case JsonToken.StartArray:
         WriteStartArray();
         break;
       case JsonToken.StartConstructor:
         string constructorName = reader.Value.ToString();
         // write a JValue date when the constructor is for a date
         if (string.Equals(constructorName, "Date", StringComparison.Ordinal))
           WriteConstructorDate(reader);
         else
           WriteStartConstructor(reader.Value.ToString());
         break;
       case JsonToken.PropertyName:
         WritePropertyName(reader.Value.ToString());
         break;
       case JsonToken.Comment:
         WriteComment(reader.Value.ToString());
         break;
       case JsonToken.Integer:
         WriteValue(Convert.ToInt64(reader.Value, CultureInfo.InvariantCulture));
         break;
       case JsonToken.Float:
         WriteValue(Convert.ToDouble(reader.Value, CultureInfo.InvariantCulture));
         break;
       case JsonToken.String:
         WriteValue(reader.Value.ToString());
         break;
       case JsonToken.Boolean:
         WriteValue(Convert.ToBoolean(reader.Value, CultureInfo.InvariantCulture));
         break;
       case JsonToken.Null:
         WriteNull();
         break;
       case JsonToken.Undefined:
         WriteUndefined();
         break;
       case JsonToken.EndObject:
         WriteEndObject();
         break;
       case JsonToken.EndArray:
         WriteEndArray();
         break;
       case JsonToken.EndConstructor:
         WriteEndConstructor();
         break;
       case JsonToken.Date:
         WriteValue((DateTime)reader.Value);
         break;
       case JsonToken.Raw:
         WriteRawValue((string)reader.Value);
         break;
       case JsonToken.Bytes:
         WriteValue((byte[])reader.Value);
         break;
       default:
         throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", reader.TokenType, "Unexpected token type.");
     }
   }
   while (
     // stop if we have reached the end of the token being read
     initialDepth - 1 < reader.Depth - (IsEndToken(reader.TokenType) ? 1 : 0)
     && reader.Read());
 }
Пример #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JsonValidatingReader"/> class that
 /// validates the content returned from the given <see cref="JsonReader"/>.
 /// </summary>
 /// <param name="reader">The <see cref="JsonReader"/> to read from while validating.</param>
 public JsonValidatingReader(JsonReader reader)
 {
   ValidationUtils.ArgumentNotNull(reader, "reader");
   _reader = reader;
   _stack = new Stack<SchemaScope>();
 }
Пример #8
0
 /// <summary>
 /// Deserializes the Json structure contained by the specified <see cref="JsonReader"/>
 /// into an instance of the specified type.
 /// </summary>
 /// <param name="reader">The <see cref="JsonReader"/> containing the object.</param>
 /// <param name="objectType">The <see cref="Type"/> of object being deserialized.</param>
 /// <returns>The instance of <paramref name="objectType"/> being deserialized.</returns>
 public object Deserialize(JsonReader reader, Type objectType)
 {
     return(DeserializeInternal(reader, objectType));
 }
Пример #9
0
 /// <summary>
 /// Deserializes the Json structure contained by the specified <see cref="JsonReader"/>
 /// into an instance of the specified type.
 /// </summary>
 /// <param name="reader">The <see cref="JsonReader"/> containing the object.</param>
 /// <typeparam name="T">The type of the object to deserialize.</typeparam>
 /// <returns>The instance of <typeparamref name="T"/> being deserialized.</returns>
 public T Deserialize <T>(JsonReader reader)
 {
     return((T)Deserialize(reader, typeof(T)));
 }
Пример #10
0
 /// <summary>
 /// Deserializes the Json structure contained by the specified <see cref="JsonReader"/>.
 /// </summary>
 /// <param name="reader">The <see cref="JsonReader"/> that contains the JSON structure to deserialize.</param>
 /// <returns>The <see cref="Object"/> being deserialized.</returns>
 public object Deserialize(JsonReader reader)
 {
     return(Deserialize(reader, null));
 }
Пример #11
0
 /// <summary>
 /// Populates the JSON values onto the target object.
 /// </summary>
 /// <param name="reader">The <see cref="JsonReader"/> that contains the JSON structure to reader values from.</param>
 /// <param name="target">The target object to populate values onto.</param>
 public void Populate(JsonReader reader, object target)
 {
     PopulateInternal(reader, target);
 }