Example #1
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"));
            }
        }
Example #2
0
        /// <summary>
        ///     Utility method to parse the start of a nested array.
        ///     <para>
        ///         This method is called when the left square bracket token is encountered.
        ///     </para>
        /// </summary>
        private void ParseStartOfNestedArray()
        {
            stateStack.AddFirst(JsonParserState.IN_ARRAY_VALUE);
            var jsonValueList = new LinkedList <JsonValue>();

            valueContainerStack.AddFirst(new JsonValueContainer(JsonArrayValue.FromList(jsonValueList), jsonValueList));
            state = JsonParserState.IN_ARRAY_START;
        }
Example #3
0
        public void SingleElementListJsonString()
        {
            ICollection <JsonValue> list = new Collection <JsonValue>();

            list.Add(JsonStringValue.FromString("Test"));

            Assert.That(JsonArrayValue.FromList(list).ToString(), Is.EqualTo("[\"Test\"]"));
        }
Example #4
0
        public string Build()
        {
            if (overriddenKeys.Count > 0)
            {
                AddNonOverridableAttribute("dt.overridden_keys", JsonArrayValue.FromList(overriddenKeys));
            }

            return(JsonObjectValue.FromDictionary(attributes).ToString());
        }
        public void CountDelegatesCallToUnderlyingList()
        {
            // given
            var jsonValues = Substitute.For <ICollection <JsonValue> >();

            jsonValues.Count.Returns(42);
            var target = JsonArrayValue.FromList(jsonValues);

            // when
            var obtained = target.Count;

            // then
            Assert.That(obtained, Is.EqualTo(42));
            _ = jsonValues.Received(1).Count;
        }
        public void GetEnumeratorDelegatesTheCallToTheUnderlyingList()
        {
            // given
            var enumerator           = Substitute.For <IEnumerator <JsonValue> >();
            var jsonValues           = Substitute.For <ICollection <JsonValue> >();
            var jsonValuesEnumerable = ((IEnumerable <JsonValue>)jsonValues);

            jsonValuesEnumerable.GetEnumerator().Returns(enumerator);

            IEnumerable <JsonValue> target = JsonArrayValue.FromList(jsonValues);

            // when
            var obtained = target.GetEnumerator();

            //then
            Assert.That(obtained, Is.Not.Null);
            using (jsonValues.Received(1).GetEnumerator()) { /* nothing, just to get rid of warning */ }
        }
Example #7
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 FromListGivesNullIfArgumentIsNull()
 {
     // when null, then
     Assert.That(JsonArrayValue.FromList(null), Is.Null);
 }
 public void JsonValueTypeIsArray()
 {
     // when, then
     Assert.That(JsonArrayValue.FromList(EmptyList).ValueType, Is.EqualTo(JsonValueType.ARRAY));
 }
Example #10
0
 public void EmptyListJsonString()
 {
     Assert.That(JsonArrayValue.FromList(EmptyList).ToString(), Is.EqualTo("[]"));
 }