jsonbuilder 0.1.0 → 0.1.2

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.
@@ -53,7 +53,6 @@ module Builder
53
53
  @path.push(key)
54
54
  else
55
55
  if _target.is_a?(String)
56
- puts _current
57
56
  eval("#{_current} = _target")
58
57
  else
59
58
  eval("#{_current} ||= {}")
@@ -1,4 +1,5 @@
1
1
  require 'activesupport'
2
+ require 'patch/active_support_json_decode'
2
3
 
3
4
  module Builder
4
5
  class JsonFormat < HashStructure
data/lib/jsonbuilder.rb CHANGED
@@ -2,7 +2,7 @@ module JsonBuilder
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- REVISION = 0
5
+ REVISION = 2
6
6
  class << self
7
7
  def to_version
8
8
  "#{MAJOR}.#{MINOR}.#{REVISION}"
@@ -0,0 +1,85 @@
1
+ # Copy of rails 2.3.2 ActiveSupport::JSON since 2.2.2 doesn't correctly decode unicode.
2
+ require 'yaml'
3
+ require 'strscan'
4
+
5
+ silence_warnings {
6
+ module ActiveSupport
7
+ module JSON
8
+ class ParseError < StandardError
9
+ end
10
+
11
+ class << self
12
+ # Converts a JSON string into a Ruby object.
13
+ def decode(json)
14
+ YAML.load(convert_json_to_yaml(json))
15
+ rescue ArgumentError => e
16
+ raise ParseError, "Invalid JSON string"
17
+ end
18
+
19
+ protected
20
+ # matches YAML-formatted dates
21
+ DATE_REGEX = /^(?:\d{4}-\d{2}-\d{2}|\d{4}-\d{1,2}-\d{1,2}[ \t]+\d{1,2}:\d{2}:\d{2}(\.[0-9]*)?(([ \t]*)Z|[-+]\d{2}?(:\d{2})?)?)$/
22
+
23
+ # Ensure that ":" and "," are always followed by a space
24
+ def convert_json_to_yaml(json) #:nodoc:
25
+ scanner, quoting, marks, pos, times = StringScanner.new(json), false, [], nil, []
26
+ while scanner.scan_until(/(\\['"]|['":,\\]|\\.)/)
27
+ case char = scanner[1]
28
+ when '"', "'"
29
+ if !quoting
30
+ quoting = char
31
+ pos = scanner.pos
32
+ elsif quoting == char
33
+ if json[pos..scanner.pos-2] =~ DATE_REGEX
34
+ # found a date, track the exact positions of the quotes so we can remove them later.
35
+ # oh, and increment them for each current mark, each one is an extra padded space that bumps
36
+ # the position in the final YAML output
37
+ total_marks = marks.size
38
+ times << pos+total_marks << scanner.pos+total_marks
39
+ end
40
+ quoting = false
41
+ end
42
+ when ":",","
43
+ marks << scanner.pos - 1 unless quoting
44
+ end
45
+ end
46
+
47
+ if marks.empty?
48
+ json.gsub(/\\([\\\/]|u[[:xdigit:]]{4})/) do
49
+ ustr = $1
50
+ if ustr.starts_with?('u')
51
+ [ustr[1..-1].to_i(16)].pack("U")
52
+ elsif ustr == '\\'
53
+ '\\\\'
54
+ else
55
+ ustr
56
+ end
57
+ end
58
+ else
59
+ left_pos = [-1].push(*marks)
60
+ right_pos = marks << scanner.pos + scanner.rest_size
61
+ output = []
62
+ left_pos.each_with_index do |left, i|
63
+ scanner.pos = left.succ
64
+ output << scanner.peek(right_pos[i] - scanner.pos + 1).gsub(/\\([\\\/]|u[[:xdigit:]]{4})/) do
65
+ ustr = $1
66
+ if ustr.starts_with?('u')
67
+ [ustr[1..-1].to_i(16)].pack("U")
68
+ elsif ustr == '\\'
69
+ '\\\\'
70
+ else
71
+ ustr
72
+ end
73
+ end
74
+ end
75
+ output = output * " "
76
+
77
+ times.each { |i| output[i-1] = ' ' }
78
+ output.gsub!(/\\\//, '/')
79
+ output
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ }
@@ -7,7 +7,7 @@ describe Builder::HashStructure, ".new" do
7
7
 
8
8
  it 'should create new instances of self with new!' do
9
9
  builder = Builder::HashStructure.new
10
- builder.new!.should be_a Builder::HashStructure
10
+ builder.new!.should be_a(Builder::HashStructure)
11
11
  end
12
12
  end
13
13
 
@@ -35,9 +35,19 @@ describe Builder::JsonFormat do
35
35
  builder << TestObject.new("Recursive Json!").to_json
36
36
  end
37
37
 
38
- builder.to_s.should == "{\"text\": \"Recursive Json!\"}"
38
+ builder.to_s.should == '{"text": "Recursive Json!"}'
39
39
  end
40
40
 
41
+ it 'should not double escape unicode strings inserts when supporting recursive calls' do
42
+ builder = Builder::JsonFormat.new
43
+ builder.objects do
44
+ builder << TestObject.new("テスト").to_json
45
+ end
46
+
47
+ builder.to_s.should == '{"text": "\u30c6\u30b9\u30c8"}'
48
+ end
49
+
50
+
41
51
  it 'should allow string inserts to support recursive calls' do
42
52
  builder = Builder::JsonFormat.new
43
53
  builder.objects do
@@ -4,7 +4,7 @@ describe Builder::XmlMarkup, "#new!" do
4
4
 
5
5
  it "should return new instance of same class" do
6
6
  builder = Builder::XmlMarkup.new
7
- builder.new!.should be_a Builder::XmlMarkup
7
+ builder.new!.should be_a(Builder::XmlMarkup)
8
8
  end
9
9
  end
10
10
 
@@ -36,7 +36,7 @@ describe Builder::XmlMarkup, "#xml_root!" do
36
36
  it "should create an element as normal" do
37
37
  builder = Builder::XmlMarkup.new
38
38
  builder.root!("root") do
39
- builder.tag "value"
39
+ builder.tag("value")
40
40
  end
41
41
  builder.target!.should == "<root><tag>value</tag></root>"
42
42
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonbuilder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - nov
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-24 00:00:00 +09:00
12
+ date: 2009-06-26 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -48,6 +48,8 @@ files:
48
48
  - lib/builder/json_format.rb
49
49
  - lib/builder/xml_markup.rb
50
50
  - lib/jsonbuilder.rb
51
+ - lib/patch
52
+ - lib/patch/active_support_json_decode.rb
51
53
  has_rdoc: true
52
54
  homepage: http://jsonbuilder.rubyforge.org
53
55
  post_install_message: