Saving complex numbers using JSON3

JSON allows objects, so on the Julia side you can define a suitable struct and write it into JSON, like this:

using JSON3, StructTypes

struct JCX
    real::Float64
    imaginary::Float64
end
JCX(x) = JCX(reim(x)...)

StructTypes.StructType(::Type{JCX}) = StructTypes.Struct()

c = 2.72+3.14im

j = JSON3.write(JCX(c))
#  {"real":2.72,"imaginary":3.14}
c1 = JSON3.read(j, JCX)
#  JCX(2.72, 3.14)
3 Likes