internal static Dictionary ToDictionary()

in src/Util/JsonUtils.cs [43:81]


        internal static Dictionary<string,string> ToDictionary(string json)
        {
            var dictionary = new Dictionary<string,string>();
            byte[] bytes = Encoding.UTF8.GetBytes(json);
            var reader = new Utf8JsonReader(new ReadOnlySpan<byte>(bytes));

            reader.Read();
            if (reader.TokenType != JsonTokenType.StartObject)
                throw new InvalidDataException("Key value pair JSON must start with an object.");

            // Read to the first property
            reader.Read();

            while(reader.TokenType != JsonTokenType.EndObject)
            {
                if (reader.TokenType != JsonTokenType.PropertyName)
                    throw new InvalidDataException("Key value pair JSON missing property name.");

                var key = reader.GetString();

                reader.Read();
                if (reader.TokenType != JsonTokenType.String)
                    throw new InvalidDataException("Key value pair JSON must only have string values.");

                var value = reader.GetString();

                // To make the existence checks easier in this library don't include null values.
                // That way rest of the library just needs to do ContainsKey check.
                if (value != null)
                {
                    dictionary[key] = value;
                }

                // Move to the next property or end of object
                reader.Read();
            }

            return dictionary;
        }