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 class PhysicalObject {
[Newtonsoft.Json.JsonProperty("id", Required = Newtonsoft.Json.Required.Default)]
[Newtonsoft.Json.JsonProperty("name", Required = Newtonsoft.Json.Required.Default)]
[Newtonsoft.Json.JsonProperty("description", Required = Newtonsoft.Json.Required.Default)]
public static partial class JsonExtensions
const string refPropertyName = "ref";
public static void ResolveRefererences(JToken root)
if (!(root is JContainer container))
var refs = container.Descendants().OfType<JObject>().Where(o => IsRefObject(o)).ToList();
Console.WriteLine(JsonConvert.SerializeObject(refs));
foreach (var refObj in refs)
var path = GetRefObjectValue(refObj);
var original = ResolveRef(root, path);
refObj.Replace(original);
static bool IsRefObject(JObject obj)
return GetRefObjectValue(obj) != null;
static string GetRefObjectValue(JObject obj)
var refValue = obj[refPropertyName];
if (refValue != null && refValue.Type == JTokenType.String)
static JToken ResolveRef(JToken token, string path)
var components = path.Split('.');
foreach (var component in components)
if (token is JObject obj)
else if (token is JArray array)
token = token[int.Parse(component, NumberFormatInfo.InvariantInfo)];
throw new JsonException("Unexpected token type.");
public static void Test()
var jsonString = GetJson();
var root = JsonConvert.DeserializeObject<JToken>(jsonString, new JsonSerializerSettings { DateParseHandling = DateParseHandling.None });
JsonExtensions.ResolveRefererences(root);
var list = root.ToObject<List<PhysicalObject>>();
Assert.IsTrue(list[0].desc == list[1].desc);
""description"":""Something you can read""
""ref"":""0.description""
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("Failed with unhandled exception: ");
public static string GetNetCoreVersion()
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];