public void DeserializeObject()
        {
            var instance1 = JsonStreamConvert.DeserializeObject <MyTestClass>(ExpectedJson);

            Assert.Equal(ExpectedInstance.Value, instance1.Value);

            var instance2 = (MyTestClass)JsonStreamConvert.DeserializeObject(ExpectedJson, typeof(MyTestClass));

            Assert.Equal(ExpectedInstance.Value, instance2.Value);
        }
Example #2
0
        public void ReadSimpleTypesFromYamlParser()
        {
            // The .NET SDK allows flag data to be read from a YAML file by first running it through a YAML
            // parser, which produces types like Dictionary, and then feeding that into JReader. So we'll
            // verify that a basic use case like that works, using YamlDotNet.
            var yamlContent = @"
---
flags:
  flag1:
    key: flag1
    ""on"": true
    version: 1
    fallthrough:
      variation: 2
    variations:
      - fall
      - ""off""
      - ""on""
flagValues:
  flag2: value2
segments:
  seg1:
    key: seg1
    version: 1
    include: [""user1""]
";
            var yaml        = new YamlDotNet.Serialization.DeserializerBuilder().Build();
            var dataIn      = yaml.Deserialize <object>(yamlContent);

            var r = JReader.FromAdapter(ReaderAdapters.FromSimpleTypes(dataIn));

            var dataOut = JsonStreamConvert.ConvertSimpleTypes.ReadJson(ref r);

            var jsonOut      = JsonStreamConvert.SerializeObject(dataOut, JsonStreamConvert.ConvertSimpleTypes);
            var expectedJson = @"{
""flags"": {
  ""flag1"": {
    ""key"": ""flag1"",
    ""on"": ""true"",
    ""version"": ""1"",
    ""fallthrough"": { ""variation"": ""2"" },
    ""variations"": [ ""fall"", ""off"", ""on"" ]
  }
},
""flagValues"": { ""flag2"": ""value2"" },
""segments"": {
  ""seg1"": { ""key"": ""seg1"", ""version"": ""1"", ""include"": [""user1""] }
}
}";

            // Note that YamlDotNet parses all the booleans and numbers as strings; that's why we provide a
            // type coercion option.
            TestUtil.AssertJsonEqual(expectedJson, jsonOut);
        }
        public void ConvertToSimpleTypes()
        {
            var json  = @"[null, true, 100, ""x"", {""a"": 1}]";
            var value = JsonStreamConvert.DeserializeObject(json, JsonStreamConvert.ConvertSimpleTypes);
            var list  = Assert.IsType <List <object> >(value);

            Assert.Collection(list,
                              e => Assert.Null(e),
                              e => Assert.Equal(true, e),
                              e => Assert.Equal((double)100, e),
                              e => Assert.Equal("x", e),
                              e =>
            {
                var dict = Assert.IsType <Dictionary <string, object> >(e);
                Assert.Equal((double)1, dict["a"]);
            }
                              );
        }
        public void ConvertFromSimpleTypes()
        {
            var value = new List <object>
            {
                null,
                true,
                100,
                "x",
                new Dictionary <string, object> {
                    { "a", 1 }
                },
                new Dictionary <object, object> {
                    { "b", 2 }
                }
            };
            var expected = @"[null, true, 100, ""x"", {""a"": 1}, {""b"": 2}]";
            var actual   = JsonStreamConvert.SerializeObject(value, JsonStreamConvert.ConvertSimpleTypes);

            TestUtil.AssertJsonEqual(expected, actual);
        }
 public void DeserializeObjectFailsForClassWithNoConverter()
 {
     Assert.Throws <ArgumentException>(() =>
                                       JsonStreamConvert.DeserializeObject <TestClassWithNoConverter>("{}"));
 }
 public void SerializeObjectFailsForClassWithNoConverter()
 {
     Assert.Throws <ArgumentException>(() =>
                                       JsonStreamConvert.SerializeObject(new TestClassWithNoConverter()));
 }
 public void SerializeObjectToUtf8Bytes()
 {
     Assert.Equal(Encoding.UTF8.GetBytes(ExpectedJson),
                  JsonStreamConvert.SerializeObjectToUtf8Bytes(ExpectedInstance));
 }
 public void SerializeObject()
 {
     Assert.Equal(ExpectedJson, JsonStreamConvert.SerializeObject(ExpectedInstance));
     Assert.Equal(ExpectedJson, JsonStreamConvert.SerializeObject(ExpectedInstance, new MyTestConverter()));
 }