better json pretty print

This commit is contained in:
2026-02-16 17:00:06 -06:00
parent 9dbe699033
commit a1ee7dd458
3 changed files with 4 additions and 24 deletions

View File

@@ -6,4 +6,4 @@ var filename = args[0]
var src = text(fd.slurp(filename))
var result = tokenize(src, filename)
var ast = parse(result.tokens, src, filename, tokenize)
print(json.encode(ast))
print(json.encode(ast, true))

View File

@@ -5762,8 +5762,7 @@ static int js_json_to_str (JSContext *ctx, JSONStringifyContext *jsc, JSValue ho
if (ret) {
if (js_get_length64 (ctx, &len, val_ref.val)) goto exception;
/* Check if this is a leaf array for compact mode.
Leaf = no element is an array, and no element is an object
that has array-valued properties. */
Leaf = every element is a primitive or string (no arrays or objects). */
BOOL was_compact = jsc->in_compact_array;
if (jsc->compact_arrays && !jsc->in_compact_array && !JS_IsEmptyString (jsc->gap)) {
BOOL is_leaf = TRUE;
@@ -5773,23 +5772,7 @@ static int js_json_to_str (JSContext *ctx, JSONStringifyContext *jsc, JSValue ho
if (JS_IsArray (v) > 0) {
is_leaf = FALSE;
} else if (mist_is_gc_object (v) && !JS_IsText (v)) {
/* Element is an object — check if any property is an array */
v_ref.val = v;
prop_ref.val = JS_GetOwnPropertyNames (ctx, v_ref.val);
if (!JS_IsException (prop_ref.val)) {
int64_t nprops;
if (!js_get_length64 (ctx, &nprops, prop_ref.val)) {
for (int64_t j = 0; j < nprops && is_leaf; j++) {
JSValue key = JS_GetPropertyNumber (ctx, prop_ref.val, j);
if (!JS_IsException (key)) {
JSValue pval = JS_GetPropertyValue (ctx, v_ref.val, key);
if (JS_IsArray (pval) > 0) is_leaf = FALSE;
}
}
}
}
v_ref.val = JS_NULL;
prop_ref.val = JS_NULL;
is_leaf = FALSE;
}
}
if (is_leaf) jsc->in_compact_array = TRUE;

View File

@@ -16,7 +16,4 @@ var ast = parse(result.tokens, src, filename, tokenize)
var folded = fold(ast)
var compiled = mcode(folded)
var optimized = streamline(compiled)
print(json.encode(optimized, true, function(k,v) {
if (is_array(v)) return json.encode(v)
return v
}))
print(json.encode(optimized, true))