Replies: 3 comments
-
/cc @dotnet/area-system-text-json for thoughts. I could not repro this in sharplab; the app outputs |
Beta Was this translation helpful? Give feedback.
-
JSON defers to Utf8Parser: which ends up calling: parsing logic being here: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Number.Parsing.cs#L983 @tannergooding might now perhaps - that logic is somewhat... optimized 😄 To be perfectly honest though I'm not seeing the behavior you're seeing: Click to see codeusing System.Text.Json;
using System.Text.Json.Serialization;
string json = """
{
"MRGID": 2554,
"gazetteerSource": "ASFA thesaurus",
"placeType": "Coast",
"latitude": 51.15,
"longitude": 2.7,
"minLatitude": null,
"minLongitude": null,
"maxLatitude": null,
"maxLongitude": null,
"precision": null,
"preferredGazetteerName": "Belgian West Coast",
"preferredGazetteerNameLang": "English",
"status": "standard",
"accepted": 2554
}
""";
GazetteerRecord record = JsonSerializer.Deserialize<GazetteerRecord>(json);
Console.WriteLine(record);
public class GazetteerRecord
{
[JsonPropertyName("MRGID")]
public int MrgId { get; set; }
[JsonPropertyName("gazetteerSource")]
public string GazetteerSource { get; set; } = string.Empty;
[JsonPropertyName("placeType")]
public string PlaceType { get; set; } = string.Empty;
[JsonPropertyName("latitude")]
public decimal? Latitude { get; set; } = null;
[JsonPropertyName("longitude")]
public decimal? Longitude { get; set; } = null;
[JsonPropertyName("minLatitude")]
public decimal? MinLatitude { get; set; } = null;
[JsonPropertyName("minLongitude")]
public decimal? MinLongitude { get; set; } = null;
[JsonPropertyName("maxLatitude")]
public decimal? MaxLatitude { get; set; } = null;
[JsonPropertyName("maxLongitude")]
public decimal? MaxLongitude { get; set; } = null;
[JsonPropertyName("precision")]
public decimal? Precision { get; set; } = null;
[JsonPropertyName("preferredGazetteerName")]
public string PreferredGazetteerName { get; set; } = string.Empty;
[JsonPropertyName("preferredGazetteerNameLang")]
public string PreferredGazetteerNameLang { get; set; } = string.Empty;
[JsonPropertyName("status")]
public string Status { get; set; } = string.Empty;
[JsonPropertyName("accepted")]
public int Accepted { get; set; }
/// <inheritdoc />
public override string ToString()
{
return $"{nameof(MrgId)}: {MrgId}, {nameof(GazetteerSource)}: {GazetteerSource}, {nameof(PlaceType)}: {PlaceType}, {nameof(Latitude)}: {Latitude}, {nameof(Longitude)}: {Longitude}, {nameof(MinLatitude)}: {MinLatitude}, {nameof(MinLongitude)}: {MinLongitude}, {nameof(MaxLatitude)}: {MaxLatitude}, {nameof(MaxLongitude)}: {MaxLongitude}, {nameof(Precision)}: {Precision}, {nameof(PreferredGazetteerName)}: {PreferredGazetteerName}, {nameof(PreferredGazetteerNameLang)}: {PreferredGazetteerNameLang}, {nameof(Status)}: {Status}, {nameof(Accepted)}: {Accepted}";
}
} The output I'm seeing is this:
Can you share environment you're running this on? (i.e. Also note you can always write JsonConverter when you're not happy with specific serialization logic for a type although in this case it's rather strange you're seeing different output |
Beta Was this translation helpful? Give feedback.
-
For the reference the project can be found here SpeciesDatabaseApi After some investigation I found that most probably is my mistake, the data from JSON rest service already comes with that error on precision (at least for C# request), and C# lib is correctly assigning the value to the model. I saw this behavior in multiple and different calls, but I've compared the data from browser display rather than what I actual got from C# request.
But if we open the request URL on browser I see stable numbers instead Now which one is lying about data? Edge browser or C#? After, I sent same request using curl and WSL, it results in same data as C# gets This makes me think that Edge somehow round and make JSON prettier resulting in my confusion about the data as I used it to compare the results I got into the model... my dotnet info:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm deserializing json to class, and using decimal type to get the exact number as set on json, but the number is set like the float precision error.
Runtime: NET 7.0
Print result:
Notice the
Latitude
andLongitude
with the precision error instead of same values from json.How can I improve this situation?
Beta Was this translation helpful? Give feedback.
All reactions