Skip to content

Commit cfa9f8f

Browse files
committed
Add interface to parse JSON into a tree like structure.
While reading back JSON data from files, I found myself fiddling again and again with the scanner-like (think: lex) interface of <yajl/yajl_parse.h>. I think that the best approach to my problem is to write a *parser*, i.e. a piece of code that gets tokens from the scanner and builds a parse tree out of it. So this is what I did. This doesn't play well with parsing streams, naturally, but that's not my use-case and the stream parsing isn't affected by an additional interface. Please keep in mind that this is work in progress. It's currently not even possible to free the returned structure. I just wanted to push it out there in the hope of some early feedback.
1 parent f4baae0 commit cfa9f8f

File tree

3 files changed

+470
-2
lines changed

3 files changed

+470
-2
lines changed

src/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030

3131
SET (SRCS yajl.c yajl_lex.c yajl_parser.c yajl_buf.c
3232
yajl_encode.c yajl_gen.c yajl_alloc.c
33-
yajl_version.c
33+
yajl_tree.c yajl_version.c
3434
)
3535
SET (HDRS yajl_parser.h yajl_lex.h yajl_buf.h yajl_encode.h yajl_alloc.h)
36-
SET (PUB_HDRS api/yajl_parse.h api/yajl_gen.h api/yajl_common.h)
36+
SET (PUB_HDRS api/yajl_parse.h api/yajl_gen.h api/yajl_common.h api/yajl_tree.h)
3737

3838
# useful when fixing lexer bugs.
3939
#ADD_DEFINITIONS(-DYAJL_LEXER_DEBUG)

src/api/yajl_tree.h

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (C) 2010 Florian Forster <ff at octo.it>
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are
6+
* met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in
13+
* the documentation and/or other materials provided with the
14+
* distribution.
15+
*
16+
* 3. Neither the name of Lloyd Hilaiel nor the names of its
17+
* contributors may be used to endorse or promote products derived
18+
* from this software without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21+
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24+
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28+
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29+
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30+
* POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
33+
#ifndef YAJL_TREE_H
34+
#define YAJL_TREE_H 1
35+
36+
#include <stdint.h>
37+
#include <inttypes.h>
38+
39+
#include <yajl/yajl_common.h>
40+
41+
struct yajl_value_s;
42+
typedef struct yajl_value_s yajl_value_t;
43+
44+
struct yajl_value_string_s
45+
{
46+
char *value;
47+
};
48+
typedef struct yajl_value_string_s yajl_value_string_t;
49+
50+
struct yajl_value_number_s
51+
{
52+
char *value;
53+
};
54+
typedef struct yajl_value_number_s yajl_value_number_t;
55+
56+
struct yajl_value_object_s
57+
{
58+
yajl_value_t **keys;
59+
yajl_value_t **values;
60+
size_t children_num;
61+
};
62+
typedef struct yajl_value_object_s yajl_value_object_t;
63+
64+
struct yajl_value_array_s
65+
{
66+
yajl_value_t **children;
67+
size_t children_num;
68+
};
69+
typedef struct yajl_value_array_s yajl_value_array_t;
70+
71+
#define VALUE_TYPE_STRING 1
72+
#define VALUE_TYPE_NUMBER 2
73+
#define VALUE_TYPE_OBJECT 3
74+
#define VALUE_TYPE_ARRAY 4
75+
#define VALUE_TYPE_TRUE 5
76+
#define VALUE_TYPE_FALSE 6
77+
#define VALUE_TYPE_NULL 7
78+
79+
struct yajl_value_s
80+
{
81+
uint8_t type;
82+
union
83+
{
84+
yajl_value_string_t string;
85+
yajl_value_number_t number;
86+
yajl_value_object_t object;
87+
yajl_value_array_t array;
88+
} data;
89+
};
90+
91+
YAJL_API yajl_value_t *yajl_tree_parse (const char *input);
92+
93+
#endif /* YAJL_TREE_H */
94+
/* vim: set sw=2 sts=2 et : */

0 commit comments

Comments
 (0)