Search code examples
ruby-on-railsrubyjsonactive-model-serializers

How do you get the serializer to output JSON?


I'm trying to use active_model_serializers to convert an object to JSON. As per this question, I created a file called config/initializers/serializers.rb with the following code:

puts 'Running serializers.rb.'
ActiveModelSerializers.config.adapter = :json_api

I see Running serializers.rb. when resetting the database, but not when running the Rails console. In the console, when I type UserSerializer.new(User.first).as_json, I get {:id=>1, :name=>"Regular User", :created_at=>Thu, 07 Jul 2016 06:03:46 UTC +00:00, :lists=>[{:id=>1, :title=>"Before Wednesday"}]}, which is clearly not JSON. How do I get the as_json method to actually return JSON?


Solution

  • UserSerializer.new(User.first).to_json

    http://apidock.com/rails/Hash/to_json