1unless defined?(::JSON::JSON_LOADED) and ::JSON::JSON_LOADED 2 require 'json' 3end 4require 'ostruct' 5 6# OpenStruct serialization/deserialization 7class OpenStruct 8 9 # Deserializes JSON string by constructing new Struct object with values 10 # <tt>v</tt> serialized by <tt>to_json</tt>. 11 def self.json_create(object) 12 new(object['t'] || object[:t]) 13 end 14 15 # Returns a hash, that will be turned into a JSON object and represent this 16 # object. 17 def as_json(*) 18 klass = self.class.name 19 klass.to_s.empty? and raise JSON::JSONError, "Only named structs are supported!" 20 { 21 JSON.create_id => klass, 22 't' => table, 23 } 24 end 25 26 # Stores class name (OpenStruct) with this struct's values <tt>v</tt> as a 27 # JSON string. 28 def to_json(*args) 29 as_json.to_json(*args) 30 end 31end 32