Esempio n. 1
0
        public static XmlElement Convert(JsonBoolean boolean)
        {
            var e = new XmlElement("boolean", true);

            e.Content.Add(new XmlText(boolean.Stringify()));
            return(e);
        }
Esempio n. 2
0
        public static void TestReplacingsonObjectPrimaryTypes()
        {
            var person1 = new JsonObject
            {
                { "name", "John" },
                { "age", 45 },
                { "is_married", true }
            };


            person1["name"] = new JsonString("Bob");
            Assert.Equal("Bob", ((JsonString)person1["name"]).Value);

            person1["age"] = new JsonNumber(55);
            Assert.Equal(55, ((JsonNumber)person1["age"]).GetInt32());

            person1["is_married"] = new JsonBoolean(false);
            Assert.False(((JsonBoolean)person1["is_married"]).Value);

            var person2 = new JsonObject
            {
                { "name", "Bob" },
                { "age", 33 },
                { "is_married", true }
            };

            // Copy property from another JsonObject
            person1["age"] = person2["age"];

            Assert.Equal(33, ((JsonNumber)person1["age"]).GetInt32());

            // Copy property of different typoe
            person1["name"] = person2["name"];
        }
Esempio n. 3
0
        public override void Start()
        {
            #region 准备

            Logger.Start("使用 Json中的工具 处理 Json 表达式。");

            const string S = "{a:1, b:[2, 3], c: {2:[7] }}";

            #endregion

            #region 初

            Logger.Info("获值");

            JsonExpression json = new JsonExpression(S);

            Logger.Write(json["c"]["2"][0]);            //  7

            Logger.Write(json["c"].Json);               //  {2:[7] }

            Logger.Write(json["a"].Value);


            Logger.Info("改值");

            json["hh"] = new JsonArray(new int[] { 3, 4, 2, 5, 6 });

            json["ss"] = new JsonBoolean(false);

            Logger.Write(json.ToString());

            #endregion
        }
Esempio n. 4
0
        public static void TestChangingValue()
        {
            var jsonBoolean = new JsonBoolean();

            jsonBoolean.Value = true;
            Assert.True(jsonBoolean.Value);
            jsonBoolean.Value = false;
            Assert.False(jsonBoolean.Value);
        }
Esempio n. 5
0
        public static void TestValueCtor()
        {
            var jsonBoolean = new JsonBoolean(true);

            Assert.True(jsonBoolean.Value);

            jsonBoolean = new JsonBoolean(false);
            Assert.False(jsonBoolean.Value);
        }
Esempio n. 6
0
        public static void TestImplicitOperator()
        {
            JsonBoolean jsonBoolean = true;

            Assert.True(jsonBoolean.Value);

            jsonBoolean = false;
            Assert.False(jsonBoolean.Value);
        }
Esempio n. 7
0
        public void ItConvertsBooleans()
        {
            var json = new JsonBoolean(false);
            var xml  = ToXml.Convert(json).Stringify();

            Assert.AreEqual("<boolean>false</boolean>", xml);

            json = new JsonBoolean(true);
            xml  = ToXml.Convert(json).Stringify();
            Assert.AreEqual("<boolean>true</boolean>", xml);
        }
        public void ShouldNotParseFalseNotLowerCaseFail(
            string jsonFragment
            )
        {
            IJsonElement parser = new JsonBoolean();
            string       jsonRemainder;

            Assert.IsFalse(
                parser.Parse(
                    jsonFragment,
                    out jsonRemainder
                    )
                );
        }
        public void ShouldParseFalseLowercaseSucceed(
            string jsonFragment
            )
        {
            IJsonElement parser = new JsonBoolean();
            string       jsonRemainder;

            Assert.IsTrue(
                parser.Parse(
                    jsonFragment,
                    out jsonRemainder
                    )
                );
        }
