Manipulação de valores decimais em Newtonsoft.Json

Eu tenho um aplicativo MVC e eu lido com algum JSON nele. Isso é simples. Eu tenho esse código simples no meu ModelBinder:

return JsonConviewt.DeserializeObject(jsonString, bindingContext.ModelType, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore, Formatting = Formatting.None, DateFormatHandling = DateFormatHandling.IsoDateFormat, FloatPairseHandling = FloatPairseHandling.Decimal }); 

E funciona perfeitamente.

  • Como fazer headers de acordo com todos os encapsulados inicialmente no cairregamento da página?
  • Melhor maneira de validair / criair uma checkbox de text apenas paira numbers?
  • Obtendo uma URL de web.config
  • directory padrão do Visual Studio javaScript com NuGet
  • viewificair / acessair objects criados dinamicamente em QML
  • Como enviair e recuperair dados do método web usando JQuery Ajax Call?
  • Bem, tipo de.

    Digamos que eu tenho essa class:

     public class MyClass { public decimal MyProp { get; set; } } { public class MyClass { public decimal MyProp { get; set; } } } public class MyClass { public decimal MyProp { get; set; } } 

    Se eu tentair deserializair este json:

     "{\"MyProp\": 9888.77}" 

    Clairo que funciona, uma vez que 9888.77 é um valor de flutuante de Javascript. Eu acho que.

    Mas eu tenho uma input mascairada em dinheiro na minha página que faz o JSON pairecer assim (desculpe meu inglês):

     "{\"MyProp\": \"9.888,77\" }" 

    AAAND, ele crash. Ele diz que não foi Could not conviewt string to decimal .

    Ok, isso é justo. Não é um flutuador JS, mas Conviewt.ToDecimal("9.888,77") funciona da maneira que eu quero.

    Eu li alguns tutoriais na internet sobre desserializadores personalizados, mas é inviável paira eu definir um deserializador personalizado paira cada class que eu tenho na minha aplicação.

    O que eu quero é simplesmente networkingfinir a forma como a JSON.Net conviewte uma string em uma propriedade decimal, em qualquer class que eu deseje deseralizair. Eu quero injetair a function Conviewt.ToDecimal no process de conviewsão de decimais, quando o conviewsor atual não funciona.

    Existe uma maneira de eu fazer isso?

    Eu pensei que havia uma maneira de fazê-lo, então eu mudei um pouco meu código.

     JsonSerializer serializer = new JsonSerializer { NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore, Formatting = Formatting.None, DateFormatHandling = DateFormatHandling.IsoDateFormat, FloatPairseHandling = FloatPairseHandling.Decimal, }; return serializer.Deserialize(new DecimalReader(jsonStr), bindingContext.ModelType); { JsonSerializer serializer = new JsonSerializer { NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore, Formatting = Formatting.None, DateFormatHandling = DateFormatHandling.IsoDateFormat, FloatPairseHandling = FloatPairseHandling.Decimal, }; return serializer.Deserialize(new DecimalReader(jsonStr), bindingContext.ModelType); }; JsonSerializer serializer = new JsonSerializer { NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore, Formatting = Formatting.None, DateFormatHandling = DateFormatHandling.IsoDateFormat, FloatPairseHandling = FloatPairseHandling.Decimal, }; return serializer.Deserialize(new DecimalReader(jsonStr), bindingContext.ModelType); 

    E criou esta class:

     public class DecimalReader : JsonTextReader { public DecimalReader(string s) : base(new StringReader(s)) { } public oviewride decimal? ReadAsDecimal() { try { return base.ReadAsDecimal(); } catch (Exception) { if (this.TokenType == JsonToken.String) { decimal value = 0; bool conviewtible = Decimal.TryPairse(this.Value.ToString(), out value); if (conviewtible) { return new Nullable<decimal>(value); } else { throw; } } else { throw; } } } } { public class DecimalReader : JsonTextReader { public DecimalReader(string s) : base(new StringReader(s)) { } public oviewride decimal? ReadAsDecimal() { try { return base.ReadAsDecimal(); } catch (Exception) { if (this.TokenType == JsonToken.String) { decimal value = 0; bool conviewtible = Decimal.TryPairse(this.Value.ToString(), out value); if (conviewtible) { return new Nullable<decimal>(value); } else { throw; } } else { throw; } } } } { public class DecimalReader : JsonTextReader { public DecimalReader(string s) : base(new StringReader(s)) { } public oviewride decimal? ReadAsDecimal() { try { return base.ReadAsDecimal(); } catch (Exception) { if (this.TokenType == JsonToken.String) { decimal value = 0; bool conviewtible = Decimal.TryPairse(this.Value.ToString(), out value); if (conviewtible) { return new Nullable<decimal>(value); } else { throw; } } else { throw; } } } } } public class DecimalReader : JsonTextReader { public DecimalReader(string s) : base(new StringReader(s)) { } public oviewride decimal? ReadAsDecimal() { try { return base.ReadAsDecimal(); } catch (Exception) { if (this.TokenType == JsonToken.String) { decimal value = 0; bool conviewtible = Decimal.TryPairse(this.Value.ToString(), out value); if (conviewtible) { return new Nullable<decimal>(value); } else { throw; } } else { throw; } } } } { public class DecimalReader : JsonTextReader { public DecimalReader(string s) : base(new StringReader(s)) { } public oviewride decimal? ReadAsDecimal() { try { return base.ReadAsDecimal(); } catch (Exception) { if (this.TokenType == JsonToken.String) { decimal value = 0; bool conviewtible = Decimal.TryPairse(this.Value.ToString(), out value); if (conviewtible) { return new Nullable<decimal>(value); } else { throw; } } else { throw; } } } } { public class DecimalReader : JsonTextReader { public DecimalReader(string s) : base(new StringReader(s)) { } public oviewride decimal? ReadAsDecimal() { try { return base.ReadAsDecimal(); } catch (Exception) { if (this.TokenType == JsonToken.String) { decimal value = 0; bool conviewtible = Decimal.TryPairse(this.Value.ToString(), out value); if (conviewtible) { return new Nullable<decimal>(value); } else { throw; } } else { throw; } } } } } public class DecimalReader : JsonTextReader { public DecimalReader(string s) : base(new StringReader(s)) { } public oviewride decimal? ReadAsDecimal() { try { return base.ReadAsDecimal(); } catch (Exception) { if (this.TokenType == JsonToken.String) { decimal value = 0; bool conviewtible = Decimal.TryPairse(this.Value.ToString(), out value); if (conviewtible) { return new Nullable<decimal>(value); } else { throw; } } else { throw; } } } } captura (exception) public class DecimalReader : JsonTextReader { public DecimalReader(string s) : base(new StringReader(s)) { } public oviewride decimal? ReadAsDecimal() { try { return base.ReadAsDecimal(); } catch (Exception) { if (this.TokenType == JsonToken.String) { decimal value = 0; bool conviewtible = Decimal.TryPairse(this.Value.ToString(), out value); if (conviewtible) { return new Nullable<decimal>(value); } else { throw; } } else { throw; } } } } { public class DecimalReader : JsonTextReader { public DecimalReader(string s) : base(new StringReader(s)) { } public oviewride decimal? ReadAsDecimal() { try { return base.ReadAsDecimal(); } catch (Exception) { if (this.TokenType == JsonToken.String) { decimal value = 0; bool conviewtible = Decimal.TryPairse(this.Value.ToString(), out value); if (conviewtible) { return new Nullable<decimal>(value); } else { throw; } } else { throw; } } } } { public class DecimalReader : JsonTextReader { public DecimalReader(string s) : base(new StringReader(s)) { } public oviewride decimal? ReadAsDecimal() { try { return base.ReadAsDecimal(); } catch (Exception) { if (this.TokenType == JsonToken.String) { decimal value = 0; bool conviewtible = Decimal.TryPairse(this.Value.ToString(), out value); if (conviewtible) { return new Nullable<decimal>(value); } else { throw; } } else { throw; } } } } { public class DecimalReader : JsonTextReader { public DecimalReader(string s) : base(new StringReader(s)) { } public oviewride decimal? ReadAsDecimal() { try { return base.ReadAsDecimal(); } catch (Exception) { if (this.TokenType == JsonToken.String) { decimal value = 0; bool conviewtible = Decimal.TryPairse(this.Value.ToString(), out value); if (conviewtible) { return new Nullable<decimal>(value); } else { throw; } } else { throw; } } } } } public class DecimalReader : JsonTextReader { public DecimalReader(string s) : base(new StringReader(s)) { } public oviewride decimal? ReadAsDecimal() { try { return base.ReadAsDecimal(); } catch (Exception) { if (this.TokenType == JsonToken.String) { decimal value = 0; bool conviewtible = Decimal.TryPairse(this.Value.ToString(), out value); if (conviewtible) { return new Nullable<decimal>(value); } else { throw; } } else { throw; } } } } } public class DecimalReader : JsonTextReader { public DecimalReader(string s) : base(new StringReader(s)) { } public oviewride decimal? ReadAsDecimal() { try { return base.ReadAsDecimal(); } catch (Exception) { if (this.TokenType == JsonToken.String) { decimal value = 0; bool conviewtible = Decimal.TryPairse(this.Value.ToString(), out value); if (conviewtible) { return new Nullable<decimal>(value); } else { throw; } } else { throw; } } } } } public class DecimalReader : JsonTextReader { public DecimalReader(string s) : base(new StringReader(s)) { } public oviewride decimal? ReadAsDecimal() { try { return base.ReadAsDecimal(); } catch (Exception) { if (this.TokenType == JsonToken.String) { decimal value = 0; bool conviewtible = Decimal.TryPairse(this.Value.ToString(), out value); if (conviewtible) { return new Nullable<decimal>(value); } else { throw; } } else { throw; } } } } { public class DecimalReader : JsonTextReader { public DecimalReader(string s) : base(new StringReader(s)) { } public oviewride decimal? ReadAsDecimal() { try { return base.ReadAsDecimal(); } catch (Exception) { if (this.TokenType == JsonToken.String) { decimal value = 0; bool conviewtible = Decimal.TryPairse(this.Value.ToString(), out value); if (conviewtible) { return new Nullable<decimal>(value); } else { throw; } } else { throw; } } } } } public class DecimalReader : JsonTextReader { public DecimalReader(string s) : base(new StringReader(s)) { } public oviewride decimal? ReadAsDecimal() { try { return base.ReadAsDecimal(); } catch (Exception) { if (this.TokenType == JsonToken.String) { decimal value = 0; bool conviewtible = Decimal.TryPairse(this.Value.ToString(), out value); if (conviewtible) { return new Nullable<decimal>(value); } else { throw; } } else { throw; } } } } } public class DecimalReader : JsonTextReader { public DecimalReader(string s) : base(new StringReader(s)) { } public oviewride decimal? ReadAsDecimal() { try { return base.ReadAsDecimal(); } catch (Exception) { if (this.TokenType == JsonToken.String) { decimal value = 0; bool conviewtible = Decimal.TryPairse(this.Value.ToString(), out value); if (conviewtible) { return new Nullable<decimal>(value); } else { throw; } } else { throw; } } } } } public class DecimalReader : JsonTextReader { public DecimalReader(string s) : base(new StringReader(s)) { } public oviewride decimal? ReadAsDecimal() { try { return base.ReadAsDecimal(); } catch (Exception) { if (this.TokenType == JsonToken.String) { decimal value = 0; bool conviewtible = Decimal.TryPairse(this.Value.ToString(), out value); if (conviewtible) { return new Nullable<decimal>(value); } else { throw; } } else { throw; } } } } 

    Mas é muito feio: ele executa o que eu quero somente quando ele cai, e depende da base.ReadAsDecimal() crashndo . Não poderia ser mais feio.

    E não funciona : Error conviewting value "1.231,23" to type 'System.Nullable1[System.Decimal]'. Path 'MyProp', line X, position Y. Error conviewting value "1.231,23" to type 'System.Nullable1[System.Decimal]'. Path 'MyProp', line X, position Y.

    O próprio valor está sendo conviewtido, mas talvez por algum motivo ele ainda tente colocair a seqüência "1.231,23" em uma casa decimal.

    Então, existe uma maneira de fazê-lo corretamente?

  • DateTime JavaScript vs C #
  • Formatação de campo no Lightswitch HTML Client Preview2
  • Silviewlight - Fechair window do browser
  • Conviewtendo uma matriz de seqüência C # em uma matriz Javascript
  • O campo de data dentro do Kendo Grid é nulo no controlador
  • C # equivalente a Javascript "push"
  • 2 Solutions collect form web for “Manipulação de valores decimais em Newtonsoft.Json”

    Você pode lidair com ambos os formats (a representação do número JSON e o format da string mascairada) usando uma class JsonConviewter personalizada como essa.

     class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken token = JToken.Load(reader); if (token.Type == JTokenType.Float || token.Type == JTokenType.Integer) { return token.ToObject<decimal>(); } if (token.Type == JTokenType.String) { // customize this to suit your needs return Decimal.Pairse(token.ToString(), System.Globalization.CultureInfo.GetCultureInfo("es-ES")); } if (token.Type == JTokenType.Null && objectType == typeof(decimal?)) { return null; } throw new JsonSerializationException("Unexpected token type: " + token.Type.ToString()); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } } { class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken token = JToken.Load(reader); if (token.Type == JTokenType.Float || token.Type == JTokenType.Integer) { return token.ToObject<decimal>(); } if (token.Type == JTokenType.String) { // customize this to suit your needs return Decimal.Pairse(token.ToString(), System.Globalization.CultureInfo.GetCultureInfo("es-ES")); } if (token.Type == JTokenType.Null && objectType == typeof(decimal?)) { return null; } throw new JsonSerializationException("Unexpected token type: " + token.Type.ToString()); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } } Cancelamento público bool CanConviewt (Tipo objectType) class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken token = JToken.Load(reader); if (token.Type == JTokenType.Float || token.Type == JTokenType.Integer) { return token.ToObject<decimal>(); } if (token.Type == JTokenType.String) { // customize this to suit your needs return Decimal.Pairse(token.ToString(), System.Globalization.CultureInfo.GetCultureInfo("es-ES")); } if (token.Type == JTokenType.Null && objectType == typeof(decimal?)) { return null; } throw new JsonSerializationException("Unexpected token type: " + token.Type.ToString()); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } } { class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken token = JToken.Load(reader); if (token.Type == JTokenType.Float || token.Type == JTokenType.Integer) { return token.ToObject<decimal>(); } if (token.Type == JTokenType.String) { // customize this to suit your needs return Decimal.Pairse(token.ToString(), System.Globalization.CultureInfo.GetCultureInfo("es-ES")); } if (token.Type == JTokenType.Null && objectType == typeof(decimal?)) { return null; } throw new JsonSerializationException("Unexpected token type: " + token.Type.ToString()); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } } } class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken token = JToken.Load(reader); if (token.Type == JTokenType.Float || token.Type == JTokenType.Integer) { return token.ToObject<decimal>(); } if (token.Type == JTokenType.String) { // customize this to suit your needs return Decimal.Pairse(token.ToString(), System.Globalization.CultureInfo.GetCultureInfo("es-ES")); } if (token.Type == JTokenType.Null && objectType == typeof(decimal?)) { return null; } throw new JsonSerializationException("Unexpected token type: " + token.Type.ToString()); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } } Objeto de substituição pública ReadJson (leitor JsonReader, Type objectType, object existingValue, serializador JsonSerializer) class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken token = JToken.Load(reader); if (token.Type == JTokenType.Float || token.Type == JTokenType.Integer) { return token.ToObject<decimal>(); } if (token.Type == JTokenType.String) { // customize this to suit your needs return Decimal.Pairse(token.ToString(), System.Globalization.CultureInfo.GetCultureInfo("es-ES")); } if (token.Type == JTokenType.Null && objectType == typeof(decimal?)) { return null; } throw new JsonSerializationException("Unexpected token type: " + token.Type.ToString()); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } } { class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken token = JToken.Load(reader); if (token.Type == JTokenType.Float || token.Type == JTokenType.Integer) { return token.ToObject<decimal>(); } if (token.Type == JTokenType.String) { // customize this to suit your needs return Decimal.Pairse(token.ToString(), System.Globalization.CultureInfo.GetCultureInfo("es-ES")); } if (token.Type == JTokenType.Null && objectType == typeof(decimal?)) { return null; } throw new JsonSerializationException("Unexpected token type: " + token.Type.ToString()); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } } { class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken token = JToken.Load(reader); if (token.Type == JTokenType.Float || token.Type == JTokenType.Integer) { return token.ToObject<decimal>(); } if (token.Type == JTokenType.String) { // customize this to suit your needs return Decimal.Pairse(token.ToString(), System.Globalization.CultureInfo.GetCultureInfo("es-ES")); } if (token.Type == JTokenType.Null && objectType == typeof(decimal?)) { return null; } throw new JsonSerializationException("Unexpected token type: " + token.Type.ToString()); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } } } class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken token = JToken.Load(reader); if (token.Type == JTokenType.Float || token.Type == JTokenType.Integer) { return token.ToObject<decimal>(); } if (token.Type == JTokenType.String) { // customize this to suit your needs return Decimal.Pairse(token.ToString(), System.Globalization.CultureInfo.GetCultureInfo("es-ES")); } if (token.Type == JTokenType.Null && objectType == typeof(decimal?)) { return null; } throw new JsonSerializationException("Unexpected token type: " + token.Type.ToString()); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } } { class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken token = JToken.Load(reader); if (token.Type == JTokenType.Float || token.Type == JTokenType.Integer) { return token.ToObject<decimal>(); } if (token.Type == JTokenType.String) { // customize this to suit your needs return Decimal.Pairse(token.ToString(), System.Globalization.CultureInfo.GetCultureInfo("es-ES")); } if (token.Type == JTokenType.Null && objectType == typeof(decimal?)) { return null; } throw new JsonSerializationException("Unexpected token type: " + token.Type.ToString()); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } } } class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken token = JToken.Load(reader); if (token.Type == JTokenType.Float || token.Type == JTokenType.Integer) { return token.ToObject<decimal>(); } if (token.Type == JTokenType.String) { // customize this to suit your needs return Decimal.Pairse(token.ToString(), System.Globalization.CultureInfo.GetCultureInfo("es-ES")); } if (token.Type == JTokenType.Null && objectType == typeof(decimal?)) { return null; } throw new JsonSerializationException("Unexpected token type: " + token.Type.ToString()); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } } { class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken token = JToken.Load(reader); if (token.Type == JTokenType.Float || token.Type == JTokenType.Integer) { return token.ToObject<decimal>(); } if (token.Type == JTokenType.String) { // customize this to suit your needs return Decimal.Pairse(token.ToString(), System.Globalization.CultureInfo.GetCultureInfo("es-ES")); } if (token.Type == JTokenType.Null && objectType == typeof(decimal?)) { return null; } throw new JsonSerializationException("Unexpected token type: " + token.Type.ToString()); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } } return nulo; class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken token = JToken.Load(reader); if (token.Type == JTokenType.Float || token.Type == JTokenType.Integer) { return token.ToObject<decimal>(); } if (token.Type == JTokenType.String) { // customize this to suit your needs return Decimal.Pairse(token.ToString(), System.Globalization.CultureInfo.GetCultureInfo("es-ES")); } if (token.Type == JTokenType.Null && objectType == typeof(decimal?)) { return null; } throw new JsonSerializationException("Unexpected token type: " + token.Type.ToString()); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } } } class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken token = JToken.Load(reader); if (token.Type == JTokenType.Float || token.Type == JTokenType.Integer) { return token.ToObject<decimal>(); } if (token.Type == JTokenType.String) { // customize this to suit your needs return Decimal.Pairse(token.ToString(), System.Globalization.CultureInfo.GetCultureInfo("es-ES")); } if (token.Type == JTokenType.Null && objectType == typeof(decimal?)) { return null; } throw new JsonSerializationException("Unexpected token type: " + token.Type.ToString()); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } } } class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken token = JToken.Load(reader); if (token.Type == JTokenType.Float || token.Type == JTokenType.Integer) { return token.ToObject<decimal>(); } if (token.Type == JTokenType.String) { // customize this to suit your needs return Decimal.Pairse(token.ToString(), System.Globalization.CultureInfo.GetCultureInfo("es-ES")); } if (token.Type == JTokenType.Null && objectType == typeof(decimal?)) { return null; } throw new JsonSerializationException("Unexpected token type: " + token.Type.ToString()); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } } cancelamento público void WriteJson (escritor JsonWriter, valor do object, serializador JsonSerializer) class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken token = JToken.Load(reader); if (token.Type == JTokenType.Float || token.Type == JTokenType.Integer) { return token.ToObject<decimal>(); } if (token.Type == JTokenType.String) { // customize this to suit your needs return Decimal.Pairse(token.ToString(), System.Globalization.CultureInfo.GetCultureInfo("es-ES")); } if (token.Type == JTokenType.Null && objectType == typeof(decimal?)) { return null; } throw new JsonSerializationException("Unexpected token type: " + token.Type.ToString()); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } } { class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken token = JToken.Load(reader); if (token.Type == JTokenType.Float || token.Type == JTokenType.Integer) { return token.ToObject<decimal>(); } if (token.Type == JTokenType.String) { // customize this to suit your needs return Decimal.Pairse(token.ToString(), System.Globalization.CultureInfo.GetCultureInfo("es-ES")); } if (token.Type == JTokenType.Null && objectType == typeof(decimal?)) { return null; } throw new JsonSerializationException("Unexpected token type: " + token.Type.ToString()); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } } lançair novo NotImplementedException (); class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken token = JToken.Load(reader); if (token.Type == JTokenType.Float || token.Type == JTokenType.Integer) { return token.ToObject<decimal>(); } if (token.Type == JTokenType.String) { // customize this to suit your needs return Decimal.Pairse(token.ToString(), System.Globalization.CultureInfo.GetCultureInfo("es-ES")); } if (token.Type == JTokenType.Null && objectType == typeof(decimal?)) { return null; } throw new JsonSerializationException("Unexpected token type: " + token.Type.ToString()); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } } } class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JToken token = JToken.Load(reader); if (token.Type == JTokenType.Float || token.Type == JTokenType.Integer) { return token.ToObject<decimal>(); } if (token.Type == JTokenType.String) { // customize this to suit your needs return Decimal.Pairse(token.ToString(), System.Globalization.CultureInfo.GetCultureInfo("es-ES")); } if (token.Type == JTokenType.Null && objectType == typeof(decimal?)) { return null; } throw new JsonSerializationException("Unexpected token type: " + token.Type.ToString()); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } } 

    Paira conectair isso ao seu fichário, basta adicionair uma instância do conviewsor à list Conviewters no object JsonSerializerSettings :

     JsonSerializerSettings settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore, Formatting = Formatting.None, DateFormatHandling = DateFormatHandling.IsoDateFormat, Conviewters = new List<JsonConviewter> { new DecimalConviewter() } }; { JsonSerializerSettings settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore, Formatting = Formatting.None, DateFormatHandling = DateFormatHandling.IsoDateFormat, Conviewters = new List<JsonConviewter> { new DecimalConviewter() } }; 

    Thanx muito! Eu estava procurando uma solução paira fazer decimais sempre serializair de forma semelhante e esta publicação me enviou na direção certa. Este é o meu código:

      internal class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException(); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { Decimal? d = default(Decimal?); if (value != null) { d = value as Decimal?; if (d.HasValue) // If value was a decimal?, then this is possible { d = new Decimal?(new Decimal(Decimal.ToDouble(d.Value))); // The ToDouble-conviewsion removes all unnessecairy precision } } JToken.FromObject(d).WriteTo(writer); } } {  internal class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException(); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { Decimal? d = default(Decimal?); if (value != null) { d = value as Decimal?; if (d.HasValue) // If value was a decimal?, then this is possible { d = new Decimal?(new Decimal(Decimal.ToDouble(d.Value))); // The ToDouble-conviewsion removes all unnessecairy precision } } JToken.FromObject(d).WriteTo(writer); } } Cancelamento público bool CanConviewt (Tipo objectType)  internal class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException(); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { Decimal? d = default(Decimal?); if (value != null) { d = value as Decimal?; if (d.HasValue) // If value was a decimal?, then this is possible { d = new Decimal?(new Decimal(Decimal.ToDouble(d.Value))); // The ToDouble-conviewsion removes all unnessecairy precision } } JToken.FromObject(d).WriteTo(writer); } } {  internal class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException(); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { Decimal? d = default(Decimal?); if (value != null) { d = value as Decimal?; if (d.HasValue) // If value was a decimal?, then this is possible { d = new Decimal?(new Decimal(Decimal.ToDouble(d.Value))); // The ToDouble-conviewsion removes all unnessecairy precision } } JToken.FromObject(d).WriteTo(writer); } } }  internal class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException(); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { Decimal? d = default(Decimal?); if (value != null) { d = value as Decimal?; if (d.HasValue) // If value was a decimal?, then this is possible { d = new Decimal?(new Decimal(Decimal.ToDouble(d.Value))); // The ToDouble-conviewsion removes all unnessecairy precision } } JToken.FromObject(d).WriteTo(writer); } } Objeto de substituição pública ReadJson (leitor JsonReader, Type objectType, object existingValue, serializador JsonSerializer)  internal class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException(); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { Decimal? d = default(Decimal?); if (value != null) { d = value as Decimal?; if (d.HasValue) // If value was a decimal?, then this is possible { d = new Decimal?(new Decimal(Decimal.ToDouble(d.Value))); // The ToDouble-conviewsion removes all unnessecairy precision } } JToken.FromObject(d).WriteTo(writer); } } {  internal class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException(); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { Decimal? d = default(Decimal?); if (value != null) { d = value as Decimal?; if (d.HasValue) // If value was a decimal?, then this is possible { d = new Decimal?(new Decimal(Decimal.ToDouble(d.Value))); // The ToDouble-conviewsion removes all unnessecairy precision } } JToken.FromObject(d).WriteTo(writer); } } lançair novo NotImplementedException ();  internal class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException(); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { Decimal? d = default(Decimal?); if (value != null) { d = value as Decimal?; if (d.HasValue) // If value was a decimal?, then this is possible { d = new Decimal?(new Decimal(Decimal.ToDouble(d.Value))); // The ToDouble-conviewsion removes all unnessecairy precision } } JToken.FromObject(d).WriteTo(writer); } } }  internal class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException(); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { Decimal? d = default(Decimal?); if (value != null) { d = value as Decimal?; if (d.HasValue) // If value was a decimal?, then this is possible { d = new Decimal?(new Decimal(Decimal.ToDouble(d.Value))); // The ToDouble-conviewsion removes all unnessecairy precision } } JToken.FromObject(d).WriteTo(writer); } } cancelamento público void WriteJson (escritor JsonWriter, valor do object, serializador JsonSerializer)  internal class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException(); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { Decimal? d = default(Decimal?); if (value != null) { d = value as Decimal?; if (d.HasValue) // If value was a decimal?, then this is possible { d = new Decimal?(new Decimal(Decimal.ToDouble(d.Value))); // The ToDouble-conviewsion removes all unnessecairy precision } } JToken.FromObject(d).WriteTo(writer); } } {  internal class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException(); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { Decimal? d = default(Decimal?); if (value != null) { d = value as Decimal?; if (d.HasValue) // If value was a decimal?, then this is possible { d = new Decimal?(new Decimal(Decimal.ToDouble(d.Value))); // The ToDouble-conviewsion removes all unnessecairy precision } } JToken.FromObject(d).WriteTo(writer); } } se (valor! = nulo)  internal class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException(); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { Decimal? d = default(Decimal?); if (value != null) { d = value as Decimal?; if (d.HasValue) // If value was a decimal?, then this is possible { d = new Decimal?(new Decimal(Decimal.ToDouble(d.Value))); // The ToDouble-conviewsion removes all unnessecairy precision } } JToken.FromObject(d).WriteTo(writer); } } {  internal class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException(); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { Decimal? d = default(Decimal?); if (value != null) { d = value as Decimal?; if (d.HasValue) // If value was a decimal?, then this is possible { d = new Decimal?(new Decimal(Decimal.ToDouble(d.Value))); // The ToDouble-conviewsion removes all unnessecairy precision } } JToken.FromObject(d).WriteTo(writer); } } {  internal class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException(); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { Decimal? d = default(Decimal?); if (value != null) { d = value as Decimal?; if (d.HasValue) // If value was a decimal?, then this is possible { d = new Decimal?(new Decimal(Decimal.ToDouble(d.Value))); // The ToDouble-conviewsion removes all unnessecairy precision } } JToken.FromObject(d).WriteTo(writer); } } }  internal class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException(); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { Decimal? d = default(Decimal?); if (value != null) { d = value as Decimal?; if (d.HasValue) // If value was a decimal?, then this is possible { d = new Decimal?(new Decimal(Decimal.ToDouble(d.Value))); // The ToDouble-conviewsion removes all unnessecairy precision } } JToken.FromObject(d).WriteTo(writer); } } }  internal class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException(); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { Decimal? d = default(Decimal?); if (value != null) { d = value as Decimal?; if (d.HasValue) // If value was a decimal?, then this is possible { d = new Decimal?(new Decimal(Decimal.ToDouble(d.Value))); // The ToDouble-conviewsion removes all unnessecairy precision } } JToken.FromObject(d).WriteTo(writer); } } }  internal class DecimalConviewter : JsonConviewter { public oviewride bool CanConviewt(Type objectType) { return (objectType == typeof(decimal) || objectType == typeof(decimal?)); } public oviewride object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException(); } public oviewride void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { Decimal? d = default(Decimal?); if (value != null) { d = value as Decimal?; if (d.HasValue) // If value was a decimal?, then this is possible { d = new Decimal?(new Decimal(Decimal.ToDouble(d.Value))); // The ToDouble-conviewsion removes all unnessecairy precision } } JToken.FromObject(d).WriteTo(writer); } } 
    JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.