0

I'm reading data from JSON with JsonConvert and JsonSerializerSettings, I haven't touched the Culture. Changing the phone OS language from en-US to nl-NL I lose my NumberFormat. Is it possible to set the NumberFormat for the app?

using (Stream stream = accessStream.AsStreamForRead((int)accessStream.Size))
                    {
                        byte[] content = new byte[stream.Length];
                        await stream.ReadAsync(content, 0, (int)stream.Length);

                        string json = Encoding.UTF8.GetString(content, 0, content.Length);

                        System.Diagnostics.Debug.WriteLine("Read from file: " + json);

                        JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
                        serializerSettings.NullValueHandling = NullValueHandling.Ignore;
                        List<T> objects = JsonConvert.DeserializeObject<List<T>>(json, serializerSettings);

                        return objects;
                    }
Boris
  • 73
  • 7

1 Answers1

0

You could try this:

   Thread.CurrentThread.CurrentUICulture.NumberFormat = new NumberFormatInfo(); // Sets the CurrenUiCulture number format to the default culture neutral format

In ASP.NET config file you could also change it using the next section. Well, it won't help if it is not asp.net app.

There is a difference between current culture and uiculture - CurrentUiCulture is responsible for formatting and other features, while CurrentCulture deals with language and resources.

But you should know that this snippet will change the number format for your entire main thread in the app. And it won't affect number formats in another threads.

OFF-TOPIC SUGGESTION? :

If you need to change only the Json serialization-deserialization then you should use the Culture property of JsonSerializerSettings

JsonSerializerSettings serializerSettings = new JsonSerializerSettings() {Culture =  CultureInfo.InvariantCulture };
Community
  • 1
  • 1
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53