ngocnicholas / airtable.net

Airtable .NET API Client
MIT License
138 stars 34 forks source link

ListRecords wont serialize other data types than string after version 1.1.6 #81

Closed TiagoBrenck closed 9 months ago

TiagoBrenck commented 9 months ago

Hi,

I noticed that on version 1.1.6, you changed Newtonsoft.Json to System.Text.Json and I believe things arent working well. After the change, your library is only able to serialize string types.

For example, given this Airtable sample:

id name
123 John
456 Mary

And this C# class:

 public class AirtableModel
 {
     public int id { get; set; }

     public string name { get; set; }
 }

When I call var airtableResult = await airtableBase.ListRecords<AirtableModel>("Sample", maxRecords: 100, view: "Github Sample");, this exception is thrown:

System.Text.Json.JsonException: Error: The JSON value could not be converted to System.Int32. Path: $.records[0].fields.id

However, changing the property id to type string works.

My production code is using the library version 1.1.5, with a C# model that has int, DateTime and nullable int datatypes, and the conversion works good. Updating the version to anything from 1.1.6 to the latest 1.4.0, causes the Json exception.

It would be really nice to have the serialization fixed, supporting all the common types like integers and datetime.

Thanks

TiagoBrenck commented 9 months ago

It is a System.Json.Text behavior and I found a solution. Use [JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)] for the integers and create a custom converter for the DateTime.

I am curious about the motivation for moving away from Newtonsoft, but I am sure there are valid reasons for it.

Thanks