Selective json deserialization c# and json.net - c#

I am using an API which returns JSON response.
The issue im facing is the way they return their error messages:
They return
["501","Invalid apikey"]
instead of the normal json response which is serialized to :
public class AvailableItemResponse : IResponse
{
public AvailableItemResponse ()
{
AvailableItems = new List<AvailableItem>();
}
public int ResponseId { get; set; }
public string SearchId { get; set; }
public int TotalFound { get; set; }
public List<AvailableItem> AvailableItems { get; set; }
}
How can I check the json format and select which type to deserialze the object to ?
Thanks for your help. :-)

If you are using HttpClient there should be a Code on the object received. Would thing something like the following will help.
if(response.code != 200){
//handle the error
}

The problem in this case is that you don't have a key-value json. You could check for the position of the elements and if it's a fixed parameter you can look at (for example) the first parameter and compare it to the code of the ok message (probably always the same). In this case the ResponseId is 501 so you shouldn't parse the json.
So if first parameter is STATUS_CODE_OK parse the json otherwise not.

Related

How can I deserialize this specific json string?

I am trying to deserialize the following json string using Newtonsoft Json. I am able to get the string successfully, but when I try using JsonConvert.DeserializeObject<ServerList>(response, settings);, the try catch fails.
[
{"endpoint":"127.0.0.1","id":6,"identifiers":["steam:","license:","xbl:","live:","discord:"],"name":"Blurr","ping":160},
{"endpoint":"127.0.0.1","id":7,"identifiers":["steam:","license:","xbl:","live:","discord:"],"name":"Knight","ping":120}
]
I believe my issue is because the players array being unnamed.
I have tried [JsonProperty("")] and [JsonProperty] for the Users var, I have also tried using List and Array instead of IList.
Here is the object. This may be completely wrong, I have tried many ways of doing this.
public class ServerList
{
// I have tried many ways of doing this array/list. This is just the latest way I tried.
[JsonProperty]
public static IList<Player> Users { get; set; }
}
public class Player
{
[JsonProperty("endpoint")]
public static string Endpoint { get; set; }
[JsonProperty("id")]
public static string ServerId { get; set; }
[JsonProperty("identifiers")]
public static IList<string> Identifiers { get; set; }
[JsonProperty("name")]
public static string Name { get; set; }
[JsonProperty("ping")]
public static int Ping { get; set; }
}
I am expecting to get a 'ServerList' object returned with a list of all the players connected to the server.
Ask any questions you need to, I don't often work with json in this format. Thank you in advance!
ERROR: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.
Simplest way: Your json is an array, so deserialize to an array:
JsonConvert.DeserializeObject<Player[]>(response,settings);
As noted in the comments, properties on the Player should not be static.
If you insist on an object structure similar to what you posted, an object with a Usersproperty, even though it's not present in the JSON, that is also possible, by implementing a custom JSON converter. There's an article with an example of that here: https://www.jerriepelser.com/blog/custom-converters-in-json-net-case-study-1/
HOWEVER; I would recommend sticking to the types present in the json, and perhaps later construct the object that makes sense to the model in your program. This way, what you deserialize are true to the json you are getting, but make no sacrifices on the model you'd like:
var players = JsonConvert.DeserializeObject<Player[]>(response,settings);
var serverList = new ServerList {Users = players};

Deserialize JSON object into a class with a "dynamic" property

