Skip to content

Commit

Permalink
Add singleton support (#191)
Browse files Browse the repository at this point in the history
* Add singleton support

* add back commented test

* bump JSON3 and StructTypes version

* bump structtypes version
  • Loading branch information
Wynand authored Sep 6, 2022
1 parent 3c6e0b0 commit 3d542d3
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "JSON3"
uuid = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
authors = ["Jacob Quinn <quinn.jacobd@gmail.com>"]
version = "1.9.6"
version = "1.10.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/structs.jl
Original file line number Diff line number Diff line change
@@ -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

Expand Down
3 changes: 3 additions & 0 deletions src/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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`"))
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ struct D
c::String
end

struct ParametricSingleton{T} end

abstract type Vehicle end

struct Car <: Vehicle
Expand Down Expand Up @@ -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()\""

This comment has been minimized.

Copy link
@quinnj

quinnj Feb 16, 2023

Owner

@Wynand, sorry for the cold ping on an old commit. I'm going back over some of the behaviors of JSON3 and I can't quite remember why we changed to outputting singletons as strings instead of like an empty object. Could you walk me through the reasoning here? (that is, if you even remember yourself!)

This comment has been minimized.

Copy link
@Wynand

Wynand Feb 17, 2023

Author Contributor

IIRC this was to both show what the singleton was (where if it was an empty object you wouldn't know which singleton it was), and "()" was added to the end to show that it's an instantiated singleton instead of a singleton class

@test JSON3.write(ParametricSingleton{Int64}()) == "\"ParametricSingleton{Int64}()\""

@test_throws MethodError JSON3.read("{}", C)
StructTypes.StructType(::Type{C}) = StructTypes.Struct()

@test JSON3.read("{}", C) == C()
Expand Down

2 comments on commit 3d542d3

@quinnj
Copy link
Owner

@quinnj quinnj commented on 3d542d3 Sep 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/67936

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.10.0 -m "<description of version>" 3d542d32bc5d9fddce2feef7ce577db626e0195c
git push origin v1.10.0

Please sign in to comment.