psv-format / psv.c

This is a reference implementation of a Markdown to JSON converter, designed specifically for parsing Markdown tables into JSON objects. It allows for easy conversion of Markdown documents containing tables into structured JSON data. https://psv-format.github.io/
2 stars 0 forks source link

Refactor JSON handling to use cJSON library #1

Open mofosyne opened 4 months ago

mofosyne commented 4 months ago

Currently, the project relies on custom JSON handling code, which can be error-prone and lacks some conveniences offered by established JSON libraries.

We should refactor the codebase to utilize the cJSON library for JSON parsing and serialization instead.

Additional Notes:


AI generated untested example of possible replacement to print_table_json().

#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"

void print_table_rows_json(Table *table, cJSON *json_array) {
    for (int i = 0; i < table->num_data_rows; i++) {
        cJSON *json_row = cJSON_CreateObject();

        for (int j = 0; j < table->num_headers; j++) {
            const char *key = table->headers[j];
            const char *data = table->data_rows[i][j];
            if (!data) {
                continue;
            }

            // Check if the data is a valid JSON number or boolean
            if (is_number(data) || is_boolean(data)) {
                cJSON_AddNumberToObject(json_row, key, atof(data));
            } else {
                cJSON_AddStringToObject(json_row, key, data);
            }
        }

        cJSON_AddItemToArray(json_array, json_row);
    }
}

void print_table_json(Table *table, FILE *output, unsigned int tableCount) {
    cJSON *json_table = cJSON_CreateObject();
    cJSON *json_headers = cJSON_CreateArray();
    cJSON *json_rows = cJSON_CreateArray();

    if (tableCount != 0) {
        cJSON_AddStringToObject(json_table, "separator", ",");
    }

    cJSON_AddStringToObject(json_table, "id", table->id);

    // Add headers to the headers array
    for (int j = 0; j < table->num_headers; j++) {
        cJSON_AddItemToArray(json_headers, cJSON_CreateString(table->headers[j]));
    }

    cJSON_AddItemToObject(json_table, "headers", json_headers);

    // Print rows
    print_table_rows_json(table, json_rows);
    cJSON_AddItemToObject(json_table, "rows", json_rows);

    // Print cJSON to file
    char *json_str = cJSON_Print(json_table);
    fprintf(output, "%s\n", json_str);
    free(json_str);

    // Cleanup cJSON objects
    cJSON_Delete(json_table);
}

int main() {
    // Example usage
    FILE *output_file = fopen("output.json", "w");
    if (output_file == NULL) {
        fprintf(stderr, "Error opening output file.\n");
        return 1;
    }

    Table example_table = {
        .id = "example_table",
        .num_headers = 3,
        .headers = (char*[]){"Name", "Age", "City"},
        .num_data_rows = 2,
        .data_rows = (char**[]){(char*[]){"Alice", "25", "New York"}, (char*[]){"Bob", "30", "Los Angeles"}}
    };

    print_table_json(&example_table, output_file, 0);

    fclose(output_file);
    return 0;
}