コード例 #1
0
        public async Task BeforeRequestAsyncTest()
        {
            var apiAction = new DefaultApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"));
            var context   = new TestRequestContext(apiAction, new
            {
                name     = "laojiu",
                birthDay = DateTime.Parse("2010-10-10")
            });

            context.HttpContext.RequestMessage.RequestUri = new Uri("http://www.webapi.com/");
            context.HttpContext.RequestMessage.Method     = HttpMethod.Post;

            var attr = new JsonContentAttribute()
            {
                CharSet = "utf-16"
            };
            await attr.OnRequestAsync(new ApiParameterContext(context, 0));

            var body = await context.HttpContext.RequestMessage.Content.ReadAsUtf8ByteArrayAsync();

            var options = context.HttpContext.HttpApiOptions.JsonSerializeOptions;

            using var buffer = new RecyclableBufferWriter <byte>();
            JsonBufferSerializer.Serialize(buffer, context.Arguments[0], options);
            var target = buffer.WrittenSpan.ToArray();

            Assert.True(body.SequenceEqual(target));
        }
コード例 #2
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));
            }
        }
コード例 #3
0
        public void ReadWriteTest()
        {
            var options = new HttpApiOptions().JsonSerializeOptions;

            var obj1 = new FormatModel {
                Age = 18, Name = "老九"
            };
            var formatter = new WebApiClientCore.Serialization.JsonSerializer();

            using var buffer = new RecyclableBufferWriter <byte>();
            formatter.Serialize(buffer, obj1, options);
            var json = buffer.WrittenSpan.ToArray();
            var obj2 = formatter.Deserialize(json, typeof(FormatModel), options);

            Assert.True(obj1.Equals(obj2));

            var dic = new System.Collections.Concurrent.ConcurrentDictionary <string, object>();

            dic.TryAdd("Key", "Value");

            buffer.Clear();

            formatter.Serialize(buffer, dic, options);
            var json2 = Encoding.UTF8.GetString(buffer.WrittenSpan.ToArray());

            Assert.Contains("key", json2);
        }
コード例 #4
0
 public void RecyclableBufferWriter()
 {
     using var writer = new RecyclableBufferWriter <byte>(WritePerSize);
     for (var i = 0; i < WriteCount; i++)
     {
         writer.Write(this.filedValue);
     }
 }
コード例 #5
0
        /// <summary>
        /// json内容
        /// </summary>
        /// <param name="value">对象值</param>
        /// <param name="jsonSerializerOptions">json序列化选项</param>
        /// <param name="encoding">编码</param>
        public JsonContent(object?value, JsonSerializerOptions?jsonSerializerOptions, Encoding?encoding)
            : base(MediaType)
        {
            if (encoding == null || Encoding.UTF8.Equals(encoding))
            {
                JsonBufferSerializer.Serialize(this, value, jsonSerializerOptions);
            }
            else
            {
                using var utf8Writer = new RecyclableBufferWriter <byte>();
                JsonBufferSerializer.Serialize(utf8Writer, value, jsonSerializerOptions);

                Encoding.UTF8.Convert(encoding, utf8Writer.WrittenSpan, this);
                this.Headers.ContentType.CharSet = encoding.WebName;
            }
        }
コード例 #6
0
        public void UrlEncode_BufferWriterTest()
        {
            var str = "c++!=c#,没错!";

            using var writer = new RecyclableBufferWriter <byte>();
            HttpUtil.UrlEncode(str, writer);
            var e1 = Encoding.UTF8.GetString(writer.WrittenSpan);
            var e2 = System.Web.HttpUtility.UrlEncode(str, Encoding.UTF8);

            Assert.Equal(e1, e2);

            str = "abc123";
            writer.Clear();
            HttpUtil.UrlEncode(str, writer);
            e1 = Encoding.UTF8.GetString(writer.WrittenSpan);
            e2 = System.Web.HttpUtility.UrlEncode(str, Encoding.UTF8);
            Assert.Equal(e1, e2);
        }
コード例 #7
0
        public void WriteTest()
        {
            using var writer = new RecyclableBufferWriter <char>(1);
            writer.Write('H');
            Assert.Equal(1, writer.WrittenCount);
            writer.Write('e');
            Assert.Equal(2, writer.WrittenCount);
            writer.Write("llo");
            Assert.True(writer.WrittenSpan.SequenceEqual("Hello"));

            writer.Clear();
            Assert.Equal(0, writer.WrittenCount);

            var span = writer.GetSpan();

            "Word".AsSpan().CopyTo(span);
            writer.Advance(4);
            Assert.True(writer.WrittenSpan.SequenceEqual("Word"));
        }