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;
public interface IJsonProperty<TModel>
public object Data { get; set; }
public class JsonProperty<TModel> : IJsonProperty<TModel>
public object Data { get; set; }
public class IJsonPropertyConverter<TModel> : JsonConverter<IJsonProperty<TModel>>
public override void WriteJson(JsonWriter writer, IJsonProperty<TModel> value, JsonSerializer serializer) =>
writer.WriteFormattedRawValue((string)value.Data);
public override IJsonProperty<TModel> ReadJson(JsonReader reader, Type objectType, IJsonProperty<TModel> existingValue, bool hasExistingValue, JsonSerializer serializer)
=> throw new NotImplementedException();
public static class JsonExtensions
public static void WriteFormattedRawValue(this JsonWriter writer, string json, DateParseHandling dateParseHandling = DateParseHandling.None, FloatParseHandling floatParseHandling = default)
writer.WriteRawValue(json);
using (var reader = new JsonTextReader(new StringReader(json)) { DateParseHandling = dateParseHandling, FloatParseHandling = floatParseHandling })
writer.WriteToken(reader);
public static void Test()
var model = new JsonProperty<object>
var settings = new JsonSerializerSettings
Converters = { new IJsonPropertyConverter<object>() }
var json1 = JsonConvert.SerializeObject(model, Formatting.Indented);
Console.WriteLine("Json without converter:");
Console.WriteLine(json1);
Console.WriteLine("Json with converter:");
var json2 = JsonConvert.SerializeObject(model, Formatting.Indented, settings);
Console.WriteLine(json2);
static string GetJson() => JToken.Parse(@"{
""title"": ""example glossary"",
""GlossTerm"": ""Standard Generalized Markup Language"",
""Abbrev"": ""ISO 8879:1986"",
""para"": ""A meta-markup language, used to create markup languages such as DocBook."",
""GlossSeeAlso"": [""GML"", ""XML""]
}").ToString(Formatting.None);
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];