mirror of
https://github.com/rafagafe/tiny-json.git
synced 2025-03-12 19:25:30 +00:00
Remove GNU extensions and some refactors are made.
This commit is contained in:
parent
2b1bdd791d
commit
a087bcabb5
@ -44,13 +44,13 @@ typedef struct jsonStaticPool_s {
|
|||||||
} jsonStaticPool_t;
|
} jsonStaticPool_t;
|
||||||
|
|
||||||
static json_t* poolInit( jsonPool_t* pool ) {
|
static json_t* poolInit( jsonPool_t* pool ) {
|
||||||
jsonStaticPool_t* spool = json_container_of(pool, jsonStaticPool_t, pool);
|
jsonStaticPool_t* spool = json_containerOf(pool, jsonStaticPool_t, pool);
|
||||||
spool->nextFree = 1;
|
spool->nextFree = 1;
|
||||||
return &spool->mem[0];
|
return &spool->mem[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
static json_t* poolNew( jsonPool_t* pool ) {
|
static json_t* poolNew( jsonPool_t* pool ) {
|
||||||
jsonStaticPool_t* spool = json_container_of(pool, jsonStaticPool_t, pool);
|
jsonStaticPool_t* spool = json_containerOf(pool, jsonStaticPool_t, pool);
|
||||||
if ( spool->nextFree >= sizeof spool->mem / sizeof spool->mem[0] ) return 0;
|
if ( spool->nextFree >= sizeof spool->mem / sizeof spool->mem[0] ) return 0;
|
||||||
return &spool->mem[spool->nextFree++];
|
return &spool->mem[spool->nextFree++];
|
||||||
}
|
}
|
||||||
@ -74,7 +74,7 @@ int main( void ) {
|
|||||||
"}\n";
|
"}\n";
|
||||||
puts( str );
|
puts( str );
|
||||||
jsonStaticPool_t spool = { .pool = { .init = poolInit, .new = poolNew } };
|
jsonStaticPool_t spool = { .pool = { .init = poolInit, .new = poolNew } };
|
||||||
json_t const* json = json_create_pool( str, &spool.pool );
|
json_t const *json = json_createWithPool( str, &spool.pool );
|
||||||
if ( !json ) {
|
if ( !json ) {
|
||||||
puts("Error json create.");
|
puts("Error json create.");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
|
20
makefile
20
makefile
@ -1,4 +1,4 @@
|
|||||||
|
CFLAGS = -std=c99 -Wall -pedantic
|
||||||
|
|
||||||
build: example-01.exe example-02.exe example-03.exe
|
build: example-01.exe example-02.exe example-03.exe
|
||||||
|
|
||||||
@ -12,28 +12,28 @@ test: test.exe
|
|||||||
./test.exe
|
./test.exe
|
||||||
|
|
||||||
example-01.exe: example-01.o tiny-json.o
|
example-01.exe: example-01.o tiny-json.o
|
||||||
gcc -std=c99 -Wall -o example-01.exe example-01.o tiny-json.o
|
gcc $(CFLAGS) -o $@ $^
|
||||||
|
|
||||||
example-02.exe: example-02.o tiny-json.o
|
example-02.exe: example-02.o tiny-json.o
|
||||||
gcc -std=c99 -Wall -o example-02.exe example-02.o tiny-json.o
|
gcc $(CFLAGS) -o $@ $^
|
||||||
|
|
||||||
example-03.exe: example-03.o tiny-json.o
|
example-03.exe: example-03.o tiny-json.o
|
||||||
gcc -std=c99 -Wall -o example-03.exe example-03.o tiny-json.o
|
gcc $(CFLAGS) -o $@ $^
|
||||||
|
|
||||||
test.exe: tests.o tiny-json.o
|
test.exe: tests.o tiny-json.o
|
||||||
gcc -std=c99 -Wall -o test.exe tests.o tiny-json.o
|
gcc $(CFLAGS) -o $@ $^
|
||||||
|
|
||||||
tiny-json.o: tiny-json.c tiny-json.h
|
tiny-json.o: tiny-json.c tiny-json.h
|
||||||
gcc -std=c99 -Wall -c tiny-json.c
|
gcc $(CFLAGS) -c tiny-json.c
|
||||||
|
|
||||||
example-01.o: example-01.c tiny-json.h
|
example-01.o: example-01.c tiny-json.h
|
||||||
gcc -std=c99 -Wall -c example-01.c
|
gcc $(CFLAGS) -c example-01.c
|
||||||
|
|
||||||
example-02.o: example-02.c tiny-json.h
|
example-02.o: example-02.c tiny-json.h
|
||||||
gcc -std=c99 -Wall -c example-02.c
|
gcc $(CFLAGS) -c example-02.c
|
||||||
|
|
||||||
example-03.o: example-03.c tiny-json.h
|
example-03.o: example-03.c tiny-json.h
|
||||||
gcc -std=c99 -Wall -c example-03.c
|
gcc $(CFLAGS) -c example-03.c
|
||||||
|
|
||||||
tests.o: tests.c tiny-json.h
|
tests.o: tests.c tiny-json.h
|
||||||
gcc -std=c99 -Wall -c tests.c
|
gcc $(CFLAGS) -c tests.c
|
||||||
|
20
tiny-json.c
20
tiny-json.c
@ -67,7 +67,7 @@ static char* setToNull( char* ch );
|
|||||||
static bool isEndOfPrimitive( char ch );
|
static bool isEndOfPrimitive( char ch );
|
||||||
|
|
||||||
/* Parse a string to get a json. */
|
/* Parse a string to get a json. */
|
||||||
json_t const* json_create_pool( char* str, jsonPool_t* pool ) {
|
json_t const* json_createWithPool( char *str, jsonPool_t *pool ) {
|
||||||
char* ptr = goBlank( str );
|
char* ptr = goBlank( str );
|
||||||
if ( !ptr || *ptr != '{' ) return 0;
|
if ( !ptr || *ptr != '{' ) return 0;
|
||||||
json_t* obj = pool->init( pool );
|
json_t* obj = pool->init( pool );
|
||||||
@ -82,10 +82,14 @@ json_t const* json_create_pool( char* str, jsonPool_t* pool ) {
|
|||||||
/* Parse a string to get a json. */
|
/* Parse a string to get a json. */
|
||||||
json_t const* json_create( char* str, json_t mem[], unsigned int qty ) {
|
json_t const* json_create( char* str, json_t mem[], unsigned int qty ) {
|
||||||
jsonStaticPool_t spool = {
|
jsonStaticPool_t spool = {
|
||||||
.mem = mem, .qty = qty,
|
.mem = mem,
|
||||||
.pool = { .init = poolInit, .new = poolNew }
|
.qty = qty,
|
||||||
|
.pool = {
|
||||||
|
.init = poolInit,
|
||||||
|
.new = poolNew
|
||||||
|
}
|
||||||
};
|
};
|
||||||
return json_create_pool( str, &spool.pool );
|
return json_createWithPool( str, &spool.pool );
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get a special character with its escape character. Examples:
|
/** Get a special character with its escape character. Examples:
|
||||||
@ -383,9 +387,9 @@ static char* objValue( char* ptr, json_t* obj, jsonPool_t* pool ) {
|
|||||||
* @param pool The handler of the pool.
|
* @param pool The handler of the pool.
|
||||||
* @return a instance of a json. */
|
* @return a instance of a json. */
|
||||||
static json_t* poolInit( jsonPool_t* pool ) {
|
static json_t* poolInit( jsonPool_t* pool ) {
|
||||||
jsonStaticPool_t* spool = json_container_of(pool, jsonStaticPool_t, pool);
|
jsonStaticPool_t *spool = json_containerOf( pool, jsonStaticPool_t, pool );
|
||||||
spool->nextFree = 1;
|
spool->nextFree = 1;
|
||||||
return &spool->mem[0];
|
return spool->mem;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Create an instance of a json from a pool.
|
/** Create an instance of a json from a pool.
|
||||||
@ -393,9 +397,9 @@ static json_t* poolInit( jsonPool_t* pool ) {
|
|||||||
* @retval The handler of the new instance if success.
|
* @retval The handler of the new instance if success.
|
||||||
* @retval Null pointer if the pool was empty. */
|
* @retval Null pointer if the pool was empty. */
|
||||||
static json_t* poolNew( jsonPool_t* pool ) {
|
static json_t* poolNew( jsonPool_t* pool ) {
|
||||||
jsonStaticPool_t* spool = json_container_of(pool, jsonStaticPool_t, pool);
|
jsonStaticPool_t *spool = json_containerOf( pool, jsonStaticPool_t, pool );
|
||||||
if ( spool->nextFree >= spool->qty ) return 0;
|
if ( spool->nextFree >= spool->qty ) return 0;
|
||||||
return &spool->mem[spool->nextFree++];
|
return spool->mem + spool->nextFree++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Checks whether an character belongs to set.
|
/** Checks whether an character belongs to set.
|
||||||
|
41
tiny-json.h
41
tiny-json.h
@ -35,13 +35,12 @@ extern "C" {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#define json_container_of(ptr, type, member) ({ \
|
#define json_containerOf( ptr, type, member ) \
|
||||||
void *__mptr = (void *)(ptr); \
|
((type*)( (char*)ptr - offsetof( type, member ) ))
|
||||||
((type *)(__mptr - offsetof(type, member))); })
|
|
||||||
|
|
||||||
/** @defgroup tinyJson Tiny JSON parser.
|
/** @defgroup tinyJson Tiny JSON parser.
|
||||||
* @{ */
|
* @{ */
|
||||||
@ -66,13 +65,6 @@ typedef struct json_s {
|
|||||||
jsonType_t type;
|
jsonType_t type;
|
||||||
} json_t;
|
} json_t;
|
||||||
|
|
||||||
/** Structure to handle a heap of JSON properties. */
|
|
||||||
typedef struct jsonPool_s jsonPool_t;
|
|
||||||
typedef struct jsonPool_s {
|
|
||||||
json_t* (*init) ( jsonPool_t* pool );
|
|
||||||
json_t* (*new) ( jsonPool_t* pool );
|
|
||||||
} jsonPool_t;
|
|
||||||
|
|
||||||
/** Parse a string to get a json.
|
/** Parse a string to get a json.
|
||||||
* @param str String pointer with a JSON object. It will be modified.
|
* @param str String pointer with a JSON object. It will be modified.
|
||||||
* @param mem Array of json properties to allocate.
|
* @param mem Array of json properties to allocate.
|
||||||
@ -82,14 +74,6 @@ typedef struct jsonPool_s {
|
|||||||
* This property is always unnamed and its type is JSON_OBJ. */
|
* This property is always unnamed and its type is JSON_OBJ. */
|
||||||
json_t const* json_create( char* str, json_t mem[], unsigned int qty );
|
json_t const* json_create( char* str, json_t mem[], unsigned int qty );
|
||||||
|
|
||||||
/** Parse a string to get a json.
|
|
||||||
* @param str String pointer with a JSON object. It will be modified.
|
|
||||||
* @param pool Custom json pool pointer.
|
|
||||||
* @retval Null pointer if any was wrong in the parse process.
|
|
||||||
* @retval If the parser process was successfully a valid handler of a json.
|
|
||||||
* This property is always unnamed and its type is JSON_OBJ. */
|
|
||||||
json_t const* json_create_pool( char* str, jsonPool_t *pool );
|
|
||||||
|
|
||||||
/** Get the name of a json property.
|
/** Get the name of a json property.
|
||||||
* @param json A valid handler of a json property.
|
* @param json A valid handler of a json property.
|
||||||
* @retval Pointer to null-terminated if property has name.
|
* @retval Pointer to null-terminated if property has name.
|
||||||
@ -156,7 +140,7 @@ static inline bool json_getBoolean( json_t const* property ) {
|
|||||||
* @param property A valid handler of a json object. Its type must be JSON_INTEGER.
|
* @param property A valid handler of a json object. Its type must be JSON_INTEGER.
|
||||||
* @return The value stdint. */
|
* @return The value stdint. */
|
||||||
static inline int64_t json_getInteger( json_t const* property ) {
|
static inline int64_t json_getInteger( json_t const* property ) {
|
||||||
return (int64_t)atoll( property->u.value );
|
return atoll( property->u.value );
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get the value of a json real property.
|
/** Get the value of a json real property.
|
||||||
@ -166,6 +150,23 @@ static inline double json_getReal( json_t const* property ) {
|
|||||||
return atof( property->u.value );
|
return atof( property->u.value );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/** Structure to handle a heap of JSON properties. */
|
||||||
|
typedef struct jsonPool_s jsonPool_t;
|
||||||
|
struct jsonPool_s {
|
||||||
|
json_t* (*init)( jsonPool_t* pool );
|
||||||
|
json_t* (*new)( jsonPool_t* pool );
|
||||||
|
};
|
||||||
|
|
||||||
|
/** Parse a string to get a json.
|
||||||
|
* @param str String pointer with a JSON object. It will be modified.
|
||||||
|
* @param pool Custom json pool pointer.
|
||||||
|
* @retval Null pointer if any was wrong in the parse process.
|
||||||
|
* @retval If the parser process was successfully a valid handler of a json.
|
||||||
|
* This property is always unnamed and its type is JSON_OBJ. */
|
||||||
|
json_t const* json_createWithPool( char* str, jsonPool_t* pool );
|
||||||
|
|
||||||
/** @ } */
|
/** @ } */
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
Loading…
Reference in New Issue
Block a user