Beispiel #1
0
        /// <summary>
        /// 获取键值对
        /// </summary>
        /// <param name="key">对象名称</param>
        /// <param name="obj">对象实例</param>
        /// <param name="objType">对象类型</param>
        /// <param name="options">选项</param>
        /// <returns></returns>
        private static IList <KeyValue> GetKeyValueList(string key, object obj, Type objType, KeyValueSerializerOptions options)
        {
            var jsonOptions = options.GetJsonSerializerOptions();

            using var bufferWriter   = new RecyclableBufferWriter <byte>();
            using var utf8JsonWriter = new Utf8JsonWriter(bufferWriter, new JsonWriterOptions
            {
                Indented       = false,
                SkipValidation = true,
                Encoder        = jsonOptions.Encoder
            });

            System.Text.Json.JsonSerializer.Serialize(utf8JsonWriter, obj, objType, jsonOptions);
            var utf8JsonReader = new Utf8JsonReader(bufferWriter.WrittenSpan, new JsonReaderOptions
            {
                MaxDepth            = jsonOptions.MaxDepth,
                CommentHandling     = jsonOptions.ReadCommentHandling,
                AllowTrailingCommas = jsonOptions.AllowTrailingCommas,
            });

            if (options.KeyNamingStyle == KeyNamingStyle.ShortName)
            {
                return(GetShortNameKeyValueList(key, ref utf8JsonReader));
            }
            else
            {
                return(GetFullNameKeyValueList(key, ref utf8JsonReader, options));
            }
        }
Beispiel #2
0
        /// <summary>
        /// 获取键值对
        /// </summary>
        /// <param name="key">对象名称</param>
        /// <param name="obj">对象实例</param>
        /// <param name="objType">对象类型</param>
        /// <param name="options">选项</param>
        /// <returns></returns>
        private IList <KeyValue> GetKeyValueList(string key, object obj, Type objType, KeyValueSerializerOptions options)
        {
            var jsonOptions = options.GetJsonSerializerOptions();

            using var bufferWriter   = new BufferWriter <byte>();
            using var utf8JsonWriter = new Utf8JsonWriter(bufferWriter, new JsonWriterOptions
            {
                Indented       = false,
                SkipValidation = true,
                Encoder        = jsonOptions.Encoder
            });

            System.Text.Json.JsonSerializer.Serialize(utf8JsonWriter, obj, objType, jsonOptions);
            var utf8JsonReader = new Utf8JsonReader(bufferWriter.GetWrittenSpan(), new JsonReaderOptions
            {
                MaxDepth            = jsonOptions.MaxDepth,
                CommentHandling     = jsonOptions.ReadCommentHandling,
                AllowTrailingCommas = jsonOptions.AllowTrailingCommas,
            });

            return(this.GetKeyValueList(key, ref utf8JsonReader, options));
        }