using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
[JsonConverter(typeof(DecimalDbValueJsonConverter))]
public struct DecimalDbValue : ISerializable
private readonly Decimal _decValue;
_decValue = decimal.MinValue;
SerializationInfo objSerializationInfo,
StreamingContext objStreamingContext)
_decValue = objSerializationInfo.GetDecimal("value");
return _decValue.Equals(Decimal.MinValue);
public void GetObjectData(
StreamingContext context)
info.AddValue("value", _decValue);
class DecimalDbValueJsonConverter : JsonConverter
public override bool CanConvert(Type objectType)
return typeof(DecimalDbValue) == objectType;
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
var value = reader.Value == null ? (decimal?)null : Convert.ToDecimal(reader.Value);
return new DecimalDbValue(value);
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
var dbValue = (DecimalDbValue)value;
writer.WriteValue(dbValue.Value);
public class FloatParseHandlingConverter : JsonConverter
readonly FloatParseHandling floatParseHandling;
public FloatParseHandlingConverter(FloatParseHandling floatParseHandling)
this.floatParseHandling = floatParseHandling;
public override bool CanConvert(Type objectType)
throw new NotImplementedException();
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
if (reader.TokenType == JsonToken.Null)
var old = reader.FloatParseHandling;
reader.FloatParseHandling = floatParseHandling;
existingValue = existingValue ?? serializer.ContractResolver.ResolveContract(objectType).DefaultCreator();
serializer.Populate(reader, existingValue);
reader.FloatParseHandling = old;
public override bool CanWrite { get { return false; } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
throw new NotImplementedException();
public abstract class RootObject
public DecimalDbValue Value { get; set; }
[JsonConverter(typeof(FloatParseHandlingConverter), FloatParseHandling.Decimal)]
public class RootObjectWithConverterApplied : RootObject
public class RootObjectWithoutConverterApplied : RootObject
public static void Test()
TestMaxDecimalDbValueWithRootObjectWithConverterApplied();
TestMaxDecimalDbValueWithRootObjectWithoutConverterApplied();
public static void TestMaxDecimalDbValueWithRootObjectWithConverterApplied()
Console.WriteLine("\nTesting {0}:", nameof(RootObjectWithConverterApplied));
var root = new RootObjectWithConverterApplied { Value = new DecimalDbValue(decimal.MaxValue) };
var json = JsonConvert.SerializeObject(root, Formatting.Indented);
var x = JsonConvert.DeserializeObject<RootObjectWithConverterApplied>(json);
Assert.AreEqual(root.Value.Value, x.Value.Value, string.Format("{0} did not round-trip!", root));
Console.WriteLine("{0} round-tripped successfully.", root);
public static void TestMaxDecimalDbValueWithRootObjectWithoutConverterApplied()
Console.WriteLine("\nTesting {0}:", nameof(RootObjectWithoutConverterApplied));
var root = new RootObjectWithoutConverterApplied { Value = new DecimalDbValue(decimal.MaxValue) };
var json = JsonConvert.SerializeObject(root, Formatting.Indented);
var x = JsonConvert.DeserializeObject<RootObjectWithoutConverterApplied>(json);
Assert.AreEqual(root.Value.Value, x.Value.Value, string.Format("{0} did not round-trip!", root));
Console.WriteLine("{0} round-tripped successfully.", root);
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
public static string GetNetCoreVersion()
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];
@toebens - if `DecimalDbValue` is the **root object** then `FloatParseHandlingConverter` won't work because it needs to be applied to the container type. Applying it to `DecimalDbValue` itself won't work because the decimal value will already have been read in as a `double` by the time `ReadJson()` is called. In such a situation, you need to set `JsonSerializerSettings.FloatParseHandling` explicitly. But when not the root object, applying `FloatParseHandlingConverter` to the container will work. See https: