better json pretty print
This commit is contained in:
2
parse.ce
2
parse.ce
@@ -6,4 +6,4 @@ var filename = args[0]
|
|||||||
var src = text(fd.slurp(filename))
|
var src = text(fd.slurp(filename))
|
||||||
var result = tokenize(src, filename)
|
var result = tokenize(src, filename)
|
||||||
var ast = parse(result.tokens, src, filename, tokenize)
|
var ast = parse(result.tokens, src, filename, tokenize)
|
||||||
print(json.encode(ast))
|
print(json.encode(ast, true))
|
||||||
|
|||||||
@@ -5762,8 +5762,7 @@ static int js_json_to_str (JSContext *ctx, JSONStringifyContext *jsc, JSValue ho
|
|||||||
if (ret) {
|
if (ret) {
|
||||||
if (js_get_length64 (ctx, &len, val_ref.val)) goto exception;
|
if (js_get_length64 (ctx, &len, val_ref.val)) goto exception;
|
||||||
/* Check if this is a leaf array for compact mode.
|
/* Check if this is a leaf array for compact mode.
|
||||||
Leaf = no element is an array, and no element is an object
|
Leaf = every element is a primitive or string (no arrays or objects). */
|
||||||
that has array-valued properties. */
|
|
||||||
BOOL was_compact = jsc->in_compact_array;
|
BOOL was_compact = jsc->in_compact_array;
|
||||||
if (jsc->compact_arrays && !jsc->in_compact_array && !JS_IsEmptyString (jsc->gap)) {
|
if (jsc->compact_arrays && !jsc->in_compact_array && !JS_IsEmptyString (jsc->gap)) {
|
||||||
BOOL is_leaf = TRUE;
|
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) {
|
if (JS_IsArray (v) > 0) {
|
||||||
is_leaf = FALSE;
|
is_leaf = FALSE;
|
||||||
} else if (mist_is_gc_object (v) && !JS_IsText (v)) {
|
} else if (mist_is_gc_object (v) && !JS_IsText (v)) {
|
||||||
/* Element is an object — check if any property is an array */
|
is_leaf = FALSE;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (is_leaf) jsc->in_compact_array = TRUE;
|
if (is_leaf) jsc->in_compact_array = TRUE;
|
||||||
|
|||||||
@@ -16,7 +16,4 @@ var ast = parse(result.tokens, src, filename, tokenize)
|
|||||||
var folded = fold(ast)
|
var folded = fold(ast)
|
||||||
var compiled = mcode(folded)
|
var compiled = mcode(folded)
|
||||||
var optimized = streamline(compiled)
|
var optimized = streamline(compiled)
|
||||||
print(json.encode(optimized, true, function(k,v) {
|
print(json.encode(optimized, true))
|
||||||
if (is_array(v)) return json.encode(v)
|
|
||||||
return v
|
|
||||||
}))
|
|
||||||
|
|||||||
Reference in New Issue
Block a user