A sample program that uses the JSON library

In this sample, you can do the following:

  • Parse the JSON string and save it to a file.
  • Generate a JSON string and save it to a file.

To use this sample, link the following library to your application program.

Header file:
lib.h : System E6.0 or later.
jansson.h
jansson_config.h
jansson_log.h
CodeConversion.h : ver.1.1.1 or later.
Library file:
libJansson.a
libCodeConversion.a : ver.1.1.1 or later.
libSTARTUPOPH5000.a : System E6.0 or later.

Note:

When adding library files, link them in the above order.

ParseJson()

Parse the following JSON string and save it to a file.

[
  {
    "name": "Sample001",
    "itemID": [
      "ID001"
    ],
    "score": 0,
    "result": null
  },
  {
    "name": "Sample002",
    "itemID": [
      "ID001",
      "ID002"
    ],
    "score": 35.5,
    "result": false
  },
  {
    "name": "Sample003",
    "itemID": [
      "ID001",
      "ID003"
    ],
    "score": 95,
    "result": true
  }
]

EditJson()

Generate the following JSON string and save it to a file.

{
  "object1": {
    "name": "Sample001",
    "itemID": [
      "ID001"
    ],
    "score": 15.5,
    "result": null
  },
  "object2": {
    "name": "Sample002",
    "itemID": [
      "ID002"
    ],
    "score": 35.5,
    "result": false
  },
  "object3": {
    "name": "Sample003",
    "itemID": [
      "ID001",
      "ID002"
    ],
    "score": 90,
    "result": true
  },
  "object4": {
    "name": "Sample004",
    "itemID": [
      "ID001",
      "ID003"
    ],
    "score": 95,
    "result": true
  }
}


Sample: main.c
#include <stdio.h>
#include <string.h>
#include "lib.h"
#include "CodeConversion.h"
#include "logapi.h"
#include "jansson.h"

#define JSON_LOG_TAG "[JSON]"

#define JSON_SUCCESS                        0
#define JSON_SYSTEM_ERROR                   (-1)

int DBG_JANSSON_API_ERROR = 1;
int DBG_JANSSON_API_INFO = 1;
int DBG_JANSSON_API_DEBUG = 0;

int DBG_JSON_SAMPLE_ERROR = 1;
int DBG_JSON_SAMPLE_INFO = 1;
int DBG_JSON_SAMPLE_DEBUG = 0;

int jsonOutput(json_t* pRoot, char* pFilePath);
int ParseJson();
int EditJson();

const char* jsonText1 = "["
"{\"name\": \"Sample001\", \"itemID\": [\"ID001\"], "
"\"score\": 0, \"result\": null}, "
"{\"name\": \"Sample002\", \"itemID\": [\"ID001\", \"ID002\"], "
"\"score\": 35.5, \"result\": false},"
"{\"name\": \"Sample003\", \"itemID\": [\"ID001\", \"ID003\"], "
"\"score\": 95, \"result\": true}"
"]";

void main(void)
{
    Cursor(AUTOWRAP);

    while (1) {
        printf("1:Parse JSON\n");
        printf("2:Edit JSON\n");

        while (1) {
            if (kbhit()) {
                int key = getchar();
                if (key == '1') {
                    ParseJson();
                    break;
                }
                if (key == '2') {
                    EditJson();
                    break;
                }
            }
            Idle();
        }
    }
}

int jsonOutput(json_t* pRoot, char* pFilePath)
{
    int result = 0;
    char* pJsonText = NULL;
    char* pEncodeText = NULL;
    char* pSeparateText = NULL;
    int errorCode = 0;
    size_t outputLength = 0;

    pJsonText = json_dumps(pRoot, JSON_ENCODE_ANY | JSON_INDENT(2));
    if (!pJsonText) {
        LOG_PRINTF(DBG_JSON_SAMPLE_ERROR, "%s jsonOutput. json_dumps error.\n", JSON_LOG_TAG);
        return JSON_SYSTEM_ERROR;
    }

    pEncodeText = CONV_Utf8ToCP437oseString(pJsonText, strlen(pJsonText),
        &errorCode, &outputLength);
    if (!pEncodeText) {
        LOG_PRINTF(DBG_JSON_SAMPLE_ERROR, "%s jsonOutput. convert error. errorCode: %d.\n",
            JSON_LOG_TAG, errorCode);
        free(pJsonText);
        return JSON_SYSTEM_ERROR;
    }

    LOG_PRINTF(DBG_JSON_SAMPLE_INFO, "%s JSON Data: \n", JSON_LOG_TAG);

    pSeparateText = strtok(pEncodeText, "\n");
    LOG_PRINTF(DBG_JSON_SAMPLE_INFO, "%s %s\n", JSON_LOG_TAG, pSeparateText);

    while (pSeparateText != NULL) {
        pSeparateText = strtok(NULL, "\n");
        LOG_PRINTF(DBG_JSON_SAMPLE_INFO, "%s %s\n", JSON_LOG_TAG, pSeparateText);
    }

    free(pEncodeText);
    free(pJsonText);

    result = json_dump_file(pRoot, pFilePath, JSON_ENCODE_ANY | JSON_INDENT(2));
    if (result == -1) {
        LOG_PRINTF(DBG_JSON_SAMPLE_ERROR, "%s json_dump_file error.\n", JSON_LOG_TAG);
        return JSON_SYSTEM_ERROR;
    }

    return JSON_SUCCESS;
}

