예제 #1
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.Compare(constructorName, "Date", StringComparison.Ordinal) == 0)
       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((long)reader.Value);
     break;
       case JsonToken.Float:
     WriteValue((double)reader.Value);
     break;
       case JsonToken.String:
     WriteValue(reader.Value.ToString());
     break;
       case JsonToken.Boolean:
     WriteValue((bool)reader.Value);
     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());
 }
예제 #2
0
        private void WriteConstructorDate(JsonReader reader)
        {
            if (!reader.Read())
            throw new Exception("Unexpected end while reading date constructor.");
              if (reader.TokenType != JsonToken.Integer)
            throw new Exception("Unexpected token while reading date constructor. Expected Integer, got " + reader.TokenType);

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

              if (!reader.Read())
            throw new Exception("Unexpected end while reading date constructor.");
              if (reader.TokenType != JsonToken.EndConstructor)
            throw new Exception("Unexpected token while reading date constructor. Expected EndConstructor, got " + reader.TokenType);

              WriteValue(date);
        }