Skip to content

Commit 5054414

Browse files
Tracy WadleighTotalVerb
authored andcommitted
Added failing test and generalization of show_json for composites.
1 parent 908d5d1 commit 5054414

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/Writer.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct CompositeTypeWrapper{T}
2020
end
2121

2222
CompositeTypeWrapper(x, syms) = CompositeTypeWrapper(x, collect(syms))
23-
CompositeTypeWrapper(x) = CompositeTypeWrapper(x, fieldnames(typeof(x)))
23+
CompositeTypeWrapper(x) = CompositeTypeWrapper(x, propertynames(x))
2424

2525
"""
2626
lower(x)
@@ -282,7 +282,7 @@ end
282282
function show_json(io::SC, s::CS, x::CompositeTypeWrapper)
283283
begin_object(io)
284284
for fn in x.fns
285-
show_pair(io, s, fn, getfield(x.wrapped, fn))
285+
show_pair(io, s, fn, getproperty(x.wrapped, fn))
286286
end
287287
end_object(io)
288288
end

test/lowering.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ JSON.lower(v::Type151{T}) where {T} = Dict(:type => T, :value => v.x)
2424
fixednum = Fixed{Int16, 15}(0.1234)
2525
@test JSON.parse(JSON.json(fixednum)) == convert(Float64, fixednum)
2626

27-
# test that the default string-serialization of enums can be overriden by
27+
# test that the default string-serialization of enums can be overridden by
2828
# `lower` if needed
2929
@enum Fruit apple orange banana
3030
JSON.lower(x::Fruit) = string("Fruit: ", x)
@@ -34,4 +34,13 @@ JSON.lower(x::Fruit) = string("Fruit: ", x)
3434
JSON.lower(x::Vegetable) = Dict(string(x) => Int(x))
3535
@test JSON.json(potato) == "{\"potato\":2}"
3636

37+
# test that the default lowering for compound types can be overridden by `propertynames` and
38+
# `getproperty` if needed
39+
struct Type152
40+
x::Int
41+
end
42+
Base.propertynames(v::Type152) = (:type, fieldnames(Type152)...)
43+
Base.getproperty(v::Type152, s::Symbol) = s == :type ? :Type152 : getfield(v, s)
44+
@test JSON.json(Type152(152)) == "{\"type\":\"Type152\",\"x\":152}"
45+
3746
end

0 commit comments

Comments
 (0)