Skip to content

Commit

Permalink
Merge pull request JuliaIO#140 from TotalVerb/optimize-parser
Browse files Browse the repository at this point in the history
Optimize and fix issues with the `Parser` module
  • Loading branch information
kmsquire authored Jun 13, 2016
2 parents bec34de + dcab8a6 commit 1082e64
Show file tree
Hide file tree
Showing 16 changed files with 551 additions and 361 deletions.
2 changes: 1 addition & 1 deletion data/jsonchecker/fail32.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"Comma instead if closing brace": true,
{"Comma instead of closing brace": true,
1 change: 1 addition & 0 deletions data/jsonchecker/fail34.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"garbage" before : "separator"}
1 change: 1 addition & 0 deletions data/jsonchecker/fail35.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"no separator"
1 change: 1 addition & 0 deletions data/jsonchecker/fail36.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"no closing brace": true
1 change: 1 addition & 0 deletions data/jsonchecker/fail37.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[
1 change: 1 addition & 0 deletions data/jsonchecker/fail38.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{
2 changes: 1 addition & 1 deletion data/jsonchecker/pass01.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@
0.1e1,
1e-1,
1e00,2e+00,2e-00
,"rosebud"]
,"rosebud"]
2 changes: 1 addition & 1 deletion data/jsonchecker/pass02.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]
[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]
73 changes: 1 addition & 72 deletions src/JSON.jl
Original file line number Diff line number Diff line change
Expand Up @@ -216,78 +216,7 @@ end

json(a, indent=0) = sprint(JSON.print, a, indent)

function determine_bracket_type(io::IO)
open_bracket = close_bracket = nothing
while open_bracket == nothing
eof(io) && throw(EOFError())
c = read(io, Char)
if c == '{'
open_bracket = '{'
close_bracket = '}'
elseif c == '['
open_bracket = '['
close_bracket = ']'
elseif c == '\0'
throw(EOFError())
end
end
open_bracket, close_bracket
end

###
# Consume a string (even if it is invalid), with ack to Douglas Crockford.
# On entry we must already have consumed the opening quotation double-quotation mark
# Add the characters of the string to obj
function consumeString(io::IO, obj::IOBuffer)
c = '"'

# When parsing for string values, we must look for " and \ characters.
while true
eof(io) && throw(EOFError())
c = read(io, Char)
if c == '"'
write(obj, c)
return
end
if c == '\\'
write(obj, c)
eof(io) && throw(EOFError())
c = read(io, Char)
end
write(obj, c)
end
throw(EOFError())
end

function parse{T<:Associative}(io::IO; dicttype::Type{T}=Dict)
open_bracket = close_bracket = nothing
try
open_bracket, close_bracket = determine_bracket_type(io)
catch exception
isa(exception, EOFError) && return
end
num_brackets_needed = 1

obj = IOBuffer()
write(obj, open_bracket)

while num_brackets_needed > 0
eof(io) && throw(EOFError())
c = read(io, Char)
write(obj, c)

if c == open_bracket
num_brackets_needed += 1
elseif c == close_bracket
num_brackets_needed -= 1
elseif c == '"'
consumeString(io, obj)
end
end
JSON.parse(takebuf_string(obj); dicttype=dicttype)
end

function parsefile{T<:Associative}(filename::AbstractString; dicttype::Type{T}=Dict, use_mmap=true)
function parsefile{T<:Associative}(filename::AbstractString; dicttype::Type{T}=Dict{Compat.UTF8String, Any}, use_mmap=true)
sz = filesize(filename)
open(filename) do io
s = use_mmap ? Compat.UTF8String(Mmap.mmap(io, Vector{UInt8}, sz)) : readall(io)
Expand Down
Loading

0 comments on commit 1082e64

Please sign in to comment.