Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

prevent quoted nulls ("null") when to_json is called on nil relationship #23

Closed
wants to merge 1 commit into from

1 participant

@soopa

Fixes the case where calling something like:

resource.to_json(:relationships => { :nested_resource => { :only => [:id] } })

When the nested resource returns nil, would return invalid JSON like…

{
  "id":1,
  "nested_resouce":"null"
}

Rather than valid JSON like…

{
  "id":1,
  "nested_resouce":null
}

(Note the quotes around null)

@soopa

Acceptable?

@soopa

Checking in, 2 years later. I've just rebased this cleanly on latest master and the build is passing.

@solnic @dkubb could one of your merge this please? I'd love to get the reference to my fork out of my Gemfile and resolve this issue for anyone else who may be stumbling upon it.

Thanks!

@soopa

Updated branch target in #41, closing this one.

@soopa soopa closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 4 additions and 1 deletion.
  1. +4 −1 lib/dm-serializer/to_json.rb
View
5 lib/dm-serializer/to_json.rb
@@ -41,7 +41,10 @@ def as_json(options = {})
if options[:relationships]
options[:relationships].each do |relationship_name, opts|
if respond_to?(relationship_name)
- result[relationship_name] = __send__(relationship_name).to_json(opts.merge(:to_json => false))
+ relationship = __send__(relationship_name)
+ result[relationship_name] = relationship ?
+ relationship.to_json(opts.merge(:to_json => false)) :
+ nil
end
end
end
Something went wrong with that request. Please try again.