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;
using Microsoft.Azure.Cosmos.Table;
public class EntityPropertyConverter : JsonConverter<EntityProperty>
public override void WriteJson(JsonWriter writer, EntityProperty value, JsonSerializer serializer) =>
serializer.Serialize(writer, value.PropertyAsObject);
public override EntityProperty ReadJson(JsonReader reader, Type objectType, EntityProperty existingValue, bool hasExistingValue, JsonSerializer serializer)
switch (reader.MoveToContentAndAssert().TokenType)
if (reader.Value is int i)
return new EntityProperty(i);
else if (reader.Value is long l)
return new EntityProperty(l);
if (reader.Value is double d)
return new EntityProperty(d);
else if (reader.Value is decimal m)
return new EntityProperty((double)m);
return new EntityProperty((string)reader.Value);
return new EntityProperty((bool)reader.Value);
if (reader.Value is DateTime dt)
return new EntityProperty(dt);
else if (reader.Value is DateTimeOffset dto)
return new EntityProperty(dto);
if (reader.Value is byte [] a)
return new EntityProperty(a);
else if (reader.Value is Guid g)
return new EntityProperty(g);
throw new JsonSerializationException(string.Format("Cannot convert value {0} to {1}", reader.TokenType, nameof(EntityProperty)));
public static partial class JsonExtensions
public static JsonReader MoveToContentAndAssert(this JsonReader reader)
throw new ArgumentNullException();
if (reader.TokenType == JsonToken.None)
while (reader.TokenType == JsonToken.Comment)
public static JsonReader ReadAndAssert(this JsonReader reader)
throw new ArgumentNullException();
throw new JsonReaderException("Unexpected end of JSON stream.");
public static void Test()
foreach (var json in GetJson())
Console.WriteLine("Deserializing JSON:");
var data = JObject.Parse(json);
var settings = new JsonSerializerSettings
Converters = { new EntityPropertyConverter() },
var dict = data.ToObject<Dictionary<string, EntityProperty>>(JsonSerializer.CreateDefault(settings));
Console.WriteLine("Re-serialized dictionary:");
Console.WriteLine(JsonConvert.SerializeObject(dict, Formatting.Indented, settings));
static IEnumerable<string> GetJson()
""dateTimeValue"": ""2021-02-19T14:55:56.1096788Z"",
""Message"": ""Passed (0.325 mA < 2[0] => 'P')"",
""HostVersion"": ""Test"",
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];