Skip to content

Commit

Permalink
Add interface to parse JSON into a tree like structure.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
octo committed Aug 5, 2010
1 parent f4baae0 commit cfa9f8f
Show file tree
Hide file tree
Showing 3 changed files with 470 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Expand Up @@ -30,10 +30,10 @@

SET (SRCS yajl.c yajl_lex.c yajl_parser.c yajl_buf.c
yajl_encode.c yajl_gen.c yajl_alloc.c
yajl_version.c
yajl_tree.c yajl_version.c
)
SET (HDRS yajl_parser.h yajl_lex.h yajl_buf.h yajl_encode.h yajl_alloc.h)
SET (PUB_HDRS api/yajl_parse.h api/yajl_gen.h api/yajl_common.h)
SET (PUB_HDRS api/yajl_parse.h api/yajl_gen.h api/yajl_common.h api/yajl_tree.h)

# useful when fixing lexer bugs.
#ADD_DEFINITIONS(-DYAJL_LEXER_DEBUG)
Expand Down
94 changes: 94 additions & 0 deletions src/api/yajl_tree.h
@@ -0,0 +1,94 @@
/*
* Copyright (C) 2010 Florian Forster <ff at octo.it>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. Neither the name of Lloyd Hilaiel nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef YAJL_TREE_H
#define YAJL_TREE_H 1

#include <stdint.h>
#include <inttypes.h>

#include <yajl/yajl_common.h>

struct yajl_value_s;
typedef struct yajl_value_s yajl_value_t;

struct yajl_value_string_s
{
char *value;
};
typedef struct yajl_value_string_s yajl_value_string_t;

struct yajl_value_number_s
{
char *value;
};
typedef struct yajl_value_number_s yajl_value_number_t;

struct yajl_value_object_s
{
yajl_value_t **keys;
yajl_value_t **values;
size_t children_num;
};
typedef struct yajl_value_object_s yajl_value_object_t;

struct yajl_value_array_s
{
yajl_value_t **children;
size_t children_num;
};
typedef struct yajl_value_array_s yajl_value_array_t;

#define VALUE_TYPE_STRING 1
#define VALUE_TYPE_NUMBER 2
#define VALUE_TYPE_OBJECT 3
#define VALUE_TYPE_ARRAY 4
#define VALUE_TYPE_TRUE 5
#define VALUE_TYPE_FALSE 6
#define VALUE_TYPE_NULL 7

struct yajl_value_s
{
uint8_t type;
union
{
yajl_value_string_t string;
yajl_value_number_t number;
yajl_value_object_t object;
yajl_value_array_t array;
} data;
};

YAJL_API yajl_value_t *yajl_tree_parse (const char *input);

#endif /* YAJL_TREE_H */
/* vim: set sw=2 sts=2 et : */

0 comments on commit cfa9f8f

Please sign in to comment.