You can’t dynamically create a Symbol
, so there’s not really a way to do this. Have you considered using an Enum
? E.g. something like:
enum State
FOO
BAR
end
class Foo
include JSON::Serializable
property state : State
def initialize(@state : State)
end
end
f = Foo.new(state: :bar)
p! f # => #<Foo:0x7f124d85eeb0 @state=BAR>
p! f.to_json # => "{\"state\":\"bar\"}"
p! Foo.from_json(f.to_json) # => #<Foo:0x7f124d85ee90 @state=BAR>
It should definitely lose it. JSON
doesn’t have the concept of a Symbol
. The closest representation is String
, hence why it loses the :
.