Пример #1
0
        public static List <object> JsonLabReaderListLoop(ref Utf8Json.Reader json)
        {
            List <object> arrayList = new List <object>();

            object value = null;

            while (json.Read())
            {
                JsonTokenType       tokenType = json.TokenType;
                ReadOnlySpan <byte> valueSpan = json.Value;
                switch (tokenType)
                {
                case JsonTokenType.True:
                case JsonTokenType.False:
                    value = valueSpan[0] == 't';
                    arrayList.Add(value);
                    break;

                case JsonTokenType.Number:
                    value = json.GetValueAsNumber();
                    arrayList.Add(value);
                    break;

                case JsonTokenType.String:
                    value = json.GetValueAsString();
                    arrayList.Add(value);
                    break;

                case JsonTokenType.Null:
                    value = null;
                    arrayList.Add(value);
                    break;

                case JsonTokenType.StartObject:
                    value = JsonLabReaderDictionaryLoop(ref json);
                    arrayList.Add(value);
                    break;

                case JsonTokenType.StartArray:
                    value = JsonLabReaderListLoop(ref json);
                    arrayList.Add(value);
                    break;

                case JsonTokenType.EndArray:
                    return(arrayList);

                case JsonTokenType.None:
                case JsonTokenType.Comment:
                default:
                    break;
                }
            }
            return(arrayList);
        }
Пример #2
0
        public static object JsonLabReaderLoop(ref Utf8Json.Reader json)
        {
            object root = null;

            while (json.Read())
            {
                JsonTokenType       tokenType = json.TokenType;
                ReadOnlySpan <byte> valueSpan = json.Value;
                switch (tokenType)
                {
                case JsonTokenType.True:
                case JsonTokenType.False:
                    root = valueSpan[0] == 't';
                    break;

                case JsonTokenType.Number:
                    root = json.GetValueAsNumber();
                    break;

                case JsonTokenType.String:
                    root = json.GetValueAsString();
                    break;

                case JsonTokenType.Null:
                    break;

                case JsonTokenType.StartObject:
                    root = JsonLabReaderDictionaryLoop(ref json);
                    break;

                case JsonTokenType.StartArray:
                    root = JsonLabReaderListLoop(ref json);
                    break;

                case JsonTokenType.EndObject:
                case JsonTokenType.EndArray:
                    break;

                case JsonTokenType.None:
                case JsonTokenType.Comment:
                default:
                    break;
                }
            }
            return(root);
        }
Пример #3
0
        public static Dictionary <string, object> JsonLabReaderDictionaryLoop(ref Utf8Json.Reader json)
        {
            Dictionary <string, object> dictionary = new Dictionary <string, object>();

            string key   = "";
            object value = null;

            while (json.Read())
            {
                JsonTokenType       tokenType = json.TokenType;
                ReadOnlySpan <byte> valueSpan = json.Value;
                switch (tokenType)
                {
                case JsonTokenType.PropertyName:
                    key = json.GetValueAsString();
                    dictionary.Add(key, null);
                    break;

                case JsonTokenType.True:
                case JsonTokenType.False:
                    value = valueSpan[0] == 't';
                    if (dictionary.TryGetValue(key, out _))
                    {
                        dictionary[key] = value;
                    }
                    else
                    {
                        dictionary.Add(key, value);
                    }
                    break;

                case JsonTokenType.Number:
                    value = json.GetValueAsNumber();
                    if (dictionary.TryGetValue(key, out _))
                    {
                        dictionary[key] = value;
                    }
                    else
                    {
                        dictionary.Add(key, value);
                    }
                    break;

                case JsonTokenType.String:
                    value = json.GetValueAsString();
                    if (dictionary.TryGetValue(key, out _))
                    {
                        dictionary[key] = value;
                    }
                    else
                    {
                        dictionary.Add(key, value);
                    }
                    break;

                case JsonTokenType.Null:
                    value = null;
                    if (dictionary.TryGetValue(key, out _))
                    {
                        dictionary[key] = value;
                    }
                    else
                    {
                        dictionary.Add(key, value);
                    }
                    break;

                case JsonTokenType.StartObject:
                    value = JsonLabReaderDictionaryLoop(ref json);
                    if (dictionary.TryGetValue(key, out _))
                    {
                        dictionary[key] = value;
                    }
                    else
                    {
                        dictionary.Add(key, value);
                    }
                    break;

                case JsonTokenType.StartArray:
                    value = JsonLabReaderListLoop(ref json);
                    if (dictionary.TryGetValue(key, out _))
                    {
                        dictionary[key] = value;
                    }
                    else
                    {
                        dictionary.Add(key, value);
                    }
                    break;

                case JsonTokenType.EndObject:
                    return(dictionary);

                case JsonTokenType.None:
                case JsonTokenType.Comment:
                default:
                    break;
                }
            }
            return(dictionary);
        }