public void ParsingSpecialUnicodeCharacter()
        {
            var jsonObjectDict = new Dictionary <string, JsonValue>();

            jsonObjectDict.Add("Test", JsonStringValue.FromString("/\b\f\n\r\t\"\\\ud834\uDD1E"));
            Assert.That(JsonObjectValue.FromDictionary(jsonObjectDict).ToString(), Is.EqualTo("{\"Test\":\"\\/\\b\\f\\n\\r\\t\\\"\\\\𝄞\"}"));
        }
Exemple #2
0
        /// <summary>
        ///     Parses the given token in init state.
        ///     <para>
        ///         This state is the state right after starting parsing the JSON string.
        ///         Valid and expected tokens in this state are simple value tokens or start of a compound value.
        ///     </para>
        /// </summary>
        /// <param name="token">the token to parse</param>
        /// <exception cref="JsonParserException">in case parsing fails.</exception>
        private void ParseInitState(JsonToken token)
        {
            EnsureTokenIsNotNull(token, "No JSON object could be decoded");

            switch (token.TokenType)
            {
            case JsonTokenType.LITERAL_NULL:     // fallthrough
            case JsonTokenType.LITERAL_BOOLEAN:  // fallthrough
            case JsonTokenType.VALUE_STRING:     // fallthrough
            case JsonTokenType.VALUE_NUMBER:     // fallthrough
                valueContainerStack.AddFirst(new JsonValueContainer(TokenToSimpleJsonValue(token)));
                state = JsonParserState.END;
                break;

            case JsonTokenType.LEFT_SQUARE_BRACKET:
                var jsonValueList = new LinkedList <JsonValue>();
                valueContainerStack.AddFirst(new JsonValueContainer(JsonArrayValue.FromList(jsonValueList),
                                                                    jsonValueList));
                state = JsonParserState.IN_ARRAY_START;
                break;

            case JsonTokenType.LEFT_BRACE:
                var jsonObjectDict = new Dictionary <string, JsonValue>();
                valueContainerStack.AddFirst(new JsonValueContainer(JsonObjectValue.FromDictionary(jsonObjectDict),
                                                                    jsonObjectDict));
                state = JsonParserState.IN_OBJECT_START;
                break;

            default:
                state = JsonParserState.ERROR;
                throw new JsonParserException(UnexpectedTokenErrorMessage(token, "at start of input"));
            }
        }
        public void EmptyObjectJsonString()
        {
            // given
            var jsonObjectDict = new Dictionary <string, JsonValue>();

            Assert.That(JsonObjectValue.FromDictionary(jsonObjectDict).ToString(), Is.EqualTo("{}"));
        }
        public void IsObjectType()
        {
            // given
            var emptyDict = new Dictionary <string, JsonValue>();

            // then
            Assert.That(JsonObjectValue.FromDictionary(emptyDict).ValueType, Is.EqualTo(JsonValueType.OBJECT));
        }
        public void SingleElementObjectJsonString()
        {
            // given
            var jsonObjectDict = new Dictionary <string, JsonValue>();

            jsonObjectDict.Add("Test", JsonBooleanValue.FromValue(false));

            Assert.That(JsonObjectValue.FromDictionary(jsonObjectDict).ToString(), Is.EqualTo("{\"Test\":false}"));
        }
Exemple #6
0
        public string Build()
        {
            if (overriddenKeys.Count > 0)
            {
                AddNonOverridableAttribute("dt.overridden_keys", JsonArrayValue.FromList(overriddenKeys));
            }

            return(JsonObjectValue.FromDictionary(attributes).ToString());
        }
Exemple #7
0
        /// <summary>
        ///     Utility method to parse the start of a nested object.
        ///     <para>
        ///         This method is called when the left brace token is encountered.
        ///     </para>
        /// </summary>
        private void ParseStartOfNestedObject()
        {
            stateStack.AddFirst(JsonParserState.IN_ARRAY_VALUE);
            var jsonObjectDict = new Dictionary <string, JsonValue>();

            valueContainerStack.AddFirst(new JsonValueContainer(JsonObjectValue.FromDictionary(jsonObjectDict),
                                                                jsonObjectDict));
            state = JsonParserState.IN_OBJECT_START;
        }
        public void IndexerReturnsNullIfKeyDoesNotExist()
        {
            // given
            var jsonObjectDict = new Dictionary <string, JsonValue>();

            var target = JsonObjectValue.FromDictionary(jsonObjectDict);

            // when
            var obtained = target["foo"];

            // then
            Assert.That(obtained, Is.Null);
        }
        public void CountDelegatesToUnderlyingDictionary()
        {
            // given
            var jsonObjectDict = Substitute.For <IDictionary <string, JsonValue> >();

            jsonObjectDict.Count.Returns(42);
            var target = JsonObjectValue.FromDictionary(jsonObjectDict);

            // when
            var obtained = target.Count;

            // then
            Assert.That(obtained, Is.EqualTo(42));
            _ = jsonObjectDict.Received(1).Count;
        }
        public void NestedObjectInObjectJsonString()
        {
            // given
            var jsonObjectDict = new Dictionary <string, JsonValue>();

            jsonObjectDict.Add("Test", JsonBooleanValue.FromValue(false));

            var jsonNestedObjectDict = new Dictionary <string, JsonValue>();

            jsonNestedObjectDict.Add("Test3", JsonNumberValue.FromLong(1));

            jsonObjectDict.Add("Test2", JsonObjectValue.FromDictionary(jsonNestedObjectDict));

            Assert.That(JsonObjectValue.FromDictionary(jsonObjectDict).ToString(), Is.EqualTo("{\"Test\":false,\"Test2\":{\"Test3\":1}}"));
        }
        public void KeysDelegatesToUnderlyingDictionary()
        {
            //given
            var jsonObjectDict        = Substitute.For <IDictionary <string, JsonValue> >();
            ICollection <string> keys = new HashSet <string> {
                "foobar"
            };

            jsonObjectDict.Keys.Returns(keys);
            var target = JsonObjectValue.FromDictionary(jsonObjectDict);

            // when
            var obtained = target.Keys;

            // then
            Assert.That(obtained, Is.Not.Null);
            _ = jsonObjectDict.Received(1).Keys;
        }
