Пример #1
0
        public void ToStringCreatesHumanReadableJsNotJsonScrewDoubleQuotes()
        {
            // Arrange
            const string sIn  = "{ a: 'b\\'c b\"c', b: 1, c: true, d: false, e: 1.11, f: [ 'g', 'h' ], i: { j: 'k', l: 2, m: [ 1, 2 ], n: { o: 3, p: 4, q: { r: 's', t: [ 1, 2, 3 ], u: [ { v: 1, w: 2 }, { x: 3, y: 4 } ] } } } }";
            var          root = new JsonTree.Node(sIn);

            // Act
            string sOut = root.ToString();

            // Assert
            Assert.AreEqual(sIn, sOut);
        }
Пример #2
0
        public void ArrayOfDictionary_ToJson()
        {
            // Arrange
            const string sIn  = "[ { 'param': { 'name': 'defaultsequence', 'value': 'idle' }}, { 'sequence': { 'group': 'idle', 'name': 'still', 'type': 'status', 'probability': '1000', 'in': 'standard', 'out': 'standard', 'src': 'idle.gif' }} ]";
            var          root = new JsonTree.Node(sIn);

            // Act
            string sOut = root.ToJson();

            // Assert
            Assert.IsFalse(String.IsNullOrEmpty(sOut));
        }
Пример #3
0
        public void ToJson()
        {
            // Arrange
            const string sIn  = "{ a: 'a', b: 1, c: true, d: 1.11, e: [ 'e1', 'e2' ], f: { f1: 'f1', f2: 'f2' } }";
            var          root = new JsonTree.Node(sIn);

            // Act
            string sOut = root.ToJson();

            // Assert
            Assert.IsFalse(String.IsNullOrEmpty(sOut));
        }
Пример #4
0
        public void SeralizeWithAddedNode()
        {
            // Arrange
            const string sIn  = @"
[
  {
    aInt: 41,
    bBool: true,
    cLong: 42000000000,
    dString: '43',
    eFloat: 3.14159265358979323
  },
  {
    fInt: 44,
    gLong: 45000000000,
    hString: ""46""
  },
  {
    iList:
    [
      {
        jInt: 47,
        kString: 'true'
      },
      {
        lInt: 49,
        mString: '50'
      }
    ],
    nMap:
    {
      oInt: 51,
      pString: '52'
    }
  }
]
";
            var          root = new JsonTree.Node(sIn);

            // Act
            root.List.ElementAt(2).Dictionary.Add("new child", new JsonTree.Node(JsonTree.Node.Type.String)
            {
                Value = "new string"
            });

            // Assert
            string sOut = root.ToJson();

            Assert.IsTrue(sOut.Contains("\"new child\":\"new string\""));
            var check = new JsonTree.Node(sOut);

            Assert.AreEqual(check.List.ElementAt(2).Dictionary["new child"].String, "new string");
        }
Пример #5
0
        public void ToJsonDefaultCreatesDoubleQuotesAndQuotedKeysAndNoFormatting()
        {
            // Arrange
            const string sIn  = "{\"a\":\"a\",\"b\":1,\"c\":true,\"d\":1.11,\"e\":[\"e1\",\"e2\"],\"f\":{\"f1\":\"f1\",\"f2\":\"f2\"}}";
            var          root = new JsonTree.Node(sIn);

            // Act
            string sOut = root.ToJson();

            // Assert
            Assert.AreEqual(sIn, sOut);
        }
