Skip to content

Commit 5e864a2

Browse files
committed
Switch to jsonapi/serializer.
1 parent 36f432d commit 5e864a2

File tree

5 files changed

+31
-16
lines changed

5 files changed

+31
-16
lines changed

Gemfile

-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@ source 'https://rubygems.org'
22

33
# Specify your gem's dependencies in jsonapi.gemspec
44
gemspec
5-
6-
gem 'jsonapi-rspec', git: 'https://github.com/jsonapi-rb/jsonapi-rspec.git'

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Main goals:
3131

3232
The available features include:
3333

34-
* object serialization (powered by Fast JSON API)
34+
* object serialization (powered by JSON:API Serializer, was `fast_jsonapi`)
3535
* [error handling](https://jsonapi.org/format/#errors) (parameters,
3636
validation, generic errors)
3737
* fetching of the data (support for
@@ -44,7 +44,7 @@ The available features include:
4444

4545
## But how?
4646

47-
Mainly by leveraging [Fast JSON API](https://github.com/Netflix/fast_jsonapi)
47+
Mainly by leveraging [JSON:API Serializer](https://github.com/jsonapi-serializer/jsonapi-serializer)
4848
and [Ransack](https://github.com/activerecord-hackery/ransack).
4949

5050
Thanks to everyone who worked on these amazing projects!
@@ -100,7 +100,7 @@ The naming scheme follows the `ModuleName::ClassNameSerializer` for an instance
100100
of the `ModuleName::ClassName`.
101101

102102
Please follow the
103-
[Fast JSON API guide](https://github.com/Netflix/fast_jsonapi#serializer-definition)
103+
[JSON:API Serializer guide](https://github.com/jsonapi-serializer/jsonapi-serializer#serializer-definition)
104104
on how to define a serializer.
105105

106106
To provide a different naming scheme implement the `jsonapi_serializer_class`

lib/jsonapi/error_serializer.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
require 'fast_jsonapi'
1+
require 'jsonapi/serializer'
22

33
module JSONAPI
44
# A simple error serializer
55
class ErrorSerializer
6-
include FastJsonapi::ObjectSerializer
6+
include JSONAPI::Serializer
77

88
set_id :object_id
99
set_type :error

lib/jsonapi/rails.rb

+24-7
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ def self.add_errors_renderer!
4242
many = JSONAPI::Rails.is_collection?(resource, options[:is_collection])
4343
resource = [resource] unless many
4444

45-
return JSONAPI::ErrorSerializer.new(resource, options)
46-
.serialized_json unless resource.is_a?(ActiveModel::Errors)
45+
return JSONAPI::Rails.serializer_to_json(
46+
JSONAPI::ErrorSerializer.new(resource, options)
47+
) unless resource.is_a?(ActiveModel::Errors)
4748

4849
errors = []
4950
model = resource.instance_variable_get('@base')
@@ -66,9 +67,11 @@ def self.add_errors_renderer!
6667
end
6768
end
6869

69-
JSONAPI::ActiveModelErrorSerializer.new(
70-
errors, params: { model: model, model_serializer: model_serializer }
71-
).serialized_json
70+
JSONAPI::Rails.serializer_to_json(
71+
JSONAPI::ActiveModelErrorSerializer.new(
72+
errors, params: { model: model, model_serializer: model_serializer }
73+
)
74+
)
7275
end
7376
end
7477

@@ -100,13 +103,15 @@ def self.add_renderer!
100103
serializer_class = JSONAPI::Rails.serializer_class(resource, many)
101104
end
102105

103-
serializer_class.new(resource, options).serialized_json
106+
JSONAPI::Rails.serializer_to_json(
107+
serializer_class.new(resource, options)
108+
)
104109
end
105110
end
106111

107112
# Checks if an object is a collection
108113
#
109-
# Stolen from [FastJsonapi::ObjectSerializer], instance method.
114+
# Stolen from [JSONAPI::Serializer], instance method.
110115
#
111116
# @param resource [Object] to check
112117
# @param force_is_collection [NilClass] flag to overwrite
@@ -126,5 +131,17 @@ def self.serializer_class(resource, is_collection)
126131

127132
"#{klass.name}Serializer".constantize
128133
end
134+
135+
# Lazily returns the serializer JSON
136+
#
137+
# @param serializer [Object] to evaluate
138+
# @return [String]
139+
def self.serializer_to_json(serializer)
140+
if serializer.respond_to?(:serialized_json)
141+
serializer.serialized_json
142+
else
143+
serializer.serializable_hash.to_json
144+
end
145+
end
129146
end
130147
end

spec/dummy.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ class Note < ActiveRecord::Base
4040
end
4141

4242
class CustomNoteSerializer
43-
include FastJsonapi::ObjectSerializer
43+
include JSONAPI::Serializer
4444

4545
set_type :note
4646
belongs_to :user
4747
attributes(:title, :quantity, :created_at, :updated_at)
4848
end
4949

5050
class UserSerializer
51-
include FastJsonapi::ObjectSerializer
51+
include JSONAPI::Serializer
5252

5353
has_many :notes, serializer: CustomNoteSerializer
5454
attributes(:last_name, :created_at, :updated_at)

0 commit comments

Comments
 (0)