A complete JSON formatter that takes a minified string and pretty-prints it with proper indentation.
ID: 1382
using uCalcSoftware;
var uc = new uCalc();
using (var t = new uCalc.Transformer(uc)) {
// 1. Define state variable in the uCalc instance.
uc.DefineVariable("indent = 0");
// 2. Define the transformation rules.
// Note: '{', '}', '[', ']' are escaped with quotes to be treated as literals.
// Rule for '{' and '[': Add newline, increment indent, add indent string.
t.FromTo("{ '{' | '[' }", "{@Self}{@nl}{@Exec: indent++}{@Eval: ' ' * indent}");
// Rule for '}' and ']': Add newline, decrement indent, add indent string.
t.FromTo("{ '}' | ']' }", "{@nl}{@Exec: indent--}{@Eval: ' ' * indent}{@Self}");
// Rule for ',': Add newline and current indent string.
t.FromTo(",", ",{@nl}{@Eval: ' ' * indent}");
// Rule for ':': Add a space after it for readability.
t.FromTo(":", ": ");
// 3. Define the minified input string.
var minifiedJson = """
{"id":123,"name":"Example","tags":["A","B"],"active":true}
""";
// 4. Run the transformation and print the result.
Console.WriteLine(t.Transform(minifiedJson));
}
{
"id": 123,
"name": "Example",
"tags": [
"A",
"B"
],
"active": true
} using uCalcSoftware; var uc = new uCalc(); using (var t = new uCalc.Transformer(uc)) { // 1. Define state variable in the uCalc instance. uc.DefineVariable("indent = 0"); // 2. Define the transformation rules. // Note: '{', '}', '[', ']' are escaped with quotes to be treated as literals. // Rule for '{' and '[': Add newline, increment indent, add indent string. t.FromTo("{ '{' | '[' }", "{@Self}{@nl}{@Exec: indent++}{@Eval: ' ' * indent}"); // Rule for '}' and ']': Add newline, decrement indent, add indent string. t.FromTo("{ '}' | ']' }", "{@nl}{@Exec: indent--}{@Eval: ' ' * indent}{@Self}"); // Rule for ',': Add newline and current indent string. t.FromTo(",", ",{@nl}{@Eval: ' ' * indent}"); // Rule for ':': Add a space after it for readability. t.FromTo(":", ": "); // 3. Define the minified input string. var minifiedJson = """ {"id":123,"name":"Example","tags":["A","B"],"active":true} """; // 4. Run the transformation and print the result. Console.WriteLine(t.Transform(minifiedJson)); }
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
{
uCalc::Transformer t(uc);
t.Owned(); // Causes t to be released when it goes out of scope
// 1. Define state variable in the uCalc instance.
uc.DefineVariable("indent = 0");
// 2. Define the transformation rules.
// Note: '{', '}', '[', ']' are escaped with quotes to be treated as literals.
// Rule for '{' and '[': Add newline, increment indent, add indent string.
t.FromTo("{ '{' | '[' }", "{@Self}{@nl}{@Exec: indent++}{@Eval: ' ' * indent}");
// Rule for '}' and ']': Add newline, decrement indent, add indent string.
t.FromTo("{ '}' | ']' }", "{@nl}{@Exec: indent--}{@Eval: ' ' * indent}{@Self}");
// Rule for ',': Add newline and current indent string.
t.FromTo(",", ",{@nl}{@Eval: ' ' * indent}");
// Rule for ':': Add a space after it for readability.
t.FromTo(":", ": ");
// 3. Define the minified input string.
auto minifiedJson = R"({"id":123,"name":"Example","tags":["A","B"],"active":true})";
// 4. Run the transformation and print the result.
cout << t.Transform(minifiedJson) << endl;
}
}
{
"id": 123,
"name": "Example",
"tags": [
"A",
"B"
],
"active": true
} #include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; { uCalc::Transformer t(uc); t.Owned(); // Causes t to be released when it goes out of scope // 1. Define state variable in the uCalc instance. uc.DefineVariable("indent = 0"); // 2. Define the transformation rules. // Note: '{', '}', '[', ']' are escaped with quotes to be treated as literals. // Rule for '{' and '[': Add newline, increment indent, add indent string. t.FromTo("{ '{' | '[' }", "{@Self}{@nl}{@Exec: indent++}{@Eval: ' ' * indent}"); // Rule for '}' and ']': Add newline, decrement indent, add indent string. t.FromTo("{ '}' | ']' }", "{@nl}{@Exec: indent--}{@Eval: ' ' * indent}{@Self}"); // Rule for ',': Add newline and current indent string. t.FromTo(",", ",{@nl}{@Eval: ' ' * indent}"); // Rule for ':': Add a space after it for readability. t.FromTo(":", ": "); // 3. Define the minified input string. auto minifiedJson = R"({"id":123,"name":"Example","tags":["A","B"],"active":true})"; // 4. Run the transformation and print the result. cout << t.Transform(minifiedJson) << endl; } }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
Using t As New uCalc.Transformer(uc)
'// 1. Define state variable in the uCalc instance.
uc.DefineVariable("indent = 0")
'// 2. Define the transformation rules.
'// Note: '{', '}', '[', ']' are escaped with quotes to be treated as literals.
'// Rule for '{' and '[': Add newline, increment indent, add indent string.
t.FromTo("{ '{' | '[' }", "{@Self}{@nl}{@Exec: indent++}{@Eval: ' ' * indent}")
'// Rule for '}' and ']': Add newline, decrement indent, add indent string.
t.FromTo("{ '}' | ']' }", "{@nl}{@Exec: indent--}{@Eval: ' ' * indent}{@Self}")
'// Rule for ',': Add newline and current indent string.
t.FromTo(",", ",{@nl}{@Eval: ' ' * indent}")
'// Rule for ':': Add a space after it for readability.
t.FromTo(":", ": ")
'// 3. Define the minified input string.
Dim minifiedJson = "{""id"":123,""name"":""Example"",""tags"":[""A"",""B""],""active"":true}"
'// 4. Run the transformation and print the result.
Console.WriteLine(t.Transform(minifiedJson))
End Using
End Sub
End Module
{
"id": 123,
"name": "Example",
"tags": [
"A",
"B"
],
"active": true
} Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() Using t As New uCalc.Transformer(uc) '// 1. Define state variable in the uCalc instance. uc.DefineVariable("indent = 0") '// 2. Define the transformation rules. '// Note: '{', '}', '[', ']' are escaped with quotes to be treated as literals. '// Rule for '{' and '[': Add newline, increment indent, add indent string. t.FromTo("{ '{' | '[' }", "{@Self}{@nl}{@Exec: indent++}{@Eval: ' ' * indent}") '// Rule for '}' and ']': Add newline, decrement indent, add indent string. t.FromTo("{ '}' | ']' }", "{@nl}{@Exec: indent--}{@Eval: ' ' * indent}{@Self}") '// Rule for ',': Add newline and current indent string. t.FromTo(",", ",{@nl}{@Eval: ' ' * indent}") '// Rule for ':': Add a space after it for readability. t.FromTo(":", ": ") '// 3. Define the minified input string. Dim minifiedJson = "{""id"":123,""name"":""Example"",""tags"":[""A"",""B""],""active"":true}" '// 4. Run the transformation and print the result. Console.WriteLine(t.Transform(minifiedJson)) End Using End Sub End Module