quinnj / JSON3.jl

Other
214 stars 47 forks source link

`StructTypes.excludes` not working #224

Open trigaten opened 2 years ago

trigaten commented 2 years ago

I have a struct which I am trying to save as a JSON. I would like to exclude one of its fields. StructTypes.excludes seems like the proper way to do this, but I getting the below error when using it. What might be going wrong?

MRE

using JSON3

mutable struct example_struct
    x
    y
    z
    example_struct() = new(1,2,3)
end

StructTypes.StructType(::Type{example_struct}) = StructTypes.Mutable()
StructTypes.excludes(::Type{example_struct}) = (:x)

ex = example_struct()

JSON3.write(ex)
ERROR: MethodError: no method matching symbolin(::Symbol, ::Symbol)
Closest candidates are:
  symbolin(::Union{Bool, Tuple{Vararg{Symbol}}}, ::Any) at ~/.julia/packages/StructTypes/Cmlkm/src/StructTypes.jl:604
Stacktrace:
 [1] foreachfield
   @ ~/.julia/packages/StructTypes/Cmlkm/src/StructTypes.jl:634 [inlined]
 [2] #write#74
   @ ~/.julia/packages/JSON3/GoF7x/src/write.jl:132 [inlined]
 [3] write
   @ ~/.julia/packages/JSON3/GoF7x/src/write.jl:130 [inlined]
 [4] write(obj::example_struct; kw::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ JSON3 ~/.julia/packages/JSON3/GoF7x/src/write.jl:39
 [5] write(obj::example_struct)
   @ JSON3 ~/.julia/packages/JSON3/GoF7x/src/write.jl:37
 [6] top-level scope
   @ REPL[28]:1
 [7] top-level scope
   @ ~/.julia/packages/CUDA/DfvRa/src/initialization.jl:52
JakubPorubcansky commented 2 years ago

The fields you want to exclude should be specified as a tuple of symbols, see the docs. This should work:

StructTypes.excludes(::Type{example_struct}) = (:x,)