Example #1
0
        public void DeserializeFromRaw()
        {
            JsonSerializerTest.PersonRaw raw = new JsonSerializerTest.PersonRaw
              {
            FirstName = "FirstNameValue",
            RawContent = new JsonRaw("[1,2,3,4,5]"),
            LastName = "LastNameValue"
              };

              JObject o = JObject.FromObject(raw);

              JsonReader reader = new JsonTokenReader(o);
              JsonSerializer serializer = new JsonSerializer();
              raw = (JsonSerializerTest.PersonRaw)serializer.Deserialize(reader, typeof(JsonSerializerTest.PersonRaw));

              Assert.AreEqual("FirstNameValue", raw.FirstName);
              Assert.AreEqual("LastNameValue", raw.LastName);
              Assert.AreEqual("[1,2,3,4,5]", raw.RawContent.Content);
        }
Example #2
0
    public void YahooFinance()
    {
      JObject o =
        new JObject(
          new JProperty("Test1", new DateTime(2000, 10, 15, 5, 5, 5, DateTimeKind.Utc)),
          new JProperty("Test2", new DateTimeOffset(2000, 10, 15, 5, 5, 5, new TimeSpan(11, 11, 0))),
          new JProperty("Test3", "Test3Value"),
          new JProperty("Test4", null)
        );

      using (JsonReader jsonReader = new JsonTokenReader(o))
      {
        jsonReader.Read();
        Assert.AreEqual(JsonToken.StartObject, jsonReader.TokenType);

        jsonReader.Read();
        Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
        Assert.AreEqual("Test1", jsonReader.Value);

        jsonReader.Read();
        Assert.AreEqual(JsonToken.Date, jsonReader.TokenType);
        Assert.AreEqual(new DateTime(2000, 10, 15, 5, 5, 5, DateTimeKind.Utc), jsonReader.Value);

        jsonReader.Read();
        Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
        Assert.AreEqual("Test2", jsonReader.Value);

        jsonReader.Read();
        Assert.AreEqual(JsonToken.Date, jsonReader.TokenType);
        Assert.AreEqual(new DateTimeOffset(2000, 10, 15, 5, 5, 5, new TimeSpan(11, 11, 0)), jsonReader.Value);

        jsonReader.Read();
        Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
        Assert.AreEqual("Test3", jsonReader.Value);

        jsonReader.Read();
        Assert.AreEqual(JsonToken.String, jsonReader.TokenType);
        Assert.AreEqual("Test3Value", jsonReader.Value);

        jsonReader.Read();
        Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
        Assert.AreEqual("Test4", jsonReader.Value);

        jsonReader.Read();
        Assert.AreEqual(JsonToken.Null, jsonReader.TokenType);
        Assert.AreEqual(null, jsonReader.Value);

        Assert.IsTrue(jsonReader.Read());
        Assert.AreEqual(JsonToken.EndObject, jsonReader.TokenType);

        Assert.IsFalse(jsonReader.Read());
      }

      using (JsonReader jsonReader = new JsonTokenReader(o.Property("Test2")))
      {
        Assert.IsTrue(jsonReader.Read());
        Assert.AreEqual(JsonToken.PropertyName, jsonReader.TokenType);
        Assert.AreEqual("Test2", jsonReader.Value);

        Assert.IsTrue(jsonReader.Read());
        Assert.AreEqual(JsonToken.Date, jsonReader.TokenType);
        Assert.AreEqual(new DateTimeOffset(2000, 10, 15, 5, 5, 5, new TimeSpan(11, 11, 0)), jsonReader.Value);

        Assert.IsFalse(jsonReader.Read());
      }
    }
Example #3
0
        public void JsonTokenReader()
        {
            JsonSerializerTest.PersonRaw raw = new JsonSerializerTest.PersonRaw
              {
            FirstName = "FirstNameValue",
            RawContent = new JsonRaw("[1,2,3,4,5]"),
            LastName = "LastNameValue"
              };

              JObject o = JObject.FromObject(raw);

              JsonReader reader = new JsonTokenReader(o);

              Assert.IsTrue(reader.Read());
              Assert.AreEqual(JsonToken.StartObject, reader.TokenType);

              Assert.IsTrue(reader.Read());
              Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);

              Assert.IsTrue(reader.Read());
              Assert.AreEqual(JsonToken.String, reader.TokenType);

              Assert.IsTrue(reader.Read());
              Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);

              Assert.IsTrue(reader.Read());
              Assert.AreEqual(JsonToken.Raw, reader.TokenType);

              Assert.IsTrue(reader.Read());
              Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);

              Assert.IsTrue(reader.Read());
              Assert.AreEqual(JsonToken.String, reader.TokenType);

              Assert.IsTrue(reader.Read());
              Assert.AreEqual(JsonToken.EndObject, reader.TokenType);

              Assert.IsFalse(reader.Read());
        }