jsonlite_result jsonlite_builder_raw_key(jsonlite_builder builder, const void *data, size_t length) { jsonlite_write_state *ws = builder->state; if (jsonlite_builder_accept(builder, jsonlite_accept_key)) { if (jsonlite_builder_accept(builder, jsonlite_accept_next)) { jsonlite_builder_raw(builder, ",", 1); } if (builder->indentation != 0) { jsonlite_builder_raw_char(builder, '\r'); jsonlite_builder_repeat(builder, ' ', (builder->state - builder->stack) * builder->indentation); } jsonlite_builder_raw_char(builder, '\"'); jsonlite_builder_raw(builder, data, length); jsonlite_builder_raw_char(builder, '\"'); if (builder->indentation != 0) { jsonlite_builder_raw(builder, ": ", 2); } else { jsonlite_builder_raw_char(builder, ':'); } *ws = jsonlite_accept_value; return jsonlite_result_ok; } return jsonlite_result_not_allowed; }
jsonlite_result jsonlite_builder_key(jsonlite_builder builder, const char *data, size_t length) { jsonlite_write_state *ws; if (builder == NULL || data == NULL) { return jsonlite_result_invalid_argument; } ws = builder->state; if (jsonlite_builder_accept(builder, jsonlite_accept_key) ) { if (jsonlite_builder_accept(builder, jsonlite_accept_next) ) { jsonlite_builder_raw_char(builder, ','); } if (builder->indentation != 0) { jsonlite_builder_raw_char(builder, '\r'); jsonlite_builder_repeat(builder, ' ', (builder->state - builder->stack) * builder->indentation); } jsonlite_builder_write_uft8(builder, data, length); if (builder->indentation != 0) { jsonlite_builder_raw(builder, ": ", 2); } else { jsonlite_builder_raw_char(builder, ':'); } ws->accept = jsonlite_accept_value; return jsonlite_result_ok; } return jsonlite_result_not_allowed; }
static void jsonlite_builder_write_uft8(jsonlite_builder builder, const char *data, size_t length) { size_t i; jsonlite_builder_raw_char(builder, '\"'); for (i = 0; i < length; i++) { switch (data[i]) { case '"': jsonlite_builder_raw(builder, "\\\"", 2); break; case '\\': jsonlite_builder_raw(builder, "\\\\", 2); break; case '\b': jsonlite_builder_raw(builder, "\\b", 2); break; case '\f': jsonlite_builder_raw(builder, "\\f", 2); break; case '\n': jsonlite_builder_raw(builder, "\\n", 2); break; case '\r': jsonlite_builder_raw(builder, "\\r", 2); break; case '\t': jsonlite_builder_raw(builder, "\\t", 2); break; default: jsonlite_builder_raw_char(builder, data[i]); break; } } jsonlite_builder_raw_char(builder, '\"'); }
jsonlite_result jsonlite_builder_array_end(jsonlite_builder builder) { if (jsonlite_builder_accept(builder, jsonlite_accept_array_end)) { jsonlite_builder_pop_state(builder); if (builder->indentation != 0) { jsonlite_builder_raw_char(builder, '\r'); jsonlite_builder_repeat(builder, ' ', (builder->state - builder->stack) * builder->indentation); } jsonlite_builder_raw_char(builder, ']'); return jsonlite_result_ok; } return jsonlite_result_not_allowed; }
static void jsonlite_builder_prepare_value_writing(jsonlite_builder builder) { jsonlite_write_state *ws = builder->state; if (jsonlite_builder_accept(builder, jsonlite_accept_values_only) ) { if (jsonlite_builder_accept(builder, jsonlite_accept_next) ) { jsonlite_builder_raw_char(builder, ','); } if (builder->indentation != 0) { jsonlite_builder_raw_char(builder, '\r'); jsonlite_builder_repeat(builder, ' ', (builder->state - builder->stack) * builder->indentation); } } else { ws->accept &= ~jsonlite_accept_value; ws->accept |= jsonlite_accept_key; } ws->accept |= jsonlite_accept_next; }
jsonlite_result jsonlite_builder_raw_string(jsonlite_builder builder, const void *data, size_t length) { jsonlite_write_state *ws = builder->state; if (jsonlite_builder_accept(builder, jsonlite_accept_value)) { jsonlite_builder_prepare_value_writing(builder); jsonlite_builder_raw_char(builder, '\"'); jsonlite_builder_raw(builder, data, length); jsonlite_builder_raw_char(builder, '\"'); if (jsonlite_builder_accept(builder, jsonlite_accept_values_only)) { *ws = jsonlite_accept_continue_array; } else { *ws = jsonlite_accept_continue_object; } return jsonlite_result_ok; } return jsonlite_result_not_allowed; }
jsonlite_result jsonlite_builder_object_end(jsonlite_builder builder) { if (builder == NULL) { return jsonlite_result_invalid_argument; } if (jsonlite_builder_accept(builder, jsonlite_accept_object_end)) { jsonlite_builder_pop_state(builder); if (builder->indentation != 0) { jsonlite_builder_raw_char(builder, '\r'); jsonlite_builder_repeat(builder, ' ', (builder->state - builder->stack) * builder->indentation); } jsonlite_builder_raw_char(builder, '}'); return jsonlite_result_ok; } return jsonlite_result_not_allowed; }
static void jsonlite_builder_write_uft8(jsonlite_builder builder, const char *data, size_t length) { char b[2] = {'\\', '?'}; const char *c = data; const char *p = data; const char *l = data + length; jsonlite_builder_raw_char(builder, '\"'); next: if (c == l) goto end; switch (*c) { case '"': b[1] = '"'; goto flush; case '\\': b[1] = '\\'; goto flush; case '\b': b[1] = 'b'; goto flush; case '\f': b[1] = 'f'; goto flush; case '\n': b[1] = 'n'; goto flush; case '\r': b[1] = 'r'; goto flush; case '\t': b[1] = 't'; goto flush; default: c++; goto next; } flush: jsonlite_stream_write(builder->stream, p, c - p); jsonlite_stream_write(builder->stream, b, 2); p = ++c; goto next; end: jsonlite_stream_write(builder->stream, p, c - p); jsonlite_builder_raw_char(builder, '\"'); }
jsonlite_result jsonlite_builder_object_begin(jsonlite_builder builder) { jsonlite_builder_check_depth(); if (jsonlite_builder_accept(builder, jsonlite_accept_object_begin)) { jsonlite_builder_prepare_value_writing(builder); *++builder->state = jsonlite_accept_object_end | jsonlite_accept_key; jsonlite_builder_raw_char(builder, '{'); return jsonlite_result_ok; } return jsonlite_result_not_allowed; }
jsonlite_result jsonlite_builder_raw_string(jsonlite_builder builder, const void *data, size_t length) { jsonlite_write_state *ws; if (builder == NULL || data == NULL || length == 0) { return jsonlite_result_invalid_argument; } ws = builder->state; if (jsonlite_builder_accept(builder, jsonlite_accept_value) ) { jsonlite_builder_prepare_value_writing(builder); jsonlite_builder_raw_char(builder, '\"'); jsonlite_builder_raw(builder, data, length); jsonlite_builder_raw_char(builder, '\"'); if (jsonlite_builder_accept(builder, jsonlite_accept_values_only) ) { ws->accept = jsonlite_accept_continue_array; } else { ws->accept = jsonlite_accept_continue_object; } return jsonlite_result_ok; } return jsonlite_result_not_allowed; }
jsonlite_result jsonlite_builder_object_begin(jsonlite_builder builder) { if (builder == NULL) { return jsonlite_result_invalid_argument; } jsonlite_builder_check_depth(); if (jsonlite_builder_accept(builder, jsonlite_accept_object_begin)) { jsonlite_builder_prepare_value_writing(builder); jsonlite_builder_push_state(builder); builder->state->accept = jsonlite_accept_object_end | jsonlite_accept_key; jsonlite_builder_raw_char(builder, '{'); return jsonlite_result_ok; } return jsonlite_result_not_allowed; }