Пример #6
0
        public void AnimationsXml_ToJson()
        {
            // Arrange
            var xml = "<config xmlns='http://schema.bluehands.de/character-config' version='1.0'> <param name='defaultsequence' value='idle'/> <sequence group='idle' name='still' type='status' probability='1000' in='standard' out='standard'><animation src='idle.gif'/></sequence> <sequence group='idle' name='idle1' type='status' probability='2' in='standard' out='standard'><animation src='idle-1.gif'/></sequence> <sequence group='idle' name='idle2' type='status' probability='5' in='standard' out='standard'><animation src='idle-2.gif'/></sequence> <sequence group='idle' name='idle3' type='status' probability='5' in='standard' out='standard'><animation src='idle-3.gif'/></sequence> <sequence group='idle' name='idle4' type='status' probability='1' in='standard' out='standard'><animation src='idle-4.gif'/></sequence> <sequence group='moveleft' name='moveleft' type='basic' probability='1' in='moveleft' out='moveleft'><animation dx='-55' dy='0' src='walk-l.gif'/></sequence> <sequence group='moveright' name='moveright' type='basic' probability='1' in='moveright' out='moveright'><animation dx='55' dy='0' src='walk-r.gif'/></sequence> <sequence group='chat' name='chat1' type='basic' probability='1000' in='standard' out='standard'><animation src='chat.gif'/></sequence> <sequence group='chat' name='chat2' type='basic' probability='100' in='standard' out='standard'><animation src='chat-2.gif'/></sequence> <sequence group='wave' name='wave' type='emote' probability='1000' in='standard' out='standard'><animation src='wave.gif'/></sequence> <sequence group='kiss' name='kiss' type='emote' probability='1000' in='standard' out='standard'><animation src='kiss.gif'/></sequence> <sequence group='cheer' name='cheer' type='emote' probability='1000' in='standard' out='standard'><animation src='cheer.gif'/></sequence> <sequence group='agree' name='agree' type='emote' probability='1000' in='standard' out='standard'><animation src='agree.gif'/></sequence> <sequence group='deny' name='deny' type='emote' probability='1000' in='standard' out='standard'><animation src='disagree.gif'/></sequence> <sequence group='clap' name='clap' type='emote' probability='1000' in='standard' out='standard'><animation src='clap.gif'/></sequence> <sequence group='dance' name='dance' type='emote' probability='1000' in='standard' out='standard'><animation src='dance.gif'/></sequence> <sequence group='yawn' name='yawn' type='emote' probability='1000' in='standard' out='standard'><animation src='yawn.gif'/></sequence> <sequence group='angry' name='angry' type='emote' probability='1000' in='standard' out='standard'><animation src='angry.gif'/></sequence> <sequence group='laugh' name='laugh' type='emote' probability='1000' in='standard' out='standard'><animation src='laugh.gif'/></sequence> <sequence group='sleep' name='sleep' type='status' probability='1000' in='standard' out='standard'><animation src='idle.gif'/></sequence> </config>";

            var doc = new XmlDocument();

            doc.LoadXml(xml);
            var nodes = doc.DocumentElement.ChildNodes;

            var json = new JsonTree.Node(JsonTree.Node.Type.List);

            var keys = new Dictionary <string, List <string> > {
                { "param", new List <string> {
                      "name", "value"
                  } },
                { "sequence", new List <string> {
                      "group", "name", "type", "probability", "in", "out"
                  } },
            };

            foreach (XmlNode node in nodes)
            {
                if (keys.ContainsKey(node.Name))
                {
                    var dict = new JsonTree.Node(JsonTree.Node.Type.Dictionary);
                    foreach (var key in keys[node.Name])
                    {
                        var attr = node.Attributes.GetNamedItem(key);
                        if (attr != null)
                        {
                            dict.AsDictionary.Add(attr.Name, new JsonTree.Node(JsonTree.Node.Type.String, attr.Value));
                        }
                    }
                    if (node.Name == "sequence")
                    {
                        var src = node.FirstChild.Attributes["src"].Value;
                        dict.AsDictionary.Add("src", new JsonTree.Node(JsonTree.Node.Type.String, src));
                    }
                    var item = new JsonTree.Node(JsonTree.Node.Type.Dictionary);
                    item.AsDictionary.Add(node.Name, dict);
                    json.AsArray.Add(item);
                }
            }

            // Act
            var sOut = json.ToJson();

            // Assert
            Assert.IsFalse(String.IsNullOrEmpty(sOut));
        }
