Skip to content

Commit

Permalink
Merge pull request #1538 from aviks/json
Browse files Browse the repository at this point in the history
Improve JSON parsing performance
  • Loading branch information
JeffBezanson committed Nov 16, 2012
2 parents 779bdab + ba5dd04 commit 3efaba0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 25 deletions.
29 changes: 24 additions & 5 deletions extras/json.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@
#Original BSD Licence, (c) 2011, François Glineur


module Json
module JSON

using Base

export parse_json
export parse


function parse_json(strng::String)
function parse(strng::String)

pos::Int = 1
len::Int = length(strng)

# String delimiters and escape characters are identified beforehand to improve speed
len_esc::Int = 0
index_esc::Int = 1

esc_locations::Array{Int64,1} = map(x->x.offset, [each_match(r"[\"\\\\]", strng)...])
len_esc=length(esc_locations)

pos = 1
len = length(strng)

function parse_object()
parse_char('{')
Expand Down Expand Up @@ -87,6 +95,17 @@ function parse_json(strng::String)
end
str = ""
while pos <= len
while index_esc <= len_esc && esc_locations[index_esc] < pos
index_esc = index_esc + 1;
end
if index_esc > len_esc
str = strcat(str, strng[pos:end]);
pos = len + 1;
break;
else
str = strcat(str, strng[pos:esc_locations[index_esc]-1]);
pos = esc_locations[index_esc];
end
nc = strng[pos]
if nc == '"'
pos = nextind(strng, pos)
Expand Down
39 changes: 19 additions & 20 deletions test/json.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
load("json")
using Json

load("test/json_samples")


@assert parse_json(a) != nothing
@assert parse_json(b) != nothing
@assert parse_json(c) != nothing
@assert parse_json(d) != nothing
@assert JSON.parse(a) != nothing
@assert JSON.parse(b) != nothing
@assert JSON.parse(c) != nothing
@assert JSON.parse(d) != nothing

j=parse_json(e)
j=JSON.parse(e)
@assert j!= nothing
typeof(j) == Dict{String, Any}
@assert length(j) == 1
Expand All @@ -24,27 +23,27 @@ typeof(j["menu"]) == Dict{String, Any}



@assert parse_json(gmaps) != nothing
@assert parse_json(colors1) != nothing
@assert parse_json(colors2) != nothing
@assert parse_json(colors3) != nothing
@assert parse_json(twitter) != nothing
@assert parse_json(facebook) != nothing
@assert JSON.parse(gmaps) != nothing
@assert JSON.parse(colors1) != nothing
@assert JSON.parse(colors2) != nothing
@assert JSON.parse(colors3) != nothing
@assert JSON.parse(twitter) != nothing
@assert JSON.parse(facebook) != nothing

k=parse_json(flickr)
k=JSON.parse(flickr)
@assert k!= nothing
@assert k["totalItems"] == 222
@assert k["items"][1]["description"][12] == '\"'
@assert parse_json(youtube) != nothing
@assert parse_json(iphone) != nothing
@assert parse_json(customer) != nothing
@assert parse_json(product) != nothing
@assert parse_json(interop) != nothing
@assert JSON.parse(youtube) != nothing
@assert JSON.parse(iphone) != nothing
@assert JSON.parse(customer) != nothing
@assert JSON.parse(product) != nothing
@assert JSON.parse(interop) != nothing

u=parse_json(unicode)
u=JSON.parse(unicode)
@assert u!=nothing
@assert u["অলিম্পিকস"]["রেকর্ড"][2]["Marathon"] == "জনি হেইস"

#Uncomment while doing timing tests
@time for i=1:100 ; parse_json(d) ; end
#@time for i=1:100 ; JSON.parse(d) ; end

0 comments on commit 3efaba0

Please sign in to comment.