Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 29 additions & 28 deletions samples/src/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,55 +51,55 @@ struct data {

/* Add a time object property in a JSON string.
"name":{"temp":-5,"hum":48}, */
char* json_weather( char* dest, char const* name, struct weather const* weather ) {
dest = json_objOpen( dest, name ); // --> "name":{\0
dest = json_int( dest, "temp", weather->temp ); // --> "name":{"temp":22,\0
dest = json_int( dest, "hum", weather->hum ); // --> "name":{"temp":22,"hum":45,\0
dest = json_objClose( dest ); // --> "name":{"temp":22,"hum":45},\0
char* json_weather( char* dest, char const* name, struct weather const* weather, size_t* remLen ) {
dest = json_objOpen( dest, name, remLen ); // --> "name":{\0
dest = json_int( dest, "temp", weather->temp, remLen ); // --> "name":{"temp":22,\0
dest = json_int( dest, "hum", weather->hum, remLen ); // --> "name":{"temp":22,"hum":45,\0
dest = json_objClose( dest, remLen ); // --> "name":{"temp":22,"hum":45},\0
return dest;
}

/* Add a time object property in a JSON string.
"name":{"hour":18,"minute":32}, */
char* json_time( char* dest, char const* name, struct time const* time ) {
dest = json_objOpen( dest, name );
dest = json_int( dest, "hour", time->hour );
dest = json_int( dest, "minute", time->minute );
dest = json_objClose( dest );
char* json_time( char* dest, char const* name, struct time const* time, size_t* remLen ) {
dest = json_objOpen( dest, name, remLen );
dest = json_int( dest, "hour", time->hour, remLen );
dest = json_int( dest, "minute", time->minute, remLen );
dest = json_objClose( dest, remLen );
return dest;
}

/* Add a measure object property in a JSON string.
"name":{"weather":{"temp":-5,"hum":48},"time":{"hour":18,"minute":32}}, */
char* json_measure( char* dest, char const* name, struct measure const* measure ) {
dest = json_objOpen( dest, name );
dest = json_weather( dest, "weather", &measure->weather );
dest = json_time( dest, "time", &measure->time );
dest = json_objClose( dest );
char* json_measure( char* dest, char const* name, struct measure const* measure, size_t* remLen ) {
dest = json_objOpen( dest, name, remLen );
dest = json_weather( dest, "weather", &measure->weather, remLen );
dest = json_time( dest, "time", &measure->time, remLen );
dest = json_objClose( dest, remLen );
return dest;
}

/* Add a data object property in a JSON string. */
char* json_data( char* dest, char const* name, struct data const* data ) {
dest = json_objOpen( dest, NULL );
dest = json_str( dest, "city", data->city );
dest = json_str( dest, "street", data->street );
dest = json_measure( dest, "measure", &data->measure );
dest = json_arrOpen( dest, "samples" );
char* json_data( char* dest, char const* name, struct data const* data, size_t* remLen ) {
dest = json_objOpen( dest, NULL, remLen );
dest = json_str( dest, "city", data->city, remLen );
dest = json_str( dest, "street", data->street, remLen );
dest = json_measure( dest, "measure", &data->measure, remLen );
dest = json_arrOpen( dest, "samples", remLen );
for( int i = 0; i < 4; ++i )
dest = json_int( dest, NULL, data->samples[i] );
dest = json_arrClose( dest );
dest = json_objClose( dest );
dest = json_int( dest, NULL, data->samples[i], remLen );
dest = json_arrClose( dest, remLen );
dest = json_objClose( dest, remLen );
return dest;
}

/** Convert a data structure to a root JSON object.
* @param dest Destination memory block.
* @param data Source data structure.
* @return The JSON string length. */
int data_to_json( char* dest, struct data const* data ) {
char* p = json_data( dest, NULL, data );
p = json_end( p );
int data_to_json( char* dest, struct data const* data, size_t* remLen ) {
char* p = json_data( dest, NULL, data, remLen );
p = json_end( p, remLen );
return p - dest;
}

Expand Down Expand Up @@ -149,7 +149,8 @@ int main(int argc, char** argv) {
}
};
char buff[512];
int len = data_to_json( buff, &data );
size_t remLen = sizeof(buff);
int len = data_to_json( buff, &data, &remLen );
if( len >= sizeof buff ) {
fprintf( stderr, "%s%d%s%d\n", "Error. Len: ", len, " Max: ", (int)sizeof buff - 1 );
return EXIT_FAILURE;
Expand Down
8 changes: 8 additions & 0 deletions src/include/json-maker/json-maker.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ char* json_ulong( char* dest, char const* name, unsigned long int value, size_t*
* @return Pointer to the new end of JSON under construction. */
char* json_verylong( char* dest, char const* name, long long int value, size_t* remLen );

/** Add a unsigned long long integer property in a JSON string.
* @param dest Pointer to the end of JSON under construction.
* @param name Pointer to null-terminated string or null for unnamed.
* @param value Value of the property.
* @param remLen Pointer to remaining length of dest
* @return Pointer to the new end of JSON under construction. */
char* json_uverylong( char* dest, char const* name, unsigned long long int value, size_t* remLen );

/** Add a double precision number property in a JSON string.
* @param dest Pointer to the end of JSON under construction.
* @param name Pointer to null-terminated string or null for unnamed.
Expand Down
208 changes: 164 additions & 44 deletions src/json-maker.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@
* @param remLen Pointer to remaining length of dest
* @return Pointer to the null character of the destination string. */
static char* chtoa( char* dest, char ch, size_t* remLen ) {
if (*remLen != 0) {
if (0 != *remLen) {
--*remLen;
*dest = ch;
*++dest = '\0';
*dest++ = ch;
}
if (0 == *remLen)
{
++*remLen;
--dest;
}
*dest = '\0';
return dest;
}

Expand All @@ -48,6 +53,11 @@ static char* chtoa( char* dest, char ch, size_t* remLen ) {
static char* atoa( char* dest, char const* src, size_t* remLen ) {
for( ; *src != '\0' && *remLen != 0; ++dest, ++src, --*remLen )
*dest = *src;
if (0 == *remLen)
{
++*remLen;
--dest;
}
*dest = '\0';
return dest;
}
Expand Down Expand Up @@ -222,16 +232,25 @@ char* json_end( char* dest, size_t* remLen ) {
if ( ',' == dest[-1] ) {
dest[-1] = '\0';
--dest;
*dest = '\0';
++*remLen;
}
return dest;
}

#ifdef NO_SPRINTF

static char* format( char* dest, int len, int isnegative ) {
if ( isnegative )
static char* format( char* dest, int len, int isnegative, size_t *remLen ) {
if ( isnegative && 0 != *remLen )
{
dest[ len++ ] = '-';
--*remLen;
}
if (0 == *remLen)
{
--len;
++*remLen;
}
dest[ len ] = '\0';
int head = 0;
int tail = len - 1;
Expand All @@ -245,36 +264,38 @@ static char* format( char* dest, int len, int isnegative ) {
return dest + len;
}

#define numtoa( func, type, utype ) \
static char* func( char* dest, type val ) { \
enum { base = 10 }; \
if ( 0 == val ) \
return chtoa( dest, '0' ); \
int const isnegative = 0 > val; \
utype num = isnegative ? -val : val; \
int len = 0; \
while( 0 != num ) { \
int rem = num % base; \
dest[ len++ ] = rem + '0'; \
num = num / base; \
} \
return format( dest, len, isnegative ); \
} \

#define json_num( func, func2, type ) \
char* func( char* dest, char const* name, type value ) { \
dest = primitivename( dest, name ); \
dest = func2( dest, value ); \
dest = chtoa( dest, ',' ); \
return dest; \
} \
#define numtoa( func, type, utype ) \
static char* func( char* dest, type val, size_t *remLen ) { \
enum { base = 10 }; \
if ( 0 == val ) \
return chtoa( dest, '0', remLen ); \
int const isnegative = 0 > val; \
utype num = isnegative ? -val : val; \
int len = 0; \
while( 0 != num && *remLen != 0 ) { \
int rem = num % base; \
dest[ len++ ] = rem + '0'; \
--*remLen; \
num = num / base; \
} \
return format( dest, len, isnegative, remLen ); \
}

#define json_num( func, func2, type ) \
char* func( char* dest, char const* name, type value, size_t *remLen ) { \
dest = primitivename( dest, name, remLen ); \
dest = func2( dest, value, remLen ); \
dest = chtoa( dest, ',', remLen ); \
return dest; \
}

#define ALL_TYPES \
X( int, int, unsigned int ) \
X( long, long, unsigned long ) \
X( uint, unsigned int, unsigned int ) \
X( ulong, unsigned long, unsigned long ) \
X( verylong, long long, unsigned long long ) \
X( int, int, unsigned int ) \
X( long, long, unsigned long ) \
X( uint, unsigned int, unsigned int ) \
X( ulong, unsigned long, unsigned long ) \
X( verylong, long long, unsigned long long ) \
X( uverylong, unsigned long long, unsigned long long )

#define X( name, type, utype ) numtoa( name##toa, type, utype )
ALL_TYPES
Expand All @@ -284,30 +305,102 @@ ALL_TYPES
ALL_TYPES
#undef X

char* json_double( char* dest, char const* name, double value ) {
return json_verylong( dest, name, value );
#ifndef JSON_DBL_PREC
#define JSON_DBL_PREC 6 /* Default fractional precision for double output */
#endif

/* Simple check for finiteness: true if neither NaN nor Inf */
static int json_isfinite(double x) {
return (x == x) && ((x - x) == 0.0); /* NaN != NaN and Inf - Inf → NaN */
}

/* Precomputed powers of ten for fractional scaling */
static const unsigned long long json_pow10[] = {
1ULL, 10ULL, 100ULL, 1000ULL, 10000ULL, 100000ULL,
1000000ULL, 10000000ULL, 100000000ULL, 1000000000ULL,
10000000000ULL, 100000000000ULL, 1000000000000ULL,
10000000000000ULL, 100000000000000ULL, 1000000000000000ULL,
};

/* Write the fractional digits with zero padding up to `prec` digits */
static char* json_write_frac_fixed(char* dest,
unsigned long long frac_scaled,
int prec,
size_t* remLen) {
for (int i = prec - 1; i >= 0; --i) {
unsigned long long div = json_pow10[i];
char d = (char)('0' + (char)((frac_scaled / div) % 10ULL));
dest = chtoa(dest, d, remLen);
}
return dest;
}

char* json_double(char* dest, char const* name, double value, size_t* remLen) {
dest = primitivename(dest, name, remLen);

if (!json_isfinite(value)) {
/* JSON does not support NaN/Infinity → map to null */
dest = atoa(dest, "null,", remLen);
return dest;
}

/* Handle sign */
if (value < 0) {
dest = chtoa(dest, '-', remLen);
value = -value;
}

/* Split into integer and fractional part */
unsigned long long ipart = (unsigned long long)value;
double frac = value - (double)ipart;

int prec = (JSON_DBL_PREC < (int)(sizeof(json_pow10)/sizeof(json_pow10[0]))-1)
? JSON_DBL_PREC
: (int)(sizeof(json_pow10)/sizeof(json_pow10[0]))-1;

unsigned long long scale = json_pow10[prec];
/* +0.5 for rounding */
unsigned long long fscaled = (unsigned long long)(frac * (double)scale + 0.5);

/* Handle fractional overflow, e.g. 0.999999 → 1.000000 */
if (fscaled >= scale) {
fscaled = 0;
++ipart;
}

/* Write integer part */
dest = uverylongtoa(dest, ipart, remLen);

if (prec > 0) {
dest = chtoa(dest, '.', remLen);
dest = json_write_frac_fixed(dest, fscaled, prec, remLen);
}

dest = chtoa(dest, ',', remLen);
return dest;
}

#else

#include <stdio.h>
#include <math.h>

#define ALL_TYPES \
X( json_int, int, "%d" ) \
X( json_long, long, "%ld" ) \
X( json_uint, unsigned int, "%u" ) \
X( json_ulong, unsigned long, "%lu" ) \
X( json_verylong, long long, "%lld" ) \
X( json_double, double, "%g" ) \
#define ALL_TYPES \
X( json_int, int, "%d" ) \
X( json_long, long, "%ld" ) \
X( json_uint, unsigned int, "%u" ) \
X( json_ulong, unsigned long, "%lu" ) \
X( json_verylong, long long, "%lld" ) \
X( json_uverylong, unsigned long long, "%llu" )


#define json_num( funcname, type, fmt ) \
#define json_num( funcname, type, fmt ) \
char* funcname( char* dest, char const* name, type value, size_t* remLen ) { \
int digitLen; \
dest = primitivename( dest, name, remLen ); \
digitLen = snprintf( dest, *remLen, fmt, value ); \
if(digitLen >= (int)*remLen+1){ \
digitLen = (int)*remLen;} \
digitLen = (int)*remLen;} \
*remLen -= (size_t)digitLen; \
dest += digitLen; \
dest = chtoa( dest, ',', remLen ); \
Expand All @@ -318,5 +411,32 @@ char* funcname( char* dest, char const* name, type value, size_t* remLen ) {
ALL_TYPES
#undef X

char* json_double(char* dest, char const* name, double value, size_t* remLen) {
dest = primitivename(dest, name, remLen);

if (!isfinite(value)) {
dest = atoa(dest, "null,", remLen);
return dest;
}

/* Use high precision for JSON serialization */
int wrote = snprintf(dest, *remLen, "%.17g", value);

if (wrote < 0) {
/* snprintf error → fallback to null */
dest = atoa(dest, "null,", remLen);
return dest;
} else if ((size_t)wrote >= *remLen) {
/* Output was truncated: snprintf wrote *remLen-1 chars + NUL */
dest += *remLen - 1;
*remLen = 1;
} else {
dest += wrote;
*remLen -= (size_t)wrote;
}

dest = chtoa(dest, ',', remLen);
return dest;
}

#endif
Loading