Beispiel #1
0
        /// <summary>
        /// 解析JsonElement
        /// </summary>
        /// <param name="key"></param>
        /// <param name="element"></param>
        /// <param name="list"></param>
        /// <param name="options"></param>
        /// <param name="withRoot"></param>
        private static void ParseJsonElement(string key, ref JsonElement element, IList <KeyValue> list, KeyNamingOptions options, bool withRoot = true)
        {
            switch (element.ValueKind)
            {
            case JsonValueKind.Null:
                list.Add(new KeyValue(key, null));
                break;

            case JsonValueKind.String:
                list.Add(new KeyValue(key, element.GetString()));
                break;

            case JsonValueKind.True:
            case JsonValueKind.False:
            case JsonValueKind.Number:
                list.Add(new KeyValue(key, element.GetRawText()));
                break;

            case JsonValueKind.Object:
                foreach (var item in element.EnumerateObject())
                {
                    var ele     = item.Value;
                    var itemKey = withRoot
                            ? $"{key}{options.KeyDelimiter}{item.Name}"
                            : item.Name;

                    ParseJsonElement(itemKey, ref ele, list, options);
                }
                break;

            case JsonValueKind.Array:
                var index = 0;
                foreach (var item in element.EnumerateArray())
                {
                    var ele     = item;
                    var itemKey = $"{key}{options.KeyArrayIndex(index)}";

                    ParseJsonElement(itemKey, ref ele, list, options);
                    index += 1;
                }
                break;
            }
        }
Beispiel #2
0
 /// <summary>
 /// 获取键值对
 /// </summary>
 /// <param name="key">根键名</param>
 /// <param name="reader">utf8JsonReader</param>
 /// <param name="options">选项</param>
 /// <returns></returns>
 protected virtual IList <KeyValue> GetKeyValueList(string key, ref Utf8JsonReader reader, KeyNamingOptions options)
 {
     if (options.KeyNamingStyle == KeyNamingStyle.ShortName)
     {
         return(GetShortNameKeyValueList(key, ref reader));
     }
     else
     {
         return(GetFullNameKeyValueList(key, ref reader, options));
     }
 }
Beispiel #3
0
        /// <summary>
        /// 获取fullName键值对
        /// </summary>
        /// <param name="key"></param>
        /// <param name="reader"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        private static IList <KeyValue> GetFullNameKeyValueList(string key, ref Utf8JsonReader reader, KeyNamingOptions options)
        {
            using var doc = JsonDocument.ParseValue(ref reader);
            var root     = doc.RootElement;
            var list     = new List <KeyValue>();
            var withRoot = options.KeyNamingStyle == KeyNamingStyle.FullNameWithRoot;

            ParseJsonElement(key, ref root, list, options, withRoot);
            return(list);
        }