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.
- checksums.yaml +4 -4
- data/README.md +42 -49
- metadata +19 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 31ecf714d21d1a852a5be133c9bb39acba2b3ba7
|
4
|
+
data.tar.gz: 235b491b8b9be8afac56a3ea450cdac4dead22a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
-
|
30
|
-
|
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
|
-
|
38
|
-
|
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
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
=> "
|
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
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
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
|
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.
|
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:
|
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
|
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
|
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.
|
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)
|