Пример #7
0
        public void JsonRecursionExceeds100()
        {
            // Arrange
            const string sIn          = "{'a':[{'b':1},{'c':[{'b':1.83},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'j','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'f','g':'h','i':'_'},{'d':[{'e':'j','g':'t','i':'_'},{'k':[{'q':[{'p':[{'q':['uDE']},{'q':['s8','s10','s16']},{'q':['t0','t1','t3','t4','t5','t7','t10','t11','t1']},{'q':['va']},{'p':[{'r':'xw'}]},{'p':[{'r':'aa1'}]}]}],'o':{'n':['m']}}],'o':{'n':['m']}}],'o':{'n':['m','145']}}],'o':{'n':['m','151']}}],'o':{'n':['m','137']}}],'o':{'n':['m','155']}}],'o':{'n':['m','156']}}],'o':{'n':['m','143']}}],'o':{'n':['m','131']}}],'o':{'n':['m','165']}}],'o':{'n':['m','170']}}],'o':{'n':['m','148']}}],'o':{'n':['m','144']}}],'o':{'n':['m','138']}}],'o':{'n':['m','150']}}],'o':{'n':['m','158']}}],'o':{'n':['m','146']}}],'o':{'n':['m','159']}}],'o':{'n':['m','147']}}],'o':{'n':['m','161']}}],'o':{'n':['m','163']}}],'o':{'n':['m','13']}}],'o':{'n':['m','154']}}],'o':{'n':['m','130']}}],'o':{'n':['m','141']}}],'o':{'n':['m','136']}}],'o':{'n':['m','139']}}],'o':{'n':['m','167']}}],'o':{'n':['m','135']}}],'o':{'n':['m','168']}}],'o':{'n':['m','160']}}],'o':{'n':['m','171']}}],'o':{'n':['m','19']}}],'o':{'n':['m','14']}}],'o':{'n':['m','166']}}],'o':{'n':['m','157']}}],'o':{'n':['m','15']}}],'o':{'n':['m','134']}}],'o':{'n':['m','153']}}],'o':{'n':['m','164']}}],'o':{'n':['m','133']}}],'o':{'n':['m','169']}}],'o':{'n':['m','140']}}],'o':{'n':['m','149']}}],'o':{'n':['m','16']}}],'o':{'n':['m','17']}}],'o':{'n':['m']}}],'o':{'n':['m','l']}}";
            const string sExpectedOut = sIn;

            // Act
            var root = new JsonTree.Node(sIn, new JsonTree.Deserializer.Options {
                RecursionLimit = 110
            });
            string sOut = root.ToJson(new JsonTree.Serializer.Options {
                EncapsulateKeys = "'", EncapsulateStrings = "'"
            });

            // Assert
            Assert.AreEqual(sOut, sExpectedOut);
        }
Пример #8
0
        public void AddNodes()
        {
            // Arrange
            // Act
            var node = new JsonTree.Node(JsonTree.Node.Type.Dictionary);

            node.AsDictionary.Add("a", new JsonTree.Node(JsonTree.Node.Type.String, "fourtytwo"));
            node.AsDictionary.Add("b", new JsonTree.Node(JsonTree.Node.Type.Int, 42));
            node.AsDictionary.Add("c", new JsonTree.Node(JsonTree.Node.Type.List, new List <JsonTree.Node> {
                new JsonTree.Node(JsonTree.Node.Type.Bool, true), new JsonTree.Node(JsonTree.Node.Type.Float, 3.14)
            }));
            string sJson = node.ToJson();

            // Assert
            Assert.AreEqual(new JsonTree.Node(sJson).Dictionary["a"].String, "fourtytwo");
            Assert.AreEqual(new JsonTree.Node(sJson).Dictionary["b"].Int, 42);
            Assert.AreEqual(new JsonTree.Node(sJson).Dictionary["c"].AsList[0].Bool, true);
            Assert.AreEqual(new JsonTree.Node(sJson).Dictionary["c"].AsList[1].Float, 3.14);
        }
Пример #9
0
 public void StringConstructor()
 {
     var root = new JsonTree.Node("42");
 }
