1
0
mirror of https://github.com/ultrajson/ultrajson.git synced 2024-11-23 12:32:02 +01:00

Support for space indent for JSON encoding

This commit is contained in:
Chris Huang 2015-02-27 13:20:11 +08:00 committed by Joakim Hamren
parent 227341a77d
commit 930dfa5525
3 changed files with 32 additions and 2 deletions

@ -245,6 +245,7 @@ typedef struct __JSONObjectEncoder
int encodeHTMLChars; int encodeHTMLChars;
/* /*
<<<<<<< HEAD
If true, '/' will be encoded as \/. If false, no escaping. */ If true, '/' will be encoded as \/. If false, no escaping. */
int escapeForwardSlashes; int escapeForwardSlashes;
@ -252,6 +253,10 @@ typedef struct __JSONObjectEncoder
If true, dictionaries are iterated through in sorted key order. */ If true, dictionaries are iterated through in sorted key order. */
int sortKeys; int sortKeys;
/*
Configuration for spaces of indent */
int indent;
/* /*
Private pointer to be used by the caller. Passed as encoder_prv in JSONTypeContext */ Private pointer to be used by the caller. Passed as encoder_prv in JSONTypeContext */
void *prv; void *prv;

@ -498,6 +498,20 @@ FASTCALL_ATTR INLINE_PREFIX void FASTCALL_MSVC strreverse(char* begin, char* end
aux = *end, *end-- = *begin, *begin++ = aux; 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) void Buffer_AppendIntUnchecked(JSONObjectEncoder *enc, JSINT32 value)
{ {
char* wstr; char* wstr;
@ -763,6 +777,7 @@ void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t cbName)
count = 0; count = 0;
Buffer_AppendCharUnchecked (enc, '['); Buffer_AppendCharUnchecked (enc, '[');
Buffer_AppendIndentNewlineUnchecked (enc);
while (enc->iterNext(obj, &tc)) 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 #ifndef JSON_NO_EXTRA_WHITESPACE
Buffer_AppendCharUnchecked (buffer, ' '); Buffer_AppendCharUnchecked (buffer, ' ');
#endif #endif
Buffer_AppendIndentNewlineUnchecked (enc);
} }
iterObj = enc->iterGetValue(obj, &tc); iterObj = enc->iterGetValue(obj, &tc);
enc->level ++; enc->level ++;
Buffer_AppendIndentUnchecked (enc, enc->level);
encode (iterObj, enc, NULL, 0); encode (iterObj, enc, NULL, 0);
count ++; count ++;
} }
enc->iterEnd(obj, &tc); enc->iterEnd(obj, &tc);
Buffer_AppendIndentNewlineUnchecked (enc);
Buffer_AppendIndentUnchecked (enc, enc->level);
Buffer_AppendCharUnchecked (enc, ']'); Buffer_AppendCharUnchecked (enc, ']');
break; break;
} }
@ -791,6 +810,7 @@ void encode(JSOBJ obj, JSONObjectEncoder *enc, const char *name, size_t cbName)
count = 0; count = 0;
Buffer_AppendCharUnchecked (enc, '{'); Buffer_AppendCharUnchecked (enc, '{');
Buffer_AppendIndentNewlineUnchecked (enc);
while (enc->iterNext(obj, &tc)) 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 #ifndef JSON_NO_EXTRA_WHITESPACE
Buffer_AppendCharUnchecked (enc, ' '); Buffer_AppendCharUnchecked (enc, ' ');
#endif #endif
Buffer_AppendIndentNewlineUnchecked (enc);
} }
iterObj = enc->iterGetValue(obj, &tc); iterObj = enc->iterGetValue(obj, &tc);
objName = enc->iterGetName(obj, &tc, &szlen); objName = enc->iterGetName(obj, &tc, &szlen);
enc->level ++; enc->level ++;
Buffer_AppendIndentUnchecked (enc, enc->level);
encode (iterObj, enc, objName, szlen); encode (iterObj, enc, objName, szlen);
count ++; count ++;
} }
enc->iterEnd(obj, &tc); enc->iterEnd(obj, &tc);
Buffer_AppendIndentNewlineUnchecked (enc);
Buffer_AppendIndentUnchecked (enc, enc->level);
Buffer_AppendCharUnchecked (enc, '}'); Buffer_AppendCharUnchecked (enc, '}');
break; break;
} }

@ -946,7 +946,7 @@ char *Object_iterGetName(JSOBJ obj, JSONTypeContext *tc, size_t *outLen)
PyObject* objToJSON(PyObject* self, PyObject *args, PyObject *kwargs) 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 buffer[65536];
char *ret; char *ret;
@ -980,13 +980,14 @@ PyObject* objToJSON(PyObject* self, PyObject *args, PyObject *kwargs)
0, //encodeHTMLChars 0, //encodeHTMLChars
1, //escapeForwardSlashes 1, //escapeForwardSlashes
0, //sortKeys 0, //sortKeys
0, //indent
NULL, //prv NULL, //prv
}; };
PRINTMARK(); 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; return NULL;
} }