Esempio n. 10
0
        public static void TestGetHashCode()
        {
            var jsonBooleanTrue  = new JsonBoolean(true);
            var jsonBooleanFalse = new JsonBoolean(false);

            Assert.Equal(jsonBooleanTrue.GetHashCode(), jsonBooleanTrue.GetHashCode());
            Assert.Equal(jsonBooleanTrue.GetHashCode(), new JsonBoolean(true).GetHashCode());
            Assert.Equal(jsonBooleanFalse.GetHashCode(), jsonBooleanFalse.GetHashCode());
            Assert.Equal(jsonBooleanFalse.GetHashCode(), new JsonBoolean(false).GetHashCode());
            Assert.Equal(jsonBooleanFalse.GetHashCode(), new JsonBoolean().GetHashCode());
            Assert.NotEqual(jsonBooleanTrue.GetHashCode(), new JsonBoolean().GetHashCode());

            JsonNode jsonNodeTrue = new JsonBoolean(true);

            Assert.Equal(jsonBooleanTrue.GetHashCode(), jsonNodeTrue.GetHashCode());
            JsonNode jsonNodeFalse = new JsonBoolean(false);

            Assert.Equal(jsonBooleanFalse.GetHashCode(), jsonNodeFalse.GetHashCode());

            Assert.NotEqual(jsonBooleanTrue.GetHashCode(), jsonBooleanFalse.GetHashCode());

            IEquatable <JsonBoolean> jsonBooleanIEquatableTrue = jsonBooleanTrue;

            Assert.Equal(jsonBooleanIEquatableTrue.GetHashCode(), jsonBooleanTrue.GetHashCode());

            IEquatable <JsonBoolean> jsonBooleanIEquatableFalse = jsonBooleanFalse;

            Assert.Equal(jsonBooleanIEquatableFalse.GetHashCode(), jsonBooleanFalse.GetHashCode());

            object jsonBooleanCopyTrue   = jsonBooleanTrue;
            object jsonBooleanObjectTrue = new JsonBoolean(true);

            Assert.Equal(jsonBooleanTrue.GetHashCode(), jsonBooleanCopyTrue.GetHashCode());
            Assert.Equal(jsonBooleanTrue.GetHashCode(), jsonBooleanObjectTrue.GetHashCode());

            object jsonBooleanCopyFalse   = jsonBooleanFalse;
            object jsonBooleanObjectFalse = new JsonBoolean(false);

            Assert.Equal(jsonBooleanCopyFalse.GetHashCode(), jsonBooleanFalse.GetHashCode());
            Assert.Equal(jsonBooleanObjectFalse.GetHashCode(), jsonBooleanFalse.GetHashCode());

            Assert.Equal(0, jsonBooleanFalse.GetHashCode());
            Assert.Equal(1, jsonBooleanTrue.GetHashCode());
        }
Esempio n. 11
0
        private JsonNode ReadLiteral()
        {
            var literal = reader.Current;

            reader.Next(); // Read the literal token

            switch (literal.Kind)
            {
            case TokenKind.Boolean: return(JsonBoolean.Parse(literal.Value));

            case TokenKind.Null: return(XNull.Instance);

            case TokenKind.Number: return(new JsonNumber(literal.Value));

            case TokenKind.String: return(new JsonString(literal.Value));

            default: throw new ParserException($"Unexpected token reading literal. Was {literal}.");
            }
        }
