[quickjs-devel] [PATCH] Make indented JSON.stringify output match the browser's/NodeJS format

  • From: deibeemoocheuguhumoh@xxxxxx
  • To: quickjs-devel@xxxxxxxxxxxxx
  • Date: Fri, 16 Aug 2019 12:34:01 +0200

From: Ondrej Jirman <megous@xxxxxxxxxx>

Signed-off-by: Ondrej Jirman <megous@xxxxxxxxxx>
---
 quickjs.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

Hello,

I guess it's just aesthetics, but here's a patch for a more NodeJS/Browser like
stringify output.

Before:

[
{
  "res": [
    {
      "id": 20,
      "b": {},
      "flo": 2.548664,
      "flo4": 2.54866,
      "ts1": "2019-08-16 12:26:21.989991",
      "ts2": "2019-08-16 12:26:21.989991+02"
      }
    ],
  "res2": [
    {
      "id": 20,
      "j": null,
      "jb": {
        "a": 2
        }
      }
    ]
  }
]

After:

[
  {
    "res": [
      {
        "id": 21,
        "b": {},
        "flo": 2.548664,
        "flo4": 2.54866,
        "ts1": "2019-08-16 12:27:19.898404",
        "ts2": "2019-08-16 12:27:19.898404+02"
      }
    ],
    "res2": [
      {
        "id": 21,
        "j": null,
        "jb": {
          "a": 2
        }
      }
    ]
  }
]

regards,
        Ondrej

diff --git a/quickjs.c b/quickjs.c
index a54faf3..caf3b7e 100644
--- a/quickjs.c
+++ b/quickjs.c
@@ -41143,7 +41143,8 @@ exception:
 
 static int js_json_to_str(JSContext *ctx, JSONStringifyContext *jsc,
                           JSValueConst holder, JSValue val,
-                          JSValueConst key, JSValueConst indent)
+                          JSValueConst key, JSValueConst prev_indent,
+                         JSValueConst indent)
 {
     JSValue indent1, sep, sep1, tab, v, prop;
     JSObject *p;
@@ -41219,12 +41220,12 @@ static int js_json_to_str(JSContext *ctx, 
JSONStringifyContext *jsc,
                     goto exception;
                 if (JS_IsUndefined(v))
                     v = JS_NULL;
-                if (js_json_to_str(ctx, jsc, val, v, JS_NewInt32(ctx, i), 
indent1))
+                if (js_json_to_str(ctx, jsc, val, v, JS_NewInt32(ctx, i), 
indent, indent1))
                     goto exception;
             }
             if (len > 0 && !JS_IsEmptyString(jsc->gap)) {
                 string_buffer_putc8(jsc->b, '\n');
-                string_buffer_concat_value(jsc->b, indent);
+                string_buffer_concat_value(jsc->b, prev_indent);
             }
             string_buffer_putc8(jsc->b, ']');
         } else {
@@ -41260,13 +41261,13 @@ static int js_json_to_str(JSContext *ctx, 
JSONStringifyContext *jsc,
                     string_buffer_concat_value(jsc->b, prop);
                     string_buffer_putc8(jsc->b, ':');
                     string_buffer_concat_value(jsc->b, sep1);
-                    if (js_json_to_str(ctx, jsc, val, v, prop, indent1))
+                    if (js_json_to_str(ctx, jsc, val, v, prop, indent, 
indent1))
                         goto exception;
                 }
             }
             if (len > 0 && JS_VALUE_GET_STRING(jsc->gap)->len != 0) {
                 string_buffer_putc8(jsc->b, '\n');
-                string_buffer_concat_value(jsc->b, indent);
+                string_buffer_concat_value(jsc->b, prev_indent);
             }
             string_buffer_putc8(jsc->b, '}');
         }
@@ -41386,7 +41387,7 @@ static JSValue js_json_stringify(JSContext *ctx, 
JSValueConst this_val,
         ret = JS_UNDEFINED;
         goto done1;
     }
-    if (js_json_to_str(ctx, jsc, JS_NULL, val, jsc->empty, jsc->empty))
+    if (js_json_to_str(ctx, jsc, JS_NULL, val, jsc->empty, jsc->empty, 
jsc->gap))
         goto exception;
 
     ret = string_buffer_end(jsc->b);
-- 
2.22.1


Other related posts:

  • » [quickjs-devel] [PATCH] Make indented JSON.stringify output match the browser's/NodeJS format - deibeemoocheuguhumoh