Exemple #12
0
        /// <summary>
        ///     Parses a token right after a JSON key-value delimiter (":") was parsed.
        /// </summary>
        /// <param name="token">the token to parse.</param>
        /// <exception cref="JsonParserException">in case parsing fails</exception>
        private void ParseInObjectColonState(JsonToken token)
        {
            EnsureTokenIsNotNull(token, UNTERMINATED_JSON_OBJECT_ERROR);
            EnsureTopLevelElementIsAJsonObject();

            switch (token.TokenType)
            {
            case JsonTokenType.VALUE_NUMBER:     // fallthrough
            case JsonTokenType.VALUE_STRING:     // fallthrough
            case JsonTokenType.LITERAL_BOOLEAN:  // fallthrough
            case JsonTokenType.LITERAL_NULL:     // fallthrough
                // simple JSON value as object value
                valueContainerStack.First.Value.LastParsedObjectValue = TokenToSimpleJsonValue(token);
                state = JsonParserState.IN_OBJECT_VALUE;
                break;

            case JsonTokenType.LEFT_BRACE:
                // value is an object
                var jsonObjectDict = new Dictionary <string, JsonValue>();
                valueContainerStack.AddFirst(new JsonValueContainer(JsonObjectValue.FromDictionary(jsonObjectDict),
                                                                    jsonObjectDict));
                stateStack.AddFirst(JsonParserState.IN_OBJECT_VALUE);
                state = JsonParserState.IN_OBJECT_START;
                break;

            case JsonTokenType.LEFT_SQUARE_BRACKET:
                // value is an array
                var jsonValueList = new LinkedList <JsonValue>();
                valueContainerStack.AddFirst(new JsonValueContainer(JsonArrayValue.FromList(jsonValueList),
                                                                    jsonValueList));
                stateStack.AddFirst(JsonParserState.IN_OBJECT_VALUE);
                state = JsonParserState.IN_ARRAY_START;
                break;

            default:
                // any other token
                throw new JsonParserException(
                          UnexpectedTokenErrorMessage(token, "after key-value pair encountered"));
            }
        }
        public void ContainsDelegatesToUnderlyingDictionary()
        {
            // given
            var jsonObjectDict = Substitute.For <IDictionary <string, JsonValue> >();

            jsonObjectDict.ContainsKey(Arg.Any <string>()).Returns(true);
            var target = JsonObjectValue.FromDictionary(jsonObjectDict);

            // when
            var obtained = target.ContainsKey("foo");

            // then
            Assert.That(obtained, Is.True);
            _ = jsonObjectDict.Received(1).ContainsKey("foo");

            // and when
            obtained = target.ContainsKey("bar");

            // then
            Assert.That(obtained, Is.True);
            _ = jsonObjectDict.Received(1).ContainsKey("bar");
        }
        public void IndexerDelegatesToUnderlyingDictionary()
        {
            // given
            var valueOne = Substitute.For <JsonValue>();
            var valueTwo = Substitute.For <JsonValue>();

            var jsonObjectDict = new Dictionary <string, JsonValue> {
                ["foo"] = valueOne, ["bar"] = valueTwo
            };

            var target = JsonObjectValue.FromDictionary(jsonObjectDict);

            // when
            var obtained = target["foo"];

            // then
            Assert.That(obtained, Is.Not.Null.And.SameAs(valueOne));

            // and when
            obtained = target["bar"];

            // then
            Assert.That(obtained, Is.Not.Null.And.SameAs(valueTwo));
        }
 public void FromDictionaryReturnsNullIfArgumentIsNull()
 {
     // when constructed with null
     Assert.That(JsonObjectValue.FromDictionary(null), Is.Null);
 }