activesupport-json_encoder 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +42 -49
  3. metadata +19 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 89a5f199ccc21838dc34b32813adaa60335303fc
4
- data.tar.gz: d429631a6f94461670f27ea8fd882be3cb396788
3
+ metadata.gz: 31ecf714d21d1a852a5be133c9bb39acba2b3ba7
4
+ data.tar.gz: 235b491b8b9be8afac56a3ea450cdac4dead22a4
5
5
  SHA512:
6
- metadata.gz: fe3371500bc06035d8191b581875cef488ff862d4cb204951d2fef586efc292a713b5fc88df8e0391ae21d33b41b1d1d2c987e5094402c5434c23849d000000b
7
- data.tar.gz: cdf863fbee06497dff7f47f08e77b2f2a6534bc2bf0b94bf03cf31d42300242acd68d4581a3d88a95012a150c80390498488082e211ed60a8629dd8a4f509e57
6
+ metadata.gz: 8eec6682fd99195dc91d1a6cdf4610e5f72e7b4ee2e9930fa6b355a70cefff7c6c2ec7580ba64a82d6ec29c9c2f3d0b55aba803556299d6ebddc1a2126b8b8c6
7
+ data.tar.gz: 18a9e00e453c6dd306e81ed5c2658247f9a17f1cbcc67aa2f02f1607011dfb04861cb1bc822526af88c25b7a7dfd1ae5b0b018f3dc3428bbb6b030115227e9af
data/README.md CHANGED
@@ -18,25 +18,22 @@ Configuration
18
18
  By default, ActiveSupport encodes `BigDecimal` objects as a string:
19
19
 
20
20
  ```ruby
21
- >> { big_number: BigDecimal.new('12345678901234567890') }.to_json
22
- => "{\"big_number\":\"12345678901234567890.0\"}"
21
+ { big_number: BigDecimal.new('12345678901234567890') }.to_json # => "{\"big_number\":\"12345678901234567890.0\"}"
23
22
  ```
24
23
 
25
24
  To change this, you can set `ActiveSupport.encode_big_decimal_as_string` to
26
25
  `false`:
27
26
 
28
27
  ```ruby
29
- >> ActiveSupport.encode_big_decimal_as_string = false
30
- >> { big_number: BigDecimal.new('12345678901234567890') }.to_json
31
- => "{\"big_number\":12345678901234567890.0}"
28
+ ActiveSupport.encode_big_decimal_as_string = false
29
+ { big_number: BigDecimal.new('12345678901234567890') }.to_json # => "{\"big_number\":12345678901234567890.0}"
32
30
  ```
33
31
 
34
32
  Beware that you may lose precision on the consuming-end if you do this:
35
33
 
36
34
  ```javascript
37
- > // Parsing this in JavaScript in the browser
38
- > JSON.parse("{\"big_number\":12345678901234567890.0}").big_number
39
- 12345678901234567000
35
+ // Parsing this in JavaScript in the browser
36
+ JSON.parse("{\"big_number\":12345678901234567890.0}").big_number // => 12345678901234567000
40
37
  ```
41
38
 
42
39
  JSON Serialization for Custom Objects
@@ -46,14 +43,13 @@ By default, when the encoder encounters a Ruby object that it does not
46
43
  recognize, it will serilizes its instance variables:
47
44
 
48
45
  ```ruby
49
- >> class MyClass
50
- >> def initialize
51
- >> @foo = :bar
52
- >> end
53
- >> end
54
- => nil
55
- >> MyClass.new.to_json
56
- => "{\"foo\":\"bar\"}"
46
+ class MyClass
47
+ def initialize
48
+ @foo = :bar
49
+ end
50
+ end
51
+
52
+ MyClass.new.to_json # => "{\"foo\":\"bar\"}"
57
53
  ```
58
54
 
59
55
  There are two ways to customize this behavior on a per-class basis. Typically,
@@ -61,16 +57,14 @@ you should override `#as_json` to return a Ruby-representation of your object.
61
57
  Any options passed to `#to_json` will be made available to this method:
62
58
 
63
59
  ```ruby
64
- >> class MyClass
65
- >> def as_json(options = {})
66
- >> options[:as_array] ? [:foo, :bar] : {foo: :bar}
67
- >> end
68
- >> end
69
- => nil
70
- >> MyClass.new.to_json
71
- => "{\"foo\":\"bar\"}"
72
- >> MyClass.new.to_json(as_array: true)
73
- => "[\"foo\",\"bar\"]"
60
+ class MyClass
61
+ def as_json(options = {})
62
+ options[:as_array] ? [:foo, :bar] : {foo: :bar}
63
+ end
64
+ end
65
+
66
+ MyClass.new.to_json # => "{\"foo\":\"bar\"}"
67
+ MyClass.new.to_json(as_array: true) # => "[\"foo\",\"bar\"]"
74
68
  ```
75
69
 
76
70
  This method is supported by all encoders.
@@ -82,28 +76,27 @@ object and is expected to return a `String` that would be injected to the JSON
82
76
  output directly:
83
77
 
84
78
  ```ruby
85
- >> class Money
86
- >> def initialize(dollars, cents)
87
- >> @dollars = dollars
88
- >> @cents = cents
89
- >> end
90
- >>
91
- >> def as_json(options = nil)
92
- >> # Opt-out from the default Object#as_json
93
- >> self
94
- >> end
95
- >>
96
- >> def encode_json(encoder)
97
- >> if @cents.to_i < 10
98
- >> "#{@dollars.to_i}.0#{@cents.to_i}"
99
- >> else
100
- >> "#{@dollars.to_i}.#{@cents.to_i}"
101
- >> end
102
- >> end
103
- >> end
104
- => nil
105
- >> { price: Money.new(0,10) }.to_json
106
- => "{\"price\":0.10}"
79
+ class Money
80
+ def initialize(dollars, cents)
81
+ @dollars = dollars
82
+ @cents = cents
83
+ end
84
+
85
+ def as_json(options = nil)
86
+ # Opt-out from the default Object#as_json
87
+ self
88
+ end
89
+
90
+ def encode_json(encoder)
91
+ if @cents.to_i < 10
92
+ "#{@dollars.to_i}.0#{@cents.to_i}"
93
+ else
94
+ "#{@dollars.to_i}.#{@cents.to_i}"
95
+ end
96
+ end
97
+ end
98
+
99
+ { price: Money.new(0,10) }.to_json # => "{\"price\":0.10}"
107
100
  ```
108
101
 
109
102
  Beware that this function is specific to this gem and is not supported by
@@ -115,4 +108,4 @@ when dealing with user input.
115
108
  Dependencies
116
109
  ------------
117
110
 
118
- * `activesupport` >= 4.1.0.beta
111
+ * `activesupport` >= 4.1.0
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activesupport-json_encoder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-05 00:00:00.000000000 Z
11
+ date: 2014-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,7 +16,7 @@ dependencies:
16
16
  requirements:
17
17
  - - '>='
18
18
  - !ruby/object:Gem::Version
19
- version: 4.1.0.beta
19
+ version: 4.1.0
20
20
  - - <
21
21
  - !ruby/object:Gem::Version
22
22
  version: '5.0'
@@ -26,10 +26,24 @@ dependencies:
26
26
  requirements:
27
27
  - - '>='
28
28
  - !ruby/object:Gem::Version
29
- version: 4.1.0.beta
29
+ version: 4.1.0
30
30
  - - <
31
31
  - !ruby/object:Gem::Version
32
32
  version: '5.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
33
47
  description: A pure-Ruby ActiveSupport JSON encoder
34
48
  email:
35
49
  - david@loudthinking.com
@@ -63,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
77
  version: '0'
64
78
  requirements: []
65
79
  rubyforge_project:
66
- rubygems_version: 2.0.3
80
+ rubygems_version: 2.2.1
67
81
  signing_key:
68
82
  specification_version: 4
69
83
  summary: A pure-Ruby ActiveSupport JSON encoder (extracted from core in Rails 4.1)