1
0
Fork 0
mirror of https://github.com/ultrajson/ultrajson.git synced 2024-05-29 02:26:09 +02: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

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
}