In our asp.net application, we recently upgraded from restsharp 106.12.0 to 110.2.0.
There were no issues with the old version, but after upgrading with few changes to code, we are getting the errors:
The JSON value could not be converted to
SubExCore.Models.Enums.YearType. Path: $.body.YearType | LineNumber: 0
| BytePositionInLine:The JSON value could not be converted to
System.Nullable`1[SubExCore.Models.Enums.SectionType]. Path:
$.body.sectionType | LineNumber: 0 | BytePositionInLine
Here is my api method to get YearBook response by deserializing json response response.
public YearBook GetYearBook()
{
RestRequest request = new RestRequest(MethodType)
{
RequestFormat = DataFormat.Json,
Resource = ApiPath,
Method = Method.GET,
JsonSerializer = new CustomJsonSerializer()
};
RestRequest request = new RestRequest() { RequestFormat = DataFormat.Json, Resource = ApiPath, Method = Method.Get };
var options = new RestClientOptions()
{
BaseUrl = new Uri(BaseAddress),
CachePolicy = new System.Net.Http.Headers.CacheControlHeaderValue() { NoCache = true, NoStore = true }
};
RestClient client = new RestClient(options);
if (!Token.IsNullOrWhiteSpace())
{
client.AddDefaultHeader("Authorization", "authToken " + Token);
}
client.AddDefaultHeader("Cache-Control", "no-cache");
var myresonse = client.Execute<YearBook>(request);
return myresonse.Data;
}
Here is my YearBook class with Enum property
[Table(TableName = "tblYearBook")]
public class YearBook
{
[Map(ColumnName = "year_id"), Key]
public int Year { get; set; }
[Map(ColumnName = "year_type")]
public YearType YearType { get; set; }
[Map(ColumnName = "section_type")]
public SectionType? Section { get; set; }
[Map(ColumnName = "college_name")]
public string CollegeName { get; set; }
[Map(ColumnName = "avg_pass")]
public decimal AvgPass { get; set; }
}
public enum YearType
{
FirstYear,
SecondYear,
ThirdYear
}
public enum SectionType
{
None,
MIT,
BIT
}
Here is the json response i get from the method.
{
"year_id": "2005",
"year_type": "FirstYear",
"section_type": "None",
"college_name": "St. Alphores Govt College",
"avg_pass" : 89
}
Earlier we have CustomSerializer implemented out of RestSharp.Serializers.ISerializer. The new Restsharp version now implementing the customserializer from IRestSerializer. There is no enough detail in Restsharp documenation neight on internet.
Could anyone give me a proper implementation of IRestSerializer which has below serialization settings.
Here is my existing custom serializer class with Restsharp version 106.12.0
public class CustomJsonSerializer : RestSharp.Serializers.ISerializer
{
public CustomJsonSerializer()
{
var restSerializer = new RestSharp.Serialization.Json.JsonSerializer();
this.ContentType = restSerializer.ContentType;
}
public string ContentType { get; set; }
public string DateFormat { get; set; }
public string Namespace { get; set; }
public string RootElement { get; set; }
private JsonSerializerSettings SerializerSettings
{
get
{
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Converters.Add(new StringEnumConverter());
settings.Converters.Add(new DateTimeJsonConverter());
settings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
return settings;
}
}
string RestSharp.Serializers.ISerializer.Serialize(object obj)
{
return Newtonsoft.Json.JsonConvert.SerializeObject(obj, SerializerSettings);
}
}
2
Answers
ok. i simplified class and using provided pieces created example for you
as you see, string response deserialized to object as expected:
The answer by Power Mouse tells you how to configure NewtonsoftJson to handle the JSON and the model from the question.
In order to use it with RestSharp, you need to tell it to use these settings.
Assuming you have a class for some API client, it can look like
It’s described in the documentation, all it takes is to look.