Skip to content

Commit 8afb601

Browse files
author
diogorac
committed
added json-maker lib without escapes
1 parent 29c40ea commit 8afb601

File tree

8 files changed

+992
-1
lines changed

8 files changed

+992
-1
lines changed

libs/json-maker

Lines changed: 0 additions & 1 deletion
This file was deleted.

libs/json-maker/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

libs/json-maker/README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# JSON Maker
2+
3+
[![Build Status](https://travis-ci.org/rafagafe/json-maker.svg?branch=master)](https://travis-ci.org/rafagafe/json-maker)
4+
5+
JSON Maker is a C library used to code JSON objects in null-terminated strings.
6+
7+
Surely the most effective method to create simple JSON objects is to use sprintf. But when you need to reuse code, nest objects or include arrays you can fall into the formatted-strings hell.
8+
9+
* Backslash escapes are automatically added. Only in the fields of type string.
10+
* By means of compilation options, the use of print can be avoided. This is very useful in embedded systems with memory constraint.
11+
12+
If you need a JSON parser please visit: https://github.com/rafagafe/tiny-json
13+
14+
# Philosophy
15+
16+
To form JSON objects in strings of characters are invoked sequences of functions that concatenate sub-strings. Each substring includes a field of the JSON object, key-value. To add fields of the type object or array you need to invoke two functions, one to open and another to close.
17+
18+
```C
19+
20+
struct weather {
21+
int temp;
22+
int hum;
23+
};
24+
25+
/** Convert a weather structure in a JSON string.
26+
* @param dest Destination memory block.
27+
* @param src Source structure.
28+
* @return The length of the null-terminated string in dest. */
29+
int weather_to_json( char* dest, struct weather const* src ) {
30+
char* p = dest; // p always points to the null character
31+
p = json_objOpen( p, NULL ); // --> {\0
32+
p = json_int( p, "temp", src->temp ); // --> {"temp":22,\0
33+
p = json_int( p, "hum", src->hum ); // --> {"temp":22,"hum":45,\0
34+
p = json_objClose( p ); // --> {"temp":22,"hum":45},\0
35+
p = json_finish( p ); // --> {"temp":22,"hum":45}\0
36+
return p - dest;
37+
}
38+
39+
```
40+
41+
The complexity of these sequences of concatenations is kept in O(n) thanks to the fluent interface of JSON Maker.
42+
43+
It is very easy to extend the library by creating methods to convert C structures into JSON fields of object type. As with the arrays.
44+
45+
```C
46+
47+
struct weather {
48+
int temp;
49+
int hum;
50+
};
51+
52+
/* Add a time object property in a JSON string.
53+
"name":{"temp":-5,"hum":48}, */
54+
char* json_weather( char* dest, char const* name, struct weather const* weather ) {
55+
// dest always points to the null character
56+
dest = json_objOpen( dest, name ); // --> "name":{\0
57+
dest = json_int( dest, "temp", weather->temp ); // --> "name":{"temp":22,\0
58+
dest = json_int( dest, "hum", weather->hum ); // --> "name":{"temp":22,"hum":45,\0
59+
dest = json_objClose( dest ); // --> "name":{"temp":22,"hum":45},\0
60+
return dest;
61+
}
62+
63+
struct time {
64+
int hour;
65+
int minute;
66+
};
67+
68+
/* Add a time object property in a JSON string.
69+
"name":{"hour":18,"minute":32}, */
70+
char* json_time( char* dest, char const* name, struct time const* time ) {
71+
dest = json_objOpen( dest, name );
72+
dest = json_int( dest, "hour", time->hour );
73+
dest = json_int( dest, "minute", time->minute );
74+
dest = json_objClose( dest );
75+
return dest;
76+
}
77+
78+
struct measure {
79+
struct weather weather;
80+
struct time time;
81+
};
82+
83+
/** Convert a weather structure in a JSON string.
84+
* {"weather":{"temp":-5,"hum":48},"time":{"hour":18,"minute":32}}
85+
* @param dest Destination memory block.
86+
* @param src Source structure.
87+
* @return The length of the null-terminated string in dest. */
88+
char* measure_to_json( char* dest, struct measure const* measure ) {
89+
char* p = json_objOpen( dest, NULL );
90+
p = json_weather( p, "weather", &measure->weather );
91+
p = json_time( p, "time", &measure->time );
92+
p = json_objClose( p );
93+
p = json_finish( p );
94+
return p - dest;
95+
}
96+
97+
```
98+
99+
To see more nested JSON objects and arrays please read example.c.

libs/json-maker/example.c

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
2+
/*
3+
<https://github.com/rafagafe/tiny-json>
4+
5+
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
6+
SPDX-License-Identifier: MIT
7+
Copyright (c) 2018 Rafa Garcia <rafagarcia77@gmail.com>.
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
24+
*/
25+
26+
#include <stdio.h>
27+
#include <stdlib.h>
28+
#include "json-maker.h"
29+
30+
struct weather {
31+
int temp;
32+
int hum;
33+
};
34+
35+
struct time {
36+
int hour;
37+
int minute;
38+
};
39+
40+
struct measure {
41+
struct weather weather;
42+
struct time time;
43+
};
44+
45+
struct data {
46+
char const* city;
47+
char const* street;
48+
struct measure measure;
49+
int samples[ 4 ];
50+
};
51+
52+
/* Add a time object property in a JSON string.
53+
"name":{"temp":-5,"hum":48}, */
54+
char* json_weather( char* dest, char const* name, struct weather const* weather ) {
55+
dest = json_objOpen( dest, name ); // --> "name":{\0
56+
dest = json_int( dest, "temp", weather->temp ); // --> "name":{"temp":22,\0
57+
dest = json_int( dest, "hum", weather->hum ); // --> "name":{"temp":22,"hum":45,\0
58+
dest = json_objClose( dest ); // --> "name":{"temp":22,"hum":45},\0
59+
return dest;
60+
}
61+
62+
/* Add a time object property in a JSON string.
63+
"name":{"hour":18,"minute":32}, */
64+
char* json_time( char* dest, char const* name, struct time const* time ) {
65+
dest = json_objOpen( dest, name );
66+
dest = json_int( dest, "hour", time->hour );
67+
dest = json_int( dest, "minute", time->minute );
68+
dest = json_objClose( dest );
69+
return dest;
70+
}
71+
72+
/* Add a measure object property in a JSON string.
73+
"name":{"weather":{"temp":-5,"hum":48},"time":{"hour":18,"minute":32}}, */
74+
char* json_measure( char* dest, char const* name, struct measure const* measure ) {
75+
dest = json_objOpen( dest, name );
76+
dest = json_weather( dest, "weather", &measure->weather );
77+
dest = json_time( dest, "time", &measure->time );
78+
dest = json_objClose( dest );
79+
return dest;
80+
}
81+
82+
/* Add a data object property in a JSON string. */
83+
char* json_data( char* dest, char const* name, struct data const* data ) {
84+
dest = json_objOpen( dest, NULL );
85+
dest = json_str( dest, "city", data->city );
86+
dest = json_str( dest, "street", data->street );
87+
dest = json_measure( dest, "measure", &data->measure );
88+
dest = json_arrOpen( dest, "samples" );
89+
for( int i = 0; i < 4; ++i )
90+
dest = json_int( dest, NULL, data->samples[i] );
91+
dest = json_arrClose( dest );
92+
dest = json_objClose( dest );
93+
return dest;
94+
}
95+
96+
/** Convert a data structure to a root JSON object.
97+
* @param dest Destination memory block.
98+
* @param data Source data structure.
99+
* @return The JSON string length. */
100+
int data_to_json( char* dest, struct data const* data ) {
101+
char* p = json_data( dest, NULL, data );
102+
p = json_end( p );
103+
return p - dest;
104+
}
105+
106+
/*
107+
* {
108+
* "city": "liverpool",
109+
* "street": "mathew",
110+
* "measure": {
111+
* "weather": {
112+
* "temp": 25,
113+
* "hum": 65
114+
* },
115+
* "time": {
116+
* "hour": 14,
117+
* "minute": 31
118+
* }
119+
* },
120+
* "samples": [
121+
* 25,
122+
* 65,
123+
* -37,
124+
* 512
125+
* ]
126+
* }
127+
*
128+
*/
129+
130+
int main(int argc, char** argv) {
131+
static struct data const data = {
132+
.city = "liverpool",
133+
.street = "mathew",
134+
.measure = {
135+
.weather = {
136+
.hum = 65,
137+
.temp = 25
138+
},
139+
.time = {
140+
.hour = 14,
141+
.minute = 31
142+
}
143+
},
144+
.samples = {
145+
25,
146+
65,
147+
-37,
148+
512
149+
}
150+
};
151+
char buff[512];
152+
int len = data_to_json( buff, &data );
153+
if( len >= sizeof buff ) {
154+
fprintf( stderr, "%s%d%s%d\n", "Error. Len: ", len, " Max: ", (int)sizeof buff - 1 );
155+
return EXIT_FAILURE;
156+
}
157+
puts( buff );
158+
return EXIT_SUCCESS;
159+
}

0 commit comments

Comments
 (0)