I am receiving JSON messages that are matched to the following class:
public class Response
{
public int Code { get; }
public string Message { get; }
public string Result { get; }
}
But the value of Result property depends on what we get in Code and does not have a fixed structure.
E.g. if Code = 1, Result will return a list of objects of type X and if Code = 2, Result will return a list of objects of type Y.
When I try to deserialize a message I am receiving, having Result type set to string does not seem to work.
var responseObj = JsonConvert.DeserializeObject<Response>(response);
if (responseObj.Code == 1)
{
return responseObj.Result;
}
The return statement above throws the following exception:
Unexpected character encountered while parsing value: [. Path 'Result'
How can I define my class so that it receives the entire text of Result and I can decide about its deserialization later? Because all my requests respond to with the above structure and I'd like to manage all these requests from the same place.
How can I define my class so that it receives the entire text of Result and I can decide about its deserialization later?
If that is what you want to do then make your Result property a JToken instead of a string as shown below. You will also need to add setters to all of the properties shown in your Response class to allow the deserialization to work properly: you can't deserialize into read-only properties.
public class Response
{
public int Code { get; set; }
public string Message { get; set; }
public JToken Result { get; set; }
}
Once you know what type the Result should be converted into, you can use the ToObject() method to do that. For example:
Response responseObj = JsonConvert.DeserializeObject<Response>(response);
if (responseObj.Code == 1)
{
var listOfX = responseObj.Result.ToObject<List<X>>();
...
}
Fiddle: https://dotnetfiddle.net/pz5m63

Using Newtonsoft JsonConvert to serialize and deserialize simple class

I'm trying to simply serialize and deserialize a simple class with JsonConvert.SerializeObject(obj) and JsonConvert.DeserializeObject(string).
I'm using this in a custom TypeConverter for a QueryParameter in .NET Web API Core 2.1.
But I'm getting very strange behavior. My class looks like this:
public class ListRequestDto {
public bool? WithCreator { get; set; }
public int? Skip { get; set; }
public int? Limit { get; set; }
}
And I'm trying to do the following:
var test = new ListRequestDto {
WithCreator = true,
Skip = 0,
Limit = 15
};
string ttt = JsonConvert.SerializeObject(test);
But I'm getting the following output:
"MyNameSpace.ListRequestDto"
If I try it the other way around:
string json = "{ WithCreator: true, Skip: 0, Limit: 15 }";
JsonConvert.DeserializeObject<ListRequestDto>(json);
I get the following exception:
Newtonsoft.Json.JsonSerializationException: "Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'MyNameSpace.ListRequestDto' because the type requires a JSON string value to deserialize correctly.
To fix this error either change the JSON to a JSON string value or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'Skip', line 1, position 8."
I tried to remove the nullable fields (replacing them with regular primitives) but that resulted int he exact same error.
The strange thing is, that if I provide the same class as a body it works. And as far as I know Web API Core also uses the Newtonsoft Json Parser.
I do not know why it is necessary, but when I put [JsonObject] on my class it suddenly works.
The exception actually told me to do that, but I do not understand why it is necessary when in no documentation this is used in such a case.
So bascially doing the following solved the problem:
[JsonObject]
public class ListRequestDto {
public bool? WithCreator { get; set; }
public int? Skip { get; set; }
public int? Limit { get; set; }
}

Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray'

I am testing my Web API. Mocking the data I have this:
var objs = ((JArray)JsonConvert.DeserializeObject("{ \"PrintId\":10,\"Header\":\"header\",\"TC\":\"tc\",\"CompanyRef\":\"00000000-0000-0000-0000-000000000000\"}")).Values<JObject>();
Which gives me the error:
Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Newtonsoft.Json.Linq.JArray'
The thing is it was working. I must have changed something, but I don't know what.
My intent is to convert this JSON object to a list of .NET objects called Print which has the fields:
PrintId
Header
TX
CompnayRef
Just make a class and deserialize it.
public class Print
{
public int PrintId { get; set; }
public string Header { get; set; }
public string TC { get; set; }
public string CompanyRef { get; set; }
}
Print printObj = JsonConvert.DeserializeObject<Print>(yourJson);
printObj.PrintId = //...
As the message says, your object is JObject so don't cast it to JArray. Try this:
var objs = JsonConvert.DeserializeObject("{ \"PrintId\":10,\"Header\":\"header\",\"TC\":\"tc\",\"CompanyRef\":\"00000000-0000-0000-0000-000000000000\"}");
Update To get a collection List<Print>, your JSON needs to be an array. Try this (I made your JSON an array and added a second object):
string json = "[{ \"PrintId\":10,\"Header\":\"header\",\"TC\":\"tc\",\"CompanyRef\":\"00000000-0000-0000-0000-000000000000\"}"
+ ",{ \"PrintId\":20,\"Header\":\"header2\",\"TC\":\"tc2\",\"CompanyRef\":\"00000000-0000-0000-0000-000000000000\"}]";
var objs = JsonConvert.DeserializeObject<List<Print>>(json);
//The loop is only for testing. Replace it with your code.
foreach(Print p in objs){
Console.WriteLine("PrintId: " + p.PrintId);
Console.WriteLine("Header: " + p.Header);
Console.WriteLine("TC: " + p.TC);
Console.WriteLine("CompanyRef: " + p.CompanyRef);
Console.WriteLine("==============================");
}
public class Print
{
public int PrintId { get; set; }
public string Header { get; set; }
public string TC { get; set; }
public string CompanyRef { get; set; }
}
Here is a fiddle.
Simply because { } is a jobject notation, to make it a jarray just put square brackets around it e.g. [{ }]. So in your case, just putting these two chars fixes the problem.
var objs = ((JArray)JsonConvert.DeserializeObject("[{ \"PrintId\":10,\"Header\":\"header\",\"TC\":\"tc\",\"CompanyRef\":\"00000000-0000-0000-0000-000000000000\"}]")).Values<JObject>();
you can try here
https://dotnetfiddle.net/RmgGw8
Note: You said it was working before. So this string input probably is a result of a serialization. You did not have a problem before, because your input was always an array with multiple items until this one. And your serializer is probably producing a single object (output starting with '{') instead of an array (output starting with '[') for single item arrays.
serializing List with single object as JSON object not JSON array
For me I was putting empty string as an object which caused the issue, I switched to "{}" which fixed my issue

JSON deserialization throws exception when nested object is empty

Hello I have a problem with deserializing JSON to object.
I have this kind of JSON:
{
"my_obj":{
"id":"test",
"nested_obj":{
"value":"testValue",
"desc":"testDesc"}
}
}
But sometimes I receive empty nested_obj:
{
"my_obj":{
"id":"test",
"nested_obj":""
}
}
My code to handle this:
public class MyObj
{
public string id { get; set; }
public NestedObj nested_obj { get; set; }
}
public class NestedObj
{
public string value { get; set; }
public string desc { get; set; }
}
public virtual T Execute<T>(IRestRequest request) where T : new()
{
request.RequestFormat = DataFormat.Json;
var response = _client.Execute<T>(request);
LogResponse(response);
CheckResponseException(response);
return response.Data;
}
When the nested_obj is not empty, then deserialization works perfectly fine. But when nested_obj is empty, I receive this exception:
Unable to cast object of type 'System.String' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'.
Is it possible to deserialize it this way? Unfortunately it is not possible to change response of the WebService, so i should parse it correctly in my app.
Simply use Newtonsoft's Json.NET. It works fine. Here's a short snippet from LINQPad:
void Main()
{
JsonConvert.DeserializeObject<A>(#"{
property: ""apweiojfwoeif"",
nested: {
prop1: ""wpeifwjefoiwj"",
prop2: ""9ghps89e4aupw3r""
}
}").Dump();
JsonConvert.DeserializeObject<A>(#"{
property: ""apweiojfwoeif"",
nested: """"
}").Dump();
}
// Define other methods and classes here
class A {
public string Property {get;set;}
public B Nested {get;set;}
}
class B {
public string Prop1 {get;set;}
public string Prop2 {get;set;}
}
And the result is
Ouch. That has nothing to do with your code as you already know. It's just that the web service is sometimes defining nested_obj as a NestedObj object, and sometimes as a string (when it sends null). So your parser doesn't know what to make of it.
Your best bet might be to write a custom parser for it. (which makes sense since it's not standard JSON due to the type morphing).
There's a section on writing a custom parser for RestSharp here
They tell you to implement IDeserializer but I'd suggest you simply extend JsonDeserializer to add your special custom functionality and delegate back to the super class for everything else.
Reason is because in
{
"my_obj":{
"id":"test",
"nested_obj":{
"value":"testValue",
"desc":"testDesc"}
}
}
nested object is receiving an object type
while in
{
"my_obj":{
"id":"test",
"nested_obj":""
}
}
it is receiving string type.
if
it returns
{
"my_obj":{
"id":"test",
"nested_obj":null
}
}
then it will be able to parse successfully.
try using http://json2csharp.com/ to convert both of your json and see the difference

Categories