Class JsonFlattener
Provides functionality to flatten a JSON object or array to a single depth JSON object of JSON Pointer-value pairs, and to unflatten a flattened JSON object.
Inheritance
Inherited Members
Namespace: JsonCons.Utilities
Assembly: JsonCons.Utilities.dll
Syntax
public static class JsonFlattener
Examples
This example shows how to flatten and unflatten a JSON value
using System;
using System.Diagnostics;
using System.Text.Json;
using JsonCons.Utilities;
public class Example
{
public static void Main()
{
using var doc = JsonDocument.Parse(@"
{
""application"": ""hiking"",
""reputons"": [
{
""rater"": ""HikingAsylum"",
""assertion"": ""advanced"",
""rated"": ""Marilyn C"",
""rating"": 0.90
},
{
""rater"": ""HikingAsylum"",
""assertion"": ""intermediate"",
""rated"": ""Hongmin"",
""rating"": 0.75
}
]
}
");
using JsonDocument flattened = JsonFlattener.Flatten(doc.RootElement);
var options = new JsonSerializerOptions() { WriteIndented = true };
Console.WriteLine($"{JsonSerializer.Serialize(flattened, options)}\n");
using JsonDocument unflattened = JsonFlattener.Unflatten(flattened.RootElement);
var comparer = JsonElementEqualityComparer.Instance;
Debug.Assert(comparer.Equals(unflattened.RootElement,doc.RootElement));
}
}
Output:
{
"/application": "hiking",
"/reputons/0/rater": "HikingAsylum",
"/reputons/0/assertion": "advanced",
"/reputons/0/rated": "Marilyn C",
"/reputons/0/rating": 0.90,
"/reputons/1/rater": "HikingAsylum",
"/reputons/1/assertion": "intermediate",
"/reputons/1/rated": "Hongmin",
"/reputons/1/rating": 0.75
}
Methods
| Improve this Doc View SourceFlatten(JsonElement)
Converts a JSON object or array into a single depth JSON object of name-value pairs, such that the names are JSON Pointer strings, and the values are either string, number, true, false, null, empty object, or empty array.
Declaration
public static JsonDocument Flatten(JsonElement value)
Parameters
Type | Name | Description |
---|---|---|
System.Text.Json.JsonElement | value | The value to be flattened. |
Returns
Type | Description |
---|---|
System.Text.Json.JsonDocument | The flattened value |
Remarks
It is the users responsibilty to properly Dispose the returned System.Text.Json.JsonDocument value
Unflatten(JsonElement, IntegerTokenUnflattening)
Recovers the orginal JSON value from a JSON object in flattened form, to the extent possible.
There may not be a unique solution, an integer token in a JSON Pointer could be an array index or
it could be an object name. The default behavior is to attempt to recover arrays. The options
parameter can be used to recover objects with integer names instead.
Declaration
public static JsonDocument Unflatten(JsonElement flattenedValue, IntegerTokenUnflattening options = IntegerTokenUnflattening.TryIndex)
Parameters
Type | Name | Description |
---|---|---|
System.Text.Json.JsonElement | flattenedValue | The flattened value, which must be a JSON object of name-value pairs, such that the names are JSON Pointer strings, and the values are either string, number, true, false, null, empty object, or empty array. |
IntegerTokenUnflattening | options | Options for handling integer tokens in the JSON Pointer. |
Returns
Type | Description |
---|---|
System.Text.Json.JsonDocument | The unflattened value |
Remarks
It is the users responsibilty to properly Dispose the returned System.Text.Json.JsonDocument value
Exceptions
Type | Condition |
---|---|
System.ArgumentException | The |