I am trying to save Complex numbers in json files formatted in a python compatible way as
using JSON3
c = 1+5im
d = Dict("c" => c)
jsonstring = JSON3.pretty(d)
#= should be
{
"c" : 1+5j
}
=#
This is how far I got, using the documentation
using JSON3
using StructTypes
StructTypes.StructType(::Type{Complex{RT}}) where {RT<:Real} = JSON3.RawType()
function StructTypes.construct(
::Type{Complex{RT}}, x::JSON3.RawValue) where {RT<:Real}
return parse(Complex{RT}, unsafe_string(pointer(x.bytes, x.pos), x.len))
end
function JSON3.rawbytes(x::Complex{RT}) where {RT<:Real}
str = x.im > zero(RT) ? "$(x.re)+$(x.im)j" : "$(x.re)$(x.im)j"
return transcode(UInt8, str)
end
c = 1+5im
d = Dict("c" => c)
JSON3.write(d)
# works fine and returns "{\"c\":1+5j}"
JSON3.read(JSON3.write(c), Complex{Int})
# works fine and returns the original number
JSON3.pretty(d)
#= crashes with
ERROR: ArgumentError: invalid JSON at byte position 7 while parsing type JSON3.Object:
ExpectedComma {"a":1+7j}
=#
Is it possible to create such a JSON serialisation of complex numbers using JSON3? And if yes, what am I missing to make JSON3.pretty work here?
I think this also relates to the more general question: If I define a serialisation via rawbytes
for a type, how do I tell JSON3
to use the correct deserialisation routine? It seems that the error I am getting stems from the unexpected “+” sign in the json string.