Program Listing for File test_cjson.c#
↰ Return to documentation for file (src/test/test_cjson.c
)
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <cjson/cJSON.h>
int main(int argc, char *argv[])
{
// read from file
FILE *fp = fopen("input/test.json", "r");
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *buffer = (char *)malloc(size + 1);
fread(buffer, 1, size, fp);
fclose(fp);
buffer[size] = '\0';
// parse
cJSON *root = cJSON_Parse(buffer);
free(buffer);
int status;
const cJSON *resolution = NULL;
const cJSON *resolutions = NULL;
const cJSON *name = NULL;
if (root == NULL)
{
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL)
{
fprintf(stderr, "Error before: %s\n", error_ptr);
}
status = 0;
goto end;
}
name = cJSON_GetObjectItemCaseSensitive(root, "name");
if (cJSON_IsString(name) && (name->valuestring != NULL))
{
printf("Checking monitor \"%s\"\n", name->valuestring);
}
resolutions = cJSON_GetObjectItemCaseSensitive(root, "resolutions");
cJSON_ArrayForEach(resolution, resolutions)
{
cJSON *width = cJSON_GetObjectItemCaseSensitive(resolution, "width");
cJSON *height = cJSON_GetObjectItemCaseSensitive(resolution, "height");
if (!cJSON_IsNumber(width) || !cJSON_IsNumber(height))
{
status = 0;
goto end;
}
printf("width: %lf, height: %lf\n", width->valuedouble, height->valuedouble);
if ((width->valuedouble == 1920) && (height->valuedouble == 1080))
{
status = 1;
goto end;
}
}
end:
cJSON_Delete(root);
return 0;
}