Restsharp: uppercase/lowercase keys in the serialized JSON

Created on 21 Mar 2013  路  5Comments  路  Source: restsharp/RestSharp

Given the class

``` c#
public class MyClass {
public string MyField {get;set;}
public string MyOtherField {get;set;}
}

the generated JSON will be 

``` javascript
{"MyField": "field value", "MyOtherField": "other field value"}

However, the REST API on the other end of the wire follows another naming convention where the keys are expected to be in lowercase, like

{"myField": "field value", "myOtherField": "other field value"}

Is there an easy way to achieve this without resorting to a custom serializer? It's such a small change that preferrably it would only be a configuration switch somewhere.

Most helpful comment

Use JsonPropertyattribute on the property to specify a different name when serialized!

public class MyClass {
    public string MyField {get;set;}
    [JsonProperty("myOtherField")]
    public string MyOtherField {get;set;}
}

All 5 comments

Use JsonPropertyattribute on the property to specify a different name when serialized!

public class MyClass {
    public string MyField {get;set;}
    [JsonProperty("myOtherField")]
    public string MyOtherField {get;set;}
}

Sounds like what I need, but I can't find this attribute in RestSharp. Do I need to import some other dll? I'm using the MonoTouch version of RestSharp.

Edit: I added the Newtonsoft.Json MonoTouch library and added the attribute, but it was still generating keys with uppercase letters.

I asked about this on the mailing list, and got a response that this is something specific to JSON.NET, not RestSharp. If that's the case, I would like to open this issue as a feature request.

It was opened a feature-request?

it was not. if this feature is needed then RestSharp gives you the ability to use a custom de/serializer so you'd have to use that for now.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mjwsteenbergen picture mjwsteenbergen  路  5Comments

AlexanderSchoenfeld picture AlexanderSchoenfeld  路  3Comments

weswitt picture weswitt  路  3Comments

wojciechrak picture wojciechrak  路  3Comments

DuBistKomisch picture DuBistKomisch  路  6Comments