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
utility/streamhelper.c@2:ece3b5c4afed, 2012-09-07 (annotated)
- Committer:
- mimil
- Date:
- Fri Sep 07 09:30:33 2012 +0000
- Revision:
- 2:ece3b5c4afed
- Parent:
- 1:6df1d1f1b372
update to last git version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mimil | 1:6df1d1f1b372 | 1 | /* |
mimil | 1:6df1d1f1b372 | 2 | * aJson |
mimil | 1:6df1d1f1b372 | 3 | * streamhelper.c |
mimil | 1:6df1d1f1b372 | 4 | * |
mimil | 1:6df1d1f1b372 | 5 | * http://interactive-matter.org/ |
mimil | 1:6df1d1f1b372 | 6 | * |
mimil | 1:6df1d1f1b372 | 7 | * This file is part of aJson. |
mimil | 1:6df1d1f1b372 | 8 | * |
mimil | 1:6df1d1f1b372 | 9 | * aJson is free software: you can redistribute it and/or modify |
mimil | 1:6df1d1f1b372 | 10 | * it under the terms of the GNU General Public License as published by |
mimil | 1:6df1d1f1b372 | 11 | * the Free Software Foundation, either version 3 of the License, or |
mimil | 1:6df1d1f1b372 | 12 | * (at your option) any later version. |
mimil | 1:6df1d1f1b372 | 13 | * |
mimil | 1:6df1d1f1b372 | 14 | * aJson is distributed in the hope that it will be useful, |
mimil | 1:6df1d1f1b372 | 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
mimil | 1:6df1d1f1b372 | 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
mimil | 1:6df1d1f1b372 | 17 | * GNU General Public License for more details. |
mimil | 1:6df1d1f1b372 | 18 | * You should have received a copy of the GNU General Public License |
mimil | 1:6df1d1f1b372 | 19 | * along with aJson. If not, see <http://www.gnu.org/licenses/>. |
mimil | 1:6df1d1f1b372 | 20 | * |
mimil | 1:6df1d1f1b372 | 21 | * Created on: 10.10.2010 |
mimil | 1:6df1d1f1b372 | 22 | * Author: marcus |
mimil | 1:6df1d1f1b372 | 23 | */ |
mimil | 1:6df1d1f1b372 | 24 | #include <stdio.h> |
mimil | 1:6df1d1f1b372 | 25 | #include <stdlib.h> |
mimil | 1:6df1d1f1b372 | 26 | #include "streamhelper.h" |
mimil | 1:6df1d1f1b372 | 27 | #include "stringbuffer.h" |
mimil | 1:6df1d1f1b372 | 28 | |
mimil | 1:6df1d1f1b372 | 29 | //internal prototypes |
mimil | 1:6df1d1f1b372 | 30 | int |
mimil | 1:6df1d1f1b372 | 31 | stringGet(FILE* stream); |
mimil | 1:6df1d1f1b372 | 32 | int |
mimil | 1:6df1d1f1b372 | 33 | stringPut(char c, FILE* stream); |
mimil | 1:6df1d1f1b372 | 34 | |
mimil | 1:6df1d1f1b372 | 35 | typedef struct |
mimil | 1:6df1d1f1b372 | 36 | { |
mimil | 1:6df1d1f1b372 | 37 | char* string; |
mimil | 1:6df1d1f1b372 | 38 | unsigned int position; |
mimil | 1:6df1d1f1b372 | 39 | } string_input_stream_info; |
mimil | 1:6df1d1f1b372 | 40 | |
mimil | 1:6df1d1f1b372 | 41 | /*FILE* |
mimil | 1:6df1d1f1b372 | 42 | openStringInputStream(char* string) |
mimil | 1:6df1d1f1b372 | 43 | { |
mimil | 1:6df1d1f1b372 | 44 | FILE* result = fdevopen(NULL, &stringGet); |
mimil | 1:6df1d1f1b372 | 45 | if (result == NULL) |
mimil | 1:6df1d1f1b372 | 46 | { |
mimil | 1:6df1d1f1b372 | 47 | return NULL; |
mimil | 1:6df1d1f1b372 | 48 | } |
mimil | 1:6df1d1f1b372 | 49 | string_input_stream_info* udata = malloc(sizeof(string_input_stream_info)); |
mimil | 1:6df1d1f1b372 | 50 | if (udata != NULL) |
mimil | 1:6df1d1f1b372 | 51 | { |
mimil | 1:6df1d1f1b372 | 52 | udata->string = string; |
mimil | 1:6df1d1f1b372 | 53 | udata->position = 0; |
mimil | 1:6df1d1f1b372 | 54 | fdev_set_udata(result,udata); |
mimil | 1:6df1d1f1b372 | 55 | } |
mimil | 1:6df1d1f1b372 | 56 | else |
mimil | 1:6df1d1f1b372 | 57 | { |
mimil | 1:6df1d1f1b372 | 58 | fclose(result); |
mimil | 1:6df1d1f1b372 | 59 | return NULL; |
mimil | 1:6df1d1f1b372 | 60 | } |
mimil | 1:6df1d1f1b372 | 61 | return result; |
mimil | 1:6df1d1f1b372 | 62 | }*/ |
mimil | 1:6df1d1f1b372 | 63 | |
mimil | 1:6df1d1f1b372 | 64 | /*void |
mimil | 1:6df1d1f1b372 | 65 | closeStringInputStream(FILE* stream) |
mimil | 1:6df1d1f1b372 | 66 | { |
mimil | 1:6df1d1f1b372 | 67 | if (stream == NULL) |
mimil | 1:6df1d1f1b372 | 68 | { |
mimil | 1:6df1d1f1b372 | 69 | return; |
mimil | 1:6df1d1f1b372 | 70 | } |
mimil | 1:6df1d1f1b372 | 71 | string_input_stream_info* udata = |
mimil | 1:6df1d1f1b372 | 72 | (string_input_stream_info*) fdev_get_udata(stream); |
mimil | 1:6df1d1f1b372 | 73 | if (udata != NULL) |
mimil | 1:6df1d1f1b372 | 74 | { |
mimil | 1:6df1d1f1b372 | 75 | free(udata); |
mimil | 1:6df1d1f1b372 | 76 | } |
mimil | 1:6df1d1f1b372 | 77 | fdev_set_udata(stream,NULL); |
mimil | 1:6df1d1f1b372 | 78 | fclose(stream); |
mimil | 1:6df1d1f1b372 | 79 | }*/ |
mimil | 1:6df1d1f1b372 | 80 | |
mimil | 1:6df1d1f1b372 | 81 | /*FILE* |
mimil | 1:6df1d1f1b372 | 82 | openStringOutputStream(void) |
mimil | 1:6df1d1f1b372 | 83 | { |
mimil | 1:6df1d1f1b372 | 84 | FILE* result = fdevopen(&stringPut, NULL); |
mimil | 1:6df1d1f1b372 | 85 | if (result == NULL) |
mimil | 1:6df1d1f1b372 | 86 | { |
mimil | 1:6df1d1f1b372 | 87 | return NULL; |
mimil | 1:6df1d1f1b372 | 88 | } |
mimil | 1:6df1d1f1b372 | 89 | string_buffer* buffer = stringBufferCreate(); |
mimil | 1:6df1d1f1b372 | 90 | if (buffer == NULL) |
mimil | 1:6df1d1f1b372 | 91 | { |
mimil | 1:6df1d1f1b372 | 92 | fclose(result); |
mimil | 1:6df1d1f1b372 | 93 | return NULL; |
mimil | 1:6df1d1f1b372 | 94 | } |
mimil | 1:6df1d1f1b372 | 95 | fdev_set_udata(result,buffer); |
mimil | 1:6df1d1f1b372 | 96 | return result; |
mimil | 1:6df1d1f1b372 | 97 | }*/ |
mimil | 1:6df1d1f1b372 | 98 | |
mimil | 1:6df1d1f1b372 | 99 | /*char* |
mimil | 1:6df1d1f1b372 | 100 | closeStringOutputStream(FILE* stream) |
mimil | 1:6df1d1f1b372 | 101 | { |
mimil | 1:6df1d1f1b372 | 102 | //write a 0 to the end - that is how a string looks like |
mimil | 1:6df1d1f1b372 | 103 | string_buffer* buffer = (string_buffer*) fdev_get_udata(stream); |
mimil | 1:6df1d1f1b372 | 104 | char* result = stringBufferToString(buffer); |
mimil | 1:6df1d1f1b372 | 105 | if (result == NULL) |
mimil | 1:6df1d1f1b372 | 106 | { |
mimil | 1:6df1d1f1b372 | 107 | fclose(stream); |
mimil | 1:6df1d1f1b372 | 108 | return NULL; |
mimil | 1:6df1d1f1b372 | 109 | } |
mimil | 1:6df1d1f1b372 | 110 | //free(buffer); |
mimil | 1:6df1d1f1b372 | 111 | fdev_set_udata(stream,NULL); |
mimil | 1:6df1d1f1b372 | 112 | fclose(stream); |
mimil | 1:6df1d1f1b372 | 113 | return result; |
mimil | 1:6df1d1f1b372 | 114 | }*/ |
mimil | 1:6df1d1f1b372 | 115 | |
mimil | 1:6df1d1f1b372 | 116 | /*int |
mimil | 1:6df1d1f1b372 | 117 | stringGet(FILE* stream) |
mimil | 1:6df1d1f1b372 | 118 | { |
mimil | 1:6df1d1f1b372 | 119 | string_input_stream_info* udata = |
mimil | 1:6df1d1f1b372 | 120 | (string_input_stream_info*) fdev_get_udata(stream); |
mimil | 1:6df1d1f1b372 | 121 | char result = udata->string[udata->position]; |
mimil | 1:6df1d1f1b372 | 122 | if (result == 0) |
mimil | 1:6df1d1f1b372 | 123 | { |
mimil | 1:6df1d1f1b372 | 124 | return EOF; |
mimil | 1:6df1d1f1b372 | 125 | } |
mimil | 1:6df1d1f1b372 | 126 | else |
mimil | 1:6df1d1f1b372 | 127 | { |
mimil | 1:6df1d1f1b372 | 128 | udata->position++; |
mimil | 1:6df1d1f1b372 | 129 | return result; |
mimil | 1:6df1d1f1b372 | 130 | } |
mimil | 1:6df1d1f1b372 | 131 | }*/ |
mimil | 1:6df1d1f1b372 | 132 | |
mimil | 1:6df1d1f1b372 | 133 | /*int |
mimil | 1:6df1d1f1b372 | 134 | stringPut(char c, FILE* stream) |
mimil | 1:6df1d1f1b372 | 135 | { |
mimil | 1:6df1d1f1b372 | 136 | string_buffer* buffer = (string_buffer*) fdev_get_udata(stream); |
mimil | 1:6df1d1f1b372 | 137 | if (stringBufferAdd(c, buffer)) |
mimil | 1:6df1d1f1b372 | 138 | { |
mimil | 1:6df1d1f1b372 | 139 | return EOF; |
mimil | 1:6df1d1f1b372 | 140 | } |
mimil | 1:6df1d1f1b372 | 141 | return 0; |
mimil | 1:6df1d1f1b372 | 142 | }*/ |