Ruby中嵌套对象转换成json的方法

JSON由于其数据结构简单便利,已逐渐成为了互联网上的主流数据交换的数据格式。

在讨论嵌套对象(Nested Object)的JSON转换方法之前,我们先看简单的ruby JSON转换。
首先,ruby对象转换为JSON字符串:

class Obj1

    def initialize(var1)

        @var1 = var1

    end

    def to_json(*a)

        {

            "json_class" => self.class,

            "data" => {"var1" => @var1}

        }.to_json(*a)

    end

    def self.json_create(json_str)

        new(json_str["data"]["var1"])

    end

end

obj1 = Obj1.new("i am obj1")

#obj1 to JSON string

json_str = obj1.to_json

puts "JSON string of obj1 = #{json_str}"

#JSON string to obj1

obj11 = JSON.parse(json_str)

puts "ob1 from json string = #{obj11.var1}"


上面代码我们可以看到,ruby与JSON string之间的转换,关键有三个点:

#引入json库,才能有下面两个方法,json是通过open class的方式,给Hash对象加上了to_json(*a)方法,关于ruby的open class参考支持Open Class特性的编程语言中的开闭原则(Open-Closed Principle)
1)require ‘json'

#定义对象转为JSON string的to_json(*a)方法,其实现是使用Hash对象的to_json(*a)方法
2)def to_json(*a)

#定义从JSON string构造对象的json_create方法,此方法是类方法
3)def self.json_create(json_str)

上面三点是Ruby中实现JSON string互相转换的基本要求。

代码运行结果为:

JSON string of obj1 = {"json_class":"Obj1","data":{"var1":"i am obj1"}}

ob1 from json string = i am obj1


现在我们来看嵌套对象的JSON string转换:

#!/usr/local/ruby/bin/ruby

require 'json'

class Obj1

    def initialize(var1)

        @var1 = var1

    end

    def to_json(*a)

        {

            "json_class" => self.class,

            "data" => {"var1" => @var1}

        }.to_json(*a)

    end

    def self.json_create(json_str)

        new(json_str["data"]["var1"])

    end

    attr_reader :var1

end

class Obj2

    def initialize(var2)

        @var2 = var2

    end

    def to_json(*a)

        {

            "json_class" => self.class,

            "data" => {"var2" => @var2}

        }.to_json(*a)

    end

    def self.json_create(json_str)

        new(json_str["data"]["var2"])

    end

    attr_reader :var2

end

class Obj

    def initialize(obj1, obj2)

        @obj1 = obj1

        @obj2 = obj2

    end

    def to_json(*a)

        {

            "json_class" => self.class,

            "data" => {"obj1" => @obj1.to_json, "obj2" => @obj2.to_json}

        }.to_json(*a)

    end

    def self.json_create(json_str)

        new(json_str["data"]["obj1"], json_str["data"]["obj2"])

    end

    def to_s

        "Hi, i am obj"

    end

    attr_reader :obj1, :obj2

end

obj1 = Obj1.new("i am obj1")

obj2 = Obj2.new("i am obj2")

obj = Obj.new(obj1,obj2)

obj_json_str = obj.to_json

puts "JSON string of obj = #{obj_json_str}"

obj_1 = JSON.parse(obj_json_str)

puts "obj_1 from json string , obj1.class = #{obj_1.obj1.class}, obj2.class = #{obj_1.obj2.class}"


上面代码中,嵌套对象我们惯性思维,是先将对象自己转换为JSON string:

"data" => {"obj1" => @obj1.to_json, "obj2" => @obj2.to_json}

上面代码输出:

JSON string of obj = {"json_class":"Obj","data":{"obj1":"{\"json_class\":\"Obj1\",\"data\":{\"var1\":\"i am obj1\"}}","obj2":"{\"json_class\":\"Obj2\",\"data\":{\"var2\":\"i am obj2\"}}"}}

obj_1 from json string , obj1.class = String, obj2.class = String

我们注意到,被嵌套的对象转换为JSON string后,多了一个反斜杠 \ :

JSON string of obj = {"json_class":"Obj","data":{"obj1":"{\"json_class\":\"Obj1\",\"data\":{\"var1\":\"i am obj1\"}}","obj2":"{\"json_class\":\"Obj2\",\"data\":{\"var2\":\"i am obj2\"}}"}}

且,JSON string转换后,obj对象中嵌套的对象obj1和obj2,其类型都为String,而不是期望的Obj1和Obj2类型

obj_1 from json string , obj1.class = String, obj2.class = String

实际上,这里是惯性思维害人,被嵌套的对象,不需要调用其to_json方法。
因此将Obj类的to_json代码:

    def to_json(*a)

        {

            "json_class" => self.class,

            "data" => {"obj1" => @obj1.to_json, "obj2" => @obj2.to_json}

        }.to_json(*a)

    end

修正为:

    def to_json(*a)

        {

            "json_class" => self.class,

            "data" => {"obj1" => @obj1, "obj2" => @obj2}

        }.to_json(*a)

    end

然后,运行代码,可以看到预期的输出:

JSON string of obj = {"json_class":"Obj","data":{"obj1":{"json_class":"Obj1","data":{"var1":"i am obj1"}},"obj2":{"json_class":"Obj2","data":{"var2":"i am obj2"}}}}

obj_1 from json string = {"json_class"=>"Obj", "data"=>{"obj1"=>#, "obj2"=>#}}