You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am not able to retrieve the attributes in the jsonapi response data object using the JsonApiSerializer. I am able to retrieve only the Id and and the type properties. I am using the DocumentRoot class. My model looks like this:
public class Commodity
{
[JsonProperty(propertyName: "Id")]
public string CommodityId { get; set; }
[JsonProperty(propertyName: "type")]
public string Type { get; set; } = "node--commodity";
[JsonProperty(propertyName: "Description")]
public string Description { get; set; }
[JsonProperty(propertyName:"GradeOverride")]
public bool GradeOverride { get; set; }
[JsonProperty(propertyName :"UnitOfMeasure")]
public string UnitOfMeasure { get; set; }
[JsonProperty(propertyName: "KgPerUnit")]
public int KgPerUnit { get; set; }
[JsonProperty(propertyName: "IsDeleted")]
public bool IsDeleted { get; set; }
[JsonProperty(propertyName: "ProductCode")]
public string ProductCode { get; set; }
[JsonProperty(propertyName: "Weighted")]
public bool Weighted { get; set; }
}
This is how I am trying to retrieve the attributes:
class Program
{
static void Main(string[] args)
{
var client = new RestClient("http://localhost/gp/api/node/commodity");
var request = new RestRequest();
request.AddHeader("Content-Type", "application/vnd.api+json");
request.AddHeader("Authorization", "Basic YXBpOmFwaQ == ");
request.AddHeader("Accept", "application/vnd.api+json");
request.Method = Method.GET;
IRestResponse res = client.Execute(request);
var response = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(res.Content), Formatting.Indented);
DocumentRoot<Commodity[]> commodities =
JsonConvert.DeserializeObject<DocumentRoot<Commodity[]>>(response, new JsonApiSerializerSettings());
Console.WriteLine(response);
Console.WriteLine();
Console.WriteLine("I am expecting my code to give me the values as on the above json. But this is what I am getting.");
Console.WriteLine();
foreach (var item in commodities.Data)
{
Console.WriteLine($"Id ::::: {item.CommodityId}");
Console.WriteLine($"GradeOverride::::: {item.GradeOverride}");
Console.WriteLine($"UnitOfMeasure::::: {item.UnitOfMeasure}");
Console.WriteLine($"Weighted::::: {item.Weighted}");
Console.WriteLine($"Description::::: {item.Description}");
Console.WriteLine($"KgPerUnit::::: {item.KgPerUnit.ToString()}");
Console.WriteLine($"ProductCode::::: {item.ProductCode}");
Console.WriteLine($"isDeleted::::: {item.IsDeleted}");
Console.WriteLine($"Type::::: {item.Type}");
}
Console.ReadLine();
}
}
As you can see from my json response, and the attributes not being set with the correct values from the response. Where am I going wrong?
It looks like it is not identifying the Commodity as a ResourceObject.
The default check to determine if its a resource object looks specifically for an Id property not taking into consideration the [JsonProperty]. As commodity does not have an Id property (only a CommodityId field) it is doing regular json deserialization on it
This is not the expected behaviour and I believe this is a legitimate bug and will attempt to get a fix for this issue
In the meantime you could rename CommodityId to Id. Or alternatively extend ResourceObjectConverter and override CanConvert to add custom logic to identify the attribute, this extended converter can be passed to the JsonApiSerializerSettings constructor
I am not able to retrieve the attributes in the jsonapi response data object using the JsonApiSerializer. I am able to retrieve only the Id and and the type properties. I am using the DocumentRoot class. My model looks like this:
This is how I am trying to retrieve the attributes:
As you can see from my json response, and the attributes not being set with the correct values from the response. Where am I going wrong?
Response
The incorrect output, only showing the Id and Type set correctly.
The text was updated successfully, but these errors were encountered: