Pierre-Marie Ancèle
/
AWS-test
test
aws-iot/src/aws_iot_json_utils.cpp@0:cd5404401c2f, 2017-04-12 (annotated)
- Committer:
- peyo
- Date:
- Wed Apr 12 14:07:09 2017 +0200
- Revision:
- 0:cd5404401c2f
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
peyo |
0:cd5404401c2f | 1 | /* |
peyo |
0:cd5404401c2f | 2 | * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
peyo |
0:cd5404401c2f | 3 | * |
peyo |
0:cd5404401c2f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"). |
peyo |
0:cd5404401c2f | 5 | * You may not use this file except in compliance with the License. |
peyo |
0:cd5404401c2f | 6 | * A copy of the License is located at |
peyo |
0:cd5404401c2f | 7 | * |
peyo |
0:cd5404401c2f | 8 | * http://aws.amazon.com/apache2.0 |
peyo |
0:cd5404401c2f | 9 | * |
peyo |
0:cd5404401c2f | 10 | * or in the "license" file accompanying this file. This file is distributed |
peyo |
0:cd5404401c2f | 11 | * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
peyo |
0:cd5404401c2f | 12 | * express or implied. See the License for the specific language governing |
peyo |
0:cd5404401c2f | 13 | * permissions and limitations under the License. |
peyo |
0:cd5404401c2f | 14 | */ |
peyo |
0:cd5404401c2f | 15 | |
peyo |
0:cd5404401c2f | 16 | /** |
peyo |
0:cd5404401c2f | 17 | * @file aws_json_utils.c |
peyo |
0:cd5404401c2f | 18 | * @brief Utilities for manipulating JSON |
peyo |
0:cd5404401c2f | 19 | * |
peyo |
0:cd5404401c2f | 20 | * json_utils provides JSON parsing utilities for use with the IoT SDK. |
peyo |
0:cd5404401c2f | 21 | * Underlying JSON parsing relies on the Jasmine JSON parser. |
peyo |
0:cd5404401c2f | 22 | * |
peyo |
0:cd5404401c2f | 23 | */ |
peyo |
0:cd5404401c2f | 24 | |
peyo |
0:cd5404401c2f | 25 | #ifdef __cplusplus |
peyo |
0:cd5404401c2f | 26 | extern "C" { |
peyo |
0:cd5404401c2f | 27 | #endif |
peyo |
0:cd5404401c2f | 28 | |
peyo |
0:cd5404401c2f | 29 | #include "aws_iot_json_utils.h" |
peyo |
0:cd5404401c2f | 30 | |
peyo |
0:cd5404401c2f | 31 | #include <stdio.h> |
peyo |
0:cd5404401c2f | 32 | #include <stdint.h> |
peyo |
0:cd5404401c2f | 33 | #include <string.h> |
peyo |
0:cd5404401c2f | 34 | |
peyo |
0:cd5404401c2f | 35 | #ifdef __cplusplus |
peyo |
0:cd5404401c2f | 36 | #include <cinttypes> |
peyo |
0:cd5404401c2f | 37 | #else |
peyo |
0:cd5404401c2f | 38 | |
peyo |
0:cd5404401c2f | 39 | #include <inttypes.h> |
peyo |
0:cd5404401c2f | 40 | |
peyo |
0:cd5404401c2f | 41 | #endif |
peyo |
0:cd5404401c2f | 42 | |
peyo |
0:cd5404401c2f | 43 | #include "aws_iot_log.h" |
peyo |
0:cd5404401c2f | 44 | |
peyo |
0:cd5404401c2f | 45 | int8_t jsoneq(const char *json, jsmntok_t *tok, const char *s) { |
peyo |
0:cd5404401c2f | 46 | if(tok->type == JSMN_STRING) { |
peyo |
0:cd5404401c2f | 47 | if((int) strlen(s) == tok->end - tok->start) { |
peyo |
0:cd5404401c2f | 48 | if(strncmp(json + tok->start, s, (size_t) (tok->end - tok->start)) == 0) { |
peyo |
0:cd5404401c2f | 49 | return 0; |
peyo |
0:cd5404401c2f | 50 | } |
peyo |
0:cd5404401c2f | 51 | } |
peyo |
0:cd5404401c2f | 52 | } |
peyo |
0:cd5404401c2f | 53 | return -1; |
peyo |
0:cd5404401c2f | 54 | } |
peyo |
0:cd5404401c2f | 55 | |
peyo |
0:cd5404401c2f | 56 | IoT_Error_t parseUnsignedInteger32Value(uint32_t *i, const char *jsonString, jsmntok_t *token) { |
peyo |
0:cd5404401c2f | 57 | if(token->type != JSMN_PRIMITIVE) { |
peyo |
0:cd5404401c2f | 58 | IOT_WARN("Token was not an integer"); |
peyo |
0:cd5404401c2f | 59 | return JSON_PARSE_ERROR; |
peyo |
0:cd5404401c2f | 60 | } |
peyo |
0:cd5404401c2f | 61 | |
peyo |
0:cd5404401c2f | 62 | if(('-' == (char) (jsonString[token->start])) || (1 != sscanf(jsonString + token->start, "%lu", i))) { |
peyo |
0:cd5404401c2f | 63 | IOT_WARN("Token was not an unsigned integer."); |
peyo |
0:cd5404401c2f | 64 | return JSON_PARSE_ERROR; |
peyo |
0:cd5404401c2f | 65 | } |
peyo |
0:cd5404401c2f | 66 | |
peyo |
0:cd5404401c2f | 67 | return SUCCESS; |
peyo |
0:cd5404401c2f | 68 | } |
peyo |
0:cd5404401c2f | 69 | |
peyo |
0:cd5404401c2f | 70 | IoT_Error_t parseUnsignedInteger16Value(uint16_t *i, const char *jsonString, jsmntok_t *token) { |
peyo |
0:cd5404401c2f | 71 | if(token->type != JSMN_PRIMITIVE) { |
peyo |
0:cd5404401c2f | 72 | IOT_WARN("Token was not an integer"); |
peyo |
0:cd5404401c2f | 73 | return JSON_PARSE_ERROR; |
peyo |
0:cd5404401c2f | 74 | } |
peyo |
0:cd5404401c2f | 75 | |
peyo |
0:cd5404401c2f | 76 | //if(('-' == (char) (jsonString[token->start])) || (1 != sscanf(jsonString + token->start, "%" SCNu16, i))) { |
peyo |
0:cd5404401c2f | 77 | if(('-' == (char) (jsonString[token->start])) || (1 != sscanf(jsonString + token->start, "%hu", i))) { |
peyo |
0:cd5404401c2f | 78 | IOT_WARN("Token was not an unsigned integer."); |
peyo |
0:cd5404401c2f | 79 | return JSON_PARSE_ERROR; |
peyo |
0:cd5404401c2f | 80 | } |
peyo |
0:cd5404401c2f | 81 | |
peyo |
0:cd5404401c2f | 82 | return SUCCESS; |
peyo |
0:cd5404401c2f | 83 | } |
peyo |
0:cd5404401c2f | 84 | |
peyo |
0:cd5404401c2f | 85 | IoT_Error_t parseUnsignedInteger8Value(uint8_t *i, const char *jsonString, jsmntok_t *token) { |
peyo |
0:cd5404401c2f | 86 | if(token->type != JSMN_PRIMITIVE) { |
peyo |
0:cd5404401c2f | 87 | IOT_WARN("Token was not an integer"); |
peyo |
0:cd5404401c2f | 88 | return JSON_PARSE_ERROR; |
peyo |
0:cd5404401c2f | 89 | } |
peyo |
0:cd5404401c2f | 90 | |
peyo |
0:cd5404401c2f | 91 | //if(('-' == (char) (jsonString[token->start])) || (1 != sscanf(jsonString + token->start, "%" SCNu8, i))) { |
peyo |
0:cd5404401c2f | 92 | if(('-' == (char) (jsonString[token->start])) || (1 != sscanf(jsonString + token->start, "%hhu", i))) { |
peyo |
0:cd5404401c2f | 93 | IOT_WARN("Token was not an unsigned integer."); |
peyo |
0:cd5404401c2f | 94 | return JSON_PARSE_ERROR; |
peyo |
0:cd5404401c2f | 95 | } |
peyo |
0:cd5404401c2f | 96 | |
peyo |
0:cd5404401c2f | 97 | return SUCCESS; |
peyo |
0:cd5404401c2f | 98 | } |
peyo |
0:cd5404401c2f | 99 | |
peyo |
0:cd5404401c2f | 100 | IoT_Error_t parseInteger32Value(int32_t *i, const char *jsonString, jsmntok_t *token) { |
peyo |
0:cd5404401c2f | 101 | if(token->type != JSMN_PRIMITIVE) { |
peyo |
0:cd5404401c2f | 102 | IOT_WARN("Token was not an integer"); |
peyo |
0:cd5404401c2f | 103 | return JSON_PARSE_ERROR; |
peyo |
0:cd5404401c2f | 104 | } |
peyo |
0:cd5404401c2f | 105 | |
peyo |
0:cd5404401c2f | 106 | if(1 != sscanf(jsonString + token->start, "%li", i)) { |
peyo |
0:cd5404401c2f | 107 | IOT_WARN("Token was not an integer."); |
peyo |
0:cd5404401c2f | 108 | return JSON_PARSE_ERROR; |
peyo |
0:cd5404401c2f | 109 | } |
peyo |
0:cd5404401c2f | 110 | |
peyo |
0:cd5404401c2f | 111 | return SUCCESS; |
peyo |
0:cd5404401c2f | 112 | } |
peyo |
0:cd5404401c2f | 113 | |
peyo |
0:cd5404401c2f | 114 | IoT_Error_t parseInteger16Value(int16_t *i, const char *jsonString, jsmntok_t *token) { |
peyo |
0:cd5404401c2f | 115 | if(token->type != JSMN_PRIMITIVE) { |
peyo |
0:cd5404401c2f | 116 | IOT_WARN("Token was not an integer"); |
peyo |
0:cd5404401c2f | 117 | return JSON_PARSE_ERROR; |
peyo |
0:cd5404401c2f | 118 | } |
peyo |
0:cd5404401c2f | 119 | |
peyo |
0:cd5404401c2f | 120 | if(1 != sscanf(jsonString + token->start, "%i", i)) { |
peyo |
0:cd5404401c2f | 121 | IOT_WARN("Token was not an integer."); |
peyo |
0:cd5404401c2f | 122 | return JSON_PARSE_ERROR; |
peyo |
0:cd5404401c2f | 123 | } |
peyo |
0:cd5404401c2f | 124 | |
peyo |
0:cd5404401c2f | 125 | return SUCCESS; |
peyo |
0:cd5404401c2f | 126 | } |
peyo |
0:cd5404401c2f | 127 | |
peyo |
0:cd5404401c2f | 128 | IoT_Error_t parseInteger8Value(int8_t *i, const char *jsonString, jsmntok_t *token) { |
peyo |
0:cd5404401c2f | 129 | if(token->type != JSMN_PRIMITIVE) { |
peyo |
0:cd5404401c2f | 130 | IOT_WARN("Token was not an integer"); |
peyo |
0:cd5404401c2f | 131 | return JSON_PARSE_ERROR; |
peyo |
0:cd5404401c2f | 132 | } |
peyo |
0:cd5404401c2f | 133 | |
peyo |
0:cd5404401c2f | 134 | //if(1 != sscanf(jsonString + token->start, "%" SCNi8, i)) { |
peyo |
0:cd5404401c2f | 135 | if(1 != sscanf(jsonString + token->start, "%hhi", i)) { |
peyo |
0:cd5404401c2f | 136 | IOT_WARN("Token was not an integer."); |
peyo |
0:cd5404401c2f | 137 | return JSON_PARSE_ERROR; |
peyo |
0:cd5404401c2f | 138 | } |
peyo |
0:cd5404401c2f | 139 | |
peyo |
0:cd5404401c2f | 140 | return SUCCESS; |
peyo |
0:cd5404401c2f | 141 | } |
peyo |
0:cd5404401c2f | 142 | |
peyo |
0:cd5404401c2f | 143 | IoT_Error_t parseFloatValue(float *f, const char *jsonString, jsmntok_t *token) { |
peyo |
0:cd5404401c2f | 144 | if(token->type != JSMN_PRIMITIVE) { |
peyo |
0:cd5404401c2f | 145 | IOT_WARN("Token was not a float."); |
peyo |
0:cd5404401c2f | 146 | return JSON_PARSE_ERROR; |
peyo |
0:cd5404401c2f | 147 | } |
peyo |
0:cd5404401c2f | 148 | |
peyo |
0:cd5404401c2f | 149 | if(1 != sscanf(jsonString + token->start, "%f", f)) { |
peyo |
0:cd5404401c2f | 150 | IOT_WARN("Token was not a float."); |
peyo |
0:cd5404401c2f | 151 | return JSON_PARSE_ERROR; |
peyo |
0:cd5404401c2f | 152 | } |
peyo |
0:cd5404401c2f | 153 | |
peyo |
0:cd5404401c2f | 154 | return SUCCESS; |
peyo |
0:cd5404401c2f | 155 | } |
peyo |
0:cd5404401c2f | 156 | |
peyo |
0:cd5404401c2f | 157 | IoT_Error_t parseDoubleValue(double *d, const char *jsonString, jsmntok_t *token) { |
peyo |
0:cd5404401c2f | 158 | if(token->type != JSMN_PRIMITIVE) { |
peyo |
0:cd5404401c2f | 159 | IOT_WARN("Token was not a double."); |
peyo |
0:cd5404401c2f | 160 | return JSON_PARSE_ERROR; |
peyo |
0:cd5404401c2f | 161 | } |
peyo |
0:cd5404401c2f | 162 | |
peyo |
0:cd5404401c2f | 163 | if(1 != sscanf(jsonString + token->start, "%lf", d)) { |
peyo |
0:cd5404401c2f | 164 | IOT_WARN("Token was not a double."); |
peyo |
0:cd5404401c2f | 165 | return JSON_PARSE_ERROR; |
peyo |
0:cd5404401c2f | 166 | } |
peyo |
0:cd5404401c2f | 167 | |
peyo |
0:cd5404401c2f | 168 | return SUCCESS; |
peyo |
0:cd5404401c2f | 169 | } |
peyo |
0:cd5404401c2f | 170 | |
peyo |
0:cd5404401c2f | 171 | IoT_Error_t parseBooleanValue(bool *b, const char *jsonString, jsmntok_t *token) { |
peyo |
0:cd5404401c2f | 172 | if(token->type != JSMN_PRIMITIVE) { |
peyo |
0:cd5404401c2f | 173 | IOT_WARN("Token was not a primitive."); |
peyo |
0:cd5404401c2f | 174 | return JSON_PARSE_ERROR; |
peyo |
0:cd5404401c2f | 175 | } |
peyo |
0:cd5404401c2f | 176 | if(jsonString[token->start] == 't' && jsonString[token->start + 1] == 'r' && jsonString[token->start + 2] == 'u' |
peyo |
0:cd5404401c2f | 177 | && jsonString[token->start + 3] == 'e') { |
peyo |
0:cd5404401c2f | 178 | *b = true; |
peyo |
0:cd5404401c2f | 179 | } else if(jsonString[token->start] == 'f' && jsonString[token->start + 1] == 'a' |
peyo |
0:cd5404401c2f | 180 | && jsonString[token->start + 2] == 'l' && jsonString[token->start + 3] == 's' |
peyo |
0:cd5404401c2f | 181 | && jsonString[token->start + 4] == 'e') { |
peyo |
0:cd5404401c2f | 182 | *b = false; |
peyo |
0:cd5404401c2f | 183 | } else { |
peyo |
0:cd5404401c2f | 184 | IOT_WARN("Token was not a bool."); |
peyo |
0:cd5404401c2f | 185 | return JSON_PARSE_ERROR; |
peyo |
0:cd5404401c2f | 186 | } |
peyo |
0:cd5404401c2f | 187 | return SUCCESS; |
peyo |
0:cd5404401c2f | 188 | } |
peyo |
0:cd5404401c2f | 189 | |
peyo |
0:cd5404401c2f | 190 | IoT_Error_t parseStringValue(char *buf, const char *jsonString, jsmntok_t *token) { |
peyo |
0:cd5404401c2f | 191 | uint16_t size = 0; |
peyo |
0:cd5404401c2f | 192 | if(token->type != JSMN_STRING) { |
peyo |
0:cd5404401c2f | 193 | IOT_WARN("Token was not a string."); |
peyo |
0:cd5404401c2f | 194 | return JSON_PARSE_ERROR; |
peyo |
0:cd5404401c2f | 195 | } |
peyo |
0:cd5404401c2f | 196 | size = (uint16_t) (token->end - token->start); |
peyo |
0:cd5404401c2f | 197 | memcpy(buf, jsonString + token->start, size); |
peyo |
0:cd5404401c2f | 198 | buf[size] = '\0'; |
peyo |
0:cd5404401c2f | 199 | return SUCCESS; |
peyo |
0:cd5404401c2f | 200 | } |
peyo |
0:cd5404401c2f | 201 | |
peyo |
0:cd5404401c2f | 202 | #ifdef __cplusplus |
peyo |
0:cd5404401c2f | 203 | } |
peyo |
0:cd5404401c2f | 204 | #endif |