static int json_write_pretty_get_array_size(const struct json_array_s* array, size_t depth, size_t indent_size, size_t newline_size, size_t* size) { struct json_array_element_s* element; *size += 1; // '[' *size += newline_size; // need a newline next if (1 < array->length) { *size += (array->length - 1); // ','s seperate each element } for (element = array->start; 0 != element; element = element->next) { // each element gets an indent and newline *size += (depth + 1) * indent_size; *size += newline_size; if (json_write_pretty_get_value_size( element->value, depth + 1, indent_size, newline_size, size)) { // value was malformed! return 1; } } *size += depth * indent_size; *size += 1; // ']' *size += newline_size; // need a newline next return 0; }
void *json_write_pretty(const struct json_value_s *value, const char *indent, const char *newline, size_t *out_size) { size_t size = 0; size_t indent_size = 0; size_t newline_size = 0; char *data = 0; char *data_end = 0; if (0 == value) { return 0; } if (0 == indent) { indent = " "; // default to two spaces } if (0 == newline) { newline = "\n"; // default to linux newlines } while ('\0' != indent[indent_size]) { ++indent_size; // skip non-null terminating characters } while ('\0' != newline[newline_size]) { ++newline_size; // skip non-null terminating characters } if (json_write_pretty_get_value_size(value, 0, indent_size, newline_size, &size)) { // value was malformed! return 0; } size += 1; // for the '\0' null terminating character data = malloc(size); if (0 == data) { // malloc failed! return 0; } data_end = json_write_pretty_value(value, 0, indent, newline, data); if (0 == data_end) { // bad chi occurred! free(data); return 0; } // null terminated the string *data_end = '\0'; if (0 != out_size) { *out_size = size; } return data; }
static int json_write_pretty_get_object_size(const struct json_object_s* object, size_t depth, size_t indent_size, size_t newline_size, size_t* size) { struct json_object_element_s* element; *size += 1; // '{' *size += newline_size; // need a newline next if (1 < object->length) { *size += object->length - 1; // ','s seperate each element } for (element = object->start; 0 != element; element = element->next) { // each element gets an indent and newline *size += (depth + 1) * indent_size; *size += newline_size; if (json_write_pretty_get_string_size(element->name, size)) { // string was malformed! return 1; } *size += 3; // seperate each name/value pair with " : " if (json_write_pretty_get_value_size(element->value, depth + 1, indent_size, newline_size, size)) { // value was malformed! return 1; } } *size += depth * indent_size; *size += 1; // '}' *size += newline_size; // need a newline next return 0; }
static int json_write_pretty_get_array_size(const struct json_array_s *array, size_t depth, size_t indent_size, size_t newline_size, size_t *size) { struct json_array_element_s *element; *size += 1; // '[' if (0 < array->length) { // if we have any elements we need to add a newline after our '[' *size += newline_size; *size += array->length - 1; // ','s seperate each element for (element = array->start; 0 != element; element = element->next) { // each element gets an indent *size += (depth + 1) * indent_size; if (json_write_pretty_get_value_size(element->value, depth + 1, indent_size, newline_size, size)) { // value was malformed! return 1; } // each element gets a newline too *size += newline_size; } // since we wrote out some elements, need to add a newline and indentation // to the trailing ']' *size += depth * indent_size; } *size += 1; // ']' return 0; }