File tree Expand file tree Collapse file tree 1 file changed +11
-8
lines changed
Expand file tree Collapse file tree 1 file changed +11
-8
lines changed Original file line number Diff line number Diff line change @@ -110,27 +110,30 @@ def decode_json(object)
110110 MultiJson . load ( object )
111111 end
112112
113- def serialize_recurse ( object )
113+ def serializable? ( object )
114+ object . respond_to? ( :serializable_hash ) ||
115+ object . kind_of? ( Array ) && !object . map { |o | o . respond_to? :serializable_hash } . include? ( false ) ||
116+ object . kind_of? ( Hash )
117+ end
118+
119+ def serialize ( object )
114120 if object . respond_to? :serializable_hash
115121 object . serializable_hash
116122 elsif object . kind_of? ( Array ) && !object . map { |o | o . respond_to? :serializable_hash } . include? ( false )
117123 object . map { |o | o . serializable_hash }
118124 elsif object . kind_of? ( Hash )
119- object . inject ( { } ) { |h , ( k , v ) | h [ k ] = serialize_recurse ( v ) ; h }
125+ object . inject ( { } ) { |h , ( k , v ) | h [ k ] = serialize ( v ) ; h }
120126 else
121127 object
122128 end
123129 end
124130
125131 def encode_json ( object )
126132 return object if object . is_a? ( String )
133+ return MultiJson . dump ( serialize ( object ) ) if serializable? ( object )
134+ return object . to_json if object . respond_to? ( :to_json )
127135
128- serialized_hash = serialize_recurse ( object )
129- if ( object == serialized_hash ) && ( object . respond_to? :to_json )
130- object . to_json
131- else
132- MultiJson . dump ( serialized_hash )
133- end
136+ MultiJson . dump ( object )
134137 end
135138
136139 def encode_txt ( object )
You can’t perform that action at this time.
0 commit comments