Skip to content

Commit

Permalink
Merge pull request #34 from lwabeke/betterSupportForNothing
Browse files Browse the repository at this point in the history
Better support for nothing
  • Loading branch information
lwabeke authored Oct 26, 2020
2 parents c97601c + 3774e41 commit b10a6cb
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Unmarshal"
uuid = "cbff2730-442d-58d7-89d1-8e530c41eb02"
authors = ["Leon Wabeke <[email protected]>", "Sunoru <[email protected]>"]
version = "0.4.1"
version = "0.4.2"

[deps]
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
Expand Down
18 changes: 13 additions & 5 deletions src/Unmarshal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,18 @@ function unmarshal(DT :: Type, parsedJson :: AbstractDict, verbose :: Bool = fal
prettyPrint(verboseLvl-1, "\\--> $(iter) <: $(DTNext) ")
end

if !haskey(parsedJson, string(iter))
if !haskey(parsedJson, string(iter)) || isnothing(parsedJson[string(iter)])
# check whether DTNext is compatible with any scheme for missing values
val = if DTNext <: Nullable
DTNext()
elseif Missing <: DTNext
missing
elseif Nothing <: DTNext
Nothing()
else
elseif !haskey(parsedJson, string(iter))
throw(ArgumentError("Key $(string(iter)) is missing from the structure $DT, and field is neither Nullable nor Missings nor Nothing-compatible"))
else
throw(ArgumentError("Key $(string(iter)) is null, but the field is neither Nullable nor Missings nor Nothing-compatible"))
end
else
val = unmarshal( DTNext, parsedJson[string(iter)], verbose, verboseLvl)
Expand Down Expand Up @@ -229,9 +231,15 @@ unmarshal(::Type{T}, x::Number, verbose :: Bool = false, verboseLvl :: Int = 0)
unmarshal(::Type{Nullable{T}}, x, verbose :: Bool = false, verboseLvl :: Int = 0) where T = Nullable(unmarshal(T, x))
unmarshal(::Type{Nullable{T}}, x::Nothing, verbose :: Bool = false, verboseLvl :: Int = 0) where T = Nullable{T}()
unmarshal(::Type{Union{T,Missing}}, x, verbose :: Bool = false, verboseLvl :: Int = 0) where T = unmarshal(T, x, verbose, verboseLvl)
unmarshal(::Type{Union{T,Nothing}}, x, verbose :: Bool = false, verboseLvl :: Int = 0) where T = unmarshal(T, x, verbose, verboseLvl)
unmarshal(::Type{Union{T,Nothing}}, x::Nothing, verbose :: Bool = false, verboseLvl :: Int = 0) where T = nothing
unmarshal(::Type{Union{T,Nothing}}, x::T, verbose :: Bool = false, verboseLvl :: Int = 0) where T = unmarshal(T, x, verbose, verboseLvl)

unmarshal(T::Type, x, verbose :: Bool = false, verboseLvl :: Int = 0) =
throw(ArgumentError("no unmarshal function defined to convert $(typeof(x)) to $(T); consider providing a specialization"))
function unmarshal(T::Type, x, verbose :: Bool = false, verboseLvl :: Int = 0)
try
T(x)
catch
throw(ArgumentError("no unmarshal function defined to convert $(typeof(x)) to $(T); consider providing a specialization"))
end
end

end # module
6 changes: 6 additions & 0 deletions src/lazyjson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,9 @@ function unmarshal(DT :: Type{Pair{TF, TS}}, parsedJson :: LazyJSON.Object, verb

(firstVal => secondVal)
end

unmarshal(::Type{T}, x::Number, verbose :: Bool = false, verboseLvl :: Int = 0) where T <: Number = T(x)
unmarshal(::Type{Union{T,Nothing}}, x::LazyJSON.Number, verbose :: Bool = false, verboseLvl :: Int = 0) where T <: Number = T(x)
unmarshal(::Type{Nullable{T}}, x::LazyJSON.Number, verbose :: Bool = false, verboseLvl :: Int = 0) where T <: Number = Nullable(T(x))
unmarshal(::Type{Union{T,Missing}}, x::LazyJSON.Number, verbose :: Bool = false, verboseLvl :: Int = 0) where T <: Number= T(x)

36 changes: 36 additions & 0 deletions test/unmarshal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,39 @@ p = ( 24 => Dict("hours"=>24, "min"=>60) )
q = ( 24 => "Test" )
@test Unmarshal.unmarshal(typeof(p), JSON.parse(JSON.json(p)), true) == p
@test_throws ArgumentError Unmarshal.unmarshal(typeof(p), JSON.parse(JSON.json(q)), true)

struct User
id::Union{Int64, Nothing}
id_str::Union{String, Nothing}
created_at::Union{String, Nothing}
name::Union{String, Nothing}
screen_name::Union{String, Nothing}
location::Union{String, Nothing}
statuses_count::Union{Int64, Nothing}
followers_count::Union{Int64, Nothing}
description::Union{String, Nothing}
profile_image_url::Union{String, Nothing}
end

struct Tweet
id::Union{Int64, Nothing}
id_str::Union{String, Nothing}
created_at::Union{String, Nothing}
favorite_count::Union{Int, Nothing}
retweet_count::Union{Int, Nothing}
full_text::Union{String, Nothing}
lang::Union{String, Nothing}
place::Union{String, Nothing}
truncated::Union{Bool, Nothing}
user::User
end

json_str = "{\"id_str\":\"1305501948074835974\",\"created_at\":\"Mon Sep 14 13:41:34 +0000 2020\",\"place\":null,\"id\":1305501948074835974,\"user\":{\"name\":\"Donald J. Trump\",\"id_str\":\"25073877\",\"created_at\":\"Wed Mar 18 13:46:38 +0000 2009\",\"id\":25073877}}"
@test isa(Unmarshal.unmarshal(Tweet, JSON.parse(json_str), true),Tweet)

mutable struct TestStruct
test::Symbol
end
packedDict = Dict{String,Any}("test" => "testValue")
@test isa(Unmarshal.unmarshal(TestStruct, packedDict), TestStruct)

2 comments on commit b10a6cb

@lwabeke
Copy link
Owner Author

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/23678

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 v0.4.2 -m "<description of version>" b10a6cb8892e311932ffc8020f7aa6ebe3f1bde6
git push origin v0.4.2

Please sign in to comment.