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 BufferWriter <byte>(); formatter.Serialize(buffer, obj1, options); var json = buffer.GetWrittenSpan().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.GetWrittenSpan().ToArray()); Assert.Contains("key", json2); }
public void SerializeTest() { var obj1 = new FormatModel { Age = 18, Name = "laojiu" }; var formatter = new KeyValueFormatter(); var kvs = formatter.Serialize("pName", obj1, null) .ToDictionary(item => item.Key, item => item.Value); Assert.True(kvs.Count == 2); Assert.True(kvs["Name"] == "laojiu"); Assert.True(kvs["Age"] == "18"); kvs = formatter.Serialize("pName", 30, null) .ToDictionary(item => item.Key, item => item.Value); Assert.True(kvs.Count == 1); Assert.True(kvs["pName"] == "30"); var dic = new System.Collections.Concurrent.ConcurrentDictionary <string, object>(); dic.TryAdd("Key", "Value"); var options = HttpApiOptions.CreateDefaultJsonOptions(); var kvs2 = formatter.Serialize("dic", dic, options); Assert.True(kvs2.First().Key == "key"); Assert.True(formatter.Serialize("null", null, null).Any()); }
/// <summary> /// 创建OAuth客户端 /// </summary> /// <returns></returns> protected IOAuthClient CreateOAuthClient() { var options = new HttpApiOptions(); options.KeyValueSerializeOptions.IgnoreNullValues = true; var client = this.services.GetRequiredService <IHttpClientFactory>().CreateClient(); return(HttpApi.Create <IOAuthClient>(client, services, options)); }
private static HttpContext GetHttpContext() { var services = new ServiceCollection(); var requestServices = services.BuildServiceProvider(); var options = new HttpApiOptions() { HttpHost = new Uri("http://www.webapi.com/") }; var httpClientContext = new HttpClientContext(new HttpClient(), requestServices, options, string.Empty); return(new HttpContext(httpClientContext, new HttpApiRequestMessageImpl())); }
private static HttpContext GetHttpContext() { var services = new ServiceCollection(); services.TryAddSingleton <IXmlSerializer, XmlSerializer>(); services.TryAddSingleton <IJsonSerializer, JsonSerializer>(); services.TryAddSingleton <IKeyValueSerializer, KeyValueSerializer>(); services.TryAddSingleton <IResponseCacheProvider, ResponseCacheProvider>(); var requestServices = services.BuildServiceProvider(); var options = new HttpApiOptions() { HttpHost = new Uri("http://www.webapi.com/") }; return(new HttpContext(new HttpClient(), requestServices, options)); }
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 void ReadWriteTest() { var options = HttpApiOptions.CreateDefaultJsonOptions(); var obj1 = new FormatModel { Age = 18, Name = "老九" }; var formatter = new WebApiClientCore.Defaults.JsonFormatter(); var json = formatter.Serialize(obj1, options); 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"); var json2 = Encoding.UTF8.GetString(formatter.Serialize(dic, options)); Assert.Contains("key", json2); }
public void JsonStringReadWriteTest() { var options = HttpApiOptions.CreateDefaultJsonOptions(); var b = new JsonString <B>(new B()); var json1 = JsonSerializer.Serialize(b, options); var json2 = JsonSerializer.Serialize(b.Value, options); var json3 = JsonSerializer.Serialize(json2, options); Assert.Equal(json1, json3); var b2 = JsonSerializer.Deserialize(json3, typeof(JsonString <B>), options) as JsonString <B>; Assert.Equal("name", b2.Value.Name); var a = new A(); var aJson = JsonSerializer.Serialize(a, options); var a2 = JsonSerializer.Deserialize(aJson, typeof(A), options) as A; Assert.Equal(a.Age, a2.Age); Assert.Equal(a.B.Value.Name, a2.B.Value.Name); }
/// <summary> /// OAuth2的Token客户端 /// </summary> /// <param name="httpClientFactory"></param> /// <param name="httpApiOptionsMonitor"></param> public OAuth2TokenClient(IHttpClientFactory httpClientFactory, IOptionsMonitor <HttpApiOptions> httpApiOptionsMonitor) { this.httpClientFactory = httpClientFactory; this.httpApiOptions = httpApiOptionsMonitor.Get(HttpApi.GetName(typeof(OAuth2TokenClient))); }