shithub: rjson


branches: front

Clone

clone: git://shithub.us/sirjofri/rjson gits://shithub.us/sirjofri/rjson
push: hjgit://shithub.us/sirjofri/rjson

Last commit

c3ed63d7 – sirjofri <sirjofri@sirjofri.de> authored on 2026/03/15 14:34
adds experimental free function

About

Reflective JSON wrapper library

To convert between structs and json easily.


EXAMPLE

---snip---
#include <json.h>
#include "rjson.h"

typedef struct Vector Vector;
struct Vector {
	double x;
	double y;
	double z;
};

RJSON_BEGIN(RVector_defs)
	RJSON_NUMBER(Vector,x)
	RJSON_NUMBER(Vector,y)
	RJSON_NUMBER(Vector,z)
RJSON_END()

typedef struct Poi Poi;
struct Poi {
	char *label;
	double priority;
	Vector pos;
};

RJSON_BEGIN(RPoi_defs)
	RJSON_STRING(Poi,label)
	RJSON_NUMBER(Poi,priority)
	RJSON_OBJECT(Poi,pos, RVector_defs)
RJSON_END()

/* Rjson* are RVector_defs and RPoi_defs */
/* void-pointers are pointers to the structures in question */
int rjsontostruct(JSON*, Rjson*, void*);
JSON *rstructtojson(void*, Rjson*);
---snap---

See the test/t.c file for a working example.


SUPPORTED TYPES

RJSON_STRING(Struct,Name)
RJSON_NUMBER(Struct,Name)
RJSON_BOOL(Struct,Name)

    Struct is the type of the struct this definition reflects.
    Name is the member variable.

RJSON_OBJECT(Struct,Name, Subobject_defs)

    Subobject_defs is the rjson definition of the subobject.

RJSON_ARRAY_OBJECT(Struct,Name, Type, Subobject_defs, Counter)

    Type is the struct name of the subobject array.
    Counter is the name of the member variable that will hold the
    number of objects within that array.

RJSON_Array(Struct,Name, JSONType, Counter)

    JSONType is the primitive type, e.g. JSONNumber. See json(2).


IMPORTANT

→ char* members are malloc'd strings.
→ the name in the macros mustn't start with a space character.
→ the counter variable name can currently start with a space
  character, and is not reflected.
→ there's currently no array support
→ there should be no leaks
→ there are probably bugs
→ I recommend deciding on a common scheme for array/counter names
  and struct types/rjson structure names.