Merge branch 'task/upstream_json' into 'master'

json: Use upstream json_generator and json_parser as submodules

See merge request app-frameworks/esp-rainmaker!184
This commit is contained in:
Piyush Shah
2020-10-23 16:26:09 +08:00
18 changed files with 39 additions and 2045 deletions

View File

@@ -7,6 +7,7 @@ variables:
MAKEFLAGS: "-j8 --no-keep-going"
IDF_PATH: "$CI_PROJECT_DIR/esp-idf"
APP_BUILD: "all"
GIT_SUBMODULE_STRATEGY: recursive
before_script:
# add gitlab ssh key

6
.gitmodules vendored Normal file
View File

@@ -0,0 +1,6 @@
[submodule "components/json_parser/upstream"]
path = components/json_parser/upstream
url = https://github.com/espressif/json_parser.git
[submodule "components/json_generator/upstream"]
path = components/json_generator/upstream
url = https://github.com/espressif/json_generator.git

View File

@@ -1,5 +1,10 @@
# Changes
## 16-Oct-2020 (json: Use upstream json_generator and json_parser as submodules)
To get these submodules, you will now have to execute `git submodule update --init --recursive` once.
For new clones, use `git clone --recursive https://github.com/espressif/esp-rainmaker.git`
## 16-Oct-2020 (app_wifi: Changes in SSID and PoP generation for Provisioning)
The PoP for Wi-Fi provisioning was being fetched from a random 8 character hex string stored in the fctry partition.

View File

@@ -18,6 +18,20 @@ The key features of ESP RainMaker are:
2. Zero configuration required on the Cloud.
3. Phone apps that dynamically render the UI as per the device information.
## Get ESP RainMaker
Please clone this repository using the below command:
```
git clone --recursive https://github.com/espressif/esp-rainmaker.git
```
> Note the --recursive option. This is required to pull in the JSON dependencies into esp-rainmaker. In case you have already cloned the repository without this option, execute this to pull in the submodules: `git submodule update --init --recursive`
Please check the ESP RainMaker documentation [here](https://rainmaker.espressif.com/docs/get-started.html) to get started.
Each example has its own README with additional information about using the example.
## Supported ESP-IDF versions
ESP RainMaker can work with ESP IDF 4.0 and above.
@@ -34,12 +48,6 @@ ESP RainMaker can work with ESP IDF 4.0 and above.
- [Apple App Store](https://apps.apple.com/app/esp-rainmaker/id1497491540)
- [Source Code](https://github.com/espressif/esp-rainmaker-ios)
## Documentation
Please check the ESP RainMaker documentation [here](http://rainmaker.espressif.com/docs/get-started.html) to get started.
Each example has its own README with additional information about using the example.
### API Documentation Build Status
[![Documentation Status](https://readthedocs.com/projects/espressif-esp-rainmaker/badge/?version=latest)](https://docs.espressif.com/projects/esp-rainmaker/en/latest/)

View File

@@ -1,5 +1,3 @@
idf_component_register(SRCS ./json_generator.c
INCLUDE_DIRS .
REQUIRES
PRIV_REQUIRES )
idf_component_register(SRCS "upstream/json_generator.c"
INCLUDE_DIRS "upstream"
)

View File

@@ -1,176 +0,0 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS

View File

@@ -1,2 +1,3 @@
COMPONENT_SRCDIRS := ./
COMPONENT_ADD_INCLUDEDIRS := ./
COMPONENT_OBJS := upstream/json_generator.o
COMPONENT_SRCDIRS := upstream
COMPONENT_ADD_INCLUDEDIRS := upstream

View File

@@ -1,294 +0,0 @@
/* Code taken from: https://github.com/shahpiyushv/json_gen_generator */
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <json_generator.h>
/* Logging management */
//#define JSON_GEN_DEBUG
#ifdef JSON_GEN_DEBUG
#define json_gen_gen_d(fmt, ...) printf("[json_gen_gen] " fmt, ##__VA_ARGS__);
#else
#define json_gen_gen_d(fmt, ...)
#endif
#define MAX_INT_IN_STR 24
#define MAX_FLOAT_IN_STR 30
static inline int json_gen_get_empty_len(json_gen_str_t *jstr)
{
return (jstr->buf_size - (jstr->free_ptr - jstr->buf) - 1);
}
/* This will add the incoming string to the JSON string buffer
* and flush it out if the buffer is full. Note that the data being
* flushed out will always be equal to the size of the buffer unless
* this is the last chunk being flushed out on json_gen_end_str()
*/
static int json_gen_add_to_str(json_gen_str_t *jstr, char *str)
{
if (!str) {
return 0;
}
int len = strlen(str);
char *cur_ptr = str;
while (1) {
int len_remaining = json_gen_get_empty_len(jstr);
int copy_len = len_remaining > len ? len : len_remaining;
memmove(jstr->free_ptr, cur_ptr, copy_len);
cur_ptr += copy_len;
jstr->free_ptr += copy_len;
len -= copy_len;
if (len) {
*jstr->free_ptr = '\0';
/* Report error if the buffer is full and no flush callback
* is registered
*/
if (!jstr->flush_cb) {
json_gen_gen_d("Cannot flush. End of string");
return -1;
}
jstr->flush_cb(jstr->buf, jstr->priv);
jstr->free_ptr = jstr->buf;
} else
break;
}
return 0;
}
void json_gen_str_start(json_gen_str_t *jstr, char *buf, int buf_size,
json_gen_flush_cb_t flush_cb, void *priv)
{
memset(jstr, 0, sizeof(json_gen_str_t));
jstr->buf = buf;
jstr->buf_size = buf_size;
jstr->flush_cb = flush_cb;
jstr->free_ptr = buf;
jstr->priv = priv;
}
void json_gen_str_end(json_gen_str_t *jstr)
{
*jstr->free_ptr = '\0';
if (jstr->flush_cb)
jstr->flush_cb(jstr->buf, jstr->priv);
memset(jstr, 0, sizeof(json_gen_str_t));
}
static inline void json_gen_handle_comma(json_gen_str_t *jstr)
{
if (jstr->comma_req)
json_gen_add_to_str(jstr, ",");
}
static int json_gen_handle_name(json_gen_str_t *jstr, char *name)
{
json_gen_add_to_str(jstr, "\"");
json_gen_add_to_str(jstr, name);
return json_gen_add_to_str(jstr, "\":");
}
int json_gen_start_object(json_gen_str_t *jstr)
{
json_gen_handle_comma(jstr);
jstr->comma_req = false;
return json_gen_add_to_str(jstr, "{");
}
int json_gen_end_object(json_gen_str_t *jstr)
{
jstr->comma_req = true;
return json_gen_add_to_str(jstr, "}");
}
int json_gen_start_array(json_gen_str_t *jstr)
{
json_gen_handle_comma(jstr);
jstr->comma_req = false;
return json_gen_add_to_str(jstr, "[");
}
int json_gen_end_array(json_gen_str_t *jstr)
{
jstr->comma_req = true;
return json_gen_add_to_str(jstr, "]");
}
int json_gen_push_object(json_gen_str_t *jstr, char *name)
{
json_gen_handle_comma(jstr);
json_gen_handle_name(jstr, name);
jstr->comma_req = false;
return json_gen_add_to_str(jstr, "{");
}
int json_gen_pop_object(json_gen_str_t *jstr)
{
jstr->comma_req = true;
return json_gen_add_to_str(jstr, "}");
}
int json_gen_push_object_str(json_gen_str_t *jstr, char *name, char *object_str)
{
json_gen_handle_comma(jstr);
json_gen_handle_name(jstr, name);
jstr->comma_req = true;
return json_gen_add_to_str(jstr, object_str);
}
int json_gen_push_array(json_gen_str_t *jstr, char *name)
{
json_gen_handle_comma(jstr);
json_gen_handle_name(jstr, name);
jstr->comma_req = false;
return json_gen_add_to_str(jstr, "[");
}
int json_gen_pop_array(json_gen_str_t *jstr)
{
jstr->comma_req = true;
return json_gen_add_to_str(jstr, "]");
}
int json_gen_push_array_str(json_gen_str_t *jstr, char *name, char *array_str)
{
json_gen_handle_comma(jstr);
json_gen_handle_name(jstr, name);
jstr->comma_req = true;
return json_gen_add_to_str(jstr, array_str);
}
static int json_gen_set_bool(json_gen_str_t *jstr, bool val)
{
jstr->comma_req = true;
if (val)
return json_gen_add_to_str(jstr, "true");
else
return json_gen_add_to_str(jstr, "false");
}
int json_gen_obj_set_bool(json_gen_str_t *jstr, char *name, bool val)
{
json_gen_handle_comma(jstr);
json_gen_handle_name(jstr, name);
return json_gen_set_bool(jstr, val);
}
int json_gen_arr_set_bool(json_gen_str_t *jstr, bool val)
{
json_gen_handle_comma(jstr);
return json_gen_set_bool(jstr, val);
}
static int json_gen_set_int(json_gen_str_t *jstr, int val)
{
jstr->comma_req = true;
char str[MAX_INT_IN_STR];
snprintf(str, MAX_INT_IN_STR, "%d", val);
return json_gen_add_to_str(jstr, str);
}
int json_gen_obj_set_int(json_gen_str_t *jstr, char *name, int val)
{
json_gen_handle_comma(jstr);
json_gen_handle_name(jstr, name);
return json_gen_set_int(jstr, val);
}
int json_gen_arr_set_int(json_gen_str_t *jstr, int val)
{
json_gen_handle_comma(jstr);
return json_gen_set_int(jstr, val);
}
static int json_gen_set_float(json_gen_str_t *jstr, float val)
{
jstr->comma_req = true;
char str[MAX_FLOAT_IN_STR];
snprintf(str, MAX_FLOAT_IN_STR, "%.*f", JSON_FLOAT_PRECISION, val);
return json_gen_add_to_str(jstr, str);
}
int json_gen_obj_set_float(json_gen_str_t *jstr, char *name, float val)
{
json_gen_handle_comma(jstr);
json_gen_handle_name(jstr, name);
return json_gen_set_float(jstr, val);
}
int json_gen_arr_set_float(json_gen_str_t *jstr, float val)
{
json_gen_handle_comma(jstr);
return json_gen_set_float(jstr, val);
}
static int json_gen_set_string(json_gen_str_t *jstr, char *val)
{
jstr->comma_req = true;
json_gen_add_to_str(jstr, "\"");
json_gen_add_to_str(jstr, val);
return json_gen_add_to_str(jstr, "\"");
}
int json_gen_obj_set_string(json_gen_str_t *jstr, char *name, char *val)
{
json_gen_handle_comma(jstr);
json_gen_handle_name(jstr, name);
return json_gen_set_string(jstr, val);
}
int json_gen_arr_set_string(json_gen_str_t *jstr, char *val)
{
json_gen_handle_comma(jstr);
return json_gen_set_string(jstr, val);
}
static int json_gen_set_long_string(json_gen_str_t *jstr, char *val)
{
jstr->comma_req = true;
json_gen_add_to_str(jstr, "\"");
return json_gen_add_to_str(jstr, val);
}
int json_gen_obj_start_long_string(json_gen_str_t *jstr, char *name, char *val)
{
json_gen_handle_comma(jstr);
json_gen_handle_name(jstr, name);
return json_gen_set_long_string(jstr, val);
}
int json_gen_arr_start_long_string(json_gen_str_t *jstr, char *val)
{
json_gen_handle_comma(jstr);
return json_gen_set_long_string(jstr, val);
}
int json_gen_add_to_long_string(json_gen_str_t *jstr, char *val)
{
return json_gen_add_to_str(jstr, val);
}
int json_gen_end_long_string(json_gen_str_t *jstr)
{
return json_gen_add_to_str(jstr, "\"");
}
static int json_gen_set_null(json_gen_str_t *jstr)
{
jstr->comma_req = true;
return json_gen_add_to_str(jstr, "null");
}
int json_gen_obj_set_null(json_gen_str_t *jstr, char *name)
{
json_gen_handle_comma(jstr);
json_gen_handle_name(jstr, name);
return json_gen_set_null(jstr);
}
int json_gen_arr_set_null(json_gen_str_t *jstr)
{
json_gen_handle_comma(jstr);
return json_gen_set_null(jstr);
}

View File

@@ -1,485 +0,0 @@
/** \file json_gen_generator.h
* \brief JSON String Generator
*
* This module can be used to create JSON strings with a facility
* to flush out data if the destination buffer is full. All commas
* and colons as required are automatically added by the APIs
*
* Code taken from: https://github.com/shahpiyushv/json_gen_generator
*/
#ifndef _JSON_GENERATOR_H
#define _JSON_GENERATOR_H
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C"
{
#endif
#define JSON_FLOAT_PRECISION 5
/** JSON string flush callback prototype
*
* This is a prototype of the function that needs to be passed to
* json_gen_str_start() and which will be invoked by the JSON generator
* module either when the buffer is full or json_gen_str_end() ins invoked.
*
* \param[in] buf Pointer to a NULL terminated JSON string
* \param[in] priv Private data to be passed to the flush callback. Will
* be the same as the one passed to json_gen_str_start()
*/
typedef void (*json_gen_flush_cb_t) (char *buf, void *priv);
/** JSON String structure
*
* Please do not set/modify any elements.
* Just define this structure and pass a pointer to it in the APIs below
*/
typedef struct {
char *buf;
int buf_size;
json_gen_flush_cb_t flush_cb;
void *priv;
bool comma_req;
char *free_ptr;
} json_gen_str_t;
/** Start a JSON String
*
* This is the first function to be called for creating a JSON string.
* It initializes the internal data structures. After the JSON string
* generation is over, the json_gen_str_end() function should be called.
*
* \param[out] jstr Pointer to an allocated \ref json_gen_str_t structure.
* This will be initialised internally and needs to be passed to all
* subsequent function calls
* \param[out] buf Pointer to an allocated buffer into which the JSON
* string will be written
* \param[in] buf_size Size of the buffer
* \param[in] Pointer to the flushing function of type \ref json_gen_flush_cb_t
* which will be invoked either when the buffer is full or when json_gen_str_end()
* is invoked. Can be left NULL.
* \param[in] priv Private data to be passed to the flushing function callback.
* Can be something like a session identifier (Eg. socket). Can be left NULL
*/
void json_gen_str_start(json_gen_str_t *jstr, char *buf, int buf_size,
json_gen_flush_cb_t flush_cb, void *priv);
/** End JSON string
*
* This should be the last function to be called after the entire JSON string
* has been generated.
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by
* json_gen_str_start()
*/
void json_gen_str_end(json_gen_str_t *jstr);
/** Start a JSON object
*
* This starts a JSON object by adding a '{'
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by
* json_gen_str_start()
*
* \return 0 on Success
* \return -1 if buffer is out of space (possible only if no callback function
* is passed to json_gen_str_start(). Else, buffer will be flushed out and new data
* added after that
*/
int json_gen_start_object(json_gen_str_t *jstr);
/** End a JSON object
*
* This ends a JSON object by adding a '}'
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by
* json_gen_str_start()
*
* \return 0 on Success
* \return -1 if buffer is out of space (possible only if no callback function
* is passed to json_gen_str_start(). Else, buffer will be flushed out and new data
* added after that
*/
int json_gen_end_object(json_gen_str_t *jstr);
/** Start a JSON array
*
* This starts a JSON object by adding a '['
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by
* json_gen_str_start()
*
* \return 0 on Success
* \return -1 if buffer is out of space (possible only if no callback function
* is passed to json_gen_str_start(). Else, buffer will be flushed out and new data
* added after that
*/
int json_gen_start_array(json_gen_str_t *jstr);
/** End a JSON object
*
* This ends a JSON object by adding a ']'
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by
* json_gen_str_start()
*
* \return 0 on Success
* \return -1 if buffer is out of space (possible only if no callback function
* is passed to json_gen_str_start(). Else, buffer will be flushed out and new data
* added after that
*/
int json_gen_end_array(json_gen_str_t *jstr);
/** Push a named JSON object
*
* This adds a JSON object like "name":{
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by
* json_gen_str_start()
* \param[in] name Name of the object
*
* \return 0 on Success
* \return -1 if buffer is out of space (possible only if no callback function
* is passed to json_gen_str_start(). Else, buffer will be flushed out and new data
* added after that
*/
int json_gen_push_object(json_gen_str_t *jstr, char *name);
/** Pop a named JSON object
*
* This ends a JSON object by adding a '}'. This is basically same as
* json_gen_end_object() but included so as to complement json_gen_push_object()
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by
* json_gen_str_start()
*
* \return 0 on Success
* \return -1 if buffer is out of space (possible only if no callback function
* is passed to json_gen_str_start(). Else, buffer will be flushed out and new data
* added after that
*/
int json_gen_pop_object(json_gen_str_t *jstr);
/** Push a JSON object string
*
* This adds a complete pre-formatted JSON object string to the JSON object.
*
* Eg. json_gen_push_object_str(jstr, "pre-formatted", "{\"a\":1,\"b\":2}");
* This will add "pre-formatted":{"a":1,"b":2}
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by
* json_gen_str_start()
* \param[in] name Name of the JSON object string
* \param[in] object_str The pre-formatted JSON object string
*
* \return 0 on Success
* \return -1 if buffer is out of space (possible only if no callback function
* is passed to json_gen_str_start(). Else, buffer will be flushed out and new data
* added after that.
*/
int json_gen_push_object_str(json_gen_str_t *jstr, char *name, char *object_str);
/** Push a named JSON array
*
* This adds a JSON array like "name":[
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by
* json_gen_str_start()
* \param[in] name Name of the array
*
* \return 0 on Success
* \return -1 if buffer is out of space (possible only if no callback function
* is passed to json_gen_str_start(). Else, buffer will be flushed out and new data
* added after that
*/
int json_gen_push_array(json_gen_str_t *jstr, char *name);
/** Pop a named JSON array
*
* This ends a JSON array by adding a ']'. This is basically same as
* json_gen_end_array() but included so as to complement json_gen_push_array()
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by
* json_gen_str_start()
*
* \return 0 on Success
* \return -1 if buffer is out of space (possible only if no callback function
* is passed to json_gen_str_start(). Else, buffer will be flushed out and new data
* added after that
*/
int json_gen_pop_array(json_gen_str_t *jstr);
/** Push a JSON array string
*
* This adds a complete pre-formatted JSON array string to the JSON object.
*
* Eg. json_gen_push_object_str(jstr, "pre-formatted", "[1,2,3]");
* This will add "pre-formatted":[1,2,3]
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by
* json_gen_str_start()
* \param[in] name Name of the JSON array string
* \param[in] array_str The pre-formatted JSON array string
*
* \return 0 on Success
* \return -1 if buffer is out of space (possible only if no callback function
* is passed to json_gen_str_start(). Else, buffer will be flushed out and new data
* added after that.
*/
int json_gen_push_array_str(json_gen_str_t *jstr, char *name, char *array_str);
/** Add a boolean element to an object
*
* This adds a boolean element to an object. Eg. "bool_val":true
*
* \note This must be called between json_gen_start_object()/json_gen_push_object()
* and json_gen_end_object()/json_gen_pop_object()
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by
* json_gen_str_start()
* \param[in] name Name of the element
* \param[in] val Boolean value of the element
*
* \return 0 on Success
* \return -1 if buffer is out of space (possible only if no callback function
* is passed to json_gen_str_start(). Else, buffer will be flushed out and new data
* added after that
*/
int json_gen_obj_set_bool(json_gen_str_t *jstr, char *name, bool val);
/** Add an integer element to an object
*
* This adds an integer element to an object. Eg. "int_val":28
*
* \note This must be called between json_gen_start_object()/json_gen_push_object()
* and json_gen_end_object()/json_gen_pop_object()
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by
* json_gen_str_start()
* \param[in] name Name of the element
* \param[in] val Integer value of the element
*
* \return 0 on Success
* \return -1 if buffer is out of space (possible only if no callback function
* is passed to json_gen_str_start(). Else, buffer will be flushed out and new data
* added after that
*/
int json_gen_obj_set_int(json_gen_str_t *jstr, char *name, int val);
/** Add a float element to an object
*
* This adds a float element to an object. Eg. "float_val":23.8
*
* \note This must be called between json_gen_start_object()/json_gen_push_object()
* and json_gen_end_object()/json_gen_pop_object()
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by
* json_gen_str_start()
* \param[in] name Name of the element
* \param[in] val Float value of the element
*
* \return 0 on Success
* \return -1 if buffer is out of space (possible only if no callback function
* is passed to json_gen_str_start(). Else, buffer will be flushed out and new data
* added after that
*/
int json_gen_obj_set_float(json_gen_str_t *jstr, char *name, float val);
/** Add a string element to an object
*
* This adds a string element to an object. Eg. "string_val":"my_string"
*
* \note This must be called between json_gen_start_object()/json_gen_push_object()
* and json_gen_end_object()/json_gen_pop_object()
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by
* json_gen_str_start()
* \param[in] name Name of the element
* \param[in] val Null terminated string value of the element
*
* \return 0 on Success
* \return -1 if buffer is out of space (possible only if no callback function
* is passed to json_gen_str_start(). Else, buffer will be flushed out and new data
* added after that
*/
int json_gen_obj_set_string(json_gen_str_t *jstr, char *name, char *val);
/** Add a NULL element to an object
*
* This adds a NULL element to an object. Eg. "null_val":null
*
* \note This must be called between json_gen_start_object()/json_gen_push_object()
* and json_gen_end_object()/json_gen_pop_object()
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by
* json_gen_str_start()
* \param[in] name Name of the element
*
* \return 0 on Success
* \return -1 if buffer is out of space (possible only if no callback function
* is passed to json_gen_str_start(). Else, buffer will be flushed out and new data
* added after that
*/
int json_gen_obj_set_null(json_gen_str_t *jstr, char *name);
/** Add a boolean element to an array
*
* \note This must be called between json_gen_start_array()/json_gen_push_array()
* and json_gen_end_array()/json_gen_pop_array()
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by
* json_gen_str_start()
* \param[in] val Boolean value of the element
*
* \return 0 on Success
* \return -1 if buffer is out of space (possible only if no callback function
* is passed to json_gen_str_start(). Else, buffer will be flushed out and new data
* added after that
*/
int json_gen_arr_set_bool(json_gen_str_t *jstr, bool val);
/** Add an integer element to an array
*
* \note This must be called between json_gen_start_array()/json_gen_push_array()
* and json_gen_end_array()/json_gen_pop_array()
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by
* json_gen_str_start()
* \param[in] val Integer value of the element
*
* \return 0 on Success
* \return -1 if buffer is out of space (possible only if no callback function
* is passed to json_gen_str_start(). Else, buffer will be flushed out and new data
* added after that
*/
int json_gen_arr_set_int(json_gen_str_t *jstr, int val);
/** Add a float element to an array
*
* \note This must be called between json_gen_start_array()/json_gen_push_array()
* and json_gen_end_array()/json_gen_pop_array()
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by
* json_gen_str_start()
* \param[in] val Float value of the element
*
* \return 0 on Success
* \return -1 if buffer is out of space (possible only if no callback function
* is passed to json_gen_str_start(). Else, buffer will be flushed out and new data
* added after that
*/
int json_gen_arr_set_float(json_gen_str_t *jstr, float val);
/** Add a string element to an array
*
* \note This must be called between json_gen_start_array()/json_gen_push_array()
* and json_gen_end_array()/json_gen_pop_array()
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by
* json_gen_str_start()
* \param[in] val Null terminated string value of the element
*
* \return 0 on Success
* \return -1 if buffer is out of space (possible only if no callback function
* is passed to json_gen_str_start(). Else, buffer will be flushed out and new data
* added after that
*/
int json_gen_arr_set_string(json_gen_str_t *jstr, char *val);
/** Add a NULL element to an array
*
* \note This must be called between json_gen_start_array()/json_gen_push_array()
* and json_gen_end_array()/json_gen_pop_array()
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by
* json_gen_str_start()
*
* \return 0 on Success
* \return -1 if buffer is out of space (possible only if no callback function
* is passed to json_gen_str_start(). Else, buffer will be flushed out and new data
* added after that
*/
int json_gen_arr_set_null(json_gen_str_t *jstr);
/** Start a Long string in an object
*
* This starts a string in an object, but does not end it (i.e., does not add the
* terminating quotes. This is useful for long strings. Eg. "string_val":"my_string.
* The API json_gen_add_to_long_string() must be used to add to this string and the API
* json_gen_end_long_string() must be used to terminate it (i.e. add the ending quotes).
*
* \note This must be called between json_gen_start_object()/json_gen_push_object()
* and json_gen_end_object()/json_gen_pop_object()
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by
* json_gen_str_start()
* \param[in] name Name of the element
* \param[in] val Null terminated initial part of the string value. It can also be NULL
*
* \return 0 on Success
* \return -1 if buffer is out of space (possible only if no callback function
* is passed to json_gen_str_start(). Else, buffer will be flushed out and new data
* added after that
*/
int json_gen_obj_start_long_string(json_gen_str_t *jstr, char *name, char *val);
/** Start a Long string in an array
*
* This starts a string in an arrayt, but does not end it (i.e., does not add the
* terminating quotes. This is useful for long strings.
* The API json_gen_add_to_long_string() must be used to add to this string and the API
* json_gen_end_long_string() must be used to terminate it (i.e. add the ending quotes).
*
* \note This must be called between json_gen_start_array()/json_gen_push_array()
* and json_gen_end_array()/json_gen_pop_array()
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by
* json_gen_str_start()
* \param[in] val Null terminated initial part of the string value. It can also be NULL
*
* \return 0 on Success
* \return -1 if buffer is out of space (possible only if no callback function
* is passed to json_gen_str_start(). Else, buffer will be flushed out and new data
* added after that
*/
int json_gen_arr_start_long_string(json_gen_str_t *jstr, char *val);
/** Add to a JSON Long string
*
* This extends the string initialised by json_gen_obj_start_long_string() or
* json_gen_arr_start_long_string(). After the entire string is created, it should be terminated
* with json_gen_end_long_string().
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by json_gen_str_start()
* \param[in] val Null terminated extending part of the string value.
*
* \return 0 on Success
* \return -1 if buffer is out of space (possible only if no callback function
* is passed to json_gen_str_start(). Else, buffer will be flushed out and new data
* added after that
*/
int json_gen_add_to_long_string(json_gen_str_t *jstr, char *val);
/** End a JSON Long string
*
* This ends the string initialised by json_gen_obj_start_long_string() or
* json_gen_arr_start_long_string() by adding the ending quotes.
*
* \param[in] jstr Pointer to the \ref json_gen_str_t structure initilised by json_gen_str_start()
*
*
* \return 0 on Success
* \return -1 if buffer is out of space (possible only if no callback function
* is passed to json_gen_str_start(). Else, buffer will be flushed out and new data
* added after that
*/
int json_gen_end_long_string(json_gen_str_t *jstr);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -1,5 +1,3 @@
idf_component_register(SRCS ./json_parser.c ./jsmn/src/jsmn-changed.c
INCLUDE_DIRS ./ ./jsmn/include
REQUIRES
PRIV_REQUIRES )
idf_component_register(SRCS "upstream/src/json_parser.c"
INCLUDE_DIRS "upstream/include" "upstream"
)

View File

@@ -1,176 +0,0 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS

View File

@@ -1,2 +1,2 @@
COMPONENT_SRCDIRS := ./ ./jsmn/src
COMPONENT_ADD_INCLUDEDIRS := ./ ./jsmn/include
COMPONENT_SRCDIRS := upstream/src
COMPONENT_ADD_INCLUDEDIRS := upstream/include upstream

View File

@@ -1,110 +0,0 @@
/*
* Copyright (c) 2010 Serge A. Zaitsev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* @file jsmn.h
* @brief Definition of the JSMN (Jasmine) JSON parser.
*
* For more information on JSMN:
* @see http://zserge.com/jsmn.html
*/
#ifndef __JSMN_CHANGED_H_
#define __JSMN_CHANGED_H_
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
#define JSMN_PARENT_LINKS
#define JSMN_STRICT
/**
* JSON type identifier. Basic types are:
* o Object
* o Array
* o String
* o Other primitive: number, boolean (true/false) or null
*/
typedef enum {
JSMN_UNDEFINED = 0,
JSMN_OBJECT = 1,
JSMN_ARRAY = 2,
JSMN_STRING = 3,
JSMN_PRIMITIVE = 4
} _jsmntype_t;
enum jsmnerr {
/* Not enough tokens were provided */
JSMN_ERROR_NOMEM = -1,
/* Invalid character inside JSON string */
JSMN_ERROR_INVAL = -2,
/* The string is not a full JSON packet, more bytes expected */
JSMN_ERROR_PART = -3
};
/**
* JSON token description.
* @param type type (object, array, string etc.)
* @param start start position in JSON data string
* @param end end position in JSON data string
*/
typedef struct {
_jsmntype_t type;
int start;
int end;
int size;
#ifdef JSMN_PARENT_LINKS
int parent;
#endif
} _jsmntok_t;
/**
* JSON parser. Contains an array of token blocks available. Also stores
* the string being parsed now and current position in that string
*/
typedef struct {
unsigned int pos; /* offset in the JSON string */
unsigned int toknext; /* next token to allocate */
int toksuper; /* superior token node, e.g parent object or array */
} _jsmn_parser;
/**
* Create JSON parser over an array of tokens
*/
void __jsmn_init(_jsmn_parser *parser);
/**
* Run JSON parser. It parses a JSON data string into and array of tokens, each describing
* a single JSON object.
*/
int __jsmn_parse(_jsmn_parser *parser, const char *js, size_t len,
_jsmntok_t *tokens, unsigned int num_tokens);
#ifdef __cplusplus
}
#endif
#endif /* __JSMN_CHANGED_H_ */

View File

@@ -1,341 +0,0 @@
/*
* Copyright (c) 2010 Serge A. Zaitsev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* @file jsmn.c
* @brief Implementation of the JSMN (Jasmine) JSON parser.
*
* For more information on JSMN:
* @see http://zserge.com/jsmn.html
*/
#include "jsmn-changed.h"
/**
* Allocates a fresh unused token from the token pull.
*/
static _jsmntok_t *jsmn_alloc_token(_jsmn_parser *parser,
_jsmntok_t *tokens, size_t num_tokens) {
_jsmntok_t *tok;
if (parser->toknext >= num_tokens) {
return NULL;
}
tok = &tokens[parser->toknext++];
tok->start = tok->end = -1;
tok->size = 0;
#ifdef JSMN_PARENT_LINKS
tok->parent = -1;
#endif
return tok;
}
/**
* Fills token type and boundaries.
*/
static void jsmn_fill_token(_jsmntok_t *token, _jsmntype_t type,
int start, int end) {
token->type = type;
token->start = start;
token->end = end;
token->size = 0;
}
/**
* Fills next available token with JSON primitive.
*/
static int jsmn_parse_primitive(_jsmn_parser *parser, const char *js,
size_t len, _jsmntok_t *tokens, size_t num_tokens) {
_jsmntok_t *token;
int start;
start = parser->pos;
for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
switch (js[parser->pos]) {
#ifndef JSMN_STRICT
/* In strict mode primitive must be followed by "," or "}" or "]" */
case ':':
#endif
case '\t' : case '\r' : case '\n' : case ' ' :
case ',' : case ']' : case '}' :
goto found;
}
if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
parser->pos = start;
return JSMN_ERROR_INVAL;
}
}
#ifdef JSMN_STRICT
/* In strict mode primitive must be followed by a comma/object/array */
parser->pos = start;
return JSMN_ERROR_PART;
#endif
found:
if (tokens == NULL) {
parser->pos--;
return 0;
}
token = jsmn_alloc_token(parser, tokens, num_tokens);
if (token == NULL) {
parser->pos = start;
return JSMN_ERROR_NOMEM;
}
jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
#ifdef JSMN_PARENT_LINKS
token->parent = parser->toksuper;
#endif
parser->pos--;
return 0;
}
/**
* Fills next token with JSON string.
*/
static int jsmn_parse_string(_jsmn_parser *parser, const char *js,
size_t len, _jsmntok_t *tokens, size_t num_tokens) {
_jsmntok_t *token;
int start = parser->pos;
parser->pos++;
/* Skip starting quote */
for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
char c = js[parser->pos];
/* Quote: end of string */
if (c == '\"') {
if (tokens == NULL) {
return 0;
}
token = jsmn_alloc_token(parser, tokens, num_tokens);
if (token == NULL) {
parser->pos = start;
return JSMN_ERROR_NOMEM;
}
jsmn_fill_token(token, JSMN_STRING, start+1, parser->pos);
#ifdef JSMN_PARENT_LINKS
token->parent = parser->toksuper;
#endif
return 0;
}
/* Backslash: Quoted symbol expected */
if (c == '\\' && parser->pos + 1 < len) {
int i;
parser->pos++;
switch (js[parser->pos]) {
/* Allowed escaped symbols */
case '\"': case '/' : case '\\' : case 'b' :
case 'f' : case 'r' : case 'n' : case 't' :
break;
/* Allows escaped symbol \uXXXX */
case 'u':
parser->pos++;
for(i = 0; i < 4 && parser->pos < len && js[parser->pos] != '\0'; i++) {
/* If it isn't a hex character we have an error */
if(!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */
(js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */
(js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */
parser->pos = start;
return JSMN_ERROR_INVAL;
}
parser->pos++;
}
parser->pos--;
break;
/* Unexpected symbol */
default:
parser->pos = start;
return JSMN_ERROR_INVAL;
}
}
}
parser->pos = start;
return JSMN_ERROR_PART;
}
/**
* Parse JSON string and fill tokens.
*/
int __jsmn_parse(_jsmn_parser *parser, const char *js, size_t len,
_jsmntok_t *tokens, unsigned int num_tokens) {
int r;
int i;
_jsmntok_t *token;
int count = parser->toknext;
for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
char c;
_jsmntype_t type;
c = js[parser->pos];
switch (c) {
case '{': case '[':
count++;
if (tokens == NULL) {
break;
}
token = jsmn_alloc_token(parser, tokens, num_tokens);
if (token == NULL)
return JSMN_ERROR_NOMEM;
if (parser->toksuper != -1) {
tokens[parser->toksuper].size++;
#ifdef JSMN_PARENT_LINKS
token->parent = parser->toksuper;
#endif
}
token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
token->start = parser->pos;
parser->toksuper = parser->toknext - 1;
break;
case '}': case ']':
if (tokens == NULL)
break;
type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
#ifdef JSMN_PARENT_LINKS
if (parser->toknext < 1) {
return JSMN_ERROR_INVAL;
}
token = &tokens[parser->toknext - 1];
for (;;) {
if (token->start != -1 && token->end == -1) {
if (token->type != type) {
return JSMN_ERROR_INVAL;
}
token->end = parser->pos + 1;
parser->toksuper = token->parent;
break;
}
if (token->parent == -1) {
break;
}
token = &tokens[token->parent];
}
#else
for (i = parser->toknext - 1; i >= 0; i--) {
token = &tokens[i];
if (token->start != -1 && token->end == -1) {
if (token->type != type) {
return JSMN_ERROR_INVAL;
}
parser->toksuper = -1;
token->end = parser->pos + 1;
break;
}
}
/* Error if unmatched closing bracket */
if (i == -1) return JSMN_ERROR_INVAL;
for (; i >= 0; i--) {
token = &tokens[i];
if (token->start != -1 && token->end == -1) {
parser->toksuper = i;
break;
}
}
#endif
break;
case '\"':
r = jsmn_parse_string(parser, js, len, tokens, num_tokens);
if (r < 0) return r;
count++;
if (parser->toksuper != -1 && tokens != NULL)
tokens[parser->toksuper].size++;
break;
case '\t' : case '\r' : case '\n' : case ' ':
break;
case ':':
parser->toksuper = parser->toknext - 1;
break;
case ',':
if (tokens != NULL && parser->toksuper != -1 &&
tokens[parser->toksuper].type != JSMN_ARRAY &&
tokens[parser->toksuper].type != JSMN_OBJECT) {
#ifdef JSMN_PARENT_LINKS
parser->toksuper = tokens[parser->toksuper].parent;
#else
for (i = parser->toknext - 1; i >= 0; i--) {
if (tokens[i].type == JSMN_ARRAY || tokens[i].type == JSMN_OBJECT) {
if (tokens[i].start != -1 && tokens[i].end == -1) {
parser->toksuper = i;
break;
}
}
}
#endif
}
break;
#ifdef JSMN_STRICT
/* In strict mode primitives are: numbers and booleans */
case '-': case '0': case '1' : case '2': case '3' : case '4':
case '5': case '6': case '7' : case '8': case '9':
case 't': case 'f': case 'n' :
/* And they must not be keys of the object */
if (tokens != NULL && parser->toksuper != -1) {
_jsmntok_t *t = &tokens[parser->toksuper];
if (t->type == JSMN_OBJECT ||
(t->type == JSMN_STRING && t->size != 0)) {
return JSMN_ERROR_INVAL;
}
}
#else
/* In non-strict mode every unquoted value is a primitive */
default:
#endif
r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens);
if (r < 0) return r;
count++;
if (parser->toksuper != -1 && tokens != NULL)
tokens[parser->toksuper].size++;
break;
#ifdef JSMN_STRICT
/* Unexpected char in strict mode */
default:
return JSMN_ERROR_INVAL;
#endif
}
}
if (tokens != NULL) {
for (i = parser->toknext - 1; i >= 0; i--) {
/* Unmatched opened object or array */
if (tokens[i].start != -1 && tokens[i].end == -1) {
return JSMN_ERROR_PART;
}
}
}
return count;
}
/**
* Creates a new parser based over a given buffer with an array of tokens
* available.
*/
void __jsmn_init(_jsmn_parser *parser) {
parser->pos = 0;
parser->toknext = 0;
parser->toksuper = -1;
}

View File

@@ -1,382 +0,0 @@
/* Code taken from: https://github.com/shahpiyushv/json_parser */
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <jsmn-changed.h>
#include <json_parser.h>
static bool token_matches_str(jparse_ctx_t *ctx, json_tok_t *tok, char *str)
{
char *js = ctx->js;
return ((strncmp(js + tok->start, str, strlen(str)) == 0)
&& (strlen(str) == (size_t) (tok->end - tok->start)));
}
static json_tok_t *json_skip_elem(json_tok_t *token)
{
json_tok_t *cur = token;
int cnt = cur->size;
while (cnt--) {
cur++;
cur = json_skip_elem(cur);
}
return cur;
}
static int json_tok_to_bool(jparse_ctx_t *jctx, json_tok_t *tok, bool *val)
{
if (token_matches_str(jctx, tok, "true") || token_matches_str(jctx, tok, "1")) {
*val = true;
} else if (token_matches_str(jctx, tok, "false") || token_matches_str(jctx, tok, "0")) {
*val = false;
} else
return -OS_FAIL;
return OS_SUCCESS;
}
static int json_tok_to_int(jparse_ctx_t *jctx, json_tok_t *tok, int *val)
{
char *tok_start = &jctx->js[tok->start];
char *tok_end = &jctx->js[tok->end];
char *endptr;
int i = strtoul(tok_start, &endptr, 10);
if (endptr == tok_end) {
*val = i;
return OS_SUCCESS;
}
return -OS_FAIL;
}
static int json_tok_to_int64(jparse_ctx_t *jctx, json_tok_t *tok, int64_t *val)
{
char *tok_start = &jctx->js[tok->start];
char *tok_end = &jctx->js[tok->end];
char *endptr;
int64_t i64 = strtoull(tok_start, &endptr, 10);
if (endptr == tok_end) {
*val = i64;
return OS_SUCCESS;
}
return -OS_FAIL;
}
static int json_tok_to_float(jparse_ctx_t *jctx, json_tok_t *tok, float *val)
{
char *tok_start = &jctx->js[tok->start];
char *tok_end = &jctx->js[tok->end];
char *endptr;
float f = strtof(tok_start, &endptr);
if (endptr == tok_end) {
*val = f;
return OS_SUCCESS;
}
return -OS_FAIL;
}
static int json_tok_to_string(jparse_ctx_t *jctx, json_tok_t *tok, char *val, int size)
{
if ((tok->end - tok->start) > (size - 1))
return -OS_FAIL;
strncpy(val, jctx->js + tok->start, tok->end - tok->start);
val[tok->end - tok->start] = 0;
return OS_SUCCESS;
}
static json_tok_t *json_obj_search(jparse_ctx_t *jctx, char *key)
{
json_tok_t *tok = jctx->cur;
int size = tok->size;
if (size <= 0)
return NULL;
if (tok->type != JSMN_OBJECT)
return NULL;
while (size--) {
tok++;
if (token_matches_str(jctx, tok, key))
return tok;
tok = json_skip_elem(tok);
}
return NULL;
}
static json_tok_t *json_obj_get_val_tok(jparse_ctx_t *jctx, char *name, _jsmntype_t type)
{
json_tok_t *tok = json_obj_search(jctx, name);
if (!tok)
return NULL;
tok++;
if (tok->type != type)
return NULL;
return tok;
}
int json_obj_get_array(jparse_ctx_t *jctx, char *name, int *num_elem)
{
json_tok_t *tok = json_obj_get_val_tok(jctx, name, JSMN_ARRAY);
if (!tok)
return -OS_FAIL;
jctx->cur = tok;
*num_elem = tok->size;
return OS_SUCCESS;
}
int json_obj_leave_array(jparse_ctx_t *jctx)
{
/* The array's parent will be the key */
if (jctx->cur->parent < 0)
return -OS_FAIL;
jctx->cur = &jctx->tokens[jctx->cur->parent];
/* The key's parent will be the actual parent object */
if (jctx->cur->parent < 0)
return -OS_FAIL;
jctx->cur = &jctx->tokens[jctx->cur->parent];
return OS_SUCCESS;
}
int json_obj_get_object(jparse_ctx_t *jctx, char *name)
{
json_tok_t *tok = json_obj_get_val_tok(jctx, name, JSMN_OBJECT);
if (!tok)
return -OS_FAIL;
jctx->cur = tok;
return OS_SUCCESS;
}
int json_obj_leave_object(jparse_ctx_t *jctx)
{
/* The objects's parent will be the key */
if (jctx->cur->parent < 0)
return -OS_FAIL;
jctx->cur = &jctx->tokens[jctx->cur->parent];
/* The key's parent will be the actual parent object */
if (jctx->cur->parent < 0)
return -OS_FAIL;
jctx->cur = &jctx->tokens[jctx->cur->parent];
return OS_SUCCESS;
}
int json_obj_get_bool(jparse_ctx_t *jctx, char *name, bool *val)
{
json_tok_t *tok = json_obj_get_val_tok(jctx, name, JSMN_PRIMITIVE);
if (!tok)
return -OS_FAIL;
return json_tok_to_bool(jctx, tok, val);
}
int json_obj_get_int(jparse_ctx_t *jctx, char *name, int *val)
{
json_tok_t *tok = json_obj_get_val_tok(jctx, name, JSMN_PRIMITIVE);
if (!tok)
return -OS_FAIL;
return json_tok_to_int(jctx, tok, val);
}
int json_obj_get_int64(jparse_ctx_t *jctx, char *name, int64_t *val)
{
json_tok_t *tok = json_obj_get_val_tok(jctx, name, JSMN_PRIMITIVE);
if (!tok)
return -OS_FAIL;
return json_tok_to_int64(jctx, tok, val);
}
int json_obj_get_float(jparse_ctx_t *jctx, char *name, float *val)
{
json_tok_t *tok = json_obj_get_val_tok(jctx, name, JSMN_PRIMITIVE);
if (!tok)
return -OS_FAIL;
return json_tok_to_float(jctx, tok, val);
}
int json_obj_get_string(jparse_ctx_t *jctx, char *name, char *val, int size)
{
json_tok_t *tok = json_obj_get_val_tok(jctx, name, JSMN_STRING);
if (!tok)
return -OS_FAIL;
return json_tok_to_string(jctx, tok, val, size);
}
int json_obj_get_strlen(jparse_ctx_t *jctx, char *name, int *strlen)
{
json_tok_t *tok = json_obj_get_val_tok(jctx, name, JSMN_STRING);
if (!tok)
return -OS_FAIL;
*strlen = tok->end - tok->start;
return OS_SUCCESS;
}
int json_obj_get_object_str(jparse_ctx_t *jctx, char *name, char *val, int size)
{
json_tok_t *tok = json_obj_get_val_tok(jctx, name, JSMN_OBJECT);
if (!tok)
return -OS_FAIL;
return json_tok_to_string(jctx, tok, val, size);
}
int json_obj_get_object_strlen(jparse_ctx_t *jctx, char *name, int *strlen)
{
json_tok_t *tok = json_obj_get_val_tok(jctx, name, JSMN_OBJECT);
if (!tok)
return -OS_FAIL;
*strlen = tok->end - tok->start;
return OS_SUCCESS;
}
int json_obj_get_array_str(jparse_ctx_t *jctx, char *name, char *val, int size)
{
json_tok_t *tok = json_obj_get_val_tok(jctx, name, JSMN_ARRAY);
if (!tok)
return -OS_FAIL;
return json_tok_to_string(jctx, tok, val, size);
}
int json_obj_get_array_strlen(jparse_ctx_t *jctx, char *name, int *strlen)
{
json_tok_t *tok = json_obj_get_val_tok(jctx, name, JSMN_ARRAY);
if (!tok)
return -OS_FAIL;
*strlen = tok->end - tok->start;
return OS_SUCCESS;
}
static json_tok_t *json_arr_search(jparse_ctx_t *ctx, uint32_t index)
{
json_tok_t *tok = ctx->cur;
if ((tok->type != JSMN_ARRAY) || (tok->size <= 0))
return NULL;
if (index > (uint32_t)(tok->size - 1))
return NULL;
/* Increment by 1, so that token points to index 0 */
tok++;
while (index--) {
tok = json_skip_elem(tok);
tok++;
}
return tok;
}
static json_tok_t *json_arr_get_val_tok(jparse_ctx_t *jctx, uint32_t index, _jsmntype_t type)
{
json_tok_t *tok = json_arr_search(jctx, index);
if (!tok)
return NULL;
if (tok->type != type)
return NULL;
return tok;
}
int json_arr_get_array(jparse_ctx_t *jctx, uint32_t index)
{
json_tok_t *tok = json_arr_get_val_tok(jctx, index, JSMN_ARRAY);
if (!tok)
return -OS_FAIL;
jctx->cur = tok;
return OS_SUCCESS;
}
int json_arr_leave_array(jparse_ctx_t *jctx)
{
if (jctx->cur->parent < 0)
return -OS_FAIL;
jctx->cur = &jctx->tokens[jctx->cur->parent];
return OS_SUCCESS;
}
int json_arr_get_object(jparse_ctx_t *jctx, uint32_t index)
{
json_tok_t *tok = json_arr_get_val_tok(jctx, index, JSMN_OBJECT);
if (!tok)
return -OS_FAIL;
jctx->cur = tok;
return OS_SUCCESS;
}
int json_arr_leave_object(jparse_ctx_t *jctx)
{
if (jctx->cur->parent < 0)
return -OS_FAIL;
jctx->cur = &jctx->tokens[jctx->cur->parent];
return OS_SUCCESS;
}
int json_arr_get_bool(jparse_ctx_t *jctx, uint32_t index, bool *val)
{
json_tok_t *tok = json_arr_get_val_tok(jctx, index, JSMN_PRIMITIVE);
if (!tok)
return -OS_FAIL;
return json_tok_to_bool(jctx, tok, val);
}
int json_arr_get_int(jparse_ctx_t *jctx, uint32_t index, int *val)
{
json_tok_t *tok = json_arr_get_val_tok(jctx, index, JSMN_PRIMITIVE);
if (!tok)
return -OS_FAIL;
return json_tok_to_int(jctx, tok, val);
}
int json_arr_get_int64(jparse_ctx_t *jctx, uint32_t index, int64_t *val)
{
json_tok_t *tok = json_arr_get_val_tok(jctx, index, JSMN_PRIMITIVE);
if (!tok)
return -OS_FAIL;
return json_tok_to_int64(jctx, tok, val);
}
int json_arr_get_float(jparse_ctx_t *jctx, uint32_t index, float *val)
{
json_tok_t *tok = json_arr_get_val_tok(jctx, index, JSMN_PRIMITIVE);
if (!tok)
return -OS_FAIL;
return json_tok_to_float(jctx, tok, val);
}
int json_arr_get_string(jparse_ctx_t *jctx, uint32_t index, char *val, int size)
{
json_tok_t *tok = json_arr_get_val_tok(jctx, index, JSMN_STRING);
if (!tok)
return -OS_FAIL;
return json_tok_to_string(jctx, tok, val, size);
}
int json_arr_get_strlen(jparse_ctx_t *jctx, uint32_t index, int *strlen)
{
json_tok_t *tok = json_arr_get_val_tok(jctx, index, JSMN_STRING);
if (!tok)
return -OS_FAIL;
*strlen = tok->end - tok->start;
return OS_SUCCESS;
}
int json_parse_start(jparse_ctx_t *jctx, char *js, int len)
{
memset(jctx, 0, sizeof(jparse_ctx_t));
__jsmn_init(&jctx->parser);
int num_tokens = __jsmn_parse(&jctx->parser, js, len, NULL, 0);
if (num_tokens <= 0)
return -OS_FAIL;
jctx->num_tokens = num_tokens;
jctx->tokens = calloc(num_tokens, sizeof(json_tok_t));
if (!jctx->tokens)
return -OS_FAIL;
jctx->js = js;
__jsmn_init(&jctx->parser);
int ret = __jsmn_parse(&jctx->parser, js, len, jctx->tokens, jctx->num_tokens);
if (ret <= 0) {
free(jctx->tokens);
memset(jctx, 0, sizeof(jparse_ctx_t));
return -OS_FAIL;
}
jctx->cur = jctx->tokens;
return OS_SUCCESS;
}
int json_parse_end(jparse_ctx_t *jctx)
{
if (jctx->tokens)
free(jctx->tokens);
memset(jctx, 0, sizeof(jparse_ctx_t));
return OS_SUCCESS;
}

View File

@@ -1,61 +0,0 @@
/* Code taken from: https://github.com/shahpiyushv/json_parser */
#ifndef _JSON_PARSER_H_
#define _JSON_PARSER_H_
#include <jsmn-changed.h>
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C"
{
#endif
#define OS_SUCCESS 0
#define OS_FAIL -1
typedef _jsmn_parser json_parser_t;
typedef _jsmntok_t json_tok_t;
typedef struct {
json_parser_t parser;
char *js;
json_tok_t *tokens;
json_tok_t *cur;
int num_tokens;
} jparse_ctx_t;
int json_parse_start(jparse_ctx_t *jctx, char *js, int len);
int json_parse_end(jparse_ctx_t *jctx);
int json_obj_get_array(jparse_ctx_t *jctx, char *name, int *num_elem);
int json_obj_leave_array(jparse_ctx_t *jctx);
int json_obj_get_object(jparse_ctx_t *jctx, char *name);
int json_obj_leave_object(jparse_ctx_t *jctx);
int json_obj_get_bool(jparse_ctx_t *jctx, char *name, bool *val);
int json_obj_get_int(jparse_ctx_t *jctx, char *name, int *val);
int json_obj_get_int64(jparse_ctx_t *jctx, char *name, int64_t *val);
int json_obj_get_float(jparse_ctx_t *jctx, char *name, float *val);
int json_obj_get_string(jparse_ctx_t *jctx, char *name, char *val, int size);
int json_obj_get_strlen(jparse_ctx_t *jctx, char *name, int *strlen);
int json_obj_get_object_str(jparse_ctx_t *jctx, char *name, char *val, int size);
int json_obj_get_object_strlen(jparse_ctx_t *jctx, char *name, int *strlen);
int json_obj_get_array_str(jparse_ctx_t *jctx, char *name, char *val, int size);
int json_obj_get_array_strlen(jparse_ctx_t *jctx, char *name, int *strlen);
int json_arr_get_array(jparse_ctx_t *jctx, uint32_t index);
int json_arr_leave_array(jparse_ctx_t *jctx);
int json_arr_get_object(jparse_ctx_t *jctx, uint32_t index);
int json_arr_leave_object(jparse_ctx_t *jctx);
int json_arr_get_bool(jparse_ctx_t *jctx, uint32_t index, bool *val);
int json_arr_get_int(jparse_ctx_t *jctx, uint32_t index, int *val);
int json_arr_get_int64(jparse_ctx_t *jctx, uint32_t index, int64_t *val);
int json_arr_get_float(jparse_ctx_t *jctx, uint32_t index, float *val);
int json_arr_get_string(jparse_ctx_t *jctx, uint32_t index, char *val, int size);
int json_arr_get_strlen(jparse_ctx_t *jctx, uint32_t index, int *strlen);
#ifdef __cplusplus
}
#endif
#endif /* _JSON_PARSER_H_ */