blob: 117d6efc373f26e4022f9b2ef5f29f3983640d23 [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
18enum json_tokener_error {
19 json_tokener_success,
Michael Clarka850f8e2007-03-13 08:26:26 +000020 json_tokener_continue,
21 json_tokener_error_depth,
22 json_tokener_error_parse_eof,
Michael Clarkf0d08882007-03-13 08:26:18 +000023 json_tokener_error_parse_unexpected,
24 json_tokener_error_parse_null,
25 json_tokener_error_parse_boolean,
26 json_tokener_error_parse_number,
27 json_tokener_error_parse_array,
Michael Clarka850f8e2007-03-13 08:26:26 +000028 json_tokener_error_parse_object_key_name,
29 json_tokener_error_parse_object_key_sep,
30 json_tokener_error_parse_object_value_sep,
Michael Clarkf0d08882007-03-13 08:26:18 +000031 json_tokener_error_parse_string,
Michael Clarka850f8e2007-03-13 08:26:26 +000032 json_tokener_error_parse_comment
Michael Clarkf0d08882007-03-13 08:26:18 +000033};
34
35enum json_tokener_state {
36 json_tokener_state_eatws,
37 json_tokener_state_start,
38 json_tokener_state_finish,
39 json_tokener_state_null,
40 json_tokener_state_comment_start,
41 json_tokener_state_comment,
42 json_tokener_state_comment_eol,
43 json_tokener_state_comment_end,
44 json_tokener_state_string,
45 json_tokener_state_string_escape,
46 json_tokener_state_escape_unicode,
47 json_tokener_state_boolean,
48 json_tokener_state_number,
49 json_tokener_state_array,
Michael Clarka850f8e2007-03-13 08:26:26 +000050 json_tokener_state_array_add,
Michael Clarkf0d08882007-03-13 08:26:18 +000051 json_tokener_state_array_sep,
Michael Clarkf0d08882007-03-13 08:26:18 +000052 json_tokener_state_object_field_start,
53 json_tokener_state_object_field,
54 json_tokener_state_object_field_end,
55 json_tokener_state_object_value,
Michael Clarka850f8e2007-03-13 08:26:26 +000056 json_tokener_state_object_value_add,
Michael Clarkf6a6e482007-03-13 08:26:23 +000057 json_tokener_state_object_sep
Michael Clarkf0d08882007-03-13 08:26:18 +000058};
59
Michael Clarka850f8e2007-03-13 08:26:26 +000060struct json_tokener_srec
Michael Clarkf0d08882007-03-13 08:26:18 +000061{
Michael Clarka850f8e2007-03-13 08:26:26 +000062 enum json_tokener_state state, saved_state;
63 struct json_object *obj;
64 struct json_object *current;
65 char *obj_field_name;
Michael Clarkf0d08882007-03-13 08:26:18 +000066};
67
Michael Clarka850f8e2007-03-13 08:26:26 +000068#define JSON_TOKENER_MAX_DEPTH 32
69
70struct json_tokener
71{
72 char *str;
73 struct printbuf *pb;
74 int depth, is_double, st_pos, char_offset;
Michael Clarkc8f4a6e2007-12-07 02:44:24 +000075 ptrdiff_t err;
Michael Clarka850f8e2007-03-13 08:26:26 +000076 unsigned int ucs_char;
77 char quote_char;
78 struct json_tokener_srec stack[JSON_TOKENER_MAX_DEPTH];
79};
80
81extern const char* json_tokener_errors[];
82
Michael Clark14862b12007-12-07 02:50:42 +000083extern struct json_tokener* json_tokener_new(void);
Michael Clarka850f8e2007-03-13 08:26:26 +000084extern void json_tokener_free(struct json_tokener *tok);
85extern void json_tokener_reset(struct json_tokener *tok);
86extern struct json_object* json_tokener_parse(char *str);
87extern struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
88 char *str, int len);
Michael Clarkf0d08882007-03-13 08:26:18 +000089
90#endif