CSharp · Console
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.IO;
using System.Data;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using NUnit.Framework; // Throws NUnit.Framework.AssertionException
//https://stackoverflow.com/questions/54550629/newtonsoft-json-net-jsonconverter-attribute-preserve-references-issue-when-deser
[JsonObject(IsReference = true)]
public abstract class ModelBase : IModelBase
{
public string ID { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime ModifiedAt { get; set; }
}
public class CustomerModel : ModelBase
{
public string Name { get; set; }
public UserModel CreatedBy { get; set; }
public UserModel ModifiedBy { get; set; }
}
public class UserModel : ModelBase
{
public string Name { get; set; }
public UserModel CreatedBy { get; set; }
public UserModel ModifiedBy { get; set; }
}
public interface IModelBase
{
}
class TestClass
{
public static void Test()
{
TestDeserialize();
}
static object Deserialize(Stream streamFromWebAPICall, Type objectType)
{
using (var sr = new StreamReader(streamFromWebAPICall))
{
using (var jtr = new JsonTextReader(sr))
{
var settings = new JsonSerializerSettings
{
DefaultValueHandling = DefaultValueHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore,
};
var js = JsonSerializer.CreateDefault(settings);
return js.Deserialize(jtr, objectType);
}
}
}
static void TestDeserialize()
{
var inputJson = GetInputJson();
var streamFromWebAPICall = new MemoryStream(Encoding.UTF8.GetBytes(inputJson));
var root = (List<CustomerModel>)Deserialize(streamFromWebAPICall, typeof(List<CustomerModel>));
var settings = new JsonSerializerSettings
{
DefaultValueHandling = DefaultValueHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore,
};
var outputJson = JsonConvert.SerializeObject(root, Formatting.Indented, settings);
Console.WriteLine("Round-tripped {0}:", root);
Console.WriteLine(outputJson);
Assert.IsTrue(JToken.DeepEquals(JToken.Parse(inputJson), JToken.Parse(outputJson)));
Assert.IsTrue(root.Count == 2 && root[1].CreatedBy == root[0].CreatedBy && root[1].ModifiedBy == root[0].CreatedBy);
}
static string GetInputJson()
{
var json = @"[
{
""$id"": ""1"",
""Name"": ""Customer1"",
""CreatedBy"": {
""$id"": ""2"",
""ID"": ""1"",
""Name"": ""User1""
},
""ModifiedBy"": {
""$id"": ""3"",
""ID"": ""3"",
""Name"": ""User3""
},
""ID"": ""1"",
""CreatedAt"": ""2019-02-06T00:00:04"",
""ModifiedAt"": ""2019-02-06T00:20:12""
},
{
""$id"": ""4"",
""Name"": ""Customer2"",
""CreatedBy"": {
""$ref"": ""2""
},
""ModifiedBy"": {
""$ref"": ""2""
},
""ID"": ""2"",
""CreatedAt"": ""2019-02-06T00:10:00"",
""ModifiedAt"": ""2019-02-06T00:10:00""
}
]";
return json;
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Environment version: " + Environment.Version);
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine();
try
{
TestClass.Test();
}
catch (Exception ex)
{
Console.WriteLine("Failed with unhandled exception: ");
Console.WriteLine(ex);
throw;
}
}
}