Skip to content

Commit 687e01b

Browse files
committed
Remove MultiJson from the list of dependencies
Fixes #1. For context, see #1 and intridea/multi_json#113 (comment).
1 parent e4304bb commit 687e01b

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

README.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,13 @@ PersonSerializer.new(person).to_json #=> '{"name":"John Doe",...}'
3030
## JSON serialization
3131

3232
Granola doesn't make assumptions about your code, so it shouldn't depend on a
33-
specific JSON backend. It uses [MultiJson][] to serialize your objects with your
34-
favorite backend.
33+
specific JSON backend. It defaults to the native JSON backend, but you're free
34+
to change it. For example, if you were using [Yajl][]:
3535

36-
Try to avoid using the default, which is the `stdlib`'s pure-ruby JSON library,
37-
since it's slow. If in doubt, I like [Yajl][].
38-
39-
If you want to pass options to `MultiJson` (like `pretty: true`), any keywords
40-
passed to `#to_json` will be forwarded to `MultiJson.dump`.
36+
``` ruby
37+
Granola.json = ->(obj, **opts) { Yajl::Encoder.encode(obj, opts) }
38+
```
4139

42-
[MultiJson]: https://github.com/intridea/multi_json
4340
[Yajl]: https://github.com/brianmario/yajl-ruby
4441

4542
## Handling lists of models

granola.gemspec

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ Gem::Specification.new do |s|
2020
"lib/granola/caching.rb",
2121
]
2222

23-
s.add_dependency "multi_json", "~> 1.10"
24-
2523
s.add_development_dependency "cutest", "~> 1.2"
26-
s.add_development_dependency "rack", "~> 1.5"
24+
s.add_development_dependency "rack", "~> 2.5"
2725
end
2826

lib/granola.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
1-
require "multi_json"
21
require "granola/version"
2+
require "json"
33

44
module Granola
5+
class << self
6+
# Public: Get/Set a Proc that takes an Object and a Hash of options and
7+
# returns a JSON String.
8+
#
9+
# The default implementation uses the standard library's JSON module, but
10+
# you're welcome to swap it out.
11+
#
12+
# Example:
13+
#
14+
# require "yajl"
15+
# Granola.json = ->(obj, **opts) { Yajl::Encoder.encode(obj, opts) }
16+
attr_accessor :json
17+
end
18+
19+
self.json = ->(obj, **opts) { JSON.dump(obj) }
20+
521
# A Serializer describes how to serialize a certain type of object, by
622
# declaring the structure of JSON objects.
723
class Serializer
@@ -35,13 +51,13 @@ def attributes
3551
fail NotImplementedError
3652
end
3753

38-
# Public: Generate the JSON string using the current MultiJson adapter.
54+
# Public: Generate the JSON String.
3955
#
40-
# **options - Any options valid for `MultiJson.dump`.
56+
# **options - Any options to be passed to the `Granola.json` Proc.
4157
#
4258
# Returns a String.
4359
def to_json(**options)
44-
MultiJson.dump(attributes, options)
60+
Granola.json.(attributes, options)
4561
end
4662

4763
# Public: Returns the MIME type generated by this serializer. By default

test/serialize_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,14 @@ def mime_type
5050
assert_equal %q|[{"a":1,"b":2},{"a":3,"b":4}]|, serializer.to_json
5151
end
5252
end
53+
54+
scope do
55+
prepare do
56+
Granola.json = ->(obj, **opts) { "success!" }
57+
end
58+
59+
test "serializes with a custom json backend" do
60+
serializer = OpenStructSerializer.new(OpenStruct.new)
61+
assert_equal "success!", serializer.to_json
62+
end
63+
end

0 commit comments

Comments
 (0)