8

I have a structure of simple container classes like so (in pseudo ruby):

class A
  attr_reader :string_field1, :string_field2
  ...
end

class B
  attr_reader: int_field3, :string_field4
  ...
end

# C includes an instance of A and B
class C
  attr_reader: :a_instance, :b_instance
  ...
end

Is there are simple way to de/serialize this to JSON in Ruby? Or should I make nested serialization methods per class as in this example?

EDIT:

In my specific scenario, I want to POST some JSON data to a server running Ruby, which will extract the data and act accordingly.

The sender of the JSON is not necessarily a Ruby process, but may be the back end of some other system. (Although it is Ruby in my test harness).

So, I don't need the JSON to be in some "Ruby specific" format, I just assumed it would be easier if that was actually built-in to Ruby.

Justicle
  • 14,761
  • 17
  • 70
  • 94

3 Answers3

7

Use Marshal, PStore, or another Ruby solution for non-JSON objects

One might reasonably wonder why a completely reflective language like Ruby doesn't automate JSON generation and parsing of arbitrary classes.

However, unless you stick to the JSON types, there is no place to send or receive the JSON objects except to another running Ruby. And in that case, I suspect that the conventional wisdom is "forget JSON, use a native Ruby interface like the core class Marshal.

So, if you are really sending those objects to PHP or something non-Ruby, then you should create directly JSON-supported Ruby data structures using Array and the like, and then you will have something that JSON.generate will directly deal with.

If you just need serialization it's possible you should use Marshal or PStore.

Update: Aha, ok, try this:

module AutoJ
  def auto_j
    h = {}
    instance_variables.each do |e|
      o = instance_variable_get e.to_sym
      h[e[1..-1]] = (o.respond_to? :auto_j) ? o.auto_j : o;
    end
    h
  end
  def to_json *a
    auto_j.to_json *a
  end
end

If you then include AutoJ in each of your classes, it should DTRT. In your example this results in

{
  "a": {
    "string_field1": "aa",
    "string_field2": "bb"
  },
  "b": {
    "int_field3": 123,
    "string_field4": "dd"
  }
}

You might want to change the auto_j method to return h.values instead of just h, in which case you get:

[
  ["aa", "bb"],
  [123, "dd"]
]
Mickael Lherminez
  • 679
  • 1
  • 10
  • 29
DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
  • Thanks, see my clarification. I *was* under the impression it would be a bit more automatic than it currently is. – Justicle Nov 06 '09 at 04:35
  • Just be careful with marshaling closures, as `lambdas` or `Procs`. They can't. I don't know if that's an extensive list, maybe there are other structures that can't be marshaled too. – ribamar Aug 04 '16 at 09:10
2

I had the same problem (mainly trying to create JSON strings of arbitrary complexity) rather than parsing them. After looking all over for a non-invasive class that will take a Ruby object (including nested arrays) and marshal it as a JSON string I finally wrote my own simple serialiser. This code also escapes special characters to create valid JSON.

http://www.keepingmyhandin.com/Downhome/Sketchup/simplejsonserializerrubyimplementation

All you have to do is:

json  = JSON.new;
jsonString = json.marshal(obj); # Where obj is a Ruby object
Kyeotic
  • 19,697
  • 10
  • 71
  • 128
myhand
  • 21
  • 1
1

Here is my approach to to_json implementation for custom classes.

There is a little magic here using self.included in a module. Here is a very nice article from 2006 about module having both instance and class methods http://blog.jayfields.com/2006/12/ruby-instance-and-class-methods-from.html

The module is designed to be included in any class to provide to_json functionality. It intercepts attr_accessor method rather than uses its own in order to require minimal changes for existing classes.

to_json implementation is based on this answer

module JSONable
  module ClassMethods
    attr_accessor :attributes

    def attr_accessor *attrs
      self.attributes = Array attrs
      super
    end
  end

  def self.included(base)
    base.extend(ClassMethods)
  end

  def as_json options = {}
    serialized = Hash.new
    self.class.attributes.each do |attribute|
      serialized[attribute] = self.public_send attribute
    end
    serialized
  end

  def to_json *a
    as_json.to_json *a
  end
end


class CustomClass
  include JSONable
  attr_accessor :b, :c 

  def initialize b: nil, c: nil
    self.b, self.c = b, c
  end
end

a = CustomClass.new(b: "q", c: 23)
puts JSON.pretty_generate a

{
  "b": "q",
  "c": 23
}
Community
  • 1
  • 1
Vadym Tyemirov
  • 8,288
  • 4
  • 42
  • 38