Пример #10
0
        public void DeserializeTypicalJson()
        {
            var sJson = @"
[
  {
    aInt: 41,
    bBool: true,
    bLong: 42000000000,
    cString: '43',
    dFloat: 3.14159265358979323
  },
  {
    aInt: 44,
    bLong: 45000000000,
    cString: ""46""
  },
  {
    aList:
    [
      {
        aInt: 47,
        bString: '48'
      },
      {
        aInt: 49,
        bString: '50'
      }
    ],
    bMap:
    {
      aInt: 51,
      bString: '52'
    }
  }
]
";
            // Act
            var root = new JsonTree.Node(sJson);

            // Assert
            Node   lastListItem     = null;
            string lastMapItemKey   = null;
            Node   lastMapItemValue = null;

            foreach (var item in root.List)
            {
                lastListItem = item;
            }
            foreach (var pair in root.List.First().Dictionary)
            {
                lastMapItemKey   = pair.Key;
                lastMapItemValue = pair.Value;
            }

            Assert.AreEqual(lastListItem.Dictionary.Count, 2);
            Assert.AreEqual(lastMapItemKey, "dFloat");
            Assert.AreEqual(lastMapItemValue.Float, 3.14159265358979323);

            // Act/Assert
            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(0).Dictionary.ElementAt(0).Key, "aInt");
            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(0).Dictionary.ElementAt(0).Value.Int, 41);
            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(0).Dictionary.ElementAt(1).Key, "bBool");
            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(0).Dictionary.ElementAt(1).Value.Bool, true);
            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(0).Dictionary.ElementAt(2).Key, "bLong");
            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(0).Dictionary.ElementAt(2).Value.Int, 42000000000);
            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(0).Dictionary.ElementAt(3).Key, "cString");
            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(0).Dictionary.ElementAt(3).Value.String, "43");
            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(0).Dictionary.ElementAt(4).Key, "dFloat");

            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(0).Dictionary.ElementAt(4).Value.Float, 3.14159265358979323);
            Assert.AreEqual(new JsonTree.Node(sJson).List[0].Dictionary.ElementAt(4).Value.Float, 3.14159265358979323);
            Assert.AreEqual(new JsonTree.Node(sJson).Array[0].Object.ElementAt(4).Value.Float, 3.14159265358979323);

            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(1).Dictionary.ElementAt(0).Key, "aInt");
            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(1).Dictionary.ElementAt(0).Value.Int, 44);
            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(1).Dictionary.ElementAt(1).Key, "bLong");
            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(1).Dictionary.ElementAt(1).Value.Int, 45000000000);
            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(1).Dictionary.ElementAt(2).Key, "cString");
            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(1).Dictionary.ElementAt(2).Value.String, "46");

            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(2).Dictionary.ElementAt(0).Key, "aList");
            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(2).Dictionary.ElementAt(0).Value.List.Count, 2);
            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(2).Dictionary.ElementAt(0).Value.List.ElementAt(0).Dictionary.ElementAt(0).Key, "aInt");
            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(2).Dictionary.ElementAt(0).Value.List.ElementAt(0).Dictionary.ElementAt(0).Value.Int, 47);
            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(2).Dictionary.ElementAt(0).Value.List.ElementAt(0).Dictionary.ElementAt(1).Key, "bString");
            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(2).Dictionary.ElementAt(0).Value.List.ElementAt(0).Dictionary.ElementAt(1).Value.String, "48");
            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(2).Dictionary.ElementAt(0).Value.List.ElementAt(1).Dictionary.ElementAt(0).Key, "aInt");
            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(2).Dictionary.ElementAt(0).Value.List.ElementAt(1).Dictionary.ElementAt(0).Value.Int, 49);
            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(2).Dictionary.ElementAt(0).Value.List.ElementAt(1).Dictionary.ElementAt(1).Key, "bString");

            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(2).Dictionary.ElementAt(0).Value.List.ElementAt(1).Dictionary.ElementAt(1).Value.String, "50");
            Assert.AreEqual(new JsonTree.Node(sJson).List[2].Dictionary["aList"].List[1].Dictionary["bString"].String, "50");
            Assert.AreEqual(new JsonTree.Node(sJson).Array[2].Object["aList"].Array[1].Object["bString"].String, "50");

            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(2).Dictionary.ElementAt(1).Key, "bMap");
            Assert.AreEqual(new JsonTree.Node(sJson).List.ElementAt(2).Dictionary.ElementAt(1).Value.Dictionary.Count, 2);
        }