Hi.
Given this…
# serial_tester.cr
require "sdl-crystal-bindings/sdl3-crystal-bindings"
require "json"
class SerialTester
include JSON::Serializable
property width : UInt32
property height : UInt32
@[JSON::Field(ignore: true)]
getter renderer : LibSDL::Renderer*
@[JSON::Field(ignore: true)]
getter pixels : Array(UInt32)
def initialize(@width, @height, @renderer)
size = @width * @height
@pixels = Array(UInt32).new(size, 0x00000000)
end
end
# app.cr
@renderer = LibSDL::create_renderer etc etc...
...
st = SerialTester.new(500, 500, @renderer)
j = st.to_json()
puts j
# outputs {"width":500,"height":500} as expected
and that :
# app.cr
@renderer = LibSDL::create_renderer etc etc...
st = SerialTester.new(500, 500, @renderer)
j = st.to_json()
st2 = SerialTester.from_json(j)
# compilation error
In /opt/homebrew/Cellar/crystal/1.16.3/share/crystal/src/json/serialization.cr:181:9
181 | def initialize(*, __pull_for_json_serializable pull : ::JSON::PullParser)
^---------
Error: instance variable '@renderer' of SerialTester was not initialized in this 'initialize', rendering it nilable
I couldn’t find a lot of JSON::Serializable examples for how to deal with custom initialize methods and am wondering what is the standard way to work with that.