Closed BoboFraggins closed 3 months ago
Shale doesn't provide as_json
method on an object instance. This method is only available on a class level e.g Test.as_json(Test.new)
.
I think some other library might bleed into your objects (e.g Rails).
Here's an example showing that everything works correctly:
class Test < Shale::Mapper
attribute :nil_attribute, Shale::Type::String
attribute :rails_attribute_name, Shale::Type::String
json do
map 'nil_attribute', to: :nil_attribute, render_nil: true
map 'json_attribute_name', to: :rails_attribute_name
end
end
puts Test.to_json(Test.new)
puts Test.as_json(Test.new)
The output is this:
{"nil_attribute":null}
{"nil_attribute"=>nil}
Gotcha. Thanks for the response, love the gem!
Given the following mapper:
I would expect both of these calls to generate the same hash:
But
as_json
ignores the attribute name change and therender_nil
option.I am happy to take a crack at fixing this, but before trying to I wanted to confirm that you believe this is a bug.