static int json_serialize_to_buffer_r()

in TranslatorCognitiveServices/src/parson.c [926:1061]


static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int level, int is_pretty,
                                      char *num_buf)
{
    const char *key = NULL, *string = NULL;
    JSON_Value *temp_value = NULL;
    JSON_Array *array = NULL;
    JSON_Object *object = NULL;
    size_t i = 0, count = 0;
    double num = 0.0;
    int written = -1, written_total = 0;

    switch (json_value_get_type(value)) {
    case JSONArray:
        array = json_value_get_array(value);
        count = json_array_get_count(array);
        APPEND_STRING("[");
        if (count > 0 && is_pretty) {
            APPEND_STRING("\n");
        }
        for (i = 0; i < count; i++) {
            if (is_pretty) {
                APPEND_INDENT(level + 1);
            }
            temp_value = json_array_get_value(array, i);
            written = json_serialize_to_buffer_r(temp_value, buf, level + 1, is_pretty, num_buf);
            if (written < 0) {
                return -1;
            }
            if (buf != NULL) {
                buf += written;
            }
            written_total += written;
            if (i < (count - 1)) {
                APPEND_STRING(",");
            }
            if (is_pretty) {
                APPEND_STRING("\n");
            }
        }
        if (count > 0 && is_pretty) {
            APPEND_INDENT(level);
        }
        APPEND_STRING("]");
        return written_total;
    case JSONObject:
        object = json_value_get_object(value);
        count = json_object_get_count(object);
        APPEND_STRING("{");
        if (count > 0 && is_pretty) {
            APPEND_STRING("\n");
        }
        for (i = 0; i < count; i++) {
            key = json_object_get_name(object, i);
            if (key == NULL) {
                return -1;
            }
            if (is_pretty) {
                APPEND_INDENT(level + 1);
            }
            written = json_serialize_string(key, buf);
            if (written < 0) {
                return -1;
            }
            if (buf != NULL) {
                buf += written;
            }
            written_total += written;
            APPEND_STRING(":");
            if (is_pretty) {
                APPEND_STRING(" ");
            }
            temp_value = json_object_get_value(object, key);
            written = json_serialize_to_buffer_r(temp_value, buf, level + 1, is_pretty, num_buf);
            if (written < 0) {
                return -1;
            }
            if (buf != NULL) {
                buf += written;
            }
            written_total += written;
            if (i < (count - 1)) {
                APPEND_STRING(",");
            }
            if (is_pretty) {
                APPEND_STRING("\n");
            }
        }
        if (count > 0 && is_pretty) {
            APPEND_INDENT(level);
        }
        APPEND_STRING("}");
        return written_total;
    case JSONString:
        string = json_value_get_string(value);
        if (string == NULL) {
            return -1;
        }
        written = json_serialize_string(string, buf);
        if (written < 0) {
            return -1;
        }
        if (buf != NULL) {
            buf += written;
        }
        written_total += written;
        return written_total;
    case JSONBoolean:
        if (json_value_get_boolean(value)) {
            APPEND_STRING("true");
        } else {
            APPEND_STRING("false");
        }
        return written_total;
    case JSONNumber:
        num = json_value_get_number(value);
        if (buf != NULL) {
            num_buf = buf;
        }
        written = sprintf(num_buf, FLOAT_FORMAT, num);
        if (written < 0) {
            return -1;
        }
        if (buf != NULL) {
            buf += written;
        }
        written_total += written;
        return written_total;
    case JSONNull:
        APPEND_STRING("null");
        return written_total;
    case JSONError:
        return -1;
    default:
        return -1;
    }
}