【问题标题】:Generic Method Json Parsing通用方法 Json 解析
【发布时间】:2019-04-20 16:17:49
【问题描述】:

我有这个通用方法,用于执行 Post 请求,然后像这样解析响应。

private async Task<object> PostAsync<T1,T2>(string uri, T2 content)
{
    using (var requestMessage = new HttpRequestMessage(HttpMethod.Post, uri))
    {
        var json = JsonConvert.SerializeObject(content);
        using (var stringContent = new StringContent(json, Encoding.UTF8, "application/json"))
        {
            requestMessage.Content = stringContent;

            HttpResponseMessage response = await _client.SendAsync(requestMessage);
            if (response.IsSuccessStatusCode)
            {
                _logger.LogInformation("Request Succeeded");

                T1 responseModel = JsonConvert.DeserializeObject<T1>(await response.Content.ReadAsStringAsync());
                return  responseModel;
            }
            else
            {
                return await GetFailureResponseModel(response);

            }
        }
    }
}

现在的问题是一些 Post 请求响应在 SnakeCase 中,而另一些在 CamelCase 中。我该如何解决这个问题。

【问题讨论】:

    标签: c# json asp.net-core


    【解决方案1】:

    鉴于您在编译时知道何时知道 snake_case 以及何时需要默认策略,您可以这样做:

    private Task<object> PostAsync<T1, T2>(string uri, T2 content)
    {
        return PostAsync<T1, T2>(uri, content, new DefaultNamingStrategy());
    }
    
    private async Task<object> PostAsync<T1, T2>(string uri, T2 content, NamingStragy namingStrategy)
    {
        using (var requestMessage = new HttpRequestMessage(HttpMethod.Post, uri))
        {
            var json = JsonConvert.SerializeObject(content);
            using (var stringContent = new StringContent(json, Encoding.UTF8, "application/json"))
            {
                requestMessage.Content = stringContent;
    
                HttpResponseMessage response = await _client.SendAsync(requestMessage);
                if (response.IsSuccessStatusCode)
                {
                    _logger.LogInformation("Request Succeeded");
    
                    var deserializerSettings = new JsonSerializerSettings
                    {
                        ContractResolver = new DefaultContractResolver
                        {
                            NamingStrategy = namingStrategy
                        }
                    };
    
                    T1 responseModel = JsonConvert.DeserializeObject<T1>(await response.Content.ReadAsStringAsync(), deserializerSettings);
                    return responseModel;
                }
                else
                {
                    return await GetFailureResponseModel(response);
    
                }
            }
        }
    }
    

    所以,当你需要默认策略时:

    await PostAsync<Some1, Some2>(uri, some2Content);
    

    并且,当您需要蛇形外壳时:

    await PostAsync<Some1, Some2>(uri, some2Content, new SnakeCaseNamingStrategy());
    

    【讨论】:

    • @camiloTervinto 我已经接受了你的回答并实现了,但是你不认为在通用方法中传递命名策略是代码异味吗?
    • @Shabirjan 一点也不,这是一种很常见的模式,你可以在 .NET Framework 源代码中找到很多
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-20
    • 2012-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多