yajl-ruby 0.7.4 → 0.7.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of yajl-ruby might be problematic. Click here for more details.

data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.7.5 (March 23rd, 2010)
4
+ * check for existence of and use as_json method on custom objects
5
+ * bugfix with read buffer when parsing from an IO (thanks to Pavel Valodzka <pavel@valodzka.name>)
6
+ * merged in latest yajl
7
+ * enable -Wextra during compilation
8
+ * brought back ability to pass a buffer to bzip/gzip/deflate #read helper methods
9
+
3
10
  ## 0.7.4 (March 3rd, 2010)
4
11
  * bugfix for the JSON gem compatibility API's Object#to_json method improperly encoding strings
5
12
 
data/VERSION.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  ---
2
- :major: 0
3
2
  :minor: 7
3
+ :patch: 5
4
4
  :build:
5
- :patch: 4
5
+ :major: 0
data/benchmark/encode.rb CHANGED
@@ -9,8 +9,12 @@ rescue LoadError
9
9
  end
10
10
  # Can't use ActiveSuport::JSON.encode with the JSON gem loaded
11
11
  # begin
12
- # require 'active_support'
12
+ # require 'active_support/json'
13
13
  # rescue LoadError
14
+ # begin
15
+ # require 'active_support'
16
+ # rescue LoadError
17
+ # end
14
18
  # end
15
19
 
16
20
  filename = ARGV[0] || 'benchmark/subjects/ohai.json'
data/benchmark/parse.rb CHANGED
@@ -7,8 +7,12 @@ begin
7
7
  rescue LoadError
8
8
  end
9
9
  begin
10
- require 'active_support'
10
+ require 'active_support/json'
11
11
  rescue LoadError
12
+ begin
13
+ require 'active_support'
14
+ rescue LoadError
15
+ end
12
16
  end
13
17
 
14
18
  filename = ARGV[0] || 'benchmark/subjects/twitter_search.json'
@@ -7,8 +7,12 @@ begin
7
7
  rescue LoadError
8
8
  end
9
9
  begin
10
- require 'active_support'
10
+ require 'active_support/json'
11
11
  rescue LoadError
12
+ begin
13
+ require 'active_support'
14
+ rescue LoadError
15
+ end
12
16
  end
13
17
 
14
18
  filename = 'benchmark/subjects/twitter_stream.json'
@@ -1,22 +1,22 @@
1
1
  /*
2
- * Copyright 2007-2009, Lloyd Hilaiel.
3
- *
2
+ * Copyright 2010, Lloyd Hilaiel.
3
+ *
4
4
  * Redistribution and use in source and binary forms, with or without
5
5
  * modification, are permitted provided that the following conditions are
6
6
  * met:
7
- *
7
+ *
8
8
  * 1. Redistributions of source code must retain the above copyright
9
9
  * notice, this list of conditions and the following disclaimer.
10
- *
10
+ *
11
11
  * 2. Redistributions in binary form must reproduce the above copyright
12
12
  * notice, this list of conditions and the following disclaimer in
13
13
  * the documentation and/or other materials provided with the
14
14
  * distribution.
15
- *
15
+ *
16
16
  * 3. Neither the name of Lloyd Hilaiel nor the names of its
17
17
  * contributors may be used to endorse or promote products derived
18
18
  * from this software without specific prior written permission.
19
- *
19
+ *
20
20
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21
21
  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22
22
  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@@ -28,14 +28,14 @@
28
28
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29
29
  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
30
  * POSSIBILITY OF SUCH DAMAGE.
31
- */
31
+ */
32
32
 
33
33
  #ifndef __YAJL_COMMON_H__
34
34
  #define __YAJL_COMMON_H__
35
35
 
36
36
  #ifdef __cplusplus
37
37
  extern "C" {
38
- #endif
38
+ #endif
39
39
 
40
40
  #define YAJL_MAX_DEPTH 128
41
41
 
@@ -50,7 +50,7 @@ extern "C" {
50
50
  # endif
51
51
  #else
52
52
  # define YAJL_API
53
- #endif
53
+ #endif
54
54
 
55
55
  /** pointer to a malloc function, supporting client overriding memory
56
56
  * allocation routines */
@@ -80,6 +80,6 @@ typedef struct
80
80
 
81
81
  #ifdef __cplusplus
82
82
  }
83
- #endif
83
+ #endif
84
84
 
85
85
  #endif
data/ext/api/yajl_gen.h CHANGED
@@ -1,22 +1,22 @@
1
1
  /*
2
- * Copyright 2007-2009, Lloyd Hilaiel.
3
- *
2
+ * Copyright 2010, Lloyd Hilaiel.
3
+ *
4
4
  * Redistribution and use in source and binary forms, with or without
5
5
  * modification, are permitted provided that the following conditions are
6
6
  * met:
7
- *
7
+ *
8
8
  * 1. Redistributions of source code must retain the above copyright
9
9
  * notice, this list of conditions and the following disclaimer.
10
- *
10
+ *
11
11
  * 2. Redistributions in binary form must reproduce the above copyright
12
12
  * notice, this list of conditions and the following disclaimer in
13
13
  * the documentation and/or other materials provided with the
14
14
  * distribution.
15
- *
15
+ *
16
16
  * 3. Neither the name of Lloyd Hilaiel nor the names of its
17
17
  * contributors may be used to endorse or promote products derived
18
18
  * from this software without specific prior written permission.
19
- *
19
+ *
20
20
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21
21
  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22
22
  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@@ -28,7 +28,7 @@
28
28
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29
29
  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
30
  * POSSIBILITY OF SUCH DAMAGE.
31
- */
31
+ */
32
32
 
33
33
  /**
34
34
  * \file yajl_gen.h
@@ -42,7 +42,7 @@
42
42
 
43
43
  #ifdef __cplusplus
44
44
  extern "C" {
45
- #endif
45
+ #endif
46
46
  /** generator status codes */
47
47
  typedef enum {
48
48
  /** no error */
@@ -57,7 +57,7 @@ extern "C" {
57
57
  * state */
58
58
  yajl_gen_in_error_state,
59
59
  /** A complete JSON document has been generated */
60
- yajl_gen_generation_complete,
60
+ yajl_gen_generation_complete,
61
61
  /** yajl_gen_double was passed an invalid floating point value
62
62
  * (infinity or NaN). */
63
63
  yajl_gen_invalid_number,
@@ -94,7 +94,7 @@ extern "C" {
94
94
  *
95
95
  * \returns an allocated handle on success, NULL on failure (bad params)
96
96
  */
97
- yajl_gen YAJL_API yajl_gen_alloc(const yajl_gen_config * config,
97
+ YAJL_API yajl_gen yajl_gen_alloc(const yajl_gen_config * config,
98
98
  const yajl_alloc_funcs * allocFuncs);
99
99
 
100
100
  /** allocate a generator handle that will print to the specified
@@ -114,46 +114,46 @@ extern "C" {
114
114
  *
115
115
  * \returns an allocated handle on success, NULL on failure (bad params)
116
116
  */
117
- yajl_gen YAJL_API yajl_gen_alloc2(yajl_print_t callback,
117
+ YAJL_API yajl_gen yajl_gen_alloc2(const yajl_print_t callback,
118
118
  const yajl_gen_config * config,
119
119
  const yajl_alloc_funcs * allocFuncs,
120
120
  void * ctx);
121
121
 
122
- /** free a generator handle */
123
- void YAJL_API yajl_gen_free(yajl_gen handle);
122
+ /** free a generator handle */
123
+ YAJL_API void yajl_gen_free(yajl_gen handle);
124
124
 
125
- yajl_gen_status YAJL_API yajl_gen_integer(yajl_gen hand, long int number);
125
+ YAJL_API yajl_gen_status yajl_gen_integer(yajl_gen hand, long int number);
126
126
  /** generate a floating point number. number may not be infinity or
127
127
  * NaN, as these have no representation in JSON. In these cases the
128
128
  * generator will return 'yajl_gen_invalid_number' */
129
- yajl_gen_status YAJL_API yajl_gen_double(yajl_gen hand, double number);
130
- yajl_gen_status YAJL_API yajl_gen_number(yajl_gen hand,
129
+ YAJL_API yajl_gen_status yajl_gen_double(yajl_gen hand, double number);
130
+ YAJL_API yajl_gen_status yajl_gen_number(yajl_gen hand,
131
131
  const char * num,
132
132
  unsigned int len);
133
- yajl_gen_status YAJL_API yajl_gen_string(yajl_gen hand,
133
+ YAJL_API yajl_gen_status yajl_gen_string(yajl_gen hand,
134
134
  const unsigned char * str,
135
135
  unsigned int len);
136
- yajl_gen_status YAJL_API yajl_gen_null(yajl_gen hand);
137
- yajl_gen_status YAJL_API yajl_gen_bool(yajl_gen hand, int boolean);
138
- yajl_gen_status YAJL_API yajl_gen_map_open(yajl_gen hand);
139
- yajl_gen_status YAJL_API yajl_gen_map_close(yajl_gen hand);
140
- yajl_gen_status YAJL_API yajl_gen_array_open(yajl_gen hand);
141
- yajl_gen_status YAJL_API yajl_gen_array_close(yajl_gen hand);
136
+ YAJL_API yajl_gen_status yajl_gen_null(yajl_gen hand);
137
+ YAJL_API yajl_gen_status yajl_gen_bool(yajl_gen hand, int boolean);
138
+ YAJL_API yajl_gen_status yajl_gen_map_open(yajl_gen hand);
139
+ YAJL_API yajl_gen_status yajl_gen_map_close(yajl_gen hand);
140
+ YAJL_API yajl_gen_status yajl_gen_array_open(yajl_gen hand);
141
+ YAJL_API yajl_gen_status yajl_gen_array_close(yajl_gen hand);
142
142
 
143
143
  /** access the null terminated generator buffer. If incrementally
144
144
  * outputing JSON, one should call yajl_gen_clear to clear the
145
145
  * buffer. This allows stream generation. */
146
- yajl_gen_status YAJL_API yajl_gen_get_buf(yajl_gen hand,
146
+ YAJL_API yajl_gen_status yajl_gen_get_buf(yajl_gen hand,
147
147
  const unsigned char ** buf,
148
148
  unsigned int * len);
149
149
 
150
150
  /** clear yajl's output buffer, but maintain all internal generation
151
151
  * state. This function will not "reset" the generator state, and is
152
152
  * intended to enable incremental JSON outputing. */
153
- void YAJL_API yajl_gen_clear(yajl_gen hand);
153
+ YAJL_API void yajl_gen_clear(yajl_gen hand);
154
154
 
155
155
  #ifdef __cplusplus
156
156
  }
157
- #endif
157
+ #endif
158
158
 
159
159
  #endif
data/ext/api/yajl_parse.h CHANGED
@@ -1,22 +1,22 @@
1
1
  /*
2
- * Copyright 2007-2009, Lloyd Hilaiel.
3
- *
2
+ * Copyright 2010, Lloyd Hilaiel.
3
+ *
4
4
  * Redistribution and use in source and binary forms, with or without
5
5
  * modification, are permitted provided that the following conditions are
6
6
  * met:
7
- *
7
+ *
8
8
  * 1. Redistributions of source code must retain the above copyright
9
9
  * notice, this list of conditions and the following disclaimer.
10
- *
10
+ *
11
11
  * 2. Redistributions in binary form must reproduce the above copyright
12
12
  * notice, this list of conditions and the following disclaimer in
13
13
  * the documentation and/or other materials provided with the
14
14
  * distribution.
15
- *
15
+ *
16
16
  * 3. Neither the name of Lloyd Hilaiel nor the names of its
17
17
  * contributors may be used to endorse or promote products derived
18
18
  * from this software without specific prior written permission.
19
- *
19
+ *
20
20
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21
21
  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22
22
  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@@ -28,7 +28,7 @@
28
28
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29
29
  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
30
  * POSSIBILITY OF SUCH DAMAGE.
31
- */
31
+ */
32
32
 
33
33
  /**
34
34
  * \file yajl_parse.h
@@ -42,7 +42,7 @@
42
42
 
43
43
  #ifdef __cplusplus
44
44
  extern "C" {
45
- #endif
45
+ #endif
46
46
  /** error codes returned from this interface */
47
47
  typedef enum {
48
48
  /** no error was encountered */
@@ -59,7 +59,7 @@ extern "C" {
59
59
  } yajl_status;
60
60
 
61
61
  /** attain a human readable, english, string for an error */
62
- const char * YAJL_API yajl_status_to_string(yajl_status code);
62
+ YAJL_API const char * yajl_status_to_string(yajl_status code);
63
63
 
64
64
  /** an opaque handle to a parser */
65
65
  typedef struct yajl_handle_t * yajl_handle;
@@ -103,12 +103,12 @@ extern "C" {
103
103
  int (* yajl_start_map)(void * ctx);
104
104
  int (* yajl_map_key)(void * ctx, const unsigned char * key,
105
105
  unsigned int stringLen);
106
- int (* yajl_end_map)(void * ctx);
106
+ int (* yajl_end_map)(void * ctx);
107
107
 
108
108
  int (* yajl_start_array)(void * ctx);
109
- int (* yajl_end_array)(void * ctx);
109
+ int (* yajl_end_array)(void * ctx);
110
110
  } yajl_callbacks;
111
-
111
+
112
112
  /** configuration structure for the generator */
113
113
  typedef struct {
114
114
  /** if nonzero, javascript style comments will be allowed in
@@ -127,7 +127,7 @@ extern "C" {
127
127
  * \param config configuration parameters for the parse.
128
128
  * \param ctx a context pointer that will be passed to callbacks.
129
129
  */
130
- yajl_handle YAJL_API yajl_alloc(const yajl_callbacks * callbacks,
130
+ YAJL_API yajl_handle yajl_alloc(const yajl_callbacks * callbacks,
131
131
  const yajl_parser_config * config,
132
132
  const yajl_alloc_funcs * allocFuncs,
133
133
  void * ctx);
@@ -143,7 +143,7 @@ extern "C" {
143
143
  * \param jsonText - a pointer to the UTF8 json text to be parsed
144
144
  * \param jsonTextLength - the length, in bytes, of input text
145
145
  */
146
- yajl_status YAJL_API yajl_parse(yajl_handle hand,
146
+ YAJL_API yajl_status yajl_parse(yajl_handle hand,
147
147
  const unsigned char * jsonText,
148
148
  unsigned int jsonTextLength);
149
149
 
@@ -156,8 +156,8 @@ extern "C" {
156
156
  *
157
157
  * \param hand - a handle to the json parser allocated with yajl_alloc
158
158
  */
159
- yajl_status yajl_parse_complete(yajl_handle hand);
160
-
159
+ YAJL_API yajl_status yajl_parse_complete(yajl_handle hand);
160
+
161
161
  /** get an error string describing the state of the
162
162
  * parse.
163
163
  *
@@ -165,18 +165,32 @@ extern "C" {
165
165
  * text where the error occured, along with an arrow pointing to
166
166
  * the specific char.
167
167
  *
168
- * A dynamically allocated string will be returned which should
169
- * be freed with yajl_free_error
168
+ * \returns A dynamically allocated string will be returned which should
169
+ * be freed with yajl_free_error
170
170
  */
171
- unsigned char * YAJL_API yajl_get_error(yajl_handle hand, int verbose,
171
+ YAJL_API unsigned char * yajl_get_error(yajl_handle hand, int verbose,
172
172
  const unsigned char * jsonText,
173
173
  unsigned int jsonTextLength);
174
174
 
175
+ /**
176
+ * get the amount of data consumed from the last chunk passed to YAJL.
177
+ *
178
+ * In the case of a successful parse this can help you understand if
179
+ * the entire buffer was consumed (which will allow you to handle
180
+ * "junk at end of input".
181
+ *
182
+ * In the event an error is encountered during parsing, this function
183
+ * affords the client a way to get the offset into the most recent
184
+ * chunk where the error occured. 0 will be returned if no error
185
+ * was encountered.
186
+ */
187
+ YAJL_API unsigned int yajl_get_bytes_consumed(yajl_handle hand);
188
+
175
189
  /** free an error returned from yajl_get_error */
176
- void YAJL_API yajl_free_error(yajl_handle hand, unsigned char * str);
190
+ YAJL_API void yajl_free_error(yajl_handle hand, unsigned char * str);
177
191
 
178
192
  #ifdef __cplusplus
179
193
  }
180
- #endif
194
+ #endif
181
195
 
182
196
  #endif
data/ext/extconf.rb CHANGED
@@ -2,7 +2,7 @@
2
2
  require 'mkmf'
3
3
  require 'rbconfig'
4
4
 
5
- $CFLAGS << ' -Wall -funroll-loops'
5
+ $CFLAGS << ' -Wall -Wextra -funroll-loops'
6
6
  # $CFLAGS << ' -O0 -ggdb'
7
7
 
8
8
  create_makefile("yajl_ext")
data/ext/yajl.c CHANGED
@@ -1,22 +1,22 @@
1
1
  /*
2
- * Copyright 2007-2009, Lloyd Hilaiel.
3
- *
2
+ * Copyright 2010, Lloyd Hilaiel.
3
+ *
4
4
  * Redistribution and use in source and binary forms, with or without
5
5
  * modification, are permitted provided that the following conditions are
6
6
  * met:
7
- *
7
+ *
8
8
  * 1. Redistributions of source code must retain the above copyright
9
9
  * notice, this list of conditions and the following disclaimer.
10
- *
10
+ *
11
11
  * 2. Redistributions in binary form must reproduce the above copyright
12
12
  * notice, this list of conditions and the following disclaimer in
13
13
  * the documentation and/or other materials provided with the
14
14
  * distribution.
15
- *
15
+ *
16
16
  * 3. Neither the name of Lloyd Hilaiel nor the names of its
17
17
  * contributors may be used to endorse or promote products derived
18
18
  * from this software without specific prior written permission.
19
- *
19
+ *
20
20
  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21
21
  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22
22
  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@@ -28,7 +28,7 @@
28
28
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29
29
  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
30
  * POSSIBILITY OF SUCH DAMAGE.
31
- */
31
+ */
32
32
 
33
33
  #include "api/yajl_parse.h"
34
34
  #include "yajl_lex.h"
@@ -70,7 +70,7 @@ yajl_alloc(const yajl_callbacks * callbacks,
70
70
  unsigned int validateUTF8 = 0;
71
71
  yajl_handle hand = NULL;
72
72
  yajl_alloc_funcs afsBuffer;
73
-
73
+
74
74
  /* first order of business is to set up memory allocation routines */
75
75
  if (afs != NULL) {
76
76
  if (afs->malloc == NULL || afs->realloc == NULL || afs->free == NULL)
@@ -95,11 +95,11 @@ yajl_alloc(const yajl_callbacks * callbacks,
95
95
  hand->callbacks = callbacks;
96
96
  hand->ctx = ctx;
97
97
  hand->lexer = yajl_lex_alloc(&(hand->alloc), allowComments, validateUTF8);
98
- hand->errorOffset = 0;
98
+ hand->bytesConsumed = 0;
99
99
  hand->decodeBuf = yajl_buf_alloc(&(hand->alloc));
100
100
  yajl_bs_init(hand->stateStack, &(hand->alloc));
101
101
 
102
- yajl_bs_push(hand->stateStack, yajl_state_start);
102
+ yajl_bs_push(hand->stateStack, yajl_state_start);
103
103
 
104
104
  return hand;
105
105
  }
@@ -122,9 +122,8 @@ yajl_status
122
122
  yajl_parse(yajl_handle hand, const unsigned char * jsonText,
123
123
  unsigned int jsonTextLen)
124
124
  {
125
- unsigned int offset = 0;
126
125
  yajl_status status;
127
- status = yajl_do_parse(hand, &offset, jsonText, jsonTextLen);
126
+ status = yajl_do_parse(hand, jsonText, jsonTextLen);
128
127
  return status;
129
128
  }
130
129
 
@@ -147,6 +146,14 @@ yajl_get_error(yajl_handle hand, int verbose,
147
146
  return yajl_render_error_string(hand, jsonText, jsonTextLen, verbose);
148
147
  }
149
148
 
149
+ unsigned int
150
+ yajl_get_bytes_consumed(yajl_handle hand)
151
+ {
152
+ if (!hand) return 0;
153
+ else return hand->bytesConsumed;
154
+ }
155
+
156
+
150
157
  void
151
158
  yajl_free_error(yajl_handle hand, unsigned char * str)
152
159
  {