using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Microsoft.VisualStudio.TestTools.UnitTesting;
public class PreferISerializableContractResolver : DefaultContractResolver
protected override JsonContract CreateContract(Type objectType)
var contract = base.CreateContract(objectType);
if (!IgnoreSerializableInterface
&& contract is JsonObjectContract
&& typeof(ISerializable).IsAssignableFrom(objectType)
&& !objectType.GetCustomAttributes(true).OfType<JsonContainerAttribute>().Any())
contract = CreateISerializableContract(objectType);
var settings = new JsonSerializerSettings
ContractResolver = new PreferISerializableContractResolver(),
AssertFailedException testAssertException = new AssertFailedException(msg: "This is a test");
JToken assertJToken = JToken.FromObject(testAssertException, JsonSerializer.CreateDefault(settings));
Console.WriteLine(assertJToken);
var newException = assertJToken.ToObject<Exception>();
Console.WriteLine("Exception deserialized successfully: ");
Console.WriteLine(newException);
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("All tests passed.");
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];