Last active
April 28, 2024 18:05
-
-
Save carlin-q-scott/4c8a9cce734fa5b10a97 to your computer and use it in GitHub Desktop.
A JSON.Net IReferenceResolver that resolves field references using a Name property if available, otherwise it uses an index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Person | |
{ | |
public string Name {get; set;} | |
public string PhoneNumber {get; set;} | |
public Person Partner {get; set;} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"$id": "Jane Doe", | |
"Name": "Jane Doe", | |
"PhoneNumber": "(555)555-5555", | |
"Partner": { | |
"$id": "John Doe", | |
"Name": "John Doe", | |
"PhoneNumber": "(555)555-5555", | |
"Partner": { | |
"$ref": "Jane Doe" | |
} | |
} | |
}, | |
{ | |
"$ref": "John Doe", | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Newtonsoft.Json.Serialization; | |
namespace MyNewtonsoftExtensions | |
{ | |
public class ReferenceResolverWithNames : IReferenceResolver | |
{ | |
private readonly ReferenceCollection _collection = new ReferenceCollection(); | |
public object ResolveReference(object context, string reference) | |
{ | |
return _collection.Get(reference); | |
} | |
public string GetReference(object context, object value) | |
{ | |
if (_collection.Contains(value)) return _collection.GetReference(value); | |
return _collection.Add(value); | |
} | |
public bool IsReferenced(object context, object value) | |
{ | |
return _collection.Contains(value); | |
} | |
public void AddReference(object context, string reference, object value) | |
{ | |
_collection.Add(value, reference); | |
} | |
class ReferenceCollection | |
{ | |
private readonly Dictionary<Type, Dictionary<string, object>> _referenceDictionary = new Dictionary<Type, Dictionary<string, object>>(); | |
internal bool Contains(object obj) | |
{ | |
var objType = obj.GetType(); | |
Dictionary<string, object> objDictionary; | |
if (_referenceDictionary.TryGetValue(objType, out objDictionary)) | |
return objDictionary.ContainsValue(obj); | |
return false; | |
} | |
internal string Add(object obj, string key = null) | |
{ | |
var objType = obj.GetType(); | |
Dictionary<string, object> objDictionary; | |
if (!_referenceDictionary.TryGetValue(objType, out objDictionary)) | |
_referenceDictionary.Add(objType, objDictionary = new Dictionary<string, object>()); | |
if (key == null) | |
{ | |
var objPropertyInfo = objType.GetProperty("Name"); | |
if (objPropertyInfo != null) key = (string) objPropertyInfo.GetValue(obj); | |
else key = objDictionary.Count.ToString(); | |
} | |
if (!objDictionary.ContainsKey(key)) objDictionary.Add(key, obj); | |
return key; | |
} | |
internal object Get(string reference) | |
{ | |
//this requires that names be unique within a json file | |
var typeDictionary = _referenceDictionary.Values.SingleOrDefault(typeDict => typeDict.ContainsKey(reference)); | |
if (typeDictionary == null) throw new InvalidOperationException(string.Format(@"value with for $ref: ""{0}"" hasn't been deserialized yet.", reference)); | |
return typeDictionary[reference]; | |
} | |
internal string GetReference(object obj) | |
{ | |
var objType = obj.GetType(); | |
var objPropertyInfo = objType.GetProperty("Name"); | |
if (objPropertyInfo != null) return (string)objPropertyInfo.GetValue(obj); | |
Dictionary<string, object> objDictionary; | |
if (!_referenceDictionary.TryGetValue(objType, out objDictionary)) | |
_referenceDictionary.Add(objType, objDictionary = new Dictionary<string, object>()); | |
return objDictionary.Count.ToString(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JsonConvert.DefaultSettings = () => new JsonSerializerSettings | |
{ | |
Formatting = Formatting.Indented, | |
ReferenceResolverProvider = () => new ReferenceResolverWithNames() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
return objDictionary.Count.ToString();
There is an error. For the case when the key is not found, you must use the IndexOf(obj)