4 class TypeMismatch < ArgumentError
8 if !val.is_a?(object_class)
9 raise TypeMismatch.new("cannot serialize #{val.class} as #{object_class}")
14 def self.legacy_load(s)
15 val = Psych.safe_load(s)
17 # If apiserver was downgraded to a YAML-only version after
18 # storing JSON in the database, the old code would have loaded
19 # the JSON document as a plain string, and then YAML-encoded
20 # it when saving it back to the database. It's too late now to
21 # make the old code behave better, but at least we can
22 # gracefully handle the mess it leaves in the database by
23 # double-decoding on the way out.
24 return SafeJSON.load(val)
33 elsif s[0] == first_json_char
35 elsif s[0..2] == "---"
38 raise "invalid serialized data #{s[0..5].inspect}"
43 class HashSerializer < Serializer
44 def self.first_json_char
53 class ArraySerializer < Serializer
54 def self.first_json_char