yajl-ruby 0.7.4 → 0.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


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

data/ext/yajl_parser.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 "yajl_lex.h"
34
34
  #include "yajl_parser.h"
@@ -48,12 +48,12 @@ unsigned char *
48
48
  yajl_render_error_string(yajl_handle hand, const unsigned char * jsonText,
49
49
  unsigned int jsonTextLen, int verbose)
50
50
  {
51
- unsigned int offset = hand->errorOffset;
51
+ unsigned int offset = hand->bytesConsumed;
52
52
  unsigned char * str;
53
53
  const char * errorType = NULL;
54
54
  const char * errorText = NULL;
55
55
  char text[72];
56
- const char * arrow = " (right here) ------^\n";
56
+ const char * arrow = " (right here) ------^\n";
57
57
 
58
58
  if (yajl_bs_current(hand->stateStack) == yajl_state_parse_error) {
59
59
  errorType = "parse";
@@ -70,18 +70,18 @@ yajl_render_error_string(yajl_handle hand, const unsigned char * jsonText,
70
70
  memneeded += strlen(errorType);
71
71
  memneeded += strlen(" error");
72
72
  if (errorText != NULL) {
73
- memneeded += strlen(": ");
74
- memneeded += strlen(errorText);
73
+ memneeded += strlen(": ");
74
+ memneeded += strlen(errorText);
75
75
  }
76
76
  str = (unsigned char *) YA_MALLOC(&(hand->alloc), memneeded + 2);
77
77
  str[0] = 0;
78
78
  strcat((char *) str, errorType);
79
- strcat((char *) str, " error");
79
+ strcat((char *) str, " error");
80
80
  if (errorText != NULL) {
81
- strcat((char *) str, ": ");
82
- strcat((char *) str, errorText);
81
+ strcat((char *) str, ": ");
82
+ strcat((char *) str, errorText);
83
83
  }
84
- strcat((char *) str, "\n");
84
+ strcat((char *) str, "\n");
85
85
  }
86
86
 
87
87
  /* now we append as many spaces as needed to make sure the error
@@ -93,7 +93,7 @@ yajl_render_error_string(yajl_handle hand, const unsigned char * jsonText,
93
93
  spacesNeeded = (offset < 30 ? 40 - offset : 10);
94
94
  start = (offset >= 30 ? offset - 30 : 0);
95
95
  end = (offset + 30 > jsonTextLen ? jsonTextLen : offset + 30);
96
-
96
+
97
97
  for (i=0;i<spacesNeeded;i++) text[i] = ' ';
98
98
 
99
99
  for (;start < end;start++, i++) {
@@ -117,7 +117,7 @@ yajl_render_error_string(yajl_handle hand, const unsigned char * jsonText,
117
117
  newStr[0] = 0;
118
118
  strcat((char *) newStr, (char *) str);
119
119
  strcat((char *) newStr, text);
120
- strcat((char *) newStr, arrow);
120
+ strcat((char *) newStr, arrow);
121
121
  YA_FREE(&(hand->alloc), str);
122
122
  str = (unsigned char *) newStr;
123
123
  }
@@ -136,20 +136,23 @@ yajl_render_error_string(yajl_handle hand, const unsigned char * jsonText,
136
136
 
137
137
 
138
138
  yajl_status
139
- yajl_do_parse(yajl_handle hand, unsigned int * offset,
140
- const unsigned char * jsonText, unsigned int jsonTextLen)
139
+ yajl_do_parse(yajl_handle hand, const unsigned char * jsonText,
140
+ unsigned int jsonTextLen)
141
141
  {
142
142
  yajl_tok tok;
143
143
  const unsigned char * buf;
144
144
  unsigned int bufLen;
145
+ unsigned int * offset = &(hand->bytesConsumed);
146
+
147
+ *offset = 0;
148
+
145
149
 
146
150
  around_again:
147
151
  switch (yajl_bs_current(hand->stateStack)) {
148
152
  case yajl_state_parse_complete:
149
153
  return yajl_status_ok;
150
154
  case yajl_state_lexical_error:
151
- case yajl_state_parse_error:
152
- hand->errorOffset = *offset;
155
+ case yajl_state_parse_error:
153
156
  return yajl_status_error;
154
157
  case yajl_state_start:
155
158
  case yajl_state_map_need_val:
@@ -187,13 +190,13 @@ yajl_do_parse(yajl_handle hand, unsigned int * offset,
187
190
  yajl_buf_len(hand->decodeBuf)));
188
191
  }
189
192
  break;
190
- case yajl_tok_bool:
193
+ case yajl_tok_bool:
191
194
  if (hand->callbacks && hand->callbacks->yajl_boolean) {
192
195
  _CC_CHK(hand->callbacks->yajl_boolean(hand->ctx,
193
196
  *buf == 't'));
194
197
  }
195
198
  break;
196
- case yajl_tok_null:
199
+ case yajl_tok_null:
197
200
  if (hand->callbacks && hand->callbacks->yajl_null) {
198
201
  _CC_CHK(hand->callbacks->yajl_null(hand->ctx));
199
202
  }
@@ -287,13 +290,13 @@ yajl_do_parse(yajl_handle hand, unsigned int * offset,
287
290
  _CC_CHK(hand->callbacks->yajl_end_array(hand->ctx));
288
291
  }
289
292
  yajl_bs_pop(hand->stateStack);
290
- goto around_again;
293
+ goto around_again;
291
294
  }
292
295
  /* intentional fall-through */
293
296
  }
294
- case yajl_tok_colon:
295
- case yajl_tok_comma:
296
- case yajl_tok_right_bracket:
297
+ case yajl_tok_colon:
298
+ case yajl_tok_comma:
299
+ case yajl_tok_right_bracket:
297
300
  yajl_bs_set(hand->stateStack, yajl_state_parse_error);
298
301
  hand->parseError =
299
302
  "unallowed token at this point in JSON text";
@@ -312,7 +315,7 @@ yajl_do_parse(yajl_handle hand, unsigned int * offset,
312
315
  yajl_reset_parser(hand);
313
316
  } else if (s == yajl_state_map_need_val) {
314
317
  yajl_bs_set(hand->stateStack, yajl_state_map_got_val);
315
- } else {
318
+ } else {
316
319
  yajl_bs_set(hand->stateStack, yajl_state_array_got_val);
317
320
  }
318
321
  }
