RestSharp序列化到JSON,符合市场预期不使用对象SerializeAs属性 [英] RestSharp serialization to JSON, object is not using SerializeAs attribute as expected

查看:740
本文介绍了RestSharp序列化到JSON,符合市场预期不使用对象SerializeAs属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的 RestSharp (通过的NuGet版本104.4)来调用一个REST Web服务。我设计了一组对象(PO​​CO)相匹配的API中的资源。然而,我的对象的属性名称不匹配的发布数据时预期的REST服务,所以我想改造他们,当我提出一个请求到REST服务,以使它们相匹配的比赛。我读了我的POCO的属性添加 SerializeAs 属性(如果指定了名称),会令他们正确的序列,但它不会。

I am using RestSharp (version 104.4 via NuGet) to make calls to a Rest Web Service. I have designed a set of objects (POCO) which matches resources exposed in the API. However, my objects property names does not match those expected by the Rest Service when posting data, so I would like to "transform" them when I make a request to the Rest service to make them match match. I read that adding SerializeAs attribute (with a Name specified) on my POCO's property will make them serialize correctly, but it won't.

我的POCO

Imports RestSharp.Serializers

<Serializable(), SerializeAs(Name:="ApiMember")>
Public Class ApiMember
    <SerializeAs(Name:="id")>
    Public Property Id As Integer?

    <SerializeAs(Name:="email")>
    Public Property EmailAddress As String

    <SerializeAs(Name:="firstname")>
    Public Property Firstname As String

    <SerializeAs(Name:="lastname")>
    Public Property Lastname As String
End Class

我的呼叫转移到API

Dim request As RestRequest = New RestRequest(Method.POST)
Dim member As ApiMember = new ApiMember()

member.EmailAddress = "me@example.com"

request.Resource = "members"
request.RequestFormat = DataFormat.Json
request.AddBody(member)

Dim client As RestClient = New RestClient()
client.BaseUrl = "http://url.com"
client.Authenticator = New HttpBasicAuthenticator("username", "password")
client.Execute(Of ApiGenericResponse)(request)

什么最终被发布

{"Id":null,"EmailAddress":"me@example.com","Firstname":null,"Lastname":null}

注意不匹配 SerializeAs 放入系统我指定的属性的名称(uppercases,EmailAddress的名称)

Notice the name of the properties does not match thoses I specified in SerializeAs (uppercases, name of EmailAddress)

我缺少的东西?

推荐答案

这是@MaxiWheat,任何人在如何使用 JSON.NET 作为JSON序列在RestSharp要求别人感兴趣。基本上,我用由帕特里克·赖利这篇博客文章:

This is for @MaxiWheat and anyone else interested in how to use JSON.NET as the JSON serializer in a RestSharp request. Basically, I used the approach described in this blog post by Patrick Riley:

// create the request
var request = new RestRequest(yourUrlHere, Method.POST) { RequestFormat = DataFormat.Json };

// attach the JSON.NET serializer for RestSharp
restRequest.JsonSerializer = new RestSharpJsonNetSerializer();

RestSharpJsonNetSerializer 是一个实现从JSON.NET家伙(约翰·希恩)的<一(小于100线$​​ C $ C) href="https://github.com/restsharp/RestSharp/blob/86b31f9adf049d7fb821de8279154f41a17b36f7/RestSharp/Serializers/JsonSerializer.cs">can在他们的Github上页找到

and the RestSharpJsonNetSerializer is an implementation (less than 100 lines of code) from the JSON.NET guys (John Sheehan) that can be found on their Github pages

通过这种设置,我的问题走了,我能有一个适当的DTO漂亮驼峰性能,而序列化的JSON使用它们中的所有小写。

With this setup, my problems went away and I was able to have a proper DTO with nice CamelCase properties, while the serialized JSON uses them in all "lowercase".

这篇关于RestSharp序列化到JSON,符合市场预期不使用对象SerializeAs属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