|
| 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