@@ -28,7 +28,6 @@ struct jsonwriter_data {
2828 unsigned char compact :1 ;
2929 unsigned char started :1 ;
3030 unsigned char dummy :5 ;
31-
3231};
3332
3433void jsonwriter_set_option (jsonwriter_handle h , enum jsonwriter_option opt ) {
@@ -103,19 +102,35 @@ static int jsonwriter_indent(struct jsonwriter_data *data, char closing) {
103102 return 0 ;
104103}
105104
106- int jsonwriter_end (jsonwriter_handle h ) { // return 0 on success
105+ static enum jsonwriter_status jsonwriter_end_aux (jsonwriter_handle h , char close_bracket ) { // return 0 on success
107106 struct jsonwriter_data * data = h ;
108107 if (data -> depth > 0 ) {
108+ if (close_bracket && data -> close_brackets [data -> depth - 1 ] != close_bracket )
109+ return jsonwriter_status_invalid_end ;
110+
109111 data -> depth -- ;
112+
110113 if (data -> depth < JSONWRITER_MAX_NESTING - 1 ) {
111114 jsonwriter_indent (data , 1 );
112115 data -> write (data -> close_brackets + data -> depth , 1 , 1 , data -> write_arg );
113116 }
114117 if (data -> depth == 0 )
115118 jsonwriter_writeln (data );
116- return 0 ;
119+ return jsonwriter_status_ok ;
117120 }
118- return 1 ; // error: nothing to close
121+ return jsonwriter_status_invalid_end ; // error: nothing to close
122+ }
123+
124+ enum jsonwriter_status jsonwriter_end (jsonwriter_handle h ) {
125+ return jsonwriter_end_aux (h , 0 );
126+ }
127+
128+ enum jsonwriter_status jsonwriter_end_array (jsonwriter_handle h ) {
129+ return jsonwriter_end_aux (h , ']' );
130+ }
131+
132+ enum jsonwriter_status jsonwriter_end_object (jsonwriter_handle h ) {
133+ return jsonwriter_end_aux (h , '}' );
119134}
120135
121136int jsonwriter_end_all (jsonwriter_handle h ) {
@@ -207,7 +222,7 @@ int jsonwriter_strn(jsonwriter_handle h, const char *s, size_t len) {
207222 return 1 ;
208223}
209224
210- int jsonwriter_object_key (jsonwriter_handle h , const char * key , size_t len_or_zero ) {
225+ static int jsonwriter_object_keyn (jsonwriter_handle h , const char * key , size_t len_or_zero ) {
211226 struct jsonwriter_data * data = h ;
212227 if (data -> depth < JSONWRITER_MAX_NESTING ) {
213228 jsonwriter_indent (data , 0 );
@@ -218,6 +233,10 @@ int jsonwriter_object_key(jsonwriter_handle h, const char *key, size_t len_or_ze
218233 return 1 ;
219234}
220235
236+ int jsonwriter_object_key (jsonwriter_handle h , const char * key ) {
237+ return jsonwriter_object_keyn (h , key , strlen (key ));
238+ }
239+
221240int jsonwriter_dblf (jsonwriter_handle h , long double d , const char * format_string , char compact ) {
222241 struct jsonwriter_data * data = h ;
223242 if (data -> depth < JSONWRITER_MAX_NESTING ) {
@@ -276,26 +295,26 @@ int jsonwriter_start_array(jsonwriter_handle h) {
276295 return jsonwriter_go_deeper ((struct jsonwriter_data * )h , '[' , ']' );
277296}
278297
279- enum jsonwriter_error jsonwriter_set_variant_handler (
298+ enum jsonwriter_status jsonwriter_set_variant_handler (
280299 jsonwriter_handle h ,
281300 struct jsonwriter_variant (* to_jsw_variant )(void * ),
282301 void (* cleanup )(void * , struct jsonwriter_variant * )
283302) {
284303 struct jsonwriter_data * d = h ;
285304 d -> after_to_jsw_variant = cleanup ;
286305 if (!(d -> to_jsw_variant = to_jsw_variant ))
287- return jsonwriter_error_invalid_value ;
306+ return jsonwriter_status_invalid_value ;
288307
289- return jsonwriter_error_ok ;
308+ return jsonwriter_status_ok ;
290309}
291310
292- enum jsonwriter_error jsonwriter_variant (jsonwriter_handle h , void * data ) {
311+ enum jsonwriter_status jsonwriter_variant (jsonwriter_handle h , void * data ) {
293312 struct jsonwriter_data * d = h ;
294313 if (!d -> to_jsw_variant )
295- return jsonwriter_error_misconfiguration ;
314+ return jsonwriter_status_misconfiguration ;
296315 struct jsonwriter_variant jv = d -> to_jsw_variant (data );
297316
298- int rc = jsonwriter_error_unrecognized_variant_type ;
317+ int rc = jsonwriter_status_unrecognized_variant_type ;
299318 switch (jv .type ) {
300319 case jsonwriter_datatype_null :
301320 rc = jsonwriter_null (h );
0 commit comments