aJson is the attempt to port a complete JSON implementation to Arduino. It is based on the cJSON implementation, reduced in size and removing one or two feature. The current mbed implementation only supports FILE* as input so you will have to use a temporary file for parsing your json input. https://github.com/interactive-matter/aJson

Dependents:   WIZwikiREST20

Committer:
mimil
Date:
Mon Aug 27 15:15:45 2012 +0000
Revision:
0:428cf9a51873
Child:
1:6df1d1f1b372
[mbed] converted /Trash/aJSON

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mimil 0:428cf9a51873 1 /*
mimil 0:428cf9a51873 2 * aJson
mimil 0:428cf9a51873 3 * stringbuffer.c
mimil 0:428cf9a51873 4 *
mimil 0:428cf9a51873 5 * http://interactive-matter.org/
mimil 0:428cf9a51873 6 *
mimil 0:428cf9a51873 7 * This file is part of aJson.
mimil 0:428cf9a51873 8 *
mimil 0:428cf9a51873 9 * aJson is free software: you can redistribute it and/or modify
mimil 0:428cf9a51873 10 * it under the terms of the GNU General Public License as published by
mimil 0:428cf9a51873 11 * the Free Software Foundation, either version 3 of the License, or
mimil 0:428cf9a51873 12 * (at your option) any later version.
mimil 0:428cf9a51873 13 *
mimil 0:428cf9a51873 14 * aJson is distributed in the hope that it will be useful,
mimil 0:428cf9a51873 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mimil 0:428cf9a51873 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
mimil 0:428cf9a51873 17 * GNU General Public License for more details.
mimil 0:428cf9a51873 18 * You should have received a copy of the GNU General Public License
mimil 0:428cf9a51873 19 * along with aJson. If not, see <http://www.gnu.org/licenses/>.
mimil 0:428cf9a51873 20 *
mimil 0:428cf9a51873 21 * Created on: 14.10.2010
mimil 0:428cf9a51873 22 * Author: marcus
mimil 0:428cf9a51873 23 */
mimil 0:428cf9a51873 24 #include <stdlib.h>
mimil 0:428cf9a51873 25 #include <string.h>
mimil 0:428cf9a51873 26 #include "stringbuffer.h"
mimil 0:428cf9a51873 27
mimil 0:428cf9a51873 28 //Default buffer size for strings
mimil 0:428cf9a51873 29 #define BUFFER_SIZE 256
mimil 0:428cf9a51873 30 //there is a static buffer allocated, which is used to decode strings to
mimil 0:428cf9a51873 31 //strings cannot be longer than the buffer
mimil 0:428cf9a51873 32 char global_buffer[BUFFER_SIZE];
mimil 0:428cf9a51873 33
mimil 0:428cf9a51873 34 string_buffer*
mimil 0:428cf9a51873 35 stringBufferCreate(void)
mimil 0:428cf9a51873 36 {
mimil 0:428cf9a51873 37 string_buffer* result = (string_buffer*)malloc(sizeof(string_buffer));
mimil 0:428cf9a51873 38 if (result == NULL)
mimil 0:428cf9a51873 39 {
mimil 0:428cf9a51873 40 return NULL;
mimil 0:428cf9a51873 41 }
mimil 0:428cf9a51873 42 result->string = global_buffer;
mimil 0:428cf9a51873 43 memset((void*) global_buffer, 0, BUFFER_SIZE);
mimil 0:428cf9a51873 44 //unused - but will be usefull after realloc got fixd
mimil 0:428cf9a51873 45 /* if (result->string==NULL) {
mimil 0:428cf9a51873 46 free(result);
mimil 0:428cf9a51873 47 return NULL;
mimil 0:428cf9a51873 48 }
mimil 0:428cf9a51873 49 result->memory=BUFFER_DEFAULT_SIZE;*/
mimil 0:428cf9a51873 50 result->memory = BUFFER_SIZE;
mimil 0:428cf9a51873 51 result->string_length = 0;
mimil 0:428cf9a51873 52 return result;
mimil 0:428cf9a51873 53 }
mimil 0:428cf9a51873 54
mimil 0:428cf9a51873 55 char
mimil 0:428cf9a51873 56 stringBufferAdd(char value, string_buffer* buffer)
mimil 0:428cf9a51873 57 {
mimil 0:428cf9a51873 58 if (buffer->string_length >= buffer->memory)
mimil 0:428cf9a51873 59 {
mimil 0:428cf9a51873 60 //this has to be enabled after realloc works
mimil 0:428cf9a51873 61 /*char* new_string = (char*) realloc((void*) buffer->string, (buffer->memory
mimil 0:428cf9a51873 62 + BUFFER_DEFAULT_SIZE) * sizeof(char));
mimil 0:428cf9a51873 63 if (new_string == NULL)
mimil 0:428cf9a51873 64 {
mimil 0:428cf9a51873 65 free(buffer->string);
mimil 0:428cf9a51873 66 buffer->string = NULL;
mimil 0:428cf9a51873 67 return -1;
mimil 0:428cf9a51873 68 } else {
mimil 0:428cf9a51873 69 buffer->string = new_string;
mimil 0:428cf9a51873 70 }
mimil 0:428cf9a51873 71 buffer->memory += BUFFER_DEFAULT_SIZE;*/
mimil 0:428cf9a51873 72 //in the meantime we just drop it
mimil 0:428cf9a51873 73 return 0; //EOF would be a better choice - but that breaks json decoding
mimil 0:428cf9a51873 74 }
mimil 0:428cf9a51873 75 buffer->string[buffer->string_length] = value;
mimil 0:428cf9a51873 76 buffer->string_length += 1;
mimil 0:428cf9a51873 77 return 0;
mimil 0:428cf9a51873 78 }
mimil 0:428cf9a51873 79
mimil 0:428cf9a51873 80 char*
mimil 0:428cf9a51873 81 stringBufferToString(string_buffer* buffer)
mimil 0:428cf9a51873 82 {
mimil 0:428cf9a51873 83 //this is the realloc dependent function - it does not work
mimil 0:428cf9a51873 84 // char* result = buffer->string;
mimil 0:428cf9a51873 85 //ensure that the string ends with 0
mimil 0:428cf9a51873 86 if (buffer->string_length == 0 || buffer->string[(buffer->string_length - 1)]
mimil 0:428cf9a51873 87 != 0)
mimil 0:428cf9a51873 88 {
mimil 0:428cf9a51873 89 stringBufferAdd(0, buffer);
mimil 0:428cf9a51873 90 }
mimil 0:428cf9a51873 91 /* char* string = realloc(result, buffer->string_length);
mimil 0:428cf9a51873 92 if (string==NULL) {
mimil 0:428cf9a51873 93 free(result);
mimil 0:428cf9a51873 94 }
mimil 0:428cf9a51873 95 buffer->string=NULL;
mimil 0:428cf9a51873 96 free(buffer);
mimil 0:428cf9a51873 97 return string;*/
mimil 0:428cf9a51873 98 char* result = (char*)malloc(buffer->string_length * sizeof(char));
mimil 0:428cf9a51873 99 if (result == NULL)
mimil 0:428cf9a51873 100 {
mimil 0:428cf9a51873 101 return NULL;
mimil 0:428cf9a51873 102 }
mimil 0:428cf9a51873 103 strcpy(result, global_buffer);
mimil 0:428cf9a51873 104 buffer->string = NULL;
mimil 0:428cf9a51873 105 free(buffer);
mimil 0:428cf9a51873 106 return result;
mimil 0:428cf9a51873 107 }
mimil 0:428cf9a51873 108
mimil 0:428cf9a51873 109 void
mimil 0:428cf9a51873 110 stringBufferFree(string_buffer* buffer)
mimil 0:428cf9a51873 111 {
mimil 0:428cf9a51873 112 if (buffer == NULL)
mimil 0:428cf9a51873 113 {
mimil 0:428cf9a51873 114 //hmm it was null before - whatever
mimil 0:428cf9a51873 115 return;
mimil 0:428cf9a51873 116 }
mimil 0:428cf9a51873 117 //this is not needed in this realloc free concept
mimil 0:428cf9a51873 118 /*
mimil 0:428cf9a51873 119 if (buffer->string!=NULL) {
mimil 0:428cf9a51873 120 free(buffer->string);
mimil 0:428cf9a51873 121 }
mimil 0:428cf9a51873 122 */
mimil 0:428cf9a51873 123 free(buffer);
mimil 0:428cf9a51873 124 }
mimil 0:428cf9a51873 125