├── .gitignore ├── .travis.yml ├── CMakeLists.txt ├── LICENSE ├── README.md ├── samples ├── CMakeLists.txt └── src │ ├── CMakeLists.txt │ └── example.c ├── src ├── CMakeLists.txt ├── include │ └── json-maker │ │ └── json-maker.h └── json-maker.c └── tests ├── CMakeLists.txt └── src ├── CMakeLists.txt └── test.c /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | TODO.txt 3 | .idea 4 | **.swp 5 | **.orig 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | compiler: 2 | - gcc 3 | git: 4 | depth: 3 5 | language: c 6 | os: freebsd 7 | 8 | env: 9 | - BUILDTYPE=RELEASE 10 | - BUILDTYPE=DEBUG 11 | script: 12 | - "mkdir -p build/$BUILDTYPE" 13 | - "cd build/$BUILDTYPE" 14 | - "cmake --configure -DCMAKE_BUILD_TYPE=$BUILDTYPE ../../" 15 | - "cmake --build ." 16 | - "cd tests" 17 | - "ctest ." 18 | 19 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.15) 2 | 3 | project(json-maker 4 | VERSION 1.1.0.0 5 | DESCRIPTION "JSON Maker is a C library used to code JSON objects in null-terminated strings." 6 | HOMEPAGE_URL "https://github.com/rafagafe/json-maker/blob/master/makefile" 7 | LANGUAGES C 8 | ) 9 | 10 | set(CMAKE_COLOR_MAKEFILE ON CACHE BOOL "ON" FORCE) 11 | set(CMAKE_EXECUTE_PROCESS_COMMAND_ECHO STDOUT) 12 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) 13 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) 14 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib/static) 15 | set(CMAKE_DEBUG_POSTFIX "-d") 16 | 17 | option(BUILD_SAMPLES "Will add sample apps to the build process." OFF) 18 | 19 | add_compile_options(-std=c99 -Wall -pedantic) 20 | 21 | add_subdirectory(src) 22 | add_subdirectory(tests) 23 | if(BUILD_SAMPLES) 24 | add_subdirectory(samples) 25 | endif() #BUILD_SAMPLES 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 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. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 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_end( 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 | int 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_end( p ); 94 | return p - dest; 95 | } 96 | 97 | ``` 98 | 99 | To see more nested JSON objects and arrays please read example.c. 100 | 101 | #Building and Testing 102 | 103 | JSON Maker is built as a static library. 104 | JSON Maker relies on CMake and CTest for building and testing (see [https://cmake.org/](https://cmake.org/) for more information). 105 | 106 | The library is found under `{project_root}/build/lib/static`, the executables under `{project_root}/build/bin`. 107 | 108 | Two configurations are supported, `Debug` and `Release`. You can use option `CMAKE_BUILD_TYPE` to change from the default (`Debug`). 109 | The following examples assume `Debug` builds, except for install which uses `Release`. 110 | ## Create the build folder 111 | 112 | ```shell 113 | mkdir build 114 | cd build 115 | ``` 116 | 117 | ##Building the static library 118 | 119 | ```shell 120 | cmake --configure .. 121 | cmake --build . 122 | ``` 123 | 124 | ##Installing the static library 125 | if you are not super-user or don't want to use sudo you can install the library in your home's `opt` folder. 126 | ```shell 127 | cmake --configure -DCMAKE_BUILD_TYPE=Release.. 128 | cmake --build . 129 | ``` 130 | As sudo/root (will use default `/usr/local/` on Unix) 131 | ``` 132 | cmake --install . 133 | ``` 134 | As a regular user 135 | ``` 136 | cmake --install . --prefix $HOME/opt 137 | ``` 138 | 139 | ##Building the sample application 140 | 141 | ```shell 142 | cmake --configure -DBUILD_SAMPLES=ON .. 143 | cmake --build . 144 | ``` 145 | 146 | ##Runing the tests 147 | 148 | ```shell 149 | cmake --configure .. 150 | cmake --build . 151 | cd tests 152 | ctest . 153 | ``` -------------------------------------------------------------------------------- /samples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(src) 2 | -------------------------------------------------------------------------------- /samples/src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable(sample_1 example.c) 2 | target_link_libraries(sample_1 PRIVATE json_maker) 3 | -------------------------------------------------------------------------------- /samples/src/example.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | 4 | 5 | Licensed under the MIT License . 6 | SPDX-License-Identifier: MIT 7 | Copyright (c) 2018 Rafa Garcia . 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 27 | #include 28 | #include "json-maker/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 | } 160 | -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_library(json_maker_api INTERFACE) 2 | target_include_directories(json_maker_api INTERFACE include) 3 | set_target_properties(json_maker_api PROPERTIES PUBLIC_HEADER include/json-maker/json-maker.h) 4 | 5 | add_library(json_maker STATIC) 6 | target_sources(json_maker PUBLIC json-maker.c) 7 | target_link_libraries(json_maker PUBLIC json_maker_api) 8 | 9 | include(GNUInstallDirs) 10 | install(TARGETS json_maker json_maker_api 11 | PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/json_maker 12 | ) 13 | -------------------------------------------------------------------------------- /src/include/json-maker/json-maker.h: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | 4 | 5 | Licensed under the MIT License . 6 | SPDX-License-Identifier: MIT 7 | Copyright (c) 2018 Rafa Garcia . 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 27 | 28 | #ifndef MAKE_JSON_H 29 | #define MAKE_JSON_H 30 | 31 | #ifdef __cplusplus 32 | extern "C" { 33 | #endif 34 | 35 | /** @defgroup makejoson Make JSON. 36 | * @{ */ 37 | 38 | /** Open a JSON object in a JSON string. 39 | * @param dest Pointer to the end of JSON under construction. 40 | * @param name Pointer to null-terminated string or null for unnamed. 41 | * @param remLen Pointer to remaining length of dest 42 | * @return Pointer to the new end of JSON under construction. */ 43 | char* json_objOpen( char* dest, char const* name, size_t* remLen ); 44 | 45 | /** Close a JSON object in a JSON string. 46 | * @param dest Pointer to the end of JSON under construction. 47 | * @param remLen Pointer to remaining length of dest 48 | * @return Pointer to the new end of JSON under construction. */ 49 | char* json_objClose( char* dest, size_t* remLen ); 50 | 51 | /** Used to finish the root JSON object. After call json_objClose(). 52 | * @param dest Pointer to the end of JSON under construction. 53 | * @param remLen Pointer to remaining length of dest 54 | * @return Pointer to the new end of JSON under construction. */ 55 | char* json_end( char* dest, size_t* remLen ); 56 | 57 | /** Open an array in a JSON string. 58 | * @param dest Pointer to the end of JSON under construction. 59 | * @param name Pointer to null-terminated string or null for unnamed. 60 | * @param remLen Pointer to remaining length of dest 61 | * @return Pointer to the new end of JSON under construction. */ 62 | char* json_arrOpen( char* dest, char const* name, size_t* remLen ); 63 | 64 | /** Close an array in a JSON string. 65 | * @param dest Pointer to the end of JSON under construction. 66 | * @param remLen Pointer to remaining length of dest 67 | * @return Pointer to the new end of JSON under construction. */ 68 | char* json_arrClose( char* dest, size_t* remLen ); 69 | /** Add a text property in a JSON string. 70 | * @param dest Pointer to the end of JSON under construction. 71 | * @param name Pointer to null-terminated string or null for unnamed. 72 | * @param value A valid null-terminated string with the value. 73 | * Backslash escapes will be added for special characters. 74 | * @param len Max length of value. < 0 for unlimit. 75 | * @param remLen Pointer to remaining length of dest 76 | * @return Pointer to the new end of JSON under construction. */ 77 | char* json_nstr( char* dest, char const* name, char const* value, int len, size_t* remLen ); 78 | 79 | /** Add a text property in a JSON string. 80 | * @param dest Pointer to the end of JSON under construction. 81 | * @param name Pointer to null-terminated string or null for unnamed. 82 | * @param value A valid null-terminated string with the value. 83 | * Backslash escapes will be added for special characters. 84 | * @param remLen Pointer to remaining length of dest 85 | * @return Pointer to the new end of JSON under construction. */ 86 | static inline char* json_str( char* dest, char const* name, char const* value, size_t* remLen ) { 87 | return json_nstr( dest, name, value, -1, remLen ); 88 | } 89 | 90 | /** Add a boolean property in a JSON string. 91 | * @param dest Pointer to the end of JSON under construction. 92 | * @param name Pointer to null-terminated string or null for unnamed. 93 | * @param value Zero for false. Non zero for true. 94 | * @param remLen Pointer to remaining length of dest 95 | * @return Pointer to the new end of JSON under construction. */ 96 | char* json_bool( char* dest, char const* name, int value, size_t* remLen ); 97 | 98 | /** Add a null property in a JSON string. 99 | * @param dest Pointer to the end of JSON under construction. 100 | * @param name Pointer to null-terminated string or null for unnamed. 101 | * @param remLen Pointer to remaining length of dest 102 | * @return Pointer to the new end of JSON under construction. */ 103 | char* json_null( char* dest, char const* name, size_t* remLen ); 104 | 105 | /** Add an integer property in a JSON string. 106 | * @param dest Pointer to the end of JSON under construction. 107 | * @param name Pointer to null-terminated string or null for unnamed. 108 | * @param value Value of the property. 109 | * @param remLen Pointer to remaining length of dest 110 | * @return Pointer to the new end of JSON under construction. */ 111 | char* json_int( char* dest, char const* name, int value, size_t* remLen ); 112 | 113 | /** Add an unsigned integer property in a JSON string. 114 | * @param dest Pointer to the end of JSON under construction. 115 | * @param name Pointer to null-terminated string or null for unnamed. 116 | * @param value Value of the property. 117 | * @param remLen Pointer to remaining length of dest 118 | * @return Pointer to the new end of JSON under construction. */ 119 | char* json_uint( char* dest, char const* name, unsigned int value, size_t* remLen ); 120 | 121 | /** Add a long integer property in a JSON string. 122 | * @param dest Pointer to the end of JSON under construction. 123 | * @param name Pointer to null-terminated string or null for unnamed. 124 | * @param value Value of the property. 125 | * @param remLen Pointer to remaining length of dest 126 | * @return Pointer to the new end of JSON under construction. */ 127 | char* json_long( char* dest, char const* name, long int value, size_t* remLen ); 128 | 129 | /** Add an unsigned long integer property in a JSON string. 130 | * @param dest Pointer to the end of JSON under construction. 131 | * @param name Pointer to null-terminated string or null for unnamed. 132 | * @param value Value of the property. 133 | * @param remLen Pointer to remaining length of dest 134 | * @return Pointer to the new end of JSON under construction. */ 135 | char* json_ulong( char* dest, char const* name, unsigned long int value, size_t* remLen ); 136 | 137 | /** Add a long long integer property in a JSON string. 138 | * @param dest Pointer to the end of JSON under construction. 139 | * @param name Pointer to null-terminated string or null for unnamed. 140 | * @param value Value of the property. 141 | * @param remLen Pointer to remaining length of dest 142 | * @return Pointer to the new end of JSON under construction. */ 143 | char* json_verylong( char* dest, char const* name, long long int value, size_t* remLen ); 144 | 145 | /** Add a double precision number property in a JSON string. 146 | * @param dest Pointer to the end of JSON under construction. 147 | * @param name Pointer to null-terminated string or null for unnamed. 148 | * @param value Value of the property. 149 | * @param remLen Pointer to remaining length of dest 150 | * @return Pointer to the new end of JSON under construction. */ 151 | char* json_double( char* dest, char const* name, double value, size_t* remLen ); 152 | 153 | /** @ } */ 154 | 155 | #ifdef __cplusplus 156 | } 157 | #endif 158 | 159 | #endif /* MAKE_JSON_H */ 160 | 161 | -------------------------------------------------------------------------------- /src/json-maker.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | 4 | 5 | Licensed under the MIT License . 6 | SPDX-License-Identifier: MIT 7 | Copyright (c) 2018 Rafa Garcia . 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 // For NULL 27 | #include "json-maker/json-maker.h" 28 | 29 | /** Add a character at the end of a string. 30 | * @param dest Pointer to the null character of the string 31 | * @param ch Value to be added. 32 | * @param remLen Pointer to remaining length of dest 33 | * @return Pointer to the null character of the destination string. */ 34 | static char* chtoa( char* dest, char ch, size_t* remLen ) { 35 | if (*remLen != 0) { 36 | --*remLen; 37 | *dest = ch; 38 | *++dest = '\0'; 39 | } 40 | return dest; 41 | } 42 | 43 | /** Copy a null-terminated string. 44 | * @param dest Destination memory block. 45 | * @param src Source string. 46 | * @param remLen Pointer to remaining length of dest 47 | * @return Pointer to the null character of the destination string. */ 48 | static char* atoa( char* dest, char const* src, size_t* remLen ) { 49 | for( ; *src != '\0' && *remLen != 0; ++dest, ++src, --*remLen ) 50 | *dest = *src; 51 | *dest = '\0'; 52 | return dest; 53 | } 54 | 55 | /* Open a JSON object in a JSON string. */ 56 | char* json_objOpen( char* dest, char const* name, size_t* remLen ) { 57 | if ( NULL == name ) 58 | dest = chtoa( dest, '{', remLen ); 59 | else { 60 | dest = chtoa( dest, '\"', remLen ); 61 | dest = atoa( dest, name, remLen ); 62 | dest = atoa( dest, "\":{", remLen ); 63 | } 64 | return dest; 65 | } 66 | 67 | /* Close a JSON object in a JSON string. */ 68 | char* json_objClose( char* dest, size_t* remLen ) { 69 | if ( dest[-1] == ',' ) 70 | { 71 | --dest; 72 | ++*remLen; 73 | } 74 | return atoa( dest, "},", remLen ); 75 | } 76 | 77 | /* Open an array in a JSON string. */ 78 | char* json_arrOpen( char* dest, char const* name, size_t* remLen ) { 79 | if ( NULL == name ) 80 | dest = chtoa( dest, '[', remLen ); 81 | else { 82 | dest = chtoa( dest, '\"', remLen ); 83 | dest = atoa( dest, name, remLen ); 84 | dest = atoa( dest, "\":[", remLen ); 85 | } 86 | return dest; 87 | } 88 | 89 | /* Close an array in a JSON string. */ 90 | char* json_arrClose( char* dest, size_t* remLen ) { 91 | if ( dest[-1] == ',') 92 | { 93 | --dest; 94 | ++*remLen; 95 | } 96 | return atoa( dest, "],", remLen ); 97 | } 98 | 99 | /** Add the name of a text property. 100 | * @param dest Destination memory. 101 | * @param name The name of the property. 102 | * @param remLen Pointer to remaining length of dest 103 | * @return Pointer to the next char. */ 104 | static char* strname( char* dest, char const* name, size_t* remLen ) { 105 | dest = chtoa( dest, '\"', remLen ); 106 | if ( NULL != name ) { 107 | dest = atoa( dest, name, remLen ); 108 | dest = atoa( dest, "\":\"", remLen ); 109 | } 110 | return dest; 111 | } 112 | 113 | /** Get the hexadecimal digit of the least significant nibble of a integer. */ 114 | static int nibbletoch( int nibble ) { 115 | return "0123456789ABCDEF"[ nibble % 16u ]; 116 | } 117 | 118 | /** Get the escape character of a non-printable. 119 | * @param ch Character source. 120 | * @return The escape character or null character if error. */ 121 | static int escape( int ch ) { 122 | int i; 123 | static struct { char code; char ch; } const pair[] = { 124 | { '\"', '\"' }, { '\\', '\\' }, { '/', '/' }, { 'b', '\b' }, 125 | { 'f', '\f' }, { 'n', '\n' }, { 'r', '\r' }, { 't', '\t' }, 126 | }; 127 | for( i = 0; i < sizeof pair / sizeof *pair; ++i ) 128 | if ( ch == pair[i].ch ) 129 | return pair[i].code; 130 | return '\0'; 131 | } 132 | 133 | /** Copy a null-terminated string inserting escape characters if needed. 134 | * @param dest Destination memory block. 135 | * @param src Source string. 136 | * @param len Max length of source. < 0 for unlimit. 137 | * @param remLen Pointer to remaining length of dest 138 | * @return Pointer to the null character of the destination string. */ 139 | static char* atoesc( char* dest, char const* src, int len, size_t* remLen ) { 140 | int i; 141 | for( i = 0; src[i] != '\0' && ( i < len || 0 > len ) && *remLen != 0; ++dest, ++i, --*remLen ) { 142 | if ( src[i] >= ' ' && src[i] != '\"' && src[i] != '\\' && src[i] != '/' ) 143 | *dest = src[i]; 144 | else { 145 | if (*remLen != 0) { 146 | *dest++ = '\\'; 147 | --*remLen; 148 | int const esc = escape( src[i] ); 149 | if ( esc ) { 150 | if (*remLen != 0) 151 | *dest = esc; 152 | } else { 153 | if (*remLen != 0) { 154 | --*remLen; 155 | *dest++ = 'u'; 156 | } 157 | if (*remLen != 0) { 158 | --*remLen; 159 | *dest++ = '0'; 160 | } 161 | if (*remLen != 0) { 162 | --*remLen; 163 | *dest++ = '0'; 164 | } 165 | if (*remLen != 0) { 166 | --*remLen; 167 | *dest++ = nibbletoch( src[i] / 16 ); 168 | } 169 | if (*remLen != 0) { 170 | --*remLen; 171 | *dest++ = nibbletoch( src[i] ); 172 | } 173 | } 174 | } 175 | } 176 | 177 | if (*remLen == 0) 178 | break; 179 | } 180 | *dest = '\0'; 181 | return dest; 182 | } 183 | 184 | /* Add a text property in a JSON string. */ 185 | char* json_nstr( char* dest, char const* name, char const* value, int len, size_t* remLen ) { 186 | dest = strname( dest, name, remLen ); 187 | dest = atoesc( dest, value, len, remLen ); 188 | dest = atoa( dest, "\",", remLen ); 189 | return dest; 190 | } 191 | 192 | /** Add the name of a primitive property. 193 | * @param dest Destination memory. 194 | * @param name The name of the property. 195 | * @param remLen Pointer to remaining length of dest 196 | * @return Pointer to the next char. */ 197 | static char* primitivename( char* dest, char const* name, size_t* remLen ) { 198 | if( NULL == name ) 199 | return dest; 200 | dest = chtoa( dest, '\"', remLen ); 201 | dest = atoa( dest, name, remLen ); 202 | dest = atoa( dest, "\":", remLen ); 203 | return dest; 204 | } 205 | 206 | /* Add a boolean property in a JSON string. */ 207 | char* json_bool( char* dest, char const* name, int value, size_t* remLen ) { 208 | dest = primitivename( dest, name, remLen ); 209 | dest = atoa( dest, value ? "true," : "false,", remLen ); 210 | return dest; 211 | } 212 | 213 | /* Add a null property in a JSON string. */ 214 | char* json_null( char* dest, char const* name, size_t* remLen ) { 215 | dest = primitivename( dest, name, remLen ); 216 | dest = atoa( dest, "null,", remLen ); 217 | return dest; 218 | } 219 | 220 | /* Used to finish the root JSON object. After call json_objClose(). */ 221 | char* json_end( char* dest, size_t* remLen ) { 222 | if ( ',' == dest[-1] ) { 223 | dest[-1] = '\0'; 224 | --dest; 225 | ++*remLen; 226 | } 227 | return dest; 228 | } 229 | 230 | #ifdef NO_SPRINTF 231 | 232 | static char* format( char* dest, int len, int isnegative ) { 233 | if ( isnegative ) 234 | dest[ len++ ] = '-'; 235 | dest[ len ] = '\0'; 236 | int head = 0; 237 | int tail = len - 1; 238 | while( head < tail ) { 239 | char tmp = dest[ head ]; 240 | dest[ head ] = dest[ tail ]; 241 | dest[ tail ] = tmp; 242 | ++head; 243 | --tail; 244 | } 245 | return dest + len; 246 | } 247 | 248 | #define numtoa( func, type, utype ) \ 249 | static char* func( char* dest, type val ) { \ 250 | enum { base = 10 }; \ 251 | if ( 0 == val ) \ 252 | return chtoa( dest, '0' ); \ 253 | int const isnegative = 0 > val; \ 254 | utype num = isnegative ? -val : val; \ 255 | int len = 0; \ 256 | while( 0 != num ) { \ 257 | int rem = num % base; \ 258 | dest[ len++ ] = rem + '0'; \ 259 | num = num / base; \ 260 | } \ 261 | return format( dest, len, isnegative ); \ 262 | } \ 263 | 264 | #define json_num( func, func2, type ) \ 265 | char* func( char* dest, char const* name, type value ) { \ 266 | dest = primitivename( dest, name ); \ 267 | dest = func2( dest, value ); \ 268 | dest = chtoa( dest, ',' ); \ 269 | return dest; \ 270 | } \ 271 | 272 | #define ALL_TYPES \ 273 | X( int, int, unsigned int ) \ 274 | X( long, long, unsigned long ) \ 275 | X( uint, unsigned int, unsigned int ) \ 276 | X( ulong, unsigned long, unsigned long ) \ 277 | X( verylong, long long, unsigned long long ) \ 278 | 279 | #define X( name, type, utype ) numtoa( name##toa, type, utype ) 280 | ALL_TYPES 281 | #undef X 282 | 283 | #define X( name, type, utype ) json_num( json_##name, name##toa, type ) 284 | ALL_TYPES 285 | #undef X 286 | 287 | char* json_double( char* dest, char const* name, double value ) { 288 | return json_verylong( dest, name, value ); 289 | } 290 | 291 | #else 292 | 293 | #include 294 | 295 | #define ALL_TYPES \ 296 | X( json_int, int, "%d" ) \ 297 | X( json_long, long, "%ld" ) \ 298 | X( json_uint, unsigned int, "%u" ) \ 299 | X( json_ulong, unsigned long, "%lu" ) \ 300 | X( json_verylong, long long, "%lld" ) \ 301 | X( json_double, double, "%g" ) \ 302 | 303 | 304 | #define json_num( funcname, type, fmt ) \ 305 | char* funcname( char* dest, char const* name, type value, size_t* remLen ) { \ 306 | int digitLen; \ 307 | dest = primitivename( dest, name, remLen ); \ 308 | digitLen = snprintf( dest, *remLen, fmt, value ); \ 309 | if(digitLen >= (int)*remLen+1){ \ 310 | digitLen = (int)*remLen;} \ 311 | *remLen -= (size_t)digitLen; \ 312 | dest += digitLen; \ 313 | dest = chtoa( dest, ',', remLen ); \ 314 | return dest; \ 315 | } 316 | 317 | #define X( name, type, fmt ) json_num( name, type, fmt ) 318 | ALL_TYPES 319 | #undef X 320 | 321 | 322 | #endif 323 | -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | enable_testing() 2 | 3 | add_subdirectory(src) 4 | -------------------------------------------------------------------------------- /tests/src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable(json_maker_test test.c) 2 | target_link_libraries(json_maker_test PRIVATE json_maker) 3 | 4 | add_test(NAME run_main_tests COMMAND json_maker_test WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}) 5 | -------------------------------------------------------------------------------- /tests/src/test.c: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | 4 | 5 | 6 | Licensed under the MIT License . 7 | SPDX-License-Identifier: MIT 8 | Copyright (c) 2018 Rafa Garcia . 9 | 10 | Permission is hereby granted, free of charge, to any person obtaining a copy 11 | of this software and associated documentation files (the "Software"), to deal 12 | in the Software without restriction, including without limitation the rights 13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 | copies of the Software, and to permit persons to whom the Software is 15 | furnished to do so, subject to the following conditions: 16 | 17 | The above copyright notice and this permission notice shall be included in all 18 | copies or substantial portions of the Software. 19 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26 | SOFTWARE. 27 | 28 | */ 29 | 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include "json-maker/json-maker.h" 35 | 36 | // ----------------------------------------------------- Test "framework": --- 37 | 38 | #define done() return 0 39 | #define fail() return __LINE__ 40 | static int checkqty = 0; 41 | #define check( x ) do { ++checkqty; if (!(x)) fail(); } while ( 0 ) 42 | 43 | struct test { 44 | int(*func)(void); 45 | char const* name; 46 | }; 47 | 48 | static int test_suit( struct test const* tests, int numtests ) { 49 | printf( "%s", "\n\nTests:\n" ); 50 | int failed = 0; 51 | for( int i = 0; i < numtests; ++i ) { 52 | printf( " %02d%s%-25s ", i, ": ", tests[i].name ); 53 | int linerr = tests[i].func(); 54 | if ( 0 == linerr ) 55 | printf( "%s", "OK\n" ); 56 | else { 57 | printf( "%s%d\n", "Failed, line: ", linerr ); 58 | ++failed; 59 | } 60 | } 61 | printf( "\n%s%d\n", "Total checks: ", checkqty ); 62 | printf( "%s[ %d / %d ]\r\n\n\n", "Tests PASS: ", numtests - failed, numtests ); 63 | return failed; 64 | } 65 | 66 | 67 | // ----------------------------------------------------------- Unit tests: --- 68 | 69 | static int escape( void ) { 70 | char buff[512]; 71 | char* p = json_objOpen( buff, NULL ); 72 | p = json_str( p, "name", "\tHello: \"man\"\n" ); 73 | p = json_objClose( p ); 74 | p = json_end( p ); 75 | printf( "\n\n%s\n\n", buff ); 76 | static char const rslt[] = "{\"name\":\"\\tHello: \\\"man\\\"\\n\"}"; 77 | check( p - buff == sizeof rslt - 1 ); 78 | check( 0 == strcmp( buff, rslt ) ); 79 | done(); 80 | } 81 | 82 | static int len( void ) { 83 | char buff[512]; 84 | char* p = json_objOpen( buff, NULL ); 85 | p = json_nstr( p, "name", "\tHello: \"man\"\n", 6 ); 86 | p = json_objClose( p ); 87 | p = json_end( p ); 88 | static char const rslt[] = "{\"name\":\"\\tHello\"}"; 89 | check( p - buff == sizeof rslt - 1 ); 90 | check( 0 == strcmp( buff, rslt ) ); 91 | done(); 92 | } 93 | 94 | static int empty( void ) { 95 | char buff[512]; 96 | { 97 | char* p = json_objOpen( buff, NULL ); 98 | p = json_objClose( p ); 99 | p = json_end( p ); 100 | static char const rslt[] = "{}"; 101 | check( p - buff == sizeof rslt - 1 ); 102 | check( 0 == strcmp( buff, rslt ) ); 103 | } 104 | { 105 | char* p = json_objOpen( buff, NULL ); 106 | p = json_arrOpen( p, "a" ); 107 | p = json_arrClose( p ); 108 | p = json_objClose( p ); 109 | p = json_end( p ); 110 | static char const rslt[] = "{\"a\":[]}"; 111 | check( p - buff == sizeof rslt - 1 ); 112 | check( 0 == strcmp( buff, rslt ) ); 113 | } 114 | { 115 | char* p = json_objOpen( buff, NULL ); 116 | p = json_arrOpen( p, "a" ); 117 | p = json_objOpen( p, NULL ); 118 | p = json_objClose( p ); 119 | p = json_objOpen( p, NULL ); 120 | p = json_objClose( p ); 121 | p = json_arrClose( p ); 122 | p = json_objClose( p ); 123 | p = json_end( p ); 124 | static char const rslt[] = "{\"a\":[{},{}]}"; 125 | check( p - buff == sizeof rslt - 1 ); 126 | check( 0 == strcmp( buff, rslt ) ); 127 | } 128 | done(); 129 | } 130 | 131 | #ifndef LONG_LONG_MAX 132 | #define LONG_LONG_MAX LLONG_MAX 133 | #define LONG_LONG_MIN LLONG_MIN 134 | #endif 135 | 136 | static int primitive( void ) { 137 | char buff[512]; 138 | char* p = json_objOpen( buff, NULL ); 139 | p = json_verylong( p, "max", LONG_LONG_MAX ); 140 | p = json_verylong( p, "min", LONG_LONG_MIN ); 141 | p = json_bool( p, "boolvar0", 0 ); 142 | p = json_bool( p, "boolvar1", 1 ); 143 | p = json_null( p, "nullvar" ); 144 | p = json_objClose( p ); 145 | p = json_end( p ); 146 | static char const rslt[] = "{" 147 | "\"max\":9223372036854775807," 148 | "\"min\":-9223372036854775808," 149 | "\"boolvar0\":false," 150 | "\"boolvar1\":true," 151 | "\"nullvar\":null" 152 | "}"; 153 | check( p - buff == sizeof rslt - 1 ); 154 | check( 0 == strcmp( buff, rslt ) ); 155 | done(); 156 | } 157 | 158 | static int integers( void ) { 159 | { 160 | char buff[64]; 161 | char* p = json_objOpen( buff, NULL ); 162 | p = json_int( p, "a", 0 ); 163 | p = json_int( p, "b", 1 ); 164 | p = json_objClose( p ); 165 | p = json_end( p ); 166 | static char const rslt[] = "{\"a\":0,\"b\":1}"; 167 | check( p - buff == sizeof rslt - 1 ); 168 | check( 0 == strcmp( buff, rslt ) ); 169 | } 170 | { 171 | char buff[64]; 172 | char* p = json_objOpen( buff, NULL ); 173 | p = json_int( p, "max", INT_MAX ); 174 | p = json_int( p, "min", INT_MIN ); 175 | p = json_objClose( p ); 176 | p = json_end( p ); 177 | char rslt[ sizeof buff ]; 178 | int len = sprintf( rslt, "{\"max\":%d,\"min\":%d}", INT_MAX, INT_MIN ); 179 | check( len < sizeof buff ); 180 | check( p - buff == len ); 181 | check( 0 == strcmp( buff, rslt ) ); 182 | } 183 | { 184 | char buff[64]; 185 | char* p = json_objOpen( buff, NULL ); 186 | p = json_uint( p, "max", UINT_MAX ); 187 | p = json_objClose( p ); 188 | p = json_end( p ); 189 | char rslt[ sizeof buff ]; 190 | int len = sprintf( rslt, "{\"max\":%u}", UINT_MAX ); 191 | check( len < sizeof buff ); 192 | check( p - buff == len ); 193 | check( 0 == strcmp( buff, rslt ) ); 194 | } 195 | { 196 | char buff[64]; 197 | char* p = json_objOpen( buff, NULL ); 198 | p = json_long( p, "max", LONG_MAX ); 199 | p = json_long( p, "min", LONG_MIN ); 200 | p = json_objClose( p ); 201 | p = json_end( p ); 202 | char rslt[ sizeof buff ]; 203 | int len = sprintf( rslt, "{\"max\":%ld,\"min\":%ld}", LONG_MAX, LONG_MIN ); 204 | check( len < sizeof buff ); 205 | check( p - buff == len ); 206 | check( 0 == strcmp( buff, rslt ) ); 207 | } 208 | { 209 | char buff[64]; 210 | char* p = json_objOpen( buff, NULL ); 211 | p = json_ulong( p, "max", ULONG_MAX ); 212 | p = json_objClose( p ); 213 | p = json_end( p ); 214 | char rslt[ sizeof buff ]; 215 | int len = sprintf( rslt, "{\"max\":%lu}", ULONG_MAX ); 216 | check( len < sizeof buff ); 217 | check( p - buff == len ); 218 | check( 0 == strcmp( buff, rslt ) ); 219 | } 220 | { 221 | char buff[64]; 222 | char* p = json_objOpen( buff, NULL ); 223 | p = json_verylong( p, "max", LONG_LONG_MAX ); 224 | p = json_verylong( p, "min", LONG_LONG_MIN ); 225 | p = json_objClose( p ); 226 | p = json_end( p ); 227 | char rslt[ sizeof buff ]; 228 | int len = sprintf( rslt, "{\"max\":%lld,\"min\":%lld}", LONG_LONG_MAX, LONG_LONG_MIN ); 229 | check( len < sizeof buff ); 230 | check( p - buff == len ); 231 | check( 0 == strcmp( buff, rslt ) ); 232 | } 233 | done(); 234 | } 235 | 236 | static int array( void ) { 237 | char buff[64]; 238 | char* p = json_objOpen( buff, NULL ); 239 | p = json_arrOpen( p, "a" ); 240 | for( int i = 0; i < 4; ++i ) 241 | p = json_int( p, NULL, i ); 242 | p = json_arrClose( p ); 243 | p = json_objClose( p ); 244 | p = json_end( p ); 245 | static char const rslt[] = "{\"a\":[0,1,2,3]}"; 246 | check( p - buff == sizeof rslt - 1 ); 247 | check( 0 == strcmp( buff, rslt ) ); 248 | done(); 249 | } 250 | 251 | static int real( void ) { 252 | char buff[64]; 253 | char* p = json_objOpen( buff, NULL ); 254 | p = json_arrOpen( p, "data" ); 255 | static double const lut[] = { 0.2, 2e-6, 5e6 }; 256 | for( int i = 0; i < sizeof lut / sizeof *lut; ++i ) 257 | p = json_double( p, NULL, lut[i] ); 258 | p = json_arrClose( p ); 259 | p = json_objClose( p ); 260 | p = json_end( p ); 261 | #ifdef NO_SPRINTF 262 | static char const rslt1[] = "{\"data\":[0,0,5000000]}"; 263 | static char const rslt2[] = "{\"data\":[0,0,5000000]}"; 264 | #else 265 | static char const rslt1[] = "{\"data\":[0.2,2e-006,5e+006]}"; 266 | static char const rslt2[] = "{\"data\":[0.2,2e-06,5e+06]}"; 267 | #endif 268 | check( p - buff == sizeof rslt1 - 1 || p - buff == sizeof rslt2 - 1 ); 269 | check( 0 == strcmp( buff, rslt1 ) || 0 == strcmp( buff, rslt2 ) ); 270 | done(); 271 | } 272 | 273 | // --------------------------------------------------------- Execute tests: --- 274 | 275 | int main( void ) { 276 | static struct test const tests[] = { 277 | { escape, "Escape characters" }, 278 | { len, "Non-null-terminated" }, 279 | { empty, "Empty objects and arrays" }, 280 | { primitive, "Primitives values" }, 281 | { integers, "Integers values" }, 282 | { array, "Array" }, 283 | { real, "Real" } 284 | }; 285 | return test_suit( tests, sizeof tests / sizeof *tests ); 286 | } 287 | --------------------------------------------------------------------------------