25

Currently in Business Central (even the latest BC23.x), when stringifying a JsonObject or JsonArray AL datatypes to a string produces a minimified single-line JSON string representation of the JSON data.


There are use cases where we need to produced a formatted JSON string (with indentation and linefeeds).


Without such a function, developers have to roll their own stream based JSON formatters or format the exported JSON via an external tool (such as VS Code, notepad++, online JSON tools, etc). Streams are important for performance but this idea should be applied to both WriteTo(text) and WriteTo(OutStream).


There are legacy JSON codeunits that ship with the Microsoft Base App to facilitate JSON formatting, but since they work with strings and not streams, they are slow and will crap out with an out of memory error when transforming very large JSON objects and arrays.


Since the AL JsonObject and JsonArray datatypes are simply wrappers to dotnet namespaces such as Newtonsoft and/or System.text.Json, it makes more sense to ammend the AL .WriteTo(var text) and .WriteTo(var OutStream) functions found in datatypes such as JsonObject and JsonArray with an optional JsonFormattingOptions parameter (currently there is no such datatype that exists in Business Central AL).


With this, developers could optionally call WriteTo as follows:


JsonObject.WriteTo(OutStream,JsonFormattingOptions);

JsonObject.WriteTo(Text,JsonFormattingOptions);


or optionally omit the second parameter which would produce an unformatted JSON string as what currently happens.


The JsonFormattingOptions datatype can include boolean getters and setters for specifying what kind of quote character to use (single or souble quote), the number of ispaces to indent, max depth, target locale, include (or don't include) CRLFs, etc).


The current JsonObject/JsonArray WriteTo procedure omitts all formatting and uses the double quote (") for JSON member declarations and JSON text values.



Category: Development
STATUS DETAILS
New