Refactor and improvements.

pull/112/head
Halil İbrahim Kalkan 8 years ago
parent 506be99144
commit f5d7070cf7

@ -31,7 +31,7 @@ namespace Volo.Abp.Http.Client.DynamicProxying
{ {
GenericInterceptAsyncMethod = typeof(DynamicHttpProxyInterceptor<TService>) GenericInterceptAsyncMethod = typeof(DynamicHttpProxyInterceptor<TService>)
.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance) .GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
.First(m => m.Name == nameof(InterceptAsync) && m.IsGenericMethodDefinition); .First(m => m.Name == nameof(MakeRequestAndGetResultAsync) && m.IsGenericMethodDefinition);
} }
public DynamicHttpProxyInterceptor( public DynamicHttpProxyInterceptor(
@ -48,9 +48,17 @@ namespace Volo.Abp.Http.Client.DynamicProxying
public override void Intercept(IAbpMethodInvocation invocation) public override void Intercept(IAbpMethodInvocation invocation)
{ {
//TODO: Handle this differently because InterceptAsync assumes that given method is async! if (invocation.Method.ReturnType == typeof(void))
{
AsyncHelper.RunSync(() => InterceptAsync(invocation)); AsyncHelper.RunSync(() => MakeRequest(invocation));
}
else
{
invocation.ReturnValue = _jsonSerializer.Deserialize(
invocation.Method.ReturnType,
AsyncHelper.RunSync(() => MakeRequest(invocation))
);
}
} }
public override Task InterceptAsync(IAbpMethodInvocation invocation) public override Task InterceptAsync(IAbpMethodInvocation invocation)
@ -67,19 +75,9 @@ namespace Volo.Abp.Http.Client.DynamicProxying
return Task.CompletedTask; return Task.CompletedTask;
} }
private async Task<T> InterceptAsync<T>(IAbpMethodInvocation invocation) private async Task<T> MakeRequestAndGetResultAsync<T>(IAbpMethodInvocation invocation)
{ {
var content = await MakeRequest(invocation); return _jsonSerializer.Deserialize<T>(await MakeRequest(invocation));
var result = JsonConvert.DeserializeObject(
content,
typeof(T),
new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
return (T)result;
} }
private async Task<string> MakeRequest(IAbpMethodInvocation invocation) private async Task<string> MakeRequest(IAbpMethodInvocation invocation)

@ -1,9 +1,13 @@
namespace Volo.Abp.Json using System;
namespace Volo.Abp.Json
{ {
public interface IJsonSerializer public interface IJsonSerializer
{ {
string Serialize(object obj, bool camelCase = true, bool indented = false); string Serialize(object obj, bool camelCase = true, bool indented = false);
T Deserialize<T>(string jsonString); T Deserialize<T>(string jsonString, bool camelCase = true);
object Deserialize(Type type, string jsonString, bool camelCase = true);
} }
} }

@ -1,3 +1,4 @@
using System;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
@ -13,33 +14,38 @@ namespace Volo.Abp.Json.Newtonsoft
_dateTimeConverter = dateTimeConverter; _dateTimeConverter = dateTimeConverter;
} }
public string Serialize(object obj, bool camelCase = false, bool indented = false) public string Serialize(object obj, bool camelCase = true, bool indented = false)
{ {
var serializerSettings = CreateDefaultSerializerSettings(); return JsonConvert.SerializeObject(obj, CreateSerializerSettings(camelCase, indented));
}
public T Deserialize<T>(string jsonString, bool camelCase = true)
{
return JsonConvert.DeserializeObject<T>(jsonString, CreateSerializerSettings(camelCase));
}
public object Deserialize(Type type, string jsonString, bool camelCase = true)
{
return JsonConvert.DeserializeObject(jsonString, type, CreateSerializerSettings(camelCase));
}
protected virtual JsonSerializerSettings CreateSerializerSettings(bool camelCase = true, bool indented = false)
{
var settings = new JsonSerializerSettings();
settings.Converters.Insert(0, _dateTimeConverter);
if (camelCase) if (camelCase)
{ {
serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
} }
if (indented) if (indented)
{ {
serializerSettings.Formatting = Formatting.Indented; settings.Formatting = Formatting.Indented;
} }
return JsonConvert.SerializeObject(obj, serializerSettings);
}
protected virtual JsonSerializerSettings CreateDefaultSerializerSettings()
{
var settings = new JsonSerializerSettings();
settings.Converters.Insert(0, _dateTimeConverter);
return settings; return settings;
} }
public T Deserialize<T>(string jsonString)
{
return JsonConvert.DeserializeObject<T>(jsonString);
}
} }
} }
Loading…
Cancel
Save