8787#define DEFAULT_DECODE_ARRAY_WITH_ARRAY_MT 0
8888#define DEFAULT_ENCODE_ESCAPE_FORWARD_SLASH 1
8989#define DEFAULT_ENCODE_SKIP_UNSUPPORTED_VALUE_TYPES 0
90+ #define DEFAULT_ENCODE_INDENT NULL
9091
9192#ifdef DISABLE_INVALID_NUMBERS
9293#undef DEFAULT_DECODE_INVALID_NUMBERS
@@ -168,6 +169,7 @@ typedef struct {
168169 int encode_keep_buffer ;
169170 int encode_empty_table_as_object ;
170171 int encode_escape_forward_slash ;
172+ const char * encode_indent ;
171173
172174 int decode_invalid_numbers ;
173175 int decode_max_depth ;
@@ -177,6 +179,7 @@ typedef struct {
177179
178180typedef struct {
179181 const char * * char2escape [256 ];
182+ const char * indent ;
180183} json_encode_options_t ;
181184
182185typedef struct {
@@ -330,6 +333,20 @@ static int json_enum_option(lua_State *l, int optindex, int *setting,
330333}
331334*/
332335
336+ /* Process string option for a configuration function */
337+ /*
338+ static int json_string_option(lua_State *l, int optindex, const char **setting)
339+ {
340+ if (!lua_isnil(l, optindex)) {
341+ const char *value = luaL_checkstring(l, optindex);
342+ *setting = value;
343+ }
344+
345+ lua_pushstring(l, *setting ? *setting : "");
346+ return 1;
347+ }
348+ */
349+
333350/* Configures handling of extremely sparse arrays:
334351 * convert: Convert extremely sparse arrays into objects? Otherwise error.
335352 * ratio: 0: always allow sparse; 1: never allow sparse; >1: use ratio
@@ -436,6 +453,20 @@ static int json_cfg_encode_keep_buffer(lua_State *l)
436453}
437454*/
438455
456+ /* Configure how to indent output */
457+ /*
458+ static int json_cfg_encode_indent(lua_State *l)
459+ {
460+ json_config_t *cfg = json_arg_init(l, 1);
461+
462+ json_string_option(l, 1, &cfg->encode_indent);
463+ // simplify further checking
464+ if (cfg->encode_indent[0] == '\0') cfg->encode_indent = NULL;
465+
466+ return 1;
467+ }
468+ */
469+
439470#if defined(DISABLE_INVALID_NUMBERS ) && !defined(USE_INTERNAL_FPCONV )
440471void json_verify_invalid_number_setting (lua_State * l , int * setting )
441472{
@@ -533,6 +564,7 @@ static void json_create_config(lua_State *l)
533564 cfg -> decode_array_with_array_mt = DEFAULT_DECODE_ARRAY_WITH_ARRAY_MT ;
534565 cfg -> encode_escape_forward_slash = DEFAULT_ENCODE_ESCAPE_FORWARD_SLASH ;
535566 cfg -> encode_skip_unsupported_value_types = DEFAULT_ENCODE_SKIP_UNSUPPORTED_VALUE_TYPES ;
567+ cfg -> encode_indent = DEFAULT_ENCODE_INDENT ;
536568
537569#if DEFAULT_ENCODE_KEEP_BUFFER > 0
538570 strbuf_init (& cfg -> encode_buf , 0 );
@@ -704,6 +736,13 @@ static void json_check_encode_depth(lua_State *l, json_config_t *cfg,
704736static int json_append_data (lua_State * l , json_encode_t * cfg ,
705737 int current_depth );
706738
739+ static void json_append_newline_and_indent (strbuf_t * json , json_encode_t * ctx , int depth )
740+ {
741+ strbuf_append_char (json , '\n' );
742+ for (int i = 0 ; i < depth ; i ++ )
743+ strbuf_append_string (json , ctx -> options -> indent );
744+ }
745+
707746/* json_append_array args:
708747 * - lua_State
709748 * - JSON strbuf
@@ -712,15 +751,22 @@ static void json_append_array(lua_State *l, json_encode_t *ctx, int current_dept
712751 int array_length , int raw )
713752{
714753 int comma , i , json_pos , err ;
754+ int has_items = 0 ;
715755 strbuf_t * json = ctx -> json ;
716756
717757 strbuf_append_char (json , '[' );
718758
719759 comma = 0 ;
720760 for (i = 1 ; i <= array_length ; i ++ ) {
761+ has_items = 1 ;
762+
721763 json_pos = strbuf_length (json );
722764 if (comma ++ > 0 )
723765 strbuf_append_char (json , ',' );
766+
767+ if (ctx -> options -> indent )
768+ json_append_newline_and_indent (json , ctx , current_depth );
769+
724770 if (raw ) {
725771 lua_rawgeti (l , -1 , i );
726772 } else {
@@ -742,6 +788,9 @@ static void json_append_array(lua_State *l, json_encode_t *ctx, int current_dept
742788 lua_pop (l , 1 );
743789 }
744790
791+ if (has_items && ctx -> options -> indent )
792+ json_append_newline_and_indent (json , ctx , current_depth - 1 );
793+
745794 strbuf_append_char (json , ']' );
746795}
747796
@@ -798,6 +847,7 @@ static void json_append_object(lua_State *l, json_encode_t *ctx,
798847 int current_depth )
799848{
800849 int comma , keytype , json_pos , err ;
850+ int has_items = 0 ;
801851 strbuf_t * json = ctx -> json ;
802852
803853 /* Object */
@@ -807,12 +857,17 @@ static void json_append_object(lua_State *l, json_encode_t *ctx,
807857 /* table, startkey */
808858 comma = 0 ;
809859 while (lua_next (l , -2 ) != 0 ) {
860+ has_items = 1 ;
861+
810862 json_pos = strbuf_length (json );
811863 if (comma ++ > 0 )
812864 strbuf_append_char (json , ',' );
813865 else
814866 comma = 1 ;
815867
868+ if (ctx -> options -> indent )
869+ json_append_newline_and_indent (json , ctx , current_depth );
870+
816871 /* table, key, value */
817872 keytype = lua_type (l , -2 );
818873 if (keytype == LUA_TNUMBER ) {
@@ -827,6 +882,8 @@ static void json_append_object(lua_State *l, json_encode_t *ctx,
827882 "table key must be a number or string" );
828883 /* never returns */
829884 }
885+ if (ctx -> options -> indent )
886+ strbuf_append_char (json , ' ' );
830887
831888 /* table, key, value */
832889 err = json_append_data (l , ctx , current_depth );
@@ -841,6 +898,9 @@ static void json_append_object(lua_State *l, json_encode_t *ctx,
841898 /* table, key */
842899 }
843900
901+ if (has_items && ctx -> options -> indent )
902+ json_append_newline_and_indent (json , ctx , current_depth - 1 );
903+
844904 strbuf_append_char (json , '}' );
845905}
846906
@@ -966,7 +1026,10 @@ static int json_append_data(lua_State *l, json_encode_t *ctx,
9661026static int json_encode (lua_State * l )
9671027{
9681028 json_config_t * cfg = json_fetch_config (l );
969- json_encode_options_t options = { .char2escape = { char2escape } };
1029+ json_encode_options_t options = {
1030+ .char2escape = { char2escape },
1031+ .indent = DEFAULT_ENCODE_INDENT ,
1032+ };
9701033 json_encode_t ctx = { .options = & options , .cfg = cfg };
9711034 strbuf_t local_encode_buf ;
9721035 strbuf_t * encode_buf ;
@@ -979,26 +1042,29 @@ static int json_encode(lua_State *l)
9791042 break ;
9801043 case 2 :
9811044 luaL_checktype (l , 2 , LUA_TTABLE );
982- lua_getfield (l , 2 , "escape_slash" );
9831045
984- /* We only handle the escape_slash option for now */
985- if (lua_isnil (l , -1 )) {
986- lua_pop (l , 2 );
987- break ;
1046+ lua_getfield (l , 2 , "escape_slash" );
1047+ if (!lua_isnil (l , -1 )) {
1048+ luaL_checktype (l , -1 , LUA_TBOOLEAN );
1049+
1050+ int escape_slash = lua_toboolean (l , -1 );
1051+ if (escape_slash ) {
1052+ /* This can be optimised by adding a new hard-coded escape table for this case,
1053+ * but this path will rarely if ever be used, so let's just memcpy. */
1054+ memcpy (customChar2escape , char2escape , sizeof (char2escape ));
1055+ customChar2escape ['/' ] = "\\/" ;
1056+ * ctx .options -> char2escape = customChar2escape ;
1057+ }
9881058 }
1059+ lua_pop (l , 1 );
9891060
990- luaL_checktype (l , -1 , LUA_TBOOLEAN );
991-
992- int escape_slash = lua_toboolean (l , -1 );
993-
994- if (escape_slash ) {
995- /* This can be optimised by adding a new hard-coded escape table for this case,
996- * but this path will rarely if ever be used, so let's just memcpy.*/
997- memcpy (customChar2escape , char2escape , sizeof (char2escape ));
998- customChar2escape ['/' ] = "\\/" ;
999- * ctx .options -> char2escape = customChar2escape ;
1061+ lua_getfield (l , 2 , "indent" );
1062+ if (!lua_isnil (l , -1 )) {
1063+ options .indent = luaL_checkstring (l , -1 );
1064+ if (options .indent [0 ] == '\0' ) options .indent = NULL ;
10001065 }
10011066
1067+ /* Also pop the opts table */
10021068 lua_pop (l , 2 );
10031069 break ;
10041070 default :
@@ -1710,6 +1776,7 @@ int lua_cjson_new(lua_State *l)
17101776 { "decode_invalid_numbers", json_cfg_decode_invalid_numbers },
17111777 { "encode_escape_forward_slash", json_cfg_encode_escape_forward_slash },
17121778 { "encode_skip_unsupported_value_types", json_cfg_encode_skip_unsupported_value_types },
1779+ { "encode_indent", json_cfg_encode_indent },
17131780 */
17141781 { "new" , lua_cjson_new },
17151782 { NULL , NULL }
0 commit comments