Ejemplo n.º 1
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));
        }
Ejemplo n.º 2
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));
        }
Ejemplo n.º 3
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");
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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));
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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);
        }