From 3d542d32bc5d9fddce2feef7ce577db626e0195c Mon Sep 17 00:00:00 2001 From: Wynand Badenhorst Date: Tue, 6 Sep 2022 16:25:11 +0100 Subject: [PATCH] Add singleton support (#191) * Add singleton support * add back commented test * bump JSON3 and StructTypes version * bump structtypes version --- Project.toml | 4 ++-- src/structs.jl | 2 +- src/write.jl | 3 +++ test/runtests.jl | 7 +++++-- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index 491ea19..548ab40 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "JSON3" uuid = "0f8b85d8-7281-11e9-16c2-39a750bddbf1" authors = ["Jacob Quinn "] -version = "1.9.6" +version = "1.10.0" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" @@ -12,7 +12,7 @@ UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" [compat] Parsers = "0.3, 1, 2" -StructTypes = "1.5" +StructTypes = "1.10" julia = "1.6" [extras] diff --git a/src/structs.jl b/src/structs.jl index 0f65267..9feac0b 100644 --- a/src/structs.jl +++ b/src/structs.jl @@ -1,4 +1,4 @@ -import StructTypes: StructType, CustomStruct, DictType, ArrayType, StringType, NumberType, BoolType, NullType, NoStructType, Struct, OrderedStruct, Mutable, construct, AbstractType, subtypes, subtypekey +import StructTypes: StructType, CustomStruct, DictType, ArrayType, StringType, NumberType, BoolType, NullType, NoStructType, SingletonType, Struct, OrderedStruct, Mutable, construct, AbstractType, subtypes, subtypekey struct RawType <: StructType end diff --git a/src/write.jl b/src/write.jl index d69f77a..1387329 100644 --- a/src/write.jl +++ b/src/write.jl @@ -86,6 +86,7 @@ write(::NumberType, buf, pos, len, ::Type{T}; kw...) where {T} = write(StringTyp write(::NullType, buf, pos, len, ::Type{T}; kw...) where {T} = write(StringType(), buf, pos, len, Base.string(T)) write(::BoolType, buf, pos, len, ::Type{T}; kw...) where {T} = write(StringType(), buf, pos, len, Base.string(T)) write(::AbstractType, buf, pos, len, ::Type{T}; kw...) where {T} = write(StringType(), buf, pos, len, Base.string(T)) +write(::SingletonType, buf, pos, len, ::Type{T}; kw...) where {T} = write(StringType(), buf, pos, len, Base.string(T)) write(::NoStructType, buf, pos, len, ::Type{T}; kw...) where {T} = write(StringType(), buf, pos, len, Base.string(T)) write(::NoStructType, buf, pos, len, ::T; kw...) where {T} = throw(ArgumentError("$T doesn't have a defined `StructTypes.StructType`")) @@ -191,6 +192,8 @@ function write(::BoolType, buf, pos, len, x::Bool; kw...) return buf, pos, len end +write(::SingletonType, buf, pos, len, x; kw...) = write(StringType(), buf, pos, len, x; kw...) + # adapted from base/intfuncs.jl function write(::NumberType, buf, pos, len, y::Integer; kw...) x, neg = Base.split_sign(y) diff --git a/test/runtests.jl b/test/runtests.jl index 563dfdf..70fdf60 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -38,6 +38,8 @@ struct D c::String end +struct ParametricSingleton{T} end + abstract type Vehicle end struct Car <: Vehicle @@ -436,9 +438,10 @@ obj2 = JSON3.read(""" @test_throws ArgumentError JSON3.read!("{\"a\": 1, a", A(1, 2, 3, 4)) @test_throws ArgumentError JSON3.read!("}", A(1, 2, 3, 4)) -@test_throws ArgumentError JSON3.read("{}", C) -@test_throws ArgumentError JSON3.write(C()) +@test JSON3.write(C()) == "\"C()\"" +@test JSON3.write(ParametricSingleton{Int64}()) == "\"ParametricSingleton{Int64}()\"" +@test_throws MethodError JSON3.read("{}", C) StructTypes.StructType(::Type{C}) = StructTypes.Struct() @test JSON3.read("{}", C) == C()