Exemplo n.º 1
0
        public void KeyNamingStyleTest()
        {
            var formatter = new KeyValueSerializer();
            var model     = new { x = new { y = 1 } };

            var value = formatter.Serialize("root", model, null).First();

            Assert.Equal("y", value.Key);

            value = formatter.Serialize("root", model, new KeyValueSerializerOptions
            {
                KeyNamingStyle = KeyNamingStyle.FullName
            }).First();
            Assert.Equal("x.y", value.Key);


            value = formatter.Serialize("root", model, new KeyValueSerializerOptions
            {
                KeyNamingStyle = KeyNamingStyle.FullName,
                KeyDelimiter   = "|"
            }).First();
            Assert.Equal("x|y", value.Key);


            value = formatter.Serialize("root", model, new KeyValueSerializerOptions
            {
                KeyNamingStyle = KeyNamingStyle.FullNameWithRoot,
                KeyDelimiter   = "-"
            }).First();
            Assert.Equal("root-x-y", value.Key);
        }
Exemplo n.º 2
0
 /// <summary>
 /// 键值对表单内容
 /// </summary>
 /// <param name="value">模型对象值</param>
 /// <param name="options">序列化选项</param>
 public FormContent(object?value, KeyValueSerializerOptions?options)
 {
     if (value != null)
     {
         var keyValues = KeyValueSerializer.Serialize(nameof(value), value, options);
         this.AddFormField(keyValues);
     }
     this.Headers.ContentType = new MediaTypeHeaderValue(MediaType);
 }
Exemplo n.º 3
0
        public void ArrayIndexFormatTest()
        {
            var formatter = new KeyValueSerializer();
            var model     = new { x = new { y = new[] { 1, 2 } } };

            var kv = formatter.Serialize("root", model, new KeyValueSerializerOptions
            {
                KeyNamingStyle = KeyNamingStyle.FullName
            }).First();

            Assert.Equal("x.y[0]", kv.Key);
            Assert.Equal("1", kv.Value);

            kv = formatter.Serialize("root", model, new KeyValueSerializerOptions
            {
                KeyNamingStyle = KeyNamingStyle.FullName,
                KeyArrayIndex  = (i) => $"({i})"
            }).First();
            Assert.Equal("x.y(0)", kv.Key);
            Assert.Equal("1", kv.Value);
        }
        public void Serialize()
        {
            var keys = serializer.Serialize(data).ToArray();

            Assert.AreEqual(5, keys.Length);
            var result = serializer.Deserialize(keys);

            Assert.AreEqual(data.Status1, result.Status1);
            Assert.AreEqual(data.Data, result.Data);
            Assert.AreEqual(data.Value, result.Value);
            Assert.AreEqual(data.Another, result.Another);
            Assert.AreEqual(data.Date, result.Date);
        }
        public void SerializeTest()
        {
            var obj1 = new FormatModel {
                Age = 18, Name = "lao九"
            };

            var options = new HttpApiOptions().KeyValueSerializeOptions;

            var kvs = KeyValueSerializer.Serialize("pName", obj1, options)
                      .ToDictionary(item => item.Key, item => item.Value, StringComparer.OrdinalIgnoreCase);

            Assert.True(kvs.Count == 2);
            Assert.True(kvs["Name"] == "lao九");
            Assert.True(kvs["Age"] == "18");


            kvs = KeyValueSerializer.Serialize("pName", 30, null)
                  .ToDictionary(item => item.Key, item => item.Value);

            Assert.True(kvs.Count == 1);
            Assert.True(kvs["pName"] == "30");

            var bools = KeyValueSerializer.Serialize("bool", true, null);

            Assert.Equal("true", bools[0].Value);

            var strings = KeyValueSerializer.Serialize("strings", "string", null);

            Assert.Equal("string", strings[0].Value);


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

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

            var kvs2 = KeyValueSerializer.Serialize("dic", dic, options);

            Assert.True(kvs2.First().Key == "key");


            Assert.True(KeyValueSerializer.Serialize("null", null, null).Any());
        }
        public async Task OnRequestAsyncTest()
        {
            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 PathQueryAttribute();
            await attr.OnRequestAsync(new ApiParameterContext(context, 0));

            var birthday = KeyValueSerializer.Serialize("time", DateTime.Parse("2010-10-10"), null)[0].Value;
            var target   = new Uri("http://www.webapi.com?name=laojiu&birthDay=" + Uri.EscapeDataString(birthday));

            Assert.True(context.HttpContext.RequestMessage.RequestUri == target);
        }
Exemplo n.º 7
0
        public async Task OnRequestAsyncTest()
        {
            var apiAction = new DefaultApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"));
            var context   = new TestRequestContext(apiAction, new
            {
                name     = "老 九",
                birthDay = DateTime.Parse("2010-10-10")
            });

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

            var attr = new FormContentAttribute();
            await attr.OnRequestAsync(new ApiParameterContext(context, 0));

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

            var time   = KeyValueSerializer.Serialize("time", DateTime.Parse("2010-10-10"), null);
            var target = $"name={HttpUtility.UrlEncode("老 九", Encoding.UTF8)}&birthDay={HttpUtility.UrlEncode(time[0].Value, Encoding.UTF8)}";

            Assert.True(body.ToUpper() == target.ToUpper());
        }
Exemplo n.º 8
0
        /// <summary>
        /// 序列化参数值为键值对
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static IList <KeyValue> SerializeToKeyValues(this ApiParameterContext context)
        {
            var options = context.HttpContext.HttpApiOptions.KeyValueSerializeOptions;

            return(KeyValueSerializer.Serialize(context.ParameterName, context.ParameterValue, options));
        }
Exemplo n.º 9
0
 public void OnSerialize(KeyValueSerializer serializer)
 {
     serializer.Serialize("EventName", ref animationGameEvent);
     serializer.Serialize("Argument", ref Argument);
     //	serializer.Serialize("ENABLEATTACK_EVENTNUM", ref enableAttackEventNum);
 }