Using ServiceStack Client with Case Sensitive REST Service

Viewed 97

My code is using a DataContract with DataMember in the response DTO and I'm attempting to consume a REST service with this response but it's not working:

{
 "results": {
  "p": 277.76,
  "s": 1,
  "x": 11,
  "P": 34.95,
  "S": 3,
  "X": 51,
  "z": 3,
  "T": "ABCD",
  "t": 1625874938819163000,
  "y": 1625874938819148500,
  "q": 55320377
 },
 "status": "OK",
 "request_id": "e16e9db61563c8f675d5400f6c9fd8c9"
}

As you can see the names are case sensitive and I'm pretty sure that is the reason. I found this article from 9+ years ago that states:

As for ServiceStack's JSON Serializer, in the latest release - the properties are case-insensitive...

So the questions if it wasn't obvious, is there's a way to work with a case sensitive names. Hopefully its just a config option that I need to set or something simple.

2 Answers

ServiceStack.Text Json Serailizer properties is case-insensitive, so you would need to use a Dictionary instead to be able to capture properties with different names, e.g:

class ApiResponse
{
    public Dictionary<string,string> Results { get; set; }
}

Alternatively you can use HTTP Utils to consume the 3rd Party API then parse the adhoc JSON with JS Utils which will deserialize it into the appropriate collection type and also preserve the types that is was returned with, e.g:

string json = url.GetJsonFromUrl();
var obj = (Dictionary<string,object>)JSON.parse(json);

obj["T"] //= "ABCD"
obj["t"] //= 1625874938819163000
Related