PreserveReferencesHandling setting

April 6, 2024 ยท View on GitHub

This sample shows how the Argon.PreserveReferencesHandling setting can be used to serialize values by reference instead of by value.

public class Directory
{
    public string Name { get; set; }
    public Directory Parent { get; set; }
    public IList<File> Files { get; set; }
}

public class File
{
    public string Name { get; set; }
    public Directory Parent { get; set; }
}

snippet source | anchor

var root = new Directory {Name = "Root"};
var documents = new Directory {Name = "My Documents", Parent = root};

var file = new File
{
    Name = "ImportantLegalDocument.docx",
    Parent = documents
};

documents.Files = [file];

try
{
    JsonConvert.SerializeObject(documents, Formatting.Indented);
}
catch (JsonSerializationException)
{
    // Self referencing loop detected for property 'Parent' with type
    // 'Argon.Tests.Documentation.Examples.ReferenceLoopHandlingObject+Directory'. Path 'Files[0]'.
}

var preserveReferencesAll = JsonConvert.SerializeObject(documents, Formatting.Indented, new JsonSerializerSettings
{
    PreserveReferencesHandling = PreserveReferencesHandling.All
});

Console.WriteLine(preserveReferencesAll);
// {
//   "$id": "1",
//   "Name": "My Documents",
//   "Parent": {
//     "$id": "2",
//     "Name": "Root",
//     "Parent": null,
//     "Files": null
//   },
//   "Files": {
//     "$id": "3",
//     "$values": [
//       {
//         "$id": "4",
//         "Name": "ImportantLegalDocument.docx",
//         "Parent": {
//           "$ref": "1"
//         }
//       }
//     ]
//   }
// }

var preserveReferenacesObjects = JsonConvert.SerializeObject(documents, Formatting.Indented, new JsonSerializerSettings
{
    PreserveReferencesHandling = PreserveReferencesHandling.Objects
});

Console.WriteLine(preserveReferenacesObjects);
// {
//   "$id": "1",
//   "Name": "My Documents",
//   "Parent": {
//     "$id": "2",
//     "Name": "Root",
//     "Parent": null,
//     "Files": null
//   },
//   "Files": [
//     {
//       "$id": "3",
//       "Name": "ImportantLegalDocument.docx",
//       "Parent": {
//         "$ref": "1"
//       }
//     }
//   ]
// }

snippet source | anchor