Esempio n. 12
0
        public static void TestEquals()
        {
            var jsonBooleanTrue  = new JsonBoolean(true);
            var jsonBooleanFalse = new JsonBoolean(false);

            Assert.True(jsonBooleanTrue.Equals(new JsonBoolean(true)));
            Assert.True(new JsonBoolean(true).Equals(jsonBooleanTrue));

            Assert.True(jsonBooleanFalse.Equals(new JsonBoolean(false)));
            Assert.True(new JsonBoolean(false).Equals(jsonBooleanFalse));
            Assert.True(new JsonBoolean().Equals(jsonBooleanFalse));
            Assert.False(new JsonBoolean().Equals(jsonBooleanTrue));
            Assert.False(jsonBooleanFalse.Equals(jsonBooleanTrue));
            Assert.False(jsonBooleanTrue.Equals(jsonBooleanFalse));

            Assert.True(jsonBooleanTrue == new JsonBoolean(true));
            Assert.True(jsonBooleanTrue != jsonBooleanFalse);

            JsonNode jsonNodeTrue = new JsonBoolean(true);

            Assert.True(jsonBooleanTrue.Equals(jsonNodeTrue));

            JsonNode jsonNodeFalse = new JsonBoolean(false);

            Assert.True(jsonBooleanFalse.Equals(jsonNodeFalse));

            IEquatable <JsonBoolean> jsonBooleanIEquatableTrue = jsonBooleanTrue;

            Assert.True(jsonBooleanIEquatableTrue.Equals(jsonBooleanTrue));
            Assert.True(jsonBooleanTrue.Equals(jsonBooleanIEquatableTrue));

            IEquatable <JsonBoolean> jsonBooleanIEquatableFalse = jsonBooleanFalse;

            Assert.True(jsonBooleanIEquatableFalse.Equals(jsonBooleanFalse));
            Assert.True(jsonBooleanFalse.Equals(jsonBooleanIEquatableFalse));

            Assert.False(jsonBooleanTrue.Equals(null));

            object jsonBooleanCopyTrue   = jsonBooleanTrue;
            object jsonBooleanObjectTrue = new JsonBoolean(true);

            Assert.True(jsonBooleanCopyTrue.Equals(jsonBooleanObjectTrue));
            Assert.True(jsonBooleanCopyTrue.Equals(jsonBooleanObjectTrue));
            Assert.True(jsonBooleanObjectTrue.Equals(jsonBooleanTrue));

            object jsonBooleanCopyFalse   = jsonBooleanFalse;
            object jsonBooleanObjectFalse = new JsonBoolean(false);

            Assert.True(jsonBooleanCopyFalse.Equals(jsonBooleanObjectFalse));
            Assert.True(jsonBooleanCopyFalse.Equals(jsonBooleanFalse));
            Assert.True(jsonBooleanObjectFalse.Equals(jsonBooleanFalse));

            Assert.False(jsonBooleanTrue.Equals(new Exception()));

            JsonBoolean jsonBooleanNull = null;

            Assert.False(jsonBooleanTrue == jsonBooleanNull);
            Assert.False(jsonBooleanNull == jsonBooleanTrue);

            Assert.True(jsonBooleanTrue != jsonBooleanNull);
            Assert.True(jsonBooleanNull != jsonBooleanTrue);

            JsonBoolean otherJsonBooleanNull = null;

            Assert.True(jsonBooleanNull == otherJsonBooleanNull);
        }
Esempio n. 13
0
        public static void TestDefaultCtor()
        {
            var jsonBoolean = new JsonBoolean();

            Assert.False(jsonBoolean.Value);
        }
