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)
110
110
MultiJson . load ( object )
111
111
end
112
112
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 )
114
120
if object . respond_to? :serializable_hash
115
121
object . serializable_hash
116
122
elsif object . kind_of? ( Array ) && !object . map { |o | o . respond_to? :serializable_hash } . include? ( false )
117
123
object . map { |o | o . serializable_hash }
118
124
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 }
120
126
else
121
127
object
122
128
end
123
129
end
124
130
125
131
def encode_json ( object )
126
132
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 )
127
135
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 )
134
137
end
135
138
136
139
def encode_txt ( object )
You can’t perform that action at this time.
0 commit comments