@@ -322,7 +325,7 @@ yajl_do_parse(yajl_handle hand, unsigned int * offset,
322
325
 
323
326
  goto around_again;
324
327
  }
325
- case yajl_state_map_start:
328
+ case yajl_state_map_start:
326
329
  case yajl_state_map_need_key: {
327
330
  /* only difference between these two states is that in
328
331
  * start '}' is valid, whereas in need_key, we've parsed
@@ -358,12 +361,12 @@ yajl_do_parse(yajl_handle hand, unsigned int * offset,
358
361
  _CC_CHK(hand->callbacks->yajl_end_map(hand->ctx));
359
362
  }
360
363
  yajl_bs_pop(hand->stateStack);
361
- goto around_again;
364
+ goto around_again;
362
365
  }
363
366
  default:
364
367
  yajl_bs_set(hand->stateStack, yajl_state_parse_error);
365
368
  hand->parseError =
366
- "invalid object key (must be a string)";
369
+ "invalid object key (must be a string)";
367
370
  goto around_again;
368
371
  }
369
372
  }
@@ -373,7 +376,7 @@ yajl_do_parse(yajl_handle hand, unsigned int * offset,
373
376
  switch (tok) {
374
377
  case yajl_tok_colon:
375
378
  yajl_bs_set(hand->stateStack, yajl_state_map_need_val);
376
- goto around_again;
379
+ goto around_again;
377
380
  case yajl_tok_eof:
378
381
  return yajl_status_insufficient_data;
379
382
  case yajl_tok_error:
@@ -395,10 +398,10 @@ yajl_do_parse(yajl_handle hand, unsigned int * offset,
395
398
  _CC_CHK(hand->callbacks->yajl_end_map(hand->ctx));
396
399
  }
397
400
  yajl_bs_pop(hand->stateStack);
398
- goto around_again;
401
+ goto around_again;
399
402
  case yajl_tok_comma:
400
403
  yajl_bs_set(hand->stateStack, yajl_state_map_need_key);
401
- goto around_again;
404
+ goto around_again;
402
405
  case yajl_tok_eof:
403
406
  return yajl_status_insufficient_data;
404
407
  case yajl_tok_error:
@@ -406,8 +409,8 @@ yajl_do_parse(yajl_handle hand, unsigned int * offset,
406
409
  goto around_again;
407
410
  default:
408
411
  yajl_bs_set(hand->stateStack, yajl_state_parse_error);
409
- hand->parseError = "after key and value, inside map, "
410
- "I expect ',' or '}'";
412
+ hand->parseError = "after key and value, inside map, "
413
+ "I expect ',' or '}'";
411
414
  /* try to restore error offset */
412
415
  if (*offset >= bufLen) *offset -= bufLen;
413
416
  else *offset = 0;
@@ -423,10 +426,10 @@ yajl_do_parse(yajl_handle hand, unsigned int * offset,
423
426
  _CC_CHK(hand->callbacks->yajl_end_array(hand->ctx));
424
427
  }
425
428
  yajl_bs_pop(hand->stateStack);
426
- goto around_again;
429
+ goto around_again;
427
430
  case yajl_tok_comma:
428
431
  yajl_bs_set(hand->stateStack, yajl_state_array_need_val);
429
- goto around_again;
432
+ goto around_again;
430
433
  case yajl_tok_eof:
431
434
  return yajl_status_insufficient_data;
432
435
  case yajl_tok_error:
@@ -440,7 +443,7 @@ yajl_do_parse(yajl_handle hand, unsigned int * offset,
440
443
  }
441
444
  }
