Show / Hide Table of Contents

Enum IntegerTokenUnflattening

Defines how the unflatten operation handles integer tokens in a JSON Pointer

Namespace: JsonCons.Utilities
Assembly: JsonCons.Utilities.dll
Syntax
public enum IntegerTokenUnflattening
Examples

This example illustrates the use of IntegerTokenUnflattening

using System;
using System.Diagnostics;
using System.Text.Json;
using JsonCons.Utilities;

public class Example
{
   public static void Main()
   {
       using var doc = JsonDocument.Parse(@"
{
    ""discards"": {
        ""1000"": ""Record does not exist"",
        ""1004"": ""Queue limit exceeded"",
        ""1010"": ""Discarding timed-out partial msg""
    },
    ""warnings"": {
        ""0"": ""Phone number missing country code"",
        ""1"": ""State code missing"",
        ""2"": ""Zip code missing""
    }
}
       ");

        var options = new JsonSerializerOptions() { WriteIndented = true };

        using JsonDocument flattened = JsonFlattener.Flatten(doc.RootElement);
        Console.WriteLine("The flattened document:\n");
        Console.WriteLine($"{JsonSerializer.Serialize(flattened, options)}\n");

        Console.WriteLine("Unflatten integer tokens as array indices if possible:\n");
        using JsonDocument unflattened1 = JsonFlattener.Unflatten(flattened.RootElement,
                                                            IntegerTokenUnflattening.TryIndex);
        Console.WriteLine($"{JsonSerializer.Serialize(unflattened1, options)}\n");

        Console.WriteLine("Always unflatten integer tokens as object names:\n");
        using JsonDocument unflattened2 = JsonFlattener.Unflatten(flattened.RootElement,
                                                            IntegerTokenUnflattening.AssumeName);
        Console.WriteLine($"{JsonSerializer.Serialize(unflattened2, options)}\n");
    }
}

Output:

The flattened document:

{
  "/discards/1000": "Record does not exist",
  "/discards/1004": "Queue limit exceeded",
  "/discards/1010": "Discarding timed-out partial msg",
  "/warnings/0": "Phone number missing country code",
  "/warnings/1": "State code missing",
  "/warnings/2": "Zip code missing"
}

Unflatten integer tokens as array indices if possible:

{
  "discards": {
    "1000": "Record does not exist",
    "1004": "Queue limit exceeded",
    "1010": "Discarding timed-out partial msg"
  },
  "warnings": [
    "Phone number missing country code",
    "State code missing",
    "Zip code missing"
  ]
}

Always unflatten integer tokens as object names:

{
  "discards": {
    "1000": "Record does not exist",
    "1004": "Queue limit exceeded",
    "1010": "Discarding timed-out partial msg"
  },
  "warnings": {
    "0": "Phone number missing country code",
    "1": "State code missing",
    "2": "Zip code missing"
  }
}

Fields

Name Description
AssumeName

The unflatten operation always unflattens into a JSON object using the integer tokens as names.

TryIndex

The unflatten operation first tries to unflatten into a JSON array using the integer tokens as sequential indices, and if that fails, unflattens into a JSON object using the integer tokens as names.

  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX