Skip to content

Commit

Permalink
Associative -> AbstractDict (#230)
Browse files Browse the repository at this point in the history
* Associative -> AbstractDict

* More updates/fixes for 0.7
  • Loading branch information
ararslan authored Jan 15, 2018
1 parent 6ef716a commit d3f95de
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ JSON.json(j)
```julia
JSON.print(io::IO, s::AbstractString)
JSON.print(io::IO, s::Union{Integer, AbstractFloat})
JSON.print(io::IO, n::Void)
JSON.print(io::IO, n::Nothing)
JSON.print(io::IO, b::Bool)
JSON.print(io::IO, a::Associative)
JSON.print(io::IO, v::AbstractVector)
Expand Down
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
julia 0.6
Compat 0.37.0
Compat 0.44.0
Nullables 0.0.1
6 changes: 3 additions & 3 deletions bench/micro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ suite["pretty-print"] = BenchmarkGroup(["serialize"])
struct CustomListType
x::Int
y::Float64
z::Union{CustomListType, Void}
z::Union{CustomListType, Nothing}
end

struct CustomTreeType
x::String
y::Union{CustomTreeType, Void}
z::Union{CustomTreeType, Void}
y::Union{CustomTreeType, Nothing}
z::Union{CustomTreeType, Nothing}
end

list(x) = x == 0 ? nothing : CustomListType(1, 1.0, list(x - 1))
Expand Down
16 changes: 8 additions & 8 deletions src/Parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function _error(message::AbstractString, ps::MemoryParserState)
orig = String(ps.utf8data)
lines = _count_before(orig, '\n', ps.s)
# Replace all special multi-line/multi-space characters with a space.
strnl = replace(orig, r"[\b\f\n\r\t\s]", " ")
strnl = replace(orig, r"[\b\f\n\r\t\s]" => " ")
li = (ps.s > 20) ? ps.s - 9 : 1 # Left index
ri = min(endof(orig), ps.s + 20) # Right index
error(message *
Expand Down Expand Up @@ -316,7 +316,7 @@ end
Parse an integer from the given bytes vector, starting at `from` and ending at
the byte before `to`. Bytes enclosed should all be ASCII characters.
"""
function int_from_bytes(pc::ParserContext{<:Associative,IntType},
function int_from_bytes(pc::ParserContext{<:AbstractDict,IntType},
ps::ParserState,
bytes::Vector{UInt8},
from::Int,
Expand Down Expand Up @@ -377,11 +377,11 @@ end


function unparameterize_type(T::Type)
candidate = typeintersect(T, Associative{String, Any})
candidate = typeintersect(T, AbstractDict{String, Any})
candidate <: Union{} ? T : candidate
end

function parse(str::AbstractString; dicttype::Type{<:Associative}=Dict{String,Any}, inttype::Type{<:Real}=Int64)
function parse(str::AbstractString; dicttype::Type{<:AbstractDict}=Dict{String,Any}, inttype::Type{<:Real}=Int64)
pc = ParserContext{unparameterize_type(dicttype), inttype}()
ps = MemoryParserState(Vector{UInt8}(String(str)), 1)
v = parse_value(pc, ps)
Expand All @@ -392,15 +392,15 @@ function parse(str::AbstractString; dicttype::Type{<:Associative}=Dict{String,An
v
end

function parse(io::IO; dicttype::Type{<:Associative}=Dict{String,Any}, inttype::Type{<:Real}=Int64)
function parse(io::IO; dicttype::Type{<:AbstractDict}=Dict{String,Any}, inttype::Type{<:Real}=Int64)
pc = ParserContext{unparameterize_type(dicttype), inttype}()
ps = StreamingParserState(io)
parse_value(pc, ps)
end

function parsefile(filename::AbstractString;
dicttype::Type{<:Associative}=Dict{String, Any},
inttype::Type{<:Real}=Int64,
function parsefile(filename::AbstractString;
dicttype::Type{<:AbstractDict}=Dict{String, Any},
inttype::Type{<:Real}=Int64,
use_mmap=true)
sz = filesize(filename)
open(filename) do io
Expand Down
12 changes: 7 additions & 5 deletions src/Writer.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Writer

using Compat
using Compat.Dates
using Nullables
using ..Common
Expand All @@ -10,6 +11,7 @@ if VERSION >= v"0.7.0-DEV.2915"
using Unicode
end


"""
Internal JSON.jl implementation detail; do not depend on this type.
Expand All @@ -27,11 +29,11 @@ CompositeTypeWrapper(x) = CompositeTypeWrapper(x, fieldnames(typeof(x)))
lower(x)
Return a value of a JSON-encodable primitive type that `x` should be lowered
into before encoding as JSON. Supported types are: `Associative` to JSON
into before encoding as JSON. Supported types are: `AbstractDict` to JSON
objects, `Tuple` and `AbstractVector` to JSON arrays, `AbstractArray` to nested
JSON arrays, `AbstractString`, `Symbol`, `Enum`, or `Char` to JSON string,
`Integer` and `AbstractFloat` to JSON number, `Bool` to JSON boolean, and
`Void` to JSON null, or any other types with a `show_json` method defined.
`Nothing` to JSON null, or any other types with a `show_json` method defined.
Extensions of this method should preserve the property that the return value is
one of the aforementioned types. If first lowering to some intermediate type is
Expand Down Expand Up @@ -263,7 +265,7 @@ function show_json(io::SC, s::CS, x::Union{Integer, AbstractFloat})
end
end

show_json(io::SC, ::CS, ::Void) = show_null(io)
show_json(io::SC, ::CS, ::Nothing) = show_null(io)

function show_json(io::SC, s::CS, a::Nullable)
if isnull(a)
Expand All @@ -273,7 +275,7 @@ function show_json(io::SC, s::CS, a::Nullable)
end
end

function show_json(io::SC, s::CS, a::Associative)
function show_json(io::SC, s::CS, a::AbstractDict)
begin_object(io)
for kv in a
show_pair(io, s, kv)
Expand Down Expand Up @@ -310,7 +312,7 @@ Serialize a multidimensional array to JSON in column-major format. That is,
function show_json(io::SC, s::CS, A::AbstractArray{<:Any,n}) where n
begin_array(io)
newdims = ntuple(_ -> :, n - 1)
for j in indices(A, n)
for j in Compat.axes(A, n)
show_element(io, s, view(A, newdims..., j))
end
end_array(io)
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ end
end

@testset "Integration" begin
# ::Void values should be encoded as null
# ::Nothing values should be encoded as null
testDict = Dict("a" => nothing)
nothingJson = JSON.json(testDict)
nothingDict = JSON.parse(nothingJson)
Expand Down

0 comments on commit d3f95de

Please sign in to comment.