442
445
  }
443
-
446
+
444
447
  abort();
445
448
  return yajl_status_error;
446
449
  }
data/ext/yajl_parser.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
  #ifndef __YAJL_PARSER_H__
34
34
  #define __YAJL_PARSER_H__
@@ -44,7 +44,7 @@ typedef enum {
44
44
  yajl_state_parse_error,
45
45
  yajl_state_lexical_error,
46
46
  yajl_state_map_start,
47
- yajl_state_map_sep,
47
+ yajl_state_map_sep,
48
48
  yajl_state_map_need_val,
49
49
  yajl_state_map_got_val,
50
50
  yajl_state_map_need_key,
@@ -58,7 +58,10 @@ struct yajl_handle_t {
58
58
  void * ctx;
59
59
  yajl_lexer lexer;
60
60
  const char * parseError;
61
- unsigned int errorOffset;
61
+ /* the number of bytes consumed from the last client buffer,
62
+ * in the case of an error this will be an error offset, in the
63
+ * case of an error this can be used as the error offset */
64
+ unsigned int bytesConsumed;
62
65
  /* temporary storage for decoded strings */
63
66
  yajl_buf decodeBuf;
64
67
  /* a stack of states. access with yajl_state_XXX routines */
@@ -68,8 +71,8 @@ struct yajl_handle_t {
68
71
  };
69
72
 
70
73
  yajl_status
71
- yajl_do_parse(yajl_handle handle, unsigned int * offset,
72
- const unsigned char * jsonText, unsigned int jsonTextLen);
74
+ yajl_do_parse(yajl_handle handle, const unsigned char * jsonText,
75
+ unsigned int jsonTextLen);
73
76
 
74
77
  unsigned char *
75
78
  yajl_render_error_string(yajl_handle hand, const unsigned char * jsonText,
data/lib/yajl.rb CHANGED
@@ -13,7 +13,7 @@ require 'yajl_ext'
13
13
  #
14
14
  # Ruby bindings to the excellent Yajl (Yet Another JSON Parser) ANSI C library.
15
15
  module Yajl
16
- VERSION = "0.7.4"
16
+ VERSION = "0.7.5"
17
17
 
18
18
  # For compatibility, has the same signature of Yajl::Parser.parse
19
19
  def self.load(str_or_io, options={}, read_bufsize=nil, &block)
@@ -4,6 +4,19 @@ module Yajl
4
4
  # This is a wrapper around Bzip::Reader to allow it's #read method to adhere
5
5
  # to the IO spec, allowing for two parameters (length, and buffer)
6
6
  class StreamReader < ::Bzip2::Reader
7
+ # A helper method to allow use similar to IO#read
8
+ def read(len=nil, buffer=nil)
9
+ if val = super(len)
10
+ unless buffer.nil?
11
+ buffer.replace(val)
12
+ return buffer
13
+ end
14
+ super(len)
15
+ else
16
+ nil
17
+ end
18
+ end
19
+
7
20
  # Helper method for one-off parsing from a bzip2-compressed stream
8
21
  #
9
22
  # See Yajl::Parser#parse for parameter documentation
@@ -12,9 +12,16 @@ module Yajl
12
12
  end
13
13
 
14
14
  # A helper method to allow use similar to IO#read
15
- def read(len=nil)
16
- return nil if finished? or ended?
17
- inflate(@io.read(len))
15
+ def read(len=nil, buffer=nil)
16
+ if val = @io.read(len)
17
+ unless buffer.nil?
18
+ buffer.replace(inflate(val))
19
+ return buffer
20
+ end
21
+ inflate(@io.read(len))
22
+ else
23
+ nil
24
+ end
18
25
  end
19
26
 
20
27
  # Helper method for one-off parsing from a deflate-compressed stream
@@ -4,6 +4,19 @@ module Yajl
4
4
  # This is a wrapper around Zlib::GzipReader to allow it's #read method to adhere
5
5
  # to the IO spec, allowing for two parameters (length, and buffer)
6
6
  class StreamReader < ::Zlib::GzipReader
7
+ # A helper method to allow use similar to IO#read
8
+ def read(len=nil, buffer=nil)
9
+ if val = super(len)
10
+ unless buffer.nil?
11
+ buffer.replace(val)
12
+ return buffer
13
+ end
14
+ super(len)
15
+ else
16
+ nil
17
+ end
18
+ end
19
+
7
20
  # Helper method for one-off parsing from a gzip-compressed stream
8
21
  #
9
22
  # See Yajl::Parser#parse for parameter documentation
@@ -3,7 +3,13 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
3
3
 
4
4
  class Dummy2
5
5
  def to_json
6
- "hawtness".dump
6
+ "{\"hawtness\":true}"
7
+ end
8
+ end
9
+
10
+ class Dummy3
11
+ def as_json
12
+ {:hawtness => true}
7
13
  end
8
14
  end
9
15
 
@@ -152,7 +158,12 @@ describe "Yajl JSON encoder" do
152
158
 
153
159
  it "should check for and call #to_json if it exists on custom objects" do
154
160
  d = Dummy2.new
155
- Yajl::Encoder.encode({:foo => d}).should eql('{"foo":"hawtness"}')
161
+ Yajl::Encoder.encode({:foo => d}).should eql('{"foo":{"hawtness":true}}')
162
+ end
163
+
164
+ it "should check for and call #as_json if it exists on custom objects" do
165
+ d = Dummy3.new
166
+ Yajl::Encoder.encode(d).should eql('{"hawtness":true}')
156
167
  end
157
168
 
158
169
  it "should encode a hash where the key and value can be symbols" do
data/spec/spec_helper.rb CHANGED
@@ -4,3 +4,4 @@ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
4
4
  require 'rubygems'
5
5
  require 'yajl'
6
6
  require 'active_support/core_ext/kernel/reporting'
7
+ require 'date'
data/yajl-ruby.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{yajl-ruby}
8
- s.version = "0.7.4"
8
+ s.version = "0.7.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Brian Lopez", "Lloyd Hilaiel"]
12
- s.date = %q{2010-03-03}
12
+ s.date = %q{2010-03-23}
13
13
  s.email = %q{seniorlopez@gmail.com}
14
14
  s.extensions = ["ext/extconf.rb"]
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 7
8
- - 4
9
- version: 0.7.4
8
+ - 5
9
+ version: 0.7.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Brian Lopez
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-03-03 00:00:00 -08:00
18
+ date: 2010-03-23 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies: []
21
21