test

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers jsmn.h Source File

jsmn.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010 Serge A. Zaitsev
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  */
00022 
00023 /**
00024  * @file jsmn.h
00025  * @brief Definition of the JSMN (Jasmine) JSON parser.
00026  *
00027  * For more information on JSMN:
00028  * @see http://zserge.com/jsmn.html
00029  */
00030 
00031 #ifndef __JSMN_H_
00032 #define __JSMN_H_
00033 
00034 #include <stddef.h>
00035 
00036 #ifdef __cplusplus
00037 extern "C" {
00038 #endif
00039 
00040 /**
00041  * JSON type identifier. Basic types are:
00042  *  o Object
00043  *  o Array
00044  *  o String
00045  *  o Other primitive: number, boolean (true/false) or null
00046  */
00047 typedef enum {
00048     JSMN_UNDEFINED = 0,
00049     JSMN_OBJECT = 1,
00050     JSMN_ARRAY = 2,
00051     JSMN_STRING = 3,
00052     JSMN_PRIMITIVE = 4
00053 } jsmntype_t;
00054 
00055 enum jsmnerr {
00056     /* Not enough tokens were provided */
00057     JSMN_ERROR_NOMEM = -1,
00058     /* Invalid character inside JSON string */
00059     JSMN_ERROR_INVAL = -2,
00060     /* The string is not a full JSON packet, more bytes expected */
00061     JSMN_ERROR_PART = -3
00062 };
00063 
00064 /**
00065  * JSON token description.
00066  * @param       type    type (object, array, string etc.)
00067  * @param       start   start position in JSON data string
00068  * @param       end     end position in JSON data string
00069  */
00070 typedef struct {
00071     jsmntype_t type;
00072     int start;
00073     int end;
00074     int size;
00075 #ifdef JSMN_PARENT_LINKS
00076     int parent;
00077 #endif
00078 } jsmntok_t;
00079 
00080 /**
00081  * JSON parser. Contains an array of token blocks available. Also stores
00082  * the string being parsed now and current position in that string
00083  */
00084 typedef struct {
00085     unsigned int pos; /* offset in the JSON string */
00086     unsigned int toknext; /* next token to allocate */
00087     int toksuper; /* superior token node, e.g parent object or array */
00088 } jsmn_parser;
00089 
00090 /**
00091  * Create JSON parser over an array of tokens
00092  */
00093 void jsmn_init(jsmn_parser *parser);
00094 
00095 /**
00096  * Run JSON parser. It parses a JSON data string into and array of tokens, each describing
00097  * a single JSON object.
00098  */
00099 int jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
00100         jsmntok_t *tokens, unsigned int num_tokens);
00101 
00102 #ifdef __cplusplus
00103 }
00104 #endif
00105 
00106 #endif /* __JSMN_H_ */