2

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 2

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

I'm sure this is something you've considered but PLEASE add an option for case-sensitive deserialization because although it is possible it seems that 10years later using another deserializer is really the only practical solution.
@LorneCash wont happen, reason for case insensitive deserialization is accommodating poor data which has no business in a typed class with public properties and as a result have no intentions increasing complexity or perf cost by adding an additional option. My answer shows different ways to cater for poor data, otherwise you'll need to use a different serializer.
0

You can set JSON mapping to be case insensitive, so mapping will work regardless of the case

var options = new JsonSerializerOptions
{
    PropertyNameCaseInsensitive = true
};
var weatherForecast = JsonSerializer.Deserialize<WeatherForecast>(jsonString, options);

https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-character-casing

1 Comment

I'm aware of System.Text.Json, my question is if ServiceStack can be forced to be case sensitive because it isn't by default.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.