Skip to content

abdimoallim/json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json

A header-only C library for parsing and serializing JSON according to the JSON specification (RFC 8259).

Features

  • Handles all JSON data types and edge cases
  • Full UTF-8 and Unicode escape sequence support including surrogate pairs
  • Detailed error messages for invalid JSON
  • Convert JSON values back to strings (compact or pretty-printed)
  • Managed memory with cleanup functions

RFC 8259 Compliance

  • UTF-8 encoding
  • Whitespace handling (space, tab, newline, carriage return)
  • All escape sequences: \", \\, \/, \b, \f, \n, \r, \t
  • Unicode escapes: \uXXXX including surrogate pairs for characters beyond U+FFFF
  • Proper number parsing (integers, decimals, scientific notation)
  • Strict validation of JSON structure
  • Control character detection and rejection

Usage

Include json.h and link with the math library, i.e. -lm.

#include "json.h"

int main(void) {
  char *error = NULL;
  json_value *root = json_parse("{\"name\": \"John\", \"age\": 30}", &error);

  if (root == NULL) {
    printf("parse error: %s\n", error);
    free(error);
    return 1;
  }

  json_value *name = json_object_get(root, "name");

  if (name && name->type == JSON_STRING)
    printf("name: %s\n", name->string_value);

  json_value *age = json_object_get(root, "age");

  if (age && age->type == JSON_NUMBER)
    printf("age: %.0f\n", age->number_value);

  char *serialized = json_serialize(root, true);
  printf("serialized:\n%s\n", serialized);
  free(serialized);

  json_free(root);

  return 0;
}

We can also build JSON programmatically.

json_value *obj = json_create_object();
json_object_set(obj, "name", json_create_string("Alice"));
json_object_set(obj, "score", json_create_number(95.5));
json_object_set(obj, "active", json_create_bool(true));

json_value *arr = json_create_array();
json_array_append(arr, json_create_number(1));
json_array_append(arr, json_create_number(2));
json_array_append(arr, json_create_number(3));
json_object_set(obj, "numbers", arr);

char *json_str = json_serialize(obj, false);
printf("%s\n", json_str);
free(json_str);

json_free(obj);

API Reference

Parsing

json_value *json_parse(const char *source, char **error);

Parse a JSON string. Returns a json_value* on success or NULL on error. If error is provided, it will be set to an error message (caller must free).

Creating values

json_value *json_create_null(void);
json_value *json_create_bool(bool value);
json_value *json_create_number(double value);
json_value *json_create_string(const char *value);
json_value *json_create_array(void);
json_value *json_create_object(void);

Array operations

void json_array_append(json_value *array, json_value *value);

Object operations

void json_object_set(json_value *object, const char *key, json_value *value);
json_value *json_object_get(json_value *object, const char *key);

Serialization

char *json_serialize(json_value *value, bool pretty);

Serialize a JSON value to a string. Set pretty to true for formatted output with indentation. Caller must free the returned string.

Cleaning values

void json_free(json_value *value);

Free a JSON value and all its children. Always call this when done with a JSON value.

Values and types

Current supported types:

  • null
  • boolean (true/false)
  • number (integers and floating-point, scientific notation)
  • string (with full escape sequence support)
  • array
  • object
typedef enum {
  JSON_NULL,
  JSON_BOOL,
  JSON_NUMBER,
  JSON_STRING,
  JSON_ARRAY,
  JSON_OBJECT
} json_type;

typedef struct json_value {
  json_type type;
  union {
    bool bool_value;
    double number_value;
    char *string_value;
    json_array array_value;
    json_object object_value;
  };
} json_value;

License

Apache v2.0 License

About

A header-only C library for parsing and serializing JSON with RFC 8259 compliance

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Contributors

Languages