Beispiel #1
0
        public void JsonBuilder_Serialization_PropertyValues()
        {
            var jsonBuilder = new TestJsonBuilderElement(_loggerFactory);

            var data = new Dictionary <string, object>()
            {
                {
                    "element",
                    new Dictionary <string, object>()
                    {
                        { "property1", 10 },
                        { "property2", new AspectPropertyValue <int>(10) },
                        { "property3", 10.1 },
                        { "property4", new AspectPropertyValue <double>(10.1) },
                        { "property5", true },
                        { "property6", new AspectPropertyValue <bool>(true) },
                        { "property7", new List <string>()
                          {
                              "itema", "itemb"
                          } },
                        { "property8", new AspectPropertyValue <IReadOnlyList <string> >(new List <string>()
                            {
                                "itema", "itemb"
                            }) },
                        { "property9", new List <string>() },
                        { "property10", new AspectPropertyValue <IReadOnlyList <string> >(new List <string>()) },
                    }
                }
            };

            var result = jsonBuilder.Serialize(data);

            Assert.AreEqual(@"{
  ""element"": {
    ""property1"": 10,
    ""property2"": 10,
    ""property3"": 10.1,
    ""property4"": 10.1,
    ""property5"": true,
    ""property6"": true,
    ""property7"": [
      ""itema"",
      ""itemb""
    ],
    ""property8"": [
      ""itema"",
      ""itemb""
    ],
    ""property9"": [],
    ""property10"": []
  }
}", result);
        }
Beispiel #2
0
        public void JsonBuilder_Serialization_Text(string valueOfProperty, TypeToBeTested typeToBeTested)
        {
            var jsonBuilder = new TestJsonBuilderElement(_loggerFactory);

            // valueInDict holds the object that will be added
            // to the dictionary to be serialized.
            object valueInDict = null;

            // ExpectedValue holds the value that we expect
            // the property to have in the generated json
            // including any surrounding tokens such as quotes,
            // square brackets, etc.
            string expectedValue = (valueOfProperty ?? "null").Replace(@"\", @"\\");

            expectedValue = expectedValue.Replace(@"""", @"\""");
            expectedValue = expectedValue.Replace(@"    ", @"\  ");
            expectedValue = expectedValue.Replace(@"
", @"\r\n");

            // Replacing as there is no carriage returnon Linux format.
            expectedValue = expectedValue.Replace(@"\r", @"");
            expectedValue = valueOfProperty == null ? expectedValue : $"\"{expectedValue}\"";

            // Create the object to be serialized
            switch (typeToBeTested)
            {
            case TypeToBeTested.String:
                valueInDict = valueOfProperty;
                break;

            case TypeToBeTested.JavaScript:
                valueInDict = new JavaScript(valueOfProperty);
                break;

            case TypeToBeTested.List:
                valueInDict = new List <string>()
                {
                    valueOfProperty
                };
                expectedValue = $@"[
      {expectedValue}
    ]";
                break;

            case TypeToBeTested.APV_String:
                valueInDict = new AspectPropertyValue <string>(valueOfProperty);
                break;

            case TypeToBeTested.APV_JavaScript:
                valueInDict = new AspectPropertyValue <JavaScript>(new JavaScript(valueOfProperty));
                break;

            case TypeToBeTested.APV_List:
                valueInDict = new AspectPropertyValue <IReadOnlyList <string> >(new List <string>()
                {
                    valueOfProperty
                });
                expectedValue = $@"[
      {expectedValue}
    ]";
                break;

            default:
                break;
            }

            // Create the dictionary to be serialized
            var data = new Dictionary <string, object>()
            {
                {
                    "element",
                    new Dictionary <string, object>()
                    {
                        { "property", valueInDict },
                    }
                }
            };

            var result = jsonBuilder.Serialize(data);

            Assert.AreEqual($@"{{
  ""element"": {{
    ""property"": {expectedValue}
  }}
}}",
                            result.Replace(@"\r", @""));
        }