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 int UserId { get; set; }
public int Id { get; set; }
public ResourceId ResourceId { get; set; }
[JsonProperty("user_id")]
return ResourceId == null ? (int ?)null : ResourceId.UserId;
ResourceId = new ResourceId();
ResourceId.UserId = value.Value;
[JsonProperty("resource_id")]
return ResourceId == null ? (int?)null : ResourceId.Id;
ResourceId = new ResourceId();
ResourceId.Id = value.Value;
[JsonProperty("resource_name")]
public string ResourceName { get; set; }
public static void Test()
var json = @"{ ""user_id"": 0, ""resource_id"": 0, ""resource_name"": ""file.xml""}";
var resource = JsonConvert.DeserializeObject<Resource>(json);
Assert.AreEqual(0, resource?.ResourceId.UserId);
Assert.AreEqual(0, resource?.ResourceId.Id);
Assert.AreEqual(0, resource?.ResourceId.UserId);
Assert.AreEqual("file.xml", resource.ResourceName);
var newJson = JsonConvert.SerializeObject(resource);
Console.WriteLine(newJson);
Assert.IsTrue(JToken.DeepEquals(JToken.Parse(json), JToken.Parse(newJson)));
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];