diff --git a/test/async.jl b/test/async.jl new file mode 100644 index 0000000..74831c8 --- /dev/null +++ b/test/async.jl @@ -0,0 +1,107 @@ +finished_async_tests = RemoteChannel() + +@async begin + s = listen(7777) + s = accept(s) + + Base.start_reading(s) + + @test JSON.parse(s) != nothing # a + @test JSON.parse(s) != nothing # b + validate_c(s) # c + @test JSON.parse(s) != nothing # d + validate_svg_tviewer_menu(s) # svg_tviewer_menu + @test JSON.parse(s) != nothing # gmaps + @test JSON.parse(s) != nothing # colors1 + @test JSON.parse(s) != nothing # colors2 + @test JSON.parse(s) != nothing # colors3 + @test JSON.parse(s) != nothing # twitter + @test JSON.parse(s) != nothing # facebook + validate_flickr(s) # flickr + @test JSON.parse(s) != nothing # youtube + @test JSON.parse(s) != nothing # iphone + @test JSON.parse(s) != nothing # customer + @test JSON.parse(s) != nothing # product + @test JSON.parse(s) != nothing # interop + validate_unicode(s) # unicode + @test JSON.parse(s) != nothing # issue5 + @test JSON.parse(s) != nothing # dollars + @test JSON.parse(s) != nothing # brackets + + put!(finished_async_tests, nothing) +end + +w = connect("localhost", 7777) + +@test JSON.parse(a) != nothing +write(w, a) + +@test JSON.parse(b) != nothing +write(w, b) + +validate_c(c) +write(w, c) + +@test JSON.parse(d) != nothing +write(w, d) + +validate_svg_tviewer_menu(svg_tviewer_menu) +write(w, svg_tviewer_menu) + +@test JSON.parse(gmaps) != nothing +write(w, gmaps) + +@test JSON.parse(colors1) != nothing +write(w, colors1) + +@test JSON.parse(colors2) != nothing +write(w, colors2) + +@test JSON.parse(colors3) != nothing +write(w, colors3) + +@test JSON.parse(twitter) != nothing +write(w, twitter) + +@test JSON.parse(facebook) != nothing +write(w, facebook) + +validate_flickr(flickr) +write(w, flickr) + +@test JSON.parse(youtube) != nothing +write(w, youtube) + +@test JSON.parse(iphone) != nothing +write(w, iphone) + +@test JSON.parse(customer) != nothing +write(w, customer) + +@test JSON.parse(product) != nothing +write(w, product) + +@test JSON.parse(interop) != nothing +write(w, interop) + +validate_unicode(unicode) +write(w, unicode) + +# issue #5 +issue5 = "[\"A\",\"B\",\"C\\n\"]" +JSON.parse(issue5) +write(w, issue5) + +# $ escaping issue +dollars = ["all of the \$s", "µniçø∂\$"] +json_dollars = json(dollars) +@test JSON.parse(json_dollars) != nothing +write(w, json_dollars) + +# unmatched brackets +brackets = Dict("foo"=>"ba}r", "be}e]p"=>"boo{p") +json_brackets = json(brackets) +@test JSON.parse(json_brackets) != nothing +write(w, json_dollars) + +fetch(finished_async_tests) diff --git a/test/enum.jl b/test/enum.jl new file mode 100644 index 0000000..ead3d99 --- /dev/null +++ b/test/enum.jl @@ -0,0 +1,4 @@ +@enum Animal zebra aardvark horse +@test json(zebra) == "\"zebra\"" +@test json([aardvark, horse, Dict("z" => zebra)]) == + "[\"aardvark\",\"horse\",{\"z\":\"zebra\"}]" diff --git a/test/indentation.jl b/test/indentation.jl new file mode 100644 index 0000000..98fa5f0 --- /dev/null +++ b/test/indentation.jl @@ -0,0 +1,10 @@ +# check indented json has same final value as non indented +fb = JSON.parse(facebook) +fbjson1 = json(fb, 2) +fbjson2 = json(fb) +@test JSON.parse(fbjson1) == JSON.parse(fbjson2) + +ev = JSON.parse(svg_tviewer_menu) +ejson1 = json(ev, 2) +ejson2 = json(ev) +@test JSON.parse(ejson1) == JSON.parse(ejson2) diff --git a/test/json-samples.jl b/test/json-samples.jl index 8e2b791..5750da7 100644 --- a/test/json-samples.jl +++ b/test/json-samples.jl @@ -37,32 +37,41 @@ b="{ } " -c="{\"widget\": { - \"debug\": \"on\", - \"window\": { - \"title\": \"Sample Konfabulator Widget\", - \"name\": \"main_window\", - \"width\": 500, - \"height\": 500 +const c = """ +{"widget": { + "debug": "on", + "window": { + "title": "Sample Konfabulator Widget", + "name": "main_window", + "width": 500, + "height": 500 }, - \"image\": { - \"src\": \"Images/Sun.png\", - \"name\": \"sun1\", - \"hOffset\": 250, - \"vOffset\": 250, - \"alignment\": \"center\" + "image": { + "src": "Images/Sun.png", + "name": "sun1", + "hOffset": 250, + "vOffset": 250, + "alignment": "center" }, - \"text\": { - \"data\": \"Click Here\", - \"size\": 36.5, - \"style\": \"bold\", - \"name\": \"text1\", - \"hOffset\": 250, - \"vOffset\": 100, - \"alignment\": \"center\", - \"onMouseUp\": \"sun1.opacity = (sun1.opacity / 100) * 90;\" + "text": { + "data": "Click Here", + "size": 36.5, + "style": "bold", + "name": "text1", + "hOffset": 250, + "vOffset": 100, + "alignment": "center", + "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;" } -}}" +}}""" +function validate_c(c) + j = JSON.parse(c) + @test j != nothing + @test typeof(j["widget"]["image"]["hOffset"]) == Int64 + @test j["widget"]["image"]["hOffset"] == 250 + @test typeof(j["widget"]["text"]["size"]) == Float64 + @test j["widget"]["text"]["size"] == 36.5 +end d = "{\"web-app\": { \"servlet\": [ @@ -153,33 +162,48 @@ d = "{\"web-app\": { \"taglib-uri\": \"cofax.tld\", \"taglib-location\": \"/WEB-INF/tlds/cofax.tld\"}}}" -e="{\"menu\": { - \"header\": \"SVG\\tViewer\\u03b1\", - \"items\": [ - {\"id\": \"Open\"}, - {\"id\": \"OpenNew\", \"label\": \"Open New\"}, +const svg_tviewer_menu = """ +{"menu": { + "header": "SVG\\tViewer\\u03b1", + "items": [ + {"id": "Open"}, + {"id": "OpenNew", "label": "Open New"}, null, - {\"id\": \"ZoomIn\", \"label\": \"Zoom In\"}, - {\"id\": \"ZoomOut\", \"label\": \"Zoom Out\"}, - {\"id\": \"OriginalView\", \"label\": \"Original View\"}, + {"id": "ZoomIn", "label": "Zoom In"}, + {"id": "ZoomOut", "label": "Zoom Out"}, + {"id": "OriginalView", "label": "Original View"}, null, - {\"id\": \"Quality\"}, - {\"id\": \"Pause\"}, - {\"id\": \"Mute\"}, + {"id": "Quality"}, + {"id": "Pause"}, + {"id": "Mute"}, null, - {\"id\": \"Find\", \"label\": \"Find...\"}, - {\"id\": \"FindAgain\", \"label\": \"Find Again\"}, - {\"id\": \"Copy\"}, - {\"id\": \"CopyAgain\", \"label\": \"Copy Again\"}, - {\"id\": \"CopySVG\", \"label\": \"Copy SVG\"}, - {\"id\": \"ViewSVG\", \"label\": \"View SVG\"}, - {\"id\": \"ViewSource\", \"label\": \"View Source\"}, - {\"id\": \"SaveAs\", \"label\": \"Save As\"}, + {"id": "Find", "label": "Find..."}, + {"id": "FindAgain", "label": "Find Again"}, + {"id": "Copy"}, + {"id": "CopyAgain", "label": "Copy Again"}, + {"id": "CopySVG", "label": "Copy SVG"}, + {"id": "ViewSVG", "label": "View SVG"}, + {"id": "ViewSource", "label": "View Source"}, + {"id": "SaveAs", "label": "Save As"}, null, - {\"id\": \"Help\"}, - {\"id\": \"About\", \"label\": \"About Adobe SVG Viewer...\"} + {"id": "Help"}, + {"id": "About", "label": "About Adobe SVG Viewer..."} ] -}}" +}}""" +function validate_svg_tviewer_menu(str) + j = JSON.parse(str) + @test j != nothing + @test typeof(j) == Dict{String, Any} + @test length(j) == 1 + @test typeof(j["menu"]) == Dict{String, Any} + @test length(j["menu"]) == 2 + @test j["menu"]["header"] == "SVG\tViewerα" + @test isa(j["menu"]["items"], Vector{Any}) + @test length(j["menu"]["items"]) == 22 + @test j["menu"]["items"][3] == nothing + @test j["menu"]["items"][2]["id"] == "OpenNew" + @test j["menu"]["items"][2]["label"] == "Open New" +end #Example JSON strings from http://www.jquery4u.com/json/10-example-json-files/ @@ -343,27 +367,33 @@ facebook= "{ ] }" -flickr = "{ - \"title\": \"Talk On Travel Pool\", - \"link\": \"http://www.flickr.com/groups/talkontravel/pool/\", - \"description\": \"Travel and vacation photos from around the world.\", - \"modified\": \"2009-02-02T11:10:27Z\", - \"generator\": \"http://www.flickr.com/\", - \"totalItems\":222, - \"items\": [ +const flickr = """{ + "title": "Talk On Travel Pool", + "link": "http://www.flickr.com/groups/talkontravel/pool/", + "description": "Travel and vacation photos from around the world.", + "modified": "2009-02-02T11:10:27Z", + "generator": "http://www.flickr.com/", + "totalItems":222, + "items": [ { - \"title\": \"View from the hotel\", - \"link\": \"http://www.flickr.com/photos/33112458@N08/3081564649/in/pool-998875@N22\", - \"media\": {\"m\":\"http://farm4.static.flickr.com/3037/3081564649_4a6569750c_m.jpg\"}, - \"date_taken\": \"2008-12-04T04:43:03-08:00\", - \"description\": \"
Talk On Travel<\/a> has added a photo to the pool:<\/p> <\/a><\/p> \",
- \"published\": \"2008-12-04T12:43:03Z\",
- \"author\": \"nobody@flickr.com (Talk On Travel)\",
- \"author_id\": \"33112458@N08\",
- \"tags\": \"spain dolphins tenerife canaries lagomera aqualand playadelasamericas junglepark losgigantos loscristines talkontravel\"
+ "title": "View from the hotel",
+ "link": "http://www.flickr.com/photos/33112458@N08/3081564649/in/pool-998875@N22",
+ "media": {"m":"http://farm4.static.flickr.com/3037/3081564649_4a6569750c_m.jpg"},
+ "date_taken": "2008-12-04T04:43:03-08:00",
+ "description": "