0

I have a website, and most of the functionality is implemented using asp.net webapi. The problem is that some users have special keyboard layouts that have non-standard number characters, one is ۰۱۲۳۴۵۶۷۸۹ instead of 0123456789.

Now I need a way to customize asp.net webapi, or Json.Net to handle conversion of these strings to numbers.

I already know how to handle this, by implementing a JsonConverter and decorate the Property with [JsonConverter] attribute, but this is error prune, as one might forget to put this on new properties and this is an error which might easily slip through testing and creep to production code.

I need one global setting where I can handle this for all numerals types (even if I need to do that for each numeral separately).

Alireza
  • 5,421
  • 5
  • 34
  • 67
  • 1
    You could add a custom converter for all `int` and `long` types to do the parsing. See [Convert long number as string in the serialization](https://stackoverflow.com/questions/17369278/convert-long-number-as-string-in-the-serialization) for an example. – dbc Aug 04 '16 at 17:16
  • Thanks, the last paragraph seems to be what I'm looking for. I'll give it a try. – Alireza Aug 04 '16 at 17:25
  • 1
    Do those special numbers appear with or without quotes? I.e. does your JSON look like `{"Value": "۱"}` or `{"Value": ۱}`? In the former case a `JsonConverter` should do the trick, but the latter is more problematic since it is, strictly speaking, invalid JSON according to the [standard](http://www.json.org/). – dbc Aug 04 '16 at 18:51
  • @dbc Great point, and another problem: in `` the browser effectively ignores these keystrokes. So we should fix the problem early in the client. – Alireza Aug 05 '16 at 07:18

1 Answers1

0

You can register you formatter globally by this way:

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new NumeralsFormatConverter());

Registered formatter will process fields that pass CanConvert method validation.