.NET Library - j-brooke/FracturedJson GitHub Wiki
.NET Library
To use FracturedJson as a library, add the NuGet package. It's available for use under the MIT license.
Requirements
The FracturedJson .NET code conforms to .NET Standard 2.0, which means it's compatible with a lot of the .NET world.
Usage
- Instantiate an instance of
FracturedJson.Formatter
- Optionally adjust settings in
Formatter.Options
. - Call
Formatter.Reformat(string jsonData, int startingDepth)
, orFormatter.Serialize<T>(T obj, int startingDepth, JsonSerializerOptions? serOpts = null)
.
Example
// Set options using defaults that won't change from one minor version to the next.
// If you want don't care about backward compatibility and just want the current
// best defaults, use FracturedJsonOptions.Recommended() instead of new.
var opts = new FracturedJsonOptions() { MaxTotalLineLength = 90 };
var formatter = new Formatter() { Options = opts };
var output = formatter.Reformat(input, 0);
Console.WriteLine(output);
Or
var input = GetWhateverYouWantToSerialize();
var fracturedJsonOptions = new FracturedJsonOptions() { MaxTotalLineLength = 90 };
var serialOptions = new JsonSerializerOptions()
{
Converters = { new JsonStringEnumConverter() }
};
var formatter = new Formatter() { Options = fracturedJsonOptions };
var output = formatter.Serialize(input, 0, serialOptions);
Options
See the Options wiki page for examples of how to control the format.
Note that the FracturedJsonOptions
constructor should give defaults that produce the same behavior for all releases with the same major version number. Starting with version 4.1.0, if you're not worried about backward compatibility, you can use the factory method Recommended()
instead of the constructor to get an object with defaults potentially including new features. For example:
var opts = FracturedJsonOptions.Recommended() with { MaxTotalLineLength = 90 };
Wide Character Support
In cases such as table formatting, FracturedJson needs to measure string lengths to make things line up. By default, it uses String.Length to determine the width of strings. That generally works fine for Western character sets covered by 16-bit Unicode, but some East Asian characters are rendered at twice the width of Latin characters. Furthermore, String.Length
measures in terms of 16-bit char
s, but some Unicode glyphs require two of those.
If your data involves any cases like that, you can provide your own function to Formatter.StringLengthFunc
. You'll probably rely on an external library such as Wcwidth. See the unit test for an example.
Of course, whether the data actually looks properly aligned will depend on your OS, font, and localization.
Serialization Options
Formatter.Serialize
relies on .NET's System.Text.Json
services to convert from .NET classes and structures to a JSON DOM. If you need to control how that conversion is performed - for example, whether enums should be written as strings or ints - you can provide an optional JsonSerializerOptions instance to the Serialize
call.