Nested objects to json?

  question, ruby

How to Convert Nested Objects into json in ruby?
For example, an object obj contains a custom instance variable b
If you use to_json, you cannot output the content of b as well.
What is needed is output like obj.to_s, but in json form
It is possible to use to_yaml, but you want to transfer json directly.

By the way, to_json and to_yaml are obviously the same function. Why are there such differences in results?

require 'oj'
 
 class A
 def initialize a=[1,2,3], b='hello'
 @a = a
 @b = b
 end
 end
puts Oj::dump a, :indent => 2, :mode => :compat

Output:

{
 "a":[
 1,
 2,
 3
 ],
 "b":"hello"
 }