int ParseJson()
{
    json_t* pRoot = NULL;
    json_error_t error;
    char* pUtf8Text = NULL;
    int errorCode = 0;
    size_t outputLength = 0;

    pUtf8Text = CONV_CP437oseToUtf8String((char*)jsonText1, strlen(jsonText1),
        &errorCode, &outputLength);
    if (!pUtf8Text) {
        LOG_PRINTF(DBG_JSON_SAMPLE_ERROR, "%s ParseJson. convert error. errorCode: %d.\n",
            JSON_LOG_TAG, errorCode);
        return JSON_SYSTEM_ERROR;
    }

    pRoot = json_loads(pUtf8Text, 0, &error);
    free(pUtf8Text);
    if (!pRoot) {
        LOG_PRINTF(DBG_JSON_SAMPLE_ERROR, "%s json_loads. error: on line %d: %s\n",
            JSON_LOG_TAG, error.line, error.text);
        return JSON_SYSTEM_ERROR;
    }

    int result = jsonOutput(pRoot, "JSON_S1.TXT");
    json_decref(pRoot);

    if (result != JSON_SUCCESS) {
        return JSON_SYSTEM_ERROR;
    }

    return JSON_SUCCESS;

    /* Output
    [
      {
        "name": "Sample001",
        "itemID": [
          "ID001"
        ],
        "score": 0,
        "result": null
      },
      {
        "name": "Sample002",
        "itemID": [
          "ID001",
          "ID002"
        ],
        "score": 35.5,
        "result": false
      },
      {
        "name": "Sample003",
        "itemID": [
          "ID001",
          "ID003"
        ],
        "score": 95,
        "result": true
      }
    ]
    */
}

int EditJson()
{
    json_t* pRoot = NULL;
    int errorCode = 0;
    size_t outputLength = 0;
    char* pUtf8Text_name = NULL;
    char* pUtf8Text_sample1 = NULL;
    char* pUtf8Text_sample2 = NULL;
    char* pUtf8Text_sample3 = NULL;
    char* pUtf8Text_sample4 = NULL;

    pUtf8Text_name = CONV_CP437oseToUtf8String("name", strlen("name"),
        &errorCode, &outputLength);
    pUtf8Text_sample1 = CONV_CP437oseToUtf8String("Sample001", strlen("Sample001"),
        &errorCode, &outputLength);
    pUtf8Text_sample2 = CONV_CP437oseToUtf8String("Sample002", strlen("Sample002"),
        &errorCode, &outputLength);
    pUtf8Text_sample3 = CONV_CP437oseToUtf8String("Sample003", strlen("Sample003"),
        &errorCode, &outputLength);
    pUtf8Text_sample4 = CONV_CP437oseToUtf8String("Sample004", strlen("Sample004"),
        &errorCode, &outputLength);
    if ((!pUtf8Text_name) || (!pUtf8Text_sample1) || (!pUtf8Text_sample2)
        || (!pUtf8Text_sample3) || (!pUtf8Text_sample4))
    {
        LOG_PRINTF(DBG_JSON_SAMPLE_ERROR, "%s EditJson. convert error.\n", JSON_LOG_TAG);
        goto Exit;
    }

    pRoot = json_pack("{s:{s:s, s:[s], s:f, s:n}, s:{s:s, s:[s], s:f, s:b}, "
        "s:{s:s, s:[s, s], s:i, s:b}, s:{s:s, s:[s, s], s:i, s:b}}",
        "object1", pUtf8Text_name, pUtf8Text_sample1,
        "itemID", "ID001", "score", 15.5, "result",
        "object2", pUtf8Text_name, pUtf8Text_sample2,
        "itemID", "ID002", "score", 35.5, "result", false,
        "object3", pUtf8Text_name, pUtf8Text_sample3,
        "itemID", "ID001", "ID002", "score", 90, "result", true,
        "object4", pUtf8Text_name, pUtf8Text_sample4,
        "itemID", "ID001", "ID003", "score", 95, "result", true);
    if (!pRoot) {
        LOG_PRINTF(DBG_JSON_SAMPLE_ERROR, "%s EditJson. json_pack error.\n", JSON_LOG_TAG);
        goto Exit;
    }

    free(pUtf8Text_name);
    free(pUtf8Text_sample1);
    free(pUtf8Text_sample2);
    free(pUtf8Text_sample3);
    free(pUtf8Text_sample4);

    int result = jsonOutput(pRoot, "JSON_S2.TXT");
    json_decref(pRoot);

    if (result != JSON_SUCCESS) {
        return JSON_SYSTEM_ERROR;
    }

    return JSON_SUCCESS;

Exit:
    if (pUtf8Text_name)
        free(pUtf8Text_name);
    if (pUtf8Text_sample1)
        free(pUtf8Text_sample1);
    if (pUtf8Text_sample2)
        free(pUtf8Text_sample2);
    if (pUtf8Text_sample3)
        free(pUtf8Text_sample3);
    if (pUtf8Text_sample4)
        free(pUtf8Text_sample4);
    return JSON_SYSTEM_ERROR;

    /* Output
    {
      "object1": {
        "name": "Sample001",
        "itemID": [
          "ID001"
        ],
        "score": 15.5,
        "result": null
      },
      "object2": {
        "name": "Sample002",
        "itemID": [
          "ID002"
        ],
        "score": 35.5,
        "result": false
      },
      "object3": {
        "name": "Sample003",
        "itemID": [
          "ID001",
          "ID002"
        ],
        "score": 90,
        "result": true
      },
      "object4": {
        "name": "Sample004",
        "itemID": [
          "ID001",
          "ID003"
        ],
        "score": 95,
        "result": true
      }
    }
    */
}

See also

Last updated: 2022/01/26