Skip to content

standardloop/c-json

Repository files navigation

c-json

https://github.com/standardloop/c-json

GitHub Release Platform: macOS C Version

Background

c-json is a json library written in C.

This was built to learn about lexing, parsing, and data structures such as dynamic arrays and hashmaps.

Dependencies

Example Programs

StringToJSON

#include <stdio.h>
#include <stdlib.h>
#include "./json.h" // or <standardloop/json.h> if using dynamic library

int main(void)
{
    JSON *json = StringToJSON("[1, 2, {\"key\": \"value\"}, \"hello\"]");
    if (json == NULL)
    {
        return EXIT_FAILURE;
    }
    PrintJSON(json);
    FreeJSON(json);
    return EXIT_SUCCESS;
}

JSONFromFile

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "./json.h" // or <standardloop/json.h> if using dynamic library

int main(void)
{
    JSON *json = JSONFromFile("example.json");

    if (json == NULL)
    {
        return EXIT_FAILURE;
    }
    PrintJSON(json);
    FreeJSON(json);
    return EXIT_SUCCESS;
}

JSONToString

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "./json.h" // or <standardloop/json.h> if using dynamic library

int main(void)
{
    JSON *json = StringToJSON("[1, 2, {\"key\": \"value\"}, \"hello\"]");
    if (json == NULL)
    {
        return EXIT_FAILURE;
    }
    char *json_string = JSONToString(json, true);
    if (json_string == NULL)
    {
        return EXIT_FAILURE;
    }
    printf("%s\n", json_string);
    free(json_string);
    return EXIT_SUCCESS;
}

Building

To see all avaiable

$ task --list-all

To build and run a test (lab) program

$ task

Using as a dynamic library

$ clang -Werror -Wextra -Wall -Wfree-nonheap-object -std=c17 \
    lab.c \
    -L/usr/local/lib/standardloop \
    -lstandardloop-json \
    -o lab

Checking for Leaks

  • The taskfile has a task to compile the code with address sanitizers.
  • The taskfile has a task to run the program with macOS leaks.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages