using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Collections.ObjectModel;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
[JsonConverter(typeof(PriceConverter))]
private readonly decimal _value;
public Price(Price value)
public Price(decimal value)
public static implicit operator decimal(Price p)
public static implicit operator Price(decimal d)
class PriceConverter : JsonConverter<Price>
public override Price ReadJson(JsonReader reader, Type objectType, Price existingValue, bool hasExistingValue, JsonSerializer serializer) =>
reader.ValueType == typeof(double) ? throw new JsonSerializationException("Double value for Price") : serializer.Deserialize<decimal>(reader);
public override void WriteJson(JsonWriter writer, Price value, JsonSerializer serializer) =>
serializer.Serialize(writer, (decimal)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();
[JsonConverter(typeof(FloatParseHandlingConverter), FloatParseHandling.Decimal)]
public string Name { get; set; }
public Price Price { get; set; }
public static void Test()
Product product1 = new Product();
string priceSer = JsonConvert.SerializeObject(product1);
Product product2 = JsonConvert.DeserializeObject<Product>(priceSer);
Console.WriteLine(priceSer);
Console.WriteLine("Initial price: {0}", (decimal)product1.Price);
Console.WriteLine("Round-tripped price: {0}", (decimal)product2.Price);
var json2 = JsonConvert.SerializeObject(product2);
Assert.AreEqual(priceSer, json2, $"Original and round-tripped {product1} serialized to JSON differ.");
Assert.AreEqual(((decimal)product1.Price).ToString(NumberFormatInfo.InvariantInfo), ((decimal)product2.Price).ToString(NumberFormatInfo.InvariantInfo), "Original and round-tripped price differ.");
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("{0} version: {1}", typeof(JsonSerializer).Assembly.GetName().Name, 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.Location.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];