Skip to content

Commit

Permalink
courtesy of [email protected], parse(IO) now handles strings with …
Browse files Browse the repository at this point in the history
…embedded closing brackets and braces
  • Loading branch information
WestleyArgentum committed Jun 6, 2013
1 parent 816062f commit 497af34
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/JSON.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,32 @@ function determine_bracket_type(io::IO)
return (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 (c = read(io, Char)) != '\0'
if c == '"'
write(obj, c)
return
end
if c == '\\'
write(obj, c)
c = read(io, Char)
if c == '\0'
error("EOF while attempting to read a string")
end
end
write(obj, c)
end
error("EOF while attempting to read a string")
end

function parse(io::IO)
open_bracket, close_bracket = determine_bracket_type(io)
num_brackets_needed = 1
Expand All @@ -105,6 +131,8 @@ function parse(io::IO)
num_brackets_needed += 1
elseif c == close_bracket
num_brackets_needed -= 1
elseif c == '"'
consumeString(io, obj)
end
end

Expand Down

0 comments on commit 497af34

Please sign in to comment.