00001 /********************************************************************************* 00002 00003 Copyright 2006-2008 MakingThings 00004 00005 Licensed under the Apache License, 00006 Version 2.0 (the "License"); you may not use this file except in compliance 00007 with the License. You may obtain a copy of the License at 00008 00009 http://www.apache.org/licenses/LICENSE-2.0 00010 00011 Unless required by applicable law or agreed to in writing, software distributed 00012 under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 00013 CONDITIONS OF ANY KIND, either express or implied. See the License for 00014 the specific language governing permissions and limitations under the License. 00015 00016 *********************************************************************************/ 00017 00018 #ifndef JSON_H 00019 #define JSON_H 00020 00021 #include "types.h" 00022 00023 #define JSON_MAX_DEPTH 32 00024 00025 // state object for encoding 00026 typedef enum 00027 { 00028 JSON_START, 00029 JSON_OBJ_START, 00030 JSON_OBJ_KEY, 00031 JSON_OBJ_VALUE, 00032 JSON_ARRAY_START, 00033 JSON_IN_ARRAY 00034 } JsonEncode_Step; 00035 00036 /** 00037 The structure used to maintain the state of a JSON encode process. 00038 You'll need to have one of these for each JSON string you want to encode. 00039 The same variable can be reused after resetting it with a call to JsonEncode_Init(). 00040 \ingroup json 00041 */ 00042 typedef struct 00043 { 00044 JsonEncode_Step steps[JSON_MAX_DEPTH]; /**< An array to keep track of the state of each step in the encoder. */ 00045 int depth; /**< The current depth of the encoder (how many elements have been opened). */ 00046 } JsonEncode_State; 00047 00048 // state object for decoding 00049 typedef enum { 00050 JSON_DECODE_OBJECT_START, 00051 JSON_DECODE_IN_OBJECT, 00052 JSON_DECODE_IN_ARRAY 00053 } JsonDecode_Step; 00054 00055 /** 00056 The structure used to maintain the state of a JSON decode process. 00057 You'll need to have one of these for each JSON string you want to encode. 00058 The same variable can be reused after resetting it with a call to JsonDecode_Init(). 00059 \ingroup json 00060 */ 00061 typedef struct { 00062 JsonDecode_Step steps[JSON_MAX_DEPTH]; /**< An array to keep track of each step of the decoder. */ 00063 int depth; /**< The current depth of the decoder (how many elements have been opened). */ 00064 bool gotcomma; /**< Used internally by the decoder. */ 00065 void* context; /**< A pointer to the user context. */ 00066 char* p; /**< A pointer to the data. */ 00067 int len; /**< The current length. */ 00068 } JsonDecode_State; 00069 00070 // Encode 00071 void JsonEncode_Init(JsonEncode_State* state); 00072 00073 char* JsonEncode_ObjectOpen(JsonEncode_State* state, char *buf, int *remaining); 00074 char* JsonEncode_ObjectKey(JsonEncode_State* state, char *buf, const char *key, int *remaining); 00075 char* JsonEncode_ObjectClose(JsonEncode_State* state, char *buf, int *remaining); 00076 char* JsonEncode_ArrayOpen(JsonEncode_State* state, char *buf, int *remaining); 00077 char* JsonEncode_ArrayClose(JsonEncode_State* state, char *buf, int *remaining); 00078 char* JsonEncode_String(JsonEncode_State* state, char *buf, const char *string, int *remaining); 00079 char* JsonEncode_Int(JsonEncode_State* state, char *buf, int value, int *remaining); 00080 char* JsonEncode_Bool(JsonEncode_State* state, char *buf, bool value, int *remaining); 00081 00082 // Decode 00083 void JsonDecode_SetIntCallback(bool(*int_callback)(void *ctx, int val)); 00084 void JsonDecode_SetFloatCallback(bool(*float_callback)(void *ctx, float val)); 00085 void JsonDecode_SetBoolCallback(bool(*bool_callback)(void *ctx, bool val)); 00086 void JsonDecode_SetStringCallback(bool(*string_callback)(void *ctx, char *string, int len)); 00087 void JsonDecode_SetStartObjCallback(bool(*start_obj_callback)(void *ctx)); 00088 void JsonDecode_SetObjKeyCallback(bool(*obj_key_callback)(void *ctx, char *key, int len)); 00089 void JsonDecode_SetEndObjCallback(bool(*end_obj_callback)(void *ctx)); 00090 void JsonDecode_SetStartArrayCallback(bool(*start_array_callback)(void *ctx)); 00091 void JsonDecode_SetEndArrayCallback(bool(*end_array_callback)(void *ctx)); 00092 00093 void JsonDecode_Init(JsonDecode_State* state, void* context); 00094 bool JsonDecode(JsonDecode_State* state, char* text, int len); 00095 00096 #endif /* JSON_H */ 00097 00098
The Make Controller Kit is an open source project maintained by MakingThings.
MakingThings code is released under the Apache 2.0 license.
Bug tracker, development wiki and status can be found at http://dev.makingthings.com.
This document was last updated on 18 May 2009.