Esempio n. 14
0
        public static JsonDocument Load(JsonReader reader)
        {
            var nodeStack = new Stack <JsonNode>();
            var doc       = new JsonDocument();

            nodeStack.Push(doc);

            var      propertyStack = new Stack <string>();
            JsonNode node          = null;

            while (reader.Read())
            {
                switch (reader.SyntaxKind)
                {
                case SyntaxKind.Comment:
                case SyntaxKind.None:
                    continue;

                case SyntaxKind.ArrayStart:
                    var arr = new JsonArray();
                    arr.Start = reader.Start;
                    nodeStack.Push(arr);
                    continue;

                case SyntaxKind.ObjectStart:
                    var obj = new JsonObject();
                    obj.Start = reader.Start;
                    nodeStack.Push(obj);
                    continue;

                case SyntaxKind.PropertyName:
                    propertyStack.Push(reader.PropertyName);

                    continue;

                case SyntaxKind.ArrayEnd:
                case SyntaxKind.ObjectEnd:
                    node     = nodeStack.Pop();
                    node.End = reader.End;
                    break;

                case SyntaxKind.StringValue:
                    node = new JsonString(reader.StringValue);
                    SetLocation(reader, node);
                    break;

                case SyntaxKind.DoubleValue:
                    node = new JsonNumber(reader.FloatValue);
                    SetLocation(reader, node);
                    break;

                case SyntaxKind.IntegerValue:
                    node = new JsonNumber(reader.IntegerValue);
                    SetLocation(reader, node);
                    break;

                case SyntaxKind.NullValue:
                    node = new JsonNull();
                    SetLocation(reader, node);
                    break;

                case SyntaxKind.TrueValue:
                    node = new JsonBoolean(true);
                    SetLocation(reader, node);
                    break;

                case SyntaxKind.FalseValue:
                    node = new JsonBoolean(false);
                    SetLocation(reader, node);
                    break;
                }

                var parent = nodeStack.Peek();

                if (parent is JsonArray a)
                {
                    a.Add(node);
                }
                else
                if (parent is JsonObject o)
                {
                    var propertyName = propertyStack.Pop();
                    o.Add(propertyName, node);
                }
                else
                if (parent is JsonDocument d)
                {
                    d.RootNode = node;
                    break;
                }
            }
            if (doc.RootNode != null)
            {
                doc.Start = doc.RootNode.Start;
                doc.End   = doc.RootNode.End;
            }

            return(doc);
        }
