blob: 7d40b40a29f4ee3639989b0081cf1f9c85f6b020 [file] [log] [blame]
Michael Clarkf0d08882007-03-13 08:26:18 +00001/*
Michael Clarka850f8e2007-03-13 08:26:26 +00002 * $Id: json_tokener.h,v 1.10 2006/07/25 03:24:50 mclark Exp $
Michael Clarkf0d08882007-03-13 08:26:18 +00003 *
Michael Clarkf6a6e482007-03-13 08:26:23 +00004 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
Michael Clarkf0d08882007-03-13 08:26:18 +00005 * Michael Clark <michael@metaparadigm.com>
6 *
Michael Clarkf6a6e482007-03-13 08:26:23 +00007 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the MIT license. See COPYING for details.
Michael Clarkf0d08882007-03-13 08:26:18 +00009 *
10 */
11
12#ifndef _json_tokener_h_
13#define _json_tokener_h_
14
Michael Clarka3272542008-01-26 01:10:47 +000015#include <stddef.h>
Michael Clarkf0d08882007-03-13 08:26:18 +000016#include "json_object.h"
17
Michael Clarkaaec1ef2009-02-25 02:31:32 +000018#ifdef __cplusplus
19extern "C" {
20#endif
21
Michael Clarkf0d08882007-03-13 08:26:18 +000022enum json_tokener_error {
23 json_tokener_success,
Michael Clarka850f8e2007-03-13 08:26:26 +000024 json_tokener_continue,
25 json_tokener_error_depth,
26 json_tokener_error_parse_eof,
Michael Clarkf0d08882007-03-13 08:26:18 +000027 json_tokener_error_parse_unexpected,
28 json_tokener_error_parse_null,
29 json_tokener_error_parse_boolean,
30 json_tokener_error_parse_number,
31 json_tokener_error_parse_array,
Michael Clarka850f8e2007-03-13 08:26:26 +000032 json_tokener_error_parse_object_key_name,
33 json_tokener_error_parse_object_key_sep,
34 json_tokener_error_parse_object_value_sep,
Michael Clarkf0d08882007-03-13 08:26:18 +000035 json_tokener_error_parse_string,
Michael Clarka850f8e2007-03-13 08:26:26 +000036 json_tokener_error_parse_comment
Michael Clarkf0d08882007-03-13 08:26:18 +000037};
38
39enum json_tokener_state {
40 json_tokener_state_eatws,
41 json_tokener_state_start,
42 json_tokener_state_finish,
43 json_tokener_state_null,
44 json_tokener_state_comment_start,
45 json_tokener_state_comment,
46 json_tokener_state_comment_eol,
47 json_tokener_state_comment_end,
48 json_tokener_state_string,
49 json_tokener_state_string_escape,
50 json_tokener_state_escape_unicode,
51 json_tokener_state_boolean,
52 json_tokener_state_number,
53 json_tokener_state_array,
Michael Clarka850f8e2007-03-13 08:26:26 +000054 json_tokener_state_array_add,
Michael Clarkf0d08882007-03-13 08:26:18 +000055 json_tokener_state_array_sep,
Michael Clarkf0d08882007-03-13 08:26:18 +000056 json_tokener_state_object_field_start,
57 json_tokener_state_object_field,
58 json_tokener_state_object_field_end,
59 json_tokener_state_object_value,
Michael Clarka850f8e2007-03-13 08:26:26 +000060 json_tokener_state_object_value_add,
Michael Clarkf6a6e482007-03-13 08:26:23 +000061 json_tokener_state_object_sep
Michael Clarkf0d08882007-03-13 08:26:18 +000062};
63
Michael Clarka850f8e2007-03-13 08:26:26 +000064struct json_tokener_srec
Michael Clarkf0d08882007-03-13 08:26:18 +000065{
Michael Clarka850f8e2007-03-13 08:26:26 +000066 enum json_tokener_state state, saved_state;
67 struct json_object *obj;
68 struct json_object *current;
69 char *obj_field_name;
Michael Clarkf0d08882007-03-13 08:26:18 +000070};
71
Michael Clarka850f8e2007-03-13 08:26:26 +000072#define JSON_TOKENER_MAX_DEPTH 32
73
74struct json_tokener
75{
76 char *str;
77 struct printbuf *pb;
78 int depth, is_double, st_pos, char_offset;
Michael Clarkc8f4a6e2007-12-07 02:44:24 +000079 ptrdiff_t err;
Michael Clarka850f8e2007-03-13 08:26:26 +000080 unsigned int ucs_char;
81 char quote_char;
82 struct json_tokener_srec stack[JSON_TOKENER_MAX_DEPTH];
83};
84
85extern const char* json_tokener_errors[];
86
Michael Clark14862b12007-12-07 02:50:42 +000087extern struct json_tokener* json_tokener_new(void);
Michael Clarka850f8e2007-03-13 08:26:26 +000088extern void json_tokener_free(struct json_tokener *tok);
89extern void json_tokener_reset(struct json_tokener *tok);
Christopher Watfordb1a22ac2009-07-08 04:02:05 +000090extern struct json_object* json_tokener_parse(const char *str);
Michael Clarka850f8e2007-03-13 08:26:26 +000091extern struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
Christopher Watfordb1a22ac2009-07-08 04:02:05 +000092 const char *str, int len);
Michael Clarkf0d08882007-03-13 08:26:18 +000093
Michael Clarkaaec1ef2009-02-25 02:31:32 +000094#ifdef __cplusplus
95}
96#endif
97
Michael Clarkf0d08882007-03-13 08:26:18 +000098#endif