mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-19 19:10:17 +00:00
fix json indent
This commit is contained in:
parent
3b85192d23
commit
1f8212dadf
@ -53,6 +53,10 @@ static void json__write_indent(c11_sbuf* buf, int n_spaces) {
|
||||
|
||||
static bool json__write_array(c11_sbuf* buf, py_TValue* arr, int length, int indent, int depth) {
|
||||
c11_sbuf__write_char(buf, '[');
|
||||
if(length == 0) {
|
||||
c11_sbuf__write_char(buf, ']');
|
||||
return true;
|
||||
}
|
||||
if(indent > 0) c11_sbuf__write_char(buf, '\n');
|
||||
int n_spaces = indent * depth;
|
||||
const char* sep = indent > 0 ? ",\n" : ", ";
|
||||
@ -125,6 +129,10 @@ static bool json__write_object(c11_sbuf* buf, py_TValue* obj, int indent, int de
|
||||
}
|
||||
case tp_dict: {
|
||||
c11_sbuf__write_char(buf, '{');
|
||||
if(py_dict_len(obj) == 0) {
|
||||
c11_sbuf__write_char(buf, '}');
|
||||
return true;
|
||||
}
|
||||
if(indent > 0) c11_sbuf__write_char(buf, '\n');
|
||||
json__write_dict_kv_ctx ctx = {.buf = buf,
|
||||
.first = true,
|
||||
@ -140,13 +148,18 @@ static bool json__write_object(c11_sbuf* buf, py_TValue* obj, int indent, int de
|
||||
return true;
|
||||
}
|
||||
case tp_namedict: {
|
||||
py_Ref original = py_getslot(obj, 0);
|
||||
c11_sbuf__write_char(buf, '{');
|
||||
if(PyObject__dict(original->_obj)->length == 0) {
|
||||
c11_sbuf__write_char(buf, '}');
|
||||
return true;
|
||||
}
|
||||
if(indent > 0) c11_sbuf__write_char(buf, '\n');
|
||||
json__write_dict_kv_ctx ctx = {.buf = buf,
|
||||
.first = true,
|
||||
.indent = indent,
|
||||
.depth = depth + 1};
|
||||
bool ok = py_applydict(py_getslot(obj, 0), json__write_namedict_kv, &ctx);
|
||||
bool ok = py_applydict(original, json__write_namedict_kv, &ctx);
|
||||
if(!ok) return false;
|
||||
if(indent > 0) {
|
||||
c11_sbuf__write_char(buf, '\n');
|
||||
|
@ -1,5 +1,10 @@
|
||||
import json
|
||||
|
||||
# test empty
|
||||
assert json.dumps({}, indent=2) == '{}'
|
||||
assert json.dumps([], indent=2) == '[]'
|
||||
assert json.dumps(object().__dict__, indent=2) == '{}'
|
||||
|
||||
assert json.dumps([1, 2, [3, 4], 5], indent=2) == '[\n 1,\n 2,\n [\n 3,\n 4\n ],\n 5\n]'
|
||||
|
||||
a = {
|
||||
|
Loading…
x
Reference in New Issue
Block a user