Reputation: 267190
I am confused why my simple ruby object is not converting to json.
>irb
>
require 'json'
class User
attr_accessor :name, :age
def initialize(name, age)
@name = name
@age = age
end
end
u1 = User.new("a", 1)
u2 = User.new("b", 2)
puts u1.to_json
"\"#<User:0x000001010e9f78>\""
What am I missing?
I want to then store these objects into an array collection, and then convert the entire collection to json.
users = []
users << User.new("a", 1)
users << User.new("b", 2)
users.to_json
Note: This is not using Rails, just plain old Ruby!
I want my json to be an array of user objects.
[
{"name": "john", "age": 22},
{"name": "john1", "age": 23}
{"name": "john2", "age": 24}
]
Upvotes: 4
Views: 181
Reputation: 176482
The default implementation of to_json
is quite simple and clearly is not doing what you would expect. And this is expected: you need to write code to explain to the interpreter how you want your program to behave.
It's a common standard to provide both a to_json
and as_json
method. The first latter returns a JSON-serializable version of the instance (generally a Hash
), the latter is the actual JSON output.
class User
attr_accessor :name, :age
def initialize(name, age)
@name = name
@age = age
end
def as_json(*)
{ name: @name, age: @age }
end
def to_json(*)
as_json.to_json()
end
end
Here's the output
2.3.0 :034 > u1.as_json
=> {:name=>"a", :age=>1}
2.3.0 :035 > puts u1.to_json
{"name":"a","age":1}
=> nil
With a little effort you can change the as_json
to automatically collect all the instance variables. However, I discourage this approach as you may end-up serializing sensitive attributes you don't really want to share (like passwords).
Upvotes: 1
Reputation: 1298
By default, classes cannot be made into JSON
strings. You must have a to_json
method in your class, so you can make it inherit from this class (type class User < JSONable
):
class JSONable
def to_json
hash = {}
self.instance_variables.each do |x|
hash[x] = self.instance_variable_get x
end
return hash.to_json
end
end
Then, you can call to_json
and it will work properly.
Test:
$ irb
irb(main):001:0> require 'json'
=> true
irb(main):002:0> class JSONable
irb(main):003:1> def to_json
irb(main):004:2> hash = {}
irb(main):005:2> self.instance_variables.each do |x|
irb(main):006:3* hash[x] = self.instance_variable_get x
irb(main):007:3> end
irb(main):008:2> return hash.to_json
irb(main):009:2> end
irb(main):010:1> end
=> nil
irb(main):011:0> class User < JSONable
irb(main):012:1> attr_accessor :name, :age
irb(main):013:1>
irb(main):014:1* def initialize(name, age)
irb(main):015:2> @name = name
irb(main):016:2> @age = age
irb(main):017:2> end
irb(main):018:1> end
=> nil
irb(main):019:0>
irb(main):020:0* user = User.new("hi",3)
=> #<User:0x007fd6c8af0a90 @name="hi", @age=3>
irb(main):021:0> puts user.to_json
{"@name":"hi","@age":3}
Upvotes: 0