Skip to content

Consider adding a JsonSerializerOptions.CultureInfo option #1366

Open
@ollie10

Description

@ollie10

Hi, I'm stuck on this problem
.Net Core 3.1 Web App with a page that uses Google Maps Autocomplete and I need to save the address and coordinates (Latitude / Longitude as decimals).

Creating an object in javascript and sending it to the controller via jQuery Ajax.

The problem is the following:

  • I'm using the default System.Text.Json serializer / deserializer
  • The decimal values returned by google Js API are in this format: 55.7539619 (for example)
  • I use a route parameter to set statically the Culture like site.com/spain/ for es-ES site.com/russia/ for ru-RU

In the controller all the other properties arrive correctly except the latitude and longitude which are decimals

The problem arises when a culture is being used and the number is not in the format of that culture in the way that the separator is comma (for Spain for example) and not the point (which is standard for javascript)

So i looked for some solutions but none of them seems solving completely the problem:

  • i could replace the dot with comma before sending but it won't work for US culture for example, and besides that I would lose precision

In Newtonsoft Json there was the possibility to specify the Culture as options of the serializer but System.Text.Json doesn't have a property like that.
I think that System.Text.Json should deserialize Json in the correct format more than 'interpreting' the culture

This is the code I'm using:

propertyToSave.OtherProp = 'text' // arrives correctly as all the other properties
propertyToSave.Latitude = selectedPlace.geometry.location.lat(); //val: 55.7539619
propertyToSave.Longitude = selectedPlace.geometry.location.lng();// val: 37.60819619999995

$.ajax({
	url: "/" + countryInfo.EnglishName.toLowerCase() + "/submit",
	dataType: "json",
	method: "POST",
	data: { property: propertyToSave, __RequestVerificationToken: $("input[name=__RequestVerificationToken]").val() },
	....

I specify that if i replace dots with commas the values arrive correctly to the controller, in any other case the properties are always 0

Any suggestion? Is a known problem? I suppose the Web API should suffer of this problem too

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-needs-workAPI needs work before it is approved, it is NOT ready for implementationarea-System.Text.Json

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions