diff --git a/lib/ultrajson.h b/lib/ultrajson.h index 6640e2d..643c449 100644 --- a/lib/ultrajson.h +++ b/lib/ultrajson.h @@ -245,6 +245,7 @@ typedef struct __JSONObjectEncoder int encodeHTMLChars; /* +<<<<<<< HEAD If true, '/' will be encoded as \/. If false, no escaping. */ int escapeForwardSlashes; @@ -252,6 +253,10 @@ typedef struct __JSONObjectEncoder If true, dictionaries are iterated through in sorted key order. */ int sortKeys; + /* + Configuration for spaces of indent */ + int indent; + /* Private pointer to be used by the caller. Passed as encoder_prv in JSONTypeContext */ void *prv; diff --git a/lib/ultrajsonenc.c b/lib/ultrajsonenc.c index 73e9ab6..47016f6 100644 --- a/lib/ultrajsonenc.c +++ b/lib/ultrajsonenc.c @@ -498,6 +498,20 @@ FASTCALL_ATTR INLINE_PREFIX void FASTCALL_MSVC strreverse(char* begin, char* end aux = *end, *end-- = *begin, *begin++ = aux; } +void Buffer_AppendIndentNewlineUnchecked(JSONObjectEncoder *enc) +{ + if (enc->indent > 0) Buffer_AppendCharUnchecked(enc, '\n'); +} + +void Buffer_AppendIndentUnchecked(JSONObjectEncoder *enc, JSINT32 value) +{ + int i; + if (enc->indent > 0) + while (value-- > 0) + for (i = 0; i < enc->indent; i++) + Buffer_AppendCharUnchecked(enc, ' '); +} + void Buffer_AppendIntUnchecked(JSONObjectEncoder *enc, JSINT32 value) { char* wstr; @@ -763,6 +777,7 @@ void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t cbName) count = 0; Buffer_AppendCharUnchecked (enc, '['); + Buffer_AppendIndentNewlineUnchecked (enc); while (enc->iterNext(obj, &tc)) { @@ -772,16 +787,20 @@ void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t cbName) #ifndef JSON_NO_EXTRA_WHITESPACE Buffer_AppendCharUnchecked (buffer, ' '); #endif + Buffer_AppendIndentNewlineUnchecked (enc); } iterObj = enc->iterGetValue(obj, &tc); enc->level ++; + Buffer_AppendIndentUnchecked (enc, enc->level); encode (iterObj, enc, NULL, 0); count ++; } enc->iterEnd(obj, &tc); + Buffer_AppendIndentNewlineUnchecked (enc); + Buffer_AppendIndentUnchecked (enc, enc->level); Buffer_AppendCharUnchecked (enc, ']'); break; } @@ -791,6 +810,7 @@ void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t cbName) count = 0; Buffer_AppendCharUnchecked (enc, '{'); + Buffer_AppendIndentNewlineUnchecked (enc); while (enc->iterNext(obj, &tc)) { @@ -800,17 +820,21 @@ void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t cbName) #ifndef JSON_NO_EXTRA_WHITESPACE Buffer_AppendCharUnchecked (enc, ' '); #endif + Buffer_AppendIndentNewlineUnchecked (enc); } iterObj = enc->iterGetValue(obj, &tc); objName = enc->iterGetName(obj, &tc, &szlen); enc->level ++; + Buffer_AppendIndentUnchecked (enc, enc->level); encode (iterObj, enc, objName, szlen); count ++; } enc->iterEnd(obj, &tc); + Buffer_AppendIndentNewlineUnchecked (enc); + Buffer_AppendIndentUnchecked (enc, enc->level); Buffer_AppendCharUnchecked (enc, '}'); break; } diff --git a/python/objToJSON.c b/python/objToJSON.c index 8f5812a..a78f3c9 100644 --- a/python/objToJSON.c +++ b/python/objToJSON.c @@ -946,7 +946,7 @@ char *Object_iterGetName(JSOBJ obj, JSONTypeContext *tc, size_t *outLen) PyObject* objToJSON(PyObject* self, PyObject *args, PyObject *kwargs) { - static char *kwlist[] = { "obj", "ensure_ascii", "double_precision", "encode_html_chars", "escape_forward_slashes", "sort_keys", NULL }; + static char *kwlist[] = { "obj", "ensure_ascii", "double_precision", "encode_html_chars", "escape_forward_slashes", "sort_keys", "indent", NULL }; char buffer[65536]; char *ret; @@ -980,13 +980,14 @@ PyObject* objToJSON(PyObject* self, PyObject *args, PyObject *kwargs) 0, //encodeHTMLChars 1, //escapeForwardSlashes 0, //sortKeys + 0, //indent NULL, //prv }; PRINTMARK(); - if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OiOOO", kwlist, &oinput, &oensureAscii, &encoder.doublePrecision, &oencodeHTMLChars, &oescapeForwardSlashes, &osortKeys)) + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|OiOOOi", kwlist, &oinput, &oensureAscii, &encoder.doublePrecision, &oencodeHTMLChars, &oescapeForwardSlashes, &osortKeys, &encoder.indent)) { return NULL; }