Esempio n. 15
0
        /// <summary>
        /// Parses the json to node.
        /// </summary>
        /// <param name="joTreeNode">The jo tree node.</param>
        /// <returns></returns>
        public ASTreeViewNode ParseJsonToNode(JsonObject joTreeNode)
        {
            JsonString  nodeText                = (JsonString)joTreeNode[this.GetObfuscatedString("node_treeNodeText")];
            JsonString  nodeValue               = (JsonString)joTreeNode[this.GetObfuscatedString("node_treeNodeValue")];
            JsonNumber  checkedState            = (JsonNumber)joTreeNode[this.GetObfuscatedString("node_checkedState")];
            JsonNumber  openState               = (JsonNumber)joTreeNode[this.GetObfuscatedString("node_openState")];
            JsonBoolean selected                = (JsonBoolean)joTreeNode[this.GetObfuscatedString("node_selected")];
            JsonBoolean enableEditContextMenu   = (JsonBoolean)joTreeNode[this.GetObfuscatedString("node_enableEditContextMenu")];
            JsonBoolean enableDeleteContextMenu = (JsonBoolean)joTreeNode[this.GetObfuscatedString("node_enableDeleteContextMenu")];
            JsonBoolean enableAddContextMenu    = (JsonBoolean)joTreeNode[this.GetObfuscatedString("node_enableAddContextMenu")];
            JsonNumber  treeNodeType            = (JsonNumber)joTreeNode[this.GetObfuscatedString("node_treeNodeType")];
            JsonString  treeNodeIcon            = null;

            if (joTreeNode.ContainsKey(this.GetObfuscatedString("node_treeNodeIcon")))
            {
                treeNodeIcon = (JsonString)joTreeNode[this.GetObfuscatedString("node_treeNodeIcon")];
            }

            ASTreeViewNodeType astNodeType = (ASTreeViewNodeType)((int)treeNodeType.Value);

            JsonBoolean enableDragDrop  = (JsonBoolean)joTreeNode[this.GetObfuscatedString("node_enableDragDrop")];
            JsonBoolean enableSiblings  = (JsonBoolean)joTreeNode[this.GetObfuscatedString("node_enableSiblings")];
            JsonBoolean enableChildren  = (JsonBoolean)joTreeNode[this.GetObfuscatedString("node_enableChildren")];
            JsonBoolean enableCheckbox  = (JsonBoolean)joTreeNode[this.GetObfuscatedString("node_enableCheckbox")];
            JsonBoolean enableSelection = (JsonBoolean)joTreeNode[this.GetObfuscatedString("node_enableSelection")];
            JsonBoolean enableOpenClose = (JsonBoolean)joTreeNode[this.GetObfuscatedString("node_enableOpenClose")];

            ASTreeViewNode node;// = new ASTreeViewNode();

            switch (astNodeType)
            {
            case ASTreeViewNodeType.LinkButton:
                node = new ASTreeViewNode();
                break;

            case ASTreeViewNodeType.HyperLink:
                node = new ASTreeViewLinkNode();
                break;

            case ASTreeViewNodeType.Text:
                node = new ASTreeViewTextNode();
                break;

            default:
                node = new ASTreeViewNode();
                break;
            }

            node.NodeText                = HttpUtility.UrlDecode(nodeText.Value);
            node.NodeValue               = HttpUtility.UrlDecode(nodeValue.Value);
            node.CheckedState            = (ASTreeViewCheckboxState)((int)checkedState.Value);
            node.OpenState               = (ASTreeViewNodeOpenState)((int)openState.Value);
            node.Selected                = selected.Value;
            node.EnableEditContextMenu   = enableEditContextMenu.Value;
            node.EnableDeleteContextMenu = enableDeleteContextMenu.Value;
            node.EnableAddContextMenu    = enableAddContextMenu.Value;

            node.EnableDragDrop  = enableDragDrop.Value;
            node.EnableSiblings  = enableSiblings.Value;
            node.EnableChildren  = enableChildren.Value;
            node.EnableCheckbox  = enableCheckbox.Value;
            node.EnableSelection = enableSelection.Value;
            node.EnableOpenClose = enableOpenClose.Value;

            #region handle tree node depth

            if (this.currentTreeView.EnableFixedDepthDragDrop)
            {
                JsonNumber treeNodeDepth = null;
                if (joTreeNode.ContainsKey(this.GetObfuscatedString("node_treeNodeDepth")))
                {
                    treeNodeDepth  = (JsonNumber)joTreeNode[this.GetObfuscatedString("node_treeNodeDepth")];
                    node.NodeDepth = (int)treeNodeDepth.Value;
                }
            }

            #endregion

            #region handle virtual node
            JsonBoolean isVirtualNode = null;
            if (joTreeNode.ContainsKey(this.GetObfuscatedString("node_isVirtualNode")))
            {
                isVirtualNode      = (JsonBoolean)joTreeNode[this.GetObfuscatedString("node_isVirtualNode")];
                node.IsVirtualNode = isVirtualNode.Value;
            }

            JsonNumber virtualNodesCount = null;
            if (joTreeNode.ContainsKey(this.GetObfuscatedString("node_virtualNodesCount")))
            {
                virtualNodesCount      = (JsonNumber)joTreeNode[this.GetObfuscatedString("node_virtualNodesCount")];
                node.VirtualNodesCount = (int)virtualNodesCount.Value;
            }

            JsonString virtualParentKey = null;
            if (joTreeNode.ContainsKey(this.GetObfuscatedString("node_virtualParentKey")))
            {
                virtualParentKey      = (JsonString)joTreeNode[this.GetObfuscatedString("node_virtualParentKey")];
                node.VirtualParentKey = virtualParentKey.Value;
            }

            JsonString jsAttr = null;
            if (joTreeNode.ContainsKey(this.GetObfuscatedString("node_additionalAttributes")))
            {
                jsAttr = (JsonString)joTreeNode[this.GetObfuscatedString("node_additionalAttributes")];
                string attrString = jsAttr.Value;

                if (!string.IsNullOrEmpty(attrString))
                {
                    try
                    {
                        StringReader rdr    = new StringReader(attrString);
                        JsonParser   parser = new JsonParser(rdr, true);
                        JsonObject   jo     = (JsonObject)parser.ParseObject();
                        List <KeyValuePair <string, string> > additionalAttributes = new List <KeyValuePair <string, string> >();
                        foreach (string key in jo.Keys)
                        {
                            additionalAttributes.Add(new KeyValuePair <string, string>(key, ((JsonString)jo[key]).Value));
                        }

                        node.AdditionalAttributes = additionalAttributes;
                    }
                    catch { }
                }
            }


            #endregion


            //handle hyperlink
            if (astNodeType == ASTreeViewNodeType.HyperLink)
            {
                ASTreeViewLinkNode hlNode  = (ASTreeViewLinkNode)node;
                JsonString         href    = (JsonString)joTreeNode[this.GetObfuscatedString("node_href")];
                JsonString         target  = (JsonString)joTreeNode[this.GetObfuscatedString("node_target")];
                JsonString         tooltip = (JsonString)joTreeNode[this.GetObfuscatedString("node_tooltip")];

                hlNode.NavigateUrl = href.Value;
                hlNode.Target      = target.Value;
                hlNode.Tooltip     = tooltip.Value;
            }

            if (treeNodeIcon != null)
            {
                node.NodeIcon = treeNodeIcon.Value;
            }

            return(node);
        }
Esempio n. 16
0
        public JsonValue Read()
        {
            JsonValue cur = null;
            string lastPropName = null;
            Stack<JsonValue> stack = new Stack<JsonValue> ();
            Stack<string> propNameStack = new Stack<string> ();
            JsonReader reader = _reader;

            while (reader.Read ()) {
                switch (reader.Token) {
                    case JsonToken.ArrayStart:
                    case JsonToken.ObjectStart:
                        if (cur != null) {
                            stack.Push (cur);
                            if (cur is JsonObject) {
                                propNameStack.Push (lastPropName);
                                lastPropName = null;
                            }
                        }
                        if (reader.Token == JsonToken.ArrayStart)
                            cur = new JsonArray (new List<JsonValue> ());
                        else
                            cur = new JsonObject (new Dictionary<string,JsonValue> ());
                        break;
                    case JsonToken.ObjectEnd:
                    case JsonToken.ArrayEnd:
                        if (stack.Count == 0)
                            return cur;
                        JsonValue parent = stack.Pop ();
                        if (parent is JsonArray) {
                            (parent as JsonArray).Value.Add (cur);
                        } else if (parent is JsonObject) {
                            lastPropName = propNameStack.Pop ();
                            if (lastPropName == null)
                                throw new JsonException ();
                            (parent as JsonObject).Value.Add (lastPropName, cur);
                            lastPropName = null;
                        }
                        cur = parent;
                        break;
                    case JsonToken.PropertyName:
                        if (lastPropName != null)
                            throw new JsonException ();
                        lastPropName = (string)reader.Value;
                        break;
                    case JsonToken.Boolean:
                    case JsonToken.Null:
                    case JsonToken.Number:
                    case JsonToken.String:
                        JsonValue value;
                        switch (reader.Token) {
                            case JsonToken.Boolean: value = new JsonBoolean ((bool)reader.Value); break;
                            case JsonToken.Null: value = new JsonNull (); break;
                            case JsonToken.Number:
                                value = new JsonNumber (Convert (reader.NumberType), (double)reader.Value, reader.ValueSignedInteger, reader.ValueUnsignedInteger);
                                break;
                            case JsonToken.String: value = new JsonString ((string)reader.Value); break;
                            default: throw new JsonException ();
                        }
                        if (cur == null)
                            return value;
                        if (cur is JsonArray) {
                            (cur as JsonArray).Value.Add (value);
                        } else if (cur is JsonObject) {
                            if (lastPropName == null)
                                throw new JsonException ();
                            (cur as JsonObject).Value.Add (lastPropName, value);
                            lastPropName = null;
                        }
                        break;
                }
            }

            if (cur == null)
                return null;
            throw new JsonException ();
        }