V.06 11/3

Dependencies:   FT6206 SDFileSystem SPI_TFT_ILI9341 TFT_fonts

Fork of ATT_AWS_IoT_demo by attiot

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers aws_iot_json_utils.cpp Source File

aws_iot_json_utils.cpp

00001 /*
00002  * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License").
00005  * You may not use this file except in compliance with the License.
00006  * A copy of the License is located at
00007  *
00008  *  http://aws.amazon.com/apache2.0
00009  *
00010  * or in the "license" file accompanying this file. This file is distributed
00011  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
00012  * express or implied. See the License for the specific language governing
00013  * permissions and limitations under the License.
00014  */
00015 
00016 /**
00017  * @file aws_json_utils.c
00018  * @brief Utilities for manipulating JSON
00019  *
00020  * json_utils provides JSON parsing utilities for use with the IoT SDK.
00021  * Underlying JSON parsing relies on the Jasmine JSON parser.
00022  *
00023  */
00024 
00025 #include "aws_iot_json_utils.h"
00026 
00027 #include <stdio.h>
00028 #include <stdint.h>
00029 #include <inttypes.h>
00030 #include <string.h>
00031 #include "aws_iot_log.h"
00032 
00033 int8_t jsoneq(const char *json, jsmntok_t *tok, const char *s) {
00034     if (tok->type == JSMN_STRING) {
00035         if ((int) strlen(s) == tok->end - tok->start) {
00036             if (strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
00037                 return 0;
00038             }
00039         }
00040     }
00041     return -1;
00042 }
00043 
00044 IoT_Error_t parseUnsignedInteger32Value(uint32_t *i, const char *jsonString, jsmntok_t *token) {
00045     if (token->type != JSMN_PRIMITIVE) {
00046         WARN("Token was not an integer");
00047         return JSON_PARSE_ERROR;
00048     }
00049 
00050     //if (1 != sscanf(jsonString + token->start, "%"PRIu32, i)) {
00051     if (1 != sscanf(jsonString + token->start, "%lu", i)) {
00052         WARN("Token was not an integer.");
00053         return JSON_PARSE_ERROR;
00054     }
00055 
00056     return NONE_ERROR;
00057 }
00058 
00059 IoT_Error_t parseUnsignedInteger16Value(uint16_t *i, const char *jsonString, jsmntok_t *token) {
00060     if (token->type != JSMN_PRIMITIVE) {
00061         WARN("Token was not an integer");
00062         return JSON_PARSE_ERROR;
00063     }
00064 
00065     //if (1 != sscanf(jsonString + token->start, "%"PRIu16, i)) {
00066     if (1 != sscanf(jsonString + token->start, "%u", i)) {
00067         WARN("Token was not an integer.");
00068         return JSON_PARSE_ERROR;
00069     }
00070 
00071     return NONE_ERROR;
00072 }
00073 
00074 IoT_Error_t parseUnsignedInteger8Value(uint8_t *i, const char *jsonString, jsmntok_t *token) {
00075     if (token->type != JSMN_PRIMITIVE) {
00076         WARN("Token was not an integer");
00077         return JSON_PARSE_ERROR;
00078     }
00079 
00080     //if (1 != sscanf(jsonString + token->start, "%"PRIu8, i)) {
00081     if (1 != sscanf(jsonString + token->start, "%u", i)) {  
00082         WARN("Token was not an integer.");
00083         return JSON_PARSE_ERROR;
00084     }
00085 
00086     return NONE_ERROR;
00087 }
00088 
00089 IoT_Error_t parseInteger32Value(int32_t *i, const char *jsonString, jsmntok_t *token) {
00090     if (token->type != JSMN_PRIMITIVE) {
00091         WARN("Token was not an integer");
00092         return JSON_PARSE_ERROR;
00093     }
00094 
00095     //if (1 != sscanf(jsonString + token->start, "%"PRIi32, i)) {
00096     if (1 != sscanf(jsonString + token->start, "%li", i)) {
00097         WARN("Token was not an integer.");
00098         return JSON_PARSE_ERROR;
00099     }
00100 
00101     return NONE_ERROR;
00102 }
00103 
00104 IoT_Error_t parseInteger16Value(int16_t *i, const char *jsonString, jsmntok_t *token) {
00105     if (token->type != JSMN_PRIMITIVE) {
00106         WARN("Token was not an integer");
00107         return JSON_PARSE_ERROR;
00108     }
00109 
00110     //if (1 != sscanf(jsonString + token->start, "%"PRIi16, i)) {
00111     if (1 != sscanf(jsonString + token->start, "%i", i)) {
00112         WARN("Token was not an integer.");
00113         return JSON_PARSE_ERROR;
00114     }
00115 
00116     return NONE_ERROR;
00117 }
00118 
00119 IoT_Error_t parseInteger8Value(int8_t *i, const char *jsonString, jsmntok_t *token) {
00120     if (token->type != JSMN_PRIMITIVE) {
00121         WARN("Token was not an integer");
00122         return JSON_PARSE_ERROR;
00123     }
00124 
00125     //if (1 != sscanf(jsonString + token->start, "%"PRIi8, i)) {
00126     if (1 != sscanf(jsonString + token->start, "%i", i)) {
00127         WARN("Token was not an integer.");
00128         return JSON_PARSE_ERROR;
00129     }
00130 
00131     return NONE_ERROR;
00132 }
00133 
00134 IoT_Error_t parseFloatValue(float *f, const char *jsonString, jsmntok_t *token) {
00135     if (token->type != JSMN_PRIMITIVE) {
00136         WARN("Token was not a float.");
00137         return JSON_PARSE_ERROR;
00138     }
00139 
00140     if (1 != sscanf(jsonString + token->start, "%f", f)) {
00141         WARN("Token was not a float.");
00142         return JSON_PARSE_ERROR;
00143     }
00144 
00145     return NONE_ERROR;
00146 }
00147 
00148 IoT_Error_t parseDoubleValue(double *d, const char *jsonString, jsmntok_t *token) {
00149     if (token->type != JSMN_PRIMITIVE) {
00150         WARN("Token was not a double.");
00151         return JSON_PARSE_ERROR;
00152     }
00153 
00154     if (1 != sscanf(jsonString + token->start, "%lf", d)) {
00155         WARN("Token was not a double.");
00156         return JSON_PARSE_ERROR;
00157     }
00158 
00159     return NONE_ERROR;
00160 }
00161 
00162 IoT_Error_t parseBooleanValue(bool *b, const char *jsonString, jsmntok_t *token) {
00163     if (token->type != JSMN_PRIMITIVE) {
00164         WARN("Token was not a primitive.");
00165         return JSON_PARSE_ERROR;
00166     }
00167     if (jsonString[token->start] == 't' && jsonString[token->start + 1] == 'r' && jsonString[token->start + 2] == 'u'
00168             && jsonString[token->start + 3] == 'e') {
00169         *b = true;
00170     } else if (jsonString[token->start] == 'f' && jsonString[token->start + 1] == 'a'
00171             && jsonString[token->start + 2] == 'l' && jsonString[token->start + 3] == 's'
00172             && jsonString[token->start + 4] == 'e') {
00173         *b = false;
00174     } else {
00175         WARN("Token was not a bool.");
00176         return JSON_PARSE_ERROR;
00177     }
00178     return NONE_ERROR;
00179 }
00180 
00181 IoT_Error_t parseStringValue(char *buf, const char *jsonString, jsmntok_t *token) {
00182     uint16_t size = 0;
00183     if (token->type != JSMN_STRING) {
00184         WARN("Token was not a string.");
00185         return JSON_PARSE_ERROR;
00186     }
00187     size = token->end - token->start;
00188     memcpy(buf, jsonString + token->start, size);
00189     buf[size] = '\0';
00190     return NONE_ERROR;
00191 }
00192