Ensure that primitive values are cast to the correct types in serialization

This commit is contained in:
Joshua Suskalo 2021-09-23 08:30:11 -05:00
parent 0f227ac6f8
commit e00e62f996

View file

@ -255,10 +255,21 @@
[obj type scope] [obj type scope]
(type-dispatch type))) (type-dispatch type)))
(def ^:private primitive-cast
"Map from primitive type names to the function to cast it to a primitive."
{::byte byte
::short short
::int int
::long long
::long-long long
::char char
::float float
::double double})
(defmethod serialize* :default (defmethod serialize* :default
[obj type _scope] [obj type _scope]
(if (primitive-type type) (if-let [prim (primitive-type type)]
obj ((primitive-cast prim) obj)
(throw (ex-info "Attempted to serialize a non-primitive type with primitive methods" (throw (ex-info "Attempted to serialize a non-primitive type with primitive methods"
{:type type {:type type
:object obj})))) :object obj}))))