Skip to content

Skip any comments that might be present when parsing json #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/Jay.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,27 @@ let ``Can serialize document with guid``() =
let txt = "{\"id\":\"{F842213A-82FB-4EEB-AB75-7CCD18676FD5}\"}"
let j = Json.parse txt
j |> Json.serialize |> should equal txt


[<Fact>]
let ``Single line comment is skipped``() =
let txt =
"""//
{
"test": true // x
}
//"""
let j = Json.parse txt
j?test.AsBool() |> should be True

[<Fact>]
let ``Multiline comment is skipped`` () =
let txt =
"""/**/
{"test": true}
/**//**/ /**/ //
/* {
"test": true
}
*/"""
let j = Json.parse txt
j?test.AsBool() |> should be True
1 change: 1 addition & 0 deletions src/Jay/Jay.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TargetFramework>netstandard2.0</TargetFramework>
<DisableImplicitFSharpCoreReference>true</DisableImplicitFSharpCoreReference>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this because I was getting a 'duplicate package reference' error between the local reference to FSharp.Core 4.5.2 and the implicit reference to 7.x from the v7 SDK


<!-- NuGet config -->
<PackageId>Jay</PackageId>
Expand Down
60 changes: 44 additions & 16 deletions src/Jay/JsonParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ type internal JsonParser(jsonText:string) =
let buf = StringBuilder() // pre-allocate buffers for strings

// Helper functions
let skipWhitespace() =
while i < s.Length && Char.IsWhiteSpace s.[i] do
i <- i + 1

let isNumChar c =
Char.IsDigit c || c = '.' || c='e' || c='E' || c='+' || c='-'

Expand All @@ -43,9 +39,41 @@ type internal JsonParser(jsonText:string) =
let ensure cond =
if not cond then throw()

let rec skipCommentsAndWhitespace () =
let skipComment () =
// Supported comment syntax:
// - // ...{newLine}
// - /* ... */
if i < s.Length && s.[i] = '/' then
i <- i + 1

if i < s.Length && s.[i] = '/' then
i <- i + 1
while i < s.Length && (s.[i] <> '\r' && s.[i] <> '\n') do
i <- i + 1
else if i < s.Length && s.[i] = '*' then
i <- i + 1
while i + 1 < s.Length && s.[i] <> '*' && s.[i + 1] <> '/' do
i <- i + 1
ensure (i + 1 < s.Length && s.[i] = '*' && s.[i + 1] = '/')
i <- i + 2
true

else
false

let skipWhitespace () =
let initialI = i
while i < s.Length && Char.IsWhiteSpace s.[i] do
i <- i + 1
initialI <> i // return true if some whitespace was skipped

if skipWhitespace () || skipComment () then
skipCommentsAndWhitespace ()

// Recursive descent parser for JSON that uses global mutable index
let rec parseValue() =
skipWhitespace()
skipCommentsAndWhitespace()
ensure(i < s.Length)
match s.[i] with
| '"' -> JString(parseString())
Expand Down Expand Up @@ -119,42 +147,42 @@ type internal JsonParser(jsonText:string) =

and parsePair() =
let key = parseString()
skipWhitespace()
skipCommentsAndWhitespace()
ensure(i < s.Length && s.[i] = ':')
i <- i + 1
skipWhitespace()
skipCommentsAndWhitespace()
key, parseValue()

and parseObject() =
ensure(i < s.Length && s.[i] = '{')
i <- i + 1
skipWhitespace()
skipCommentsAndWhitespace()
let pairs = ResizeArray<_>()
if i < s.Length && s.[i] = '"' then
pairs.Add(parsePair())
skipWhitespace()
skipCommentsAndWhitespace()
while i < s.Length && s.[i] = ',' do
i <- i + 1
skipWhitespace()
skipCommentsAndWhitespace()
pairs.Add(parsePair())
skipWhitespace()
skipCommentsAndWhitespace()
ensure(i < s.Length && s.[i] = '}')
i <- i + 1
JObject(pairs.ToArray())

and parseArray() =
ensure(i < s.Length && s.[i] = '[')
i <- i + 1
skipWhitespace()
skipCommentsAndWhitespace()
let vals = ResizeArray<_>()
if i < s.Length && s.[i] <> ']' then
vals.Add(parseValue())
skipWhitespace()
skipCommentsAndWhitespace()
while i < s.Length && s.[i] = ',' do
i <- i + 1
skipWhitespace()
skipCommentsAndWhitespace()
vals.Add(parseValue())
skipWhitespace()
skipCommentsAndWhitespace()
ensure(i < s.Length && s.[i] = ']')
i <- i + 1
JArray(vals.ToArray())
Expand All @@ -169,7 +197,7 @@ type internal JsonParser(jsonText:string) =
// Start by parsing the top-level value
member _.Parse() =
let value = parseValue()
skipWhitespace()
skipCommentsAndWhitespace()
if i <> s.Length then
throw()
value