Fixed various issue with parsing string (such as \t and related),

Implemented escaping of slash '/' only in case of '</' to avoid potential issue with javascript and </script>
Many feature renaming to match Eiffel style and naming convention, kept previous feature as obsolete.
Restructured the library to make easy extraction of "converter" classes if needed in the future.
Updated part of the code to use new feature names.
This commit is contained in:
2014-09-24 20:08:12 +02:00
parent de282948e6
commit 19dbbf89e7
40 changed files with 1121 additions and 788 deletions

View File

@@ -48,7 +48,7 @@ feature {NONE} -- Implementation
name_key: JSON_STRING
-- Author's name label.
once
create Result.make_json ("name")
create Result.make_from_string ("name")
end
end -- class JSON_AUTHOR_CONVERTER

View File

@@ -60,7 +60,7 @@ feature -- Conversion
to_json (o: like object): JSON_OBJECT
do
create Result.make
create Result.make_with_capacity (2)
Result.put (json.value (o.name), name_key)
Result.put (json.value (o.books), books_key)
end
@@ -70,13 +70,13 @@ feature {NONE} -- Implementation
name_key: JSON_STRING
-- Collection's name label.
once
create Result.make_json ("name")
create Result.make_from_string ("name")
end
books_key: JSON_STRING
-- Book list label.
once
create Result.make_json ("books")
create Result.make_from_string ("books")
end
end -- class JSON_BOOK_COLLECTION_CONVERTER

View File

@@ -45,7 +45,7 @@ feature -- Conversion
to_json (o: like object): JSON_OBJECT
do
create Result.make
create Result.make_with_capacity (3)
Result.put (json.value (o.title), title_key)
Result.put (json.value (o.isbn), isbn_key)
Result.put (json.value (o.author), author_key)
@@ -56,19 +56,19 @@ feature {NONE} -- Implementation
title_key: JSON_STRING
-- Book's title label.
once
create Result.make_json ("title")
create Result.make_from_string ("title")
end
isbn_key: JSON_STRING
-- Book ISBN label.
once
create Result.make_json ("isbn")
create Result.make_from_string ("isbn")
end
author_key: JSON_STRING
-- Author label.
once
create Result.make_json ("author")
create Result.make_from_string ("author")
end
end -- class JSON_BOOK_CONVERTER

View File

@@ -51,13 +51,13 @@ feature -- Test
if attached {HASH_TABLE [ANY, HASHABLE]} json.object (l_value, "HASH_TABLE") as t2 then
create l_ucs_key.make_from_string ("1")
if attached {STRING_32} t2 [l_ucs_key] as l_ucs_value then
assert ("ucs_value.string.is_equal (%"foo%")", l_ucs_value.string.is_equal ("foo"))
assert ("ucs_value.string.is_equal (%"foo%")", l_ucs_value.same_string_general ("foo"))
else
assert ("ucs_value /= Void", False)
end
create l_ucs_key.make_from_string ("2")
if attached {STRING_32} t2 [l_ucs_key] as l_ucs_value then
assert ("ucs_value.string.is_equal (%"bar%")", l_ucs_value.string.is_equal ("bar"))
assert ("ucs_value.string.is_equal (%"bar%")", l_ucs_value.same_string_general ("bar"))
else
assert ("ucs_value /= Void", False)
end

View File

@@ -10,6 +10,11 @@ inherit
EQA_TEST_SET
JSON_PARSER_ACCESS
undefine
default_create
end
feature -- Test
test_json_number_and_integer
@@ -35,15 +40,15 @@ feature -- Test
-- that can represent the value of the JSON number, in this case
-- we know it is INTEGER_8 since the value is 42
jrep := "42"
create parser.make_parser (jrep)
if attached {JSON_NUMBER} parser.parse as l_jn then
create parser.make_with_string (jrep)
if attached {JSON_NUMBER} parser.next_parsed_json_value as l_jn then
if attached {INTEGER_8} json.object (jn, Void) as l_i8 then
assert ("l_i8 = 42", l_i8 = 42)
else
assert ("json.object (jn, Void) is a INTEGER_8", False)
end
else
assert ("parser.parse is a JSON_NUMBER", False)
assert ("parser.next_parsed_json_value is a JSON_NUMBER", False)
end
end
@@ -70,15 +75,15 @@ feature -- Test
-- that can represent the value of the JSON number, in this case
-- we know it is INTEGER_8 since the value is 42
jrep := "42"
create parser.make_parser (jrep)
if attached {JSON_NUMBER} parser.parse as l_jn then
create parser.make_with_string (jrep)
if attached {JSON_NUMBER} parser.next_parsed_json_value as l_jn then
if attached {INTEGER_8} json.object (jn, Void) as l_i8 then
assert ("l_i8 = 42", l_i8 = 42)
else
assert ("json.object (jn, Void) is a INTEGER_8", False)
end
else
assert ("parser.parse is a JSON_NUMBER", False)
assert ("parser.next_parsed_json_value is a JSON_NUMBER", False)
end
end
@@ -105,15 +110,15 @@ feature -- Test
-- that can represent the value of the JSON number, in this case
-- we know it is INTEGER_16 since the value is 300
jrep := "300"
create parser.make_parser (jrep)
if attached {JSON_NUMBER} parser.parse as l_jn then
create parser.make_with_string (jrep)
if attached {JSON_NUMBER} parser.next_parsed_json_value as l_jn then
if attached {INTEGER_16} json.object (jn, Void) as l_i16 then
assert ("l_i16 = 300", l_i16 = 300)
else
assert ("json.object (jn, Void) is a INTEGER_16", False)
end
else
assert ("parser.parse is a JSON_NUMBER", False)
assert ("parser.next_parsed_json_value is a JSON_NUMBER", False)
end
end
@@ -140,15 +145,15 @@ feature -- Test
-- that can represent the value of the JSON number, in this case
-- we know it is INTEGER_32 since the value is 100000
jrep := "100000"
create parser.make_parser (jrep)
if attached {JSON_NUMBER} parser.parse as l_jn then
create parser.make_with_string (jrep)
if attached {JSON_NUMBER} parser.next_parsed_json_value as l_jn then
if attached {INTEGER_32} json.object (jn, Void) as l_i32 then
assert ("l_i32 = 100000", l_i32 = 100000)
else
assert ("json.object (jn, Void) is a INTEGER_32", False)
end
else
assert ("parser.parse is a JSON_NUMBER", False)
assert ("parser.next_parsed_json_value is a JSON_NUMBER", False)
end
end
@@ -175,15 +180,15 @@ feature -- Test
-- that can represent the value of the JSON number, in this case
-- we know it is INTEGER_32 since the value is 42949672960
jrep := "42949672960"
create parser.make_parser (jrep)
if attached {JSON_NUMBER} parser.parse as l_jn then
create parser.make_with_string (jrep)
if attached {JSON_NUMBER} parser.next_parsed_json_value as l_jn then
if attached {INTEGER_64} json.object (jn, Void) as l_i64 then
assert ("l_i64 = 42949672960", l_i64 = 42949672960)
else
assert ("json.object (jn, Void) is a INTEGER_64", False)
end
else
assert ("parser.parse is a JSON_NUMBER", False)
assert ("parser.next_parsed_json_value is a JSON_NUMBER", False)
end
end
@@ -210,15 +215,15 @@ feature -- Test
-- that can represent the value of the JSON number, in this case
-- we know it is INTEGER_16 since the value is 200
jrep := "200"
create parser.make_parser (jrep)
if attached {JSON_NUMBER} parser.parse as l_jn then
create parser.make_with_string (jrep)
if attached {JSON_NUMBER} parser.next_parsed_json_value as l_jn then
if attached {INTEGER_16} json.object (jn, Void) as i16 then
assert ("i16 = 200", i16 = 200)
else
assert ("json.object (jn, Void) is an INTEGER_16", False)
end
else
assert ("parser.parse is a JSON_NUMBER", False)
assert ("parser.next_parsed_json_value is a JSON_NUMBER", False)
end
end
@@ -245,15 +250,15 @@ feature -- Test
-- that can represent the value of the JSON number, in this case
-- we know it is INTEGER_32 since the value is 32768
jrep := "32768"
create parser.make_parser (jrep)
if attached {JSON_NUMBER} parser.parse as l_jn then
create parser.make_with_string (jrep)
if attached {JSON_NUMBER} parser.next_parsed_json_value as l_jn then
if attached {INTEGER_32} json.object (jn, Void) as i32 then
assert ("i32 = 32768", i32 = 32768)
else
assert ("json.object (jn, Void) is a INTEGER_32", False)
end
else
assert ("parser.parse is a JSON_NUMBER", False)
assert ("parser.next_parsed_json_value is a JSON_NUMBER", False)
end
end
@@ -280,15 +285,15 @@ feature -- Test
-- that can represent the value of the JSON number, in this case
-- we know it is INTEGER_64 since the value is 2147483648
jrep := "2147483648"
create parser.make_parser (jrep)
if attached {JSON_NUMBER} parser.parse as l_jn then
create parser.make_with_string (jrep)
if attached {JSON_NUMBER} parser.next_parsed_json_value as l_jn then
if attached {INTEGER_64} json.object (jn, Void) as i64 then
assert ("i64 = 2147483648", i64 = 2147483648)
else
assert ("json.object (jn, Void) is a INTEGER_64", False)
end
else
assert ("parser.parse is a JSON_NUMBER", False)
assert ("parser.next_parsed_json_value is a JSON_NUMBER", False)
end
end
@@ -315,15 +320,15 @@ feature -- Test
-- that can represent the value of the JSON number, in this case
-- we know it is INTEGER_32 since the value is 42949672960
jrep := "9223372036854775808" -- 1 higher than largest positive number that can be represented by INTEGER 64
create parser.make_parser (jrep)
if attached {JSON_NUMBER} parser.parse as l_jn then
create parser.make_with_string (jrep)
if attached {JSON_NUMBER} parser.next_parsed_json_value as l_jn then
if attached {NATURAL_64} json.object (jn, Void) as l_n64 then
assert ("l_n64 = 9223372036854775808", l_n64 = 9223372036854775808)
else
assert ("json.object (jn, Void) is a NATURAL_64", False)
end
else
assert ("parser.parse is a JSON_NUMBER", False)
assert ("parser.next_parsed_json_value is a JSON_NUMBER", False)
end
end
@@ -349,15 +354,15 @@ feature -- Test
-- Note: The JSON_FACTORY will always return a REAL_64 if the value
-- of the JSON number is a floating point number
jrep := "3.14"
create parser.make_parser (jrep)
if attached {JSON_NUMBER} parser.parse as l_jn then
create parser.make_with_string (jrep)
if attached {JSON_NUMBER} parser.next_parsed_json_value as l_jn then
if attached {REAL_64} json.object (jn, Void) as r64 then
assert ("3.14 <= r64 and r64 <= 3.141", 3.14 <= r64 and r64 <= 3.141)
else
assert ("json.object (jn, Void) is a REAL_64", False)
end
else
assert ("parser.parse is a JSON_NUMBER", False)
assert ("parser.next_parsed_json_value is a JSON_NUMBER", False)
end
end
@@ -381,15 +386,15 @@ feature -- Test
-- JSON representation -> JSON value -> Eiffel value
jrep := "3.1400001049041748"
create parser.make_parser (jrep)
if attached {JSON_NUMBER} parser.parse as l_jn then
create parser.make_with_string (jrep)
if attached {JSON_NUMBER} parser.next_parsed_json_value as l_jn then
if attached {REAL_64} json.object (l_jn, Void) as r64 then
assert ("r64 = 3.1400001049041748", r64 = 3.1400001049041748)
else
assert ("json.object (l_jn, Void) is a REAL_64", False)
end
else
assert ("parser.parse is a JSON_NUMBER", False)
assert ("parser.next_parsed_json_value is a JSON_NUMBER", False)
end
end
@@ -414,15 +419,15 @@ feature -- Test
-- JSON representation -> JSON value -> Eiffel value
jrep := "3.1415926535897931"
create parser.make_parser (jrep)
if attached {JSON_NUMBER} parser.parse as l_jn then
create parser.make_with_string (jrep)
if attached {JSON_NUMBER} parser.next_parsed_json_value as l_jn then
if attached {REAL_64} json.object (jn, Void) as l_r64 then
assert ("l_r64 = 3.1415926535897931", l_r64 = 3.1415926535897931)
else
assert ("json.object (jn, Void) is a REAL_64", False)
end
else
assert ("parser.parse is a JSON_NUMBER", False)
assert ("parser.next_parsed_json_value is a JSON_NUMBER", False)
end
end
@@ -434,7 +439,7 @@ feature -- Test
do
-- Eiffel value -> JSON value -> JSON representation
b := True
create jb.make_boolean (b)
create jb.make (b)
assert ("jb.representation.is_equal (%"true%")", jb.representation.is_equal ("true"))
-- Eiffel value -> JSON value -> JSON representation with factory
if attached {JSON_BOOLEAN} json.value (b) as l_jb then
@@ -444,20 +449,20 @@ feature -- Test
end
-- JSON representation -> JSON value -> Eiffel value
create parser.make_parser ("true")
if attached {JSON_BOOLEAN} parser.parse as l_jb then
create parser.make_with_string ("true")
if attached {JSON_BOOLEAN} parser.next_parsed_json_value as l_jb then
if attached {BOOLEAN} json.object (l_jb, Void) as l_b then
assert ("l_b = True", l_b = True)
else
assert ("json.object (l_jb, Void) is BOOLEAN", False)
end
else
assert ("parser.parse is a JSON_BOOLEAN", False)
assert ("parser.next_parsed_json_value is a JSON_BOOLEAN", False)
end
-- Eiffel value -> JSON value -> JSON representation
b := False
create jb.make_boolean (b)
create jb.make (b)
assert ("jb.representation.same_string (%"false%")", jb.representation.same_string ("false"))
-- Eiffel value -> JSON value -> JSON representation with factory
if attached {JSON_BOOLEAN} json.value (b) as l_jb then
@@ -467,15 +472,15 @@ feature -- Test
end
-- JSON representation -> JSON value -> Eiffel value
create parser.make_parser ("false")
if attached {JSON_BOOLEAN} parser.parse as l_jb then
create parser.make_with_string ("false")
if attached {JSON_BOOLEAN} parser.next_parsed_json_value as l_jb then
if attached {BOOLEAN} json.object (l_jb, Void) as l_b then
assert ("l_b = False", l_b = False)
else
assert ("json.object (l_jb, Void) is a BOOLEAN", False)
end
else
assert ("parser.parse is a JSON_BOOLEAN", False)
assert ("parser.next_parsed_json_value is a JSON_BOOLEAN", False)
end
end
@@ -497,11 +502,11 @@ feature -- Test
end
-- JSON representation -> JSON value -> Eiffel value
create parser.make_parser (jrep)
if attached parser.parse as l_json_null then
create parser.make_with_string (jrep)
if attached parser.next_parsed_json_value as l_json_null then
assert ("a = Void", json.object (l_json_null, Void) = Void)
else
assert ("parser.parse /= Void", False)
assert ("parser.next_parsed_json_value /= Void", False)
end
end
@@ -514,7 +519,7 @@ feature -- Test
do
c := 'a'
-- Eiffel value -> JSON value -> JSON representation
create js.make_json (c.out)
create js.make_from_string (c.out)
assert ("js.representation.is_equal (%"%"a%"%")", js.representation.is_equal ("%"a%""))
-- Eiffel value -> JSON value -> JSON representation with factory
if attached {JSON_STRING} json.value (c) as l_json_str then
@@ -525,13 +530,13 @@ feature -- Test
-- JSON representation -> JSON value -> Eiffel value
jrep := "%"a%""
create parser.make_parser (jrep)
if attached {JSON_STRING} parser.parse as l_json_str then
create parser.make_with_string (jrep)
if attached {JSON_STRING} parser.next_parsed_json_value as l_json_str then
if attached {STRING_32} json.object (l_json_str, Void) as ucs then
assert ("ucs.string.is_equal (%"a%")", ucs.string.is_equal ("a"))
end
else
assert ("parser.parse /= Void", False)
assert ("parser.next_parsed_json_value /= Void", False)
end
end
@@ -546,7 +551,7 @@ feature -- Test
jrep := "%"foobar%""
-- Eiffel value -> JSON value -> JSON representation
create js.make_json (s)
create js.make_from_string (s)
assert ("js.representation.is_equal (%"%"foobar%"%")", js.representation.is_equal (jrep))
-- Eiffel value -> JSON value -> JSON representation with factory
if attached {JSON_STRING} json.value (s) as l_js then
@@ -556,13 +561,13 @@ feature -- Test
end
-- JSON representation -> JSON value -> Eiffel value
create parser.make_parser (jrep)
if attached {JSON_STRING} parser.parse as l_js then
create parser.make_with_string (jrep)
if attached {JSON_STRING} parser.next_parsed_json_value as l_js then
if attached {STRING_32} json.object (l_js, Void) as l_ucs then
assert ("ucs.string.is_equal (%"foobar%")", l_ucs.string.is_equal (s))
end
else
assert ("parser.parse /= Void", False)
assert ("parser.next_parsed_json_value /= Void", False)
end
end
@@ -578,7 +583,7 @@ feature -- Test
create ucs.make_from_string (s)
-- Eiffel value -> JSON value -> JSON representation
create js.make_json (ucs)
create js.make_from_string (ucs)
assert ("js.representation.is_equal (%"%"foobar%"%")", js.representation.is_equal (jrep))
-- Eiffel value -> JSON value -> JSON representation with factory
@@ -589,15 +594,15 @@ feature -- Test
end
-- JSON representation -> JSON value -> Eiffel value
create parser.make_parser (jrep)
if attached {JSON_STRING} parser.parse as l_js then
create parser.make_with_string (jrep)
if attached {JSON_STRING} parser.next_parsed_json_value as l_js then
if attached {STRING_32} json.object (l_js, Void) as l_ucs then
assert ("ucs.string.is_equal (%"foobar%")", l_ucs.string.is_equal (s))
else
assert ("json.object (js, Void) /= Void", False)
end
else
assert ("parser.parse /= Void", False)
assert ("parser.next_parsed_json_value /= Void", False)
end
end
@@ -610,7 +615,7 @@ feature -- Test
do
jrep := "%"foo\\bar%""
create s.make_from_string ("foo\bar")
create js.make_json (s)
create js.make_from_string (s)
assert ("js.representation.same_string (%"%"foo\\bar%"%")", js.representation.same_string (jrep))
-- Eiffel value -> JSON value -> JSON representation with factory
@@ -621,32 +626,84 @@ feature -- Test
end
-- JSON representation -> JSON value -> Eiffel value
create parser.make_parser (jrep)
if attached {JSON_STRING} parser.parse as l_js then
create parser.make_with_string (jrep)
if attached {JSON_STRING} parser.next_parsed_json_value as l_js then
if attached {STRING_32} json.object (l_js, Void) as l_ucs then
assert ("ucs.same_string (%"foo\bar%")", l_ucs.same_string ("foo\bar"))
end
else
assert ("parser.parse /= Void", False)
assert ("parser.next_parsed_json_value /= Void", False)
end
jrep := "%"foo\\bar%""
create parser.make_parser (jrep)
if attached {JSON_STRING} parser.parse as jstring then
create parser.make_with_string (jrep)
if attached {JSON_STRING} parser.next_parsed_json_value as jstring then
assert ("unescaped string %"foo\\bar%" to %"foo\bar%"", jstring.unescaped_string_8.same_string ("foo\bar"))
else
assert ("parser.parse /= Void", False)
assert ("parser.next_parsed_json_value /= Void", False)
end
create js.make_json_from_string_32 ({STRING_32} "你好")
create js.make_from_string_32 ({STRING_32} "你好")
assert ("escaping unicode string32 %"%%/20320/%%/22909/%" %"\u4F60\u597D%"", js.item.same_string ("\u4F60\u597D"))
jrep := "%"\u4F60\u597D%"" --| Ni hao
create parser.make_parser (jrep)
if attached {JSON_STRING} parser.parse as jstring then
create parser.make_with_string (jrep)
if attached {JSON_STRING} parser.next_parsed_json_value as jstring then
assert ("same unicode string32 %"%%/20320/%%/22909/%"", jstring.unescaped_string_32.same_string ({STRING_32} "你好"))
else
assert ("parser.parse /= Void", False)
assert ("parser.next_parsed_json_value /= Void", False)
end
end
test_json_string_and_special_characters_2
local
js: detachable JSON_STRING
s,j: STRING
do
s := "foo%Tbar"
j := "foo\tbar"
create js.make_from_string (s)
assert ("string %"" + s + "%" to json %"" + j + "%"", js.item.same_string (j))
create js.make_from_escaped_json_string (js.item)
assert ("json %"" + j + "%" to string %"" + s + "%"", js.unescaped_string_8.same_string (s))
s := "tab=%T cr=%R newline=%N backslash=%H slash=/ end"
j := "tab=\t cr=\r newline=\n backslash=\\ slash=/ end"
create js.make_from_string (s)
assert ("string %"" + s + "%" to json %"" + j + "%"", js.item.same_string (j))
create js.make_from_escaped_json_string (js.item)
assert ("json %"" + j + "%" to string %"" + s + "%"", js.unescaped_string_8.same_string (s))
s := "<script>tab=%T cr=%R newline=%N backslash=%H slash=/ end</script>"
j := "<script>tab=\t cr=\r newline=\n backslash=\\ slash=/ end<\/script>"
create js.make_from_string (s)
assert ("string %"" + s + "%" to json %"" + j + "%"", js.item.same_string (j))
create js.make_from_escaped_json_string (js.item)
assert ("json %"" + j + "%" to string %"" + s + "%"", js.unescaped_string_8.same_string (s))
create js.make_from_escaped_json_string ("tab=\t")
assert ("js.item.same_string (%"tab=\t%")", js.item.same_string ("tab=\t"))
create js.make_from_escaped_json_string (js.item)
assert ("js.item.same_string (%"tab=\t%")", js.item.same_string ("tab=\t"))
-- <\/script>
create js.make_from_escaped_json_string ("<script>tab=\t<\/script>")
assert ("js.item.same_string (%"<script>tab=\t<\/script>%")", js.item.same_string ("<script>tab=\t<\/script>"))
assert ("js.unescaped_string_8.same_string (%"<script>tab=%%T</script>%")", js.unescaped_string_8.same_string ("<script>tab=%T</script>"))
create js.make_from_escaped_json_string (js.item)
assert ("js.item.same_string (%"<script>tab=\t<\/script>%")", js.item.same_string ("<script>tab=\t<\/script>"))
assert ("js.unescaped_string_8.same_string (%"<script>tab=%%T</script>%")", js.unescaped_string_8.same_string ("<script>tab=%T</script>"))
-- </script>
create js.make_from_escaped_json_string ("<script>tab=\t</script>")
assert ("js.item.same_string (%"<script>tab=\t</script>%")", js.item.same_string ("<script>tab=\t</script>"))
assert ("js.unescaped_string_8.same_string (%"<script>tab=%%T</script>%")", js.unescaped_string_8.same_string ("<script>tab=%T</script>"))
create js.make_from_escaped_json_string (js.item)
assert ("js.item.same_string (%"<script>tab=\t<\/script>%")", js.item.same_string ("<script>tab=\t</script>"))
assert ("js.unescaped_string_8.same_string (%"<script>tab=%%T</script>%")", js.unescaped_string_8.same_string ("<script>tab=%T</script>"))
end
test_json_array
local
ll: LINKED_LIST [INTEGER_8]
@@ -665,7 +722,7 @@ feature -- Test
ll.extend (5)
-- Note: Currently there is no simple way of creating a JSON_ARRAY
-- from an LINKED_LIST.
create ja.make_array
create ja.make (ll.count)
from
ll.start
until
@@ -690,15 +747,15 @@ feature -- Test
-- it means we will get an LINKED_LIST [ANY] containing the INTEGER_8
-- values 0, 1, 1, 2, 3, 5
jrep := "[0,1,1,2,3,5]"
create parser.make_parser (jrep)
if attached {JSON_ARRAY} parser.parse as l_ja then
create parser.make_with_string (jrep)
if attached {JSON_ARRAY} parser.next_parsed_json_value as l_ja then
if attached {LINKED_LIST [detachable ANY]} json.object (ja, Void) as l_ll2 then
assert ("ll2.is_equal (ll)", l_ll2.is_equal (ll))
else
assert ("json.object (ja, Void) /= Void", False)
end
else
assert ("parser.parse /= Void", False)
assert ("parser.next_parsed_json_value /= Void", False)
end
end
@@ -720,14 +777,14 @@ feature -- Test
-- a HASH_TABLE, so we do it manually.
-- t = {"name": "foobar", "size": 42, "contents", [0, 1, 1, 2, 3, 5]}
create jo.make
create js_key.make_json ("name")
create js.make_json ("foobar")
create js_key.make_from_string ("name")
create js.make_from_string ("foobar")
jo.put (js, js_key)
create js_key.make_json ("size")
create js_key.make_from_string ("size")
create jn.make_integer (42)
jo.put (jn, js_key)
create js_key.make_json ("contents")
create ja.make_array
create js_key.make_from_string ("contents")
create ja.make (6)
create jn.make_integer (0)
ja.add (jn)
create jn.make_integer (1)
@@ -763,8 +820,8 @@ feature -- Test
-- JSON representation -> JSON value -> Eiffel value -> JSON value -> JSON representation
jrep := "{%"name%":%"foobar%",%"size%":42,%"contents%":[0,1,1,2,3,5]}"
create parser.make_parser (jrep)
if attached {JSON_OBJECT} parser.parse as l_jo then
create parser.make_with_string (jrep)
if attached {JSON_OBJECT} parser.next_parsed_json_value as l_jo then
if attached {HASH_TABLE [detachable ANY, STRING_GENERAL]} json.object (l_jo, Void) as l_t2 then
if attached json.value (l_t2) as l_jo_2 then
assert ("jrep.is_equal (jo.representation)", jrep.is_equal (l_jo_2.representation))
@@ -775,7 +832,7 @@ feature -- Test
assert ("json.object (jo, Void) /= Void", False)
end
else
assert ("parser.parse /= Void", jo /= Void)
assert ("parser.next_parsed_json_value /= Void", jo /= Void)
end
end

View File

@@ -1,4 +1,4 @@
note
note
description: "[
Eiffel tests that can be executed by testing tool.
]"
@@ -11,7 +11,6 @@ class
TEST_JSON_SUITE
inherit
EQA_TEST_SET
redefine
on_prepare
@@ -28,38 +27,38 @@ feature {NONE} -- Events
feature -- Tests Pass
test_json_pass1
--
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("pass1.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("pass1.json", parse_json.is_parsed = True)
parse_json.parse_content
assert ("pass1.json", parse_json.is_valid)
end
end
test_json_pass2
--
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("pass2.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("pass2.json", parse_json.is_parsed = True)
parse_json.parse_content
assert ("pass2.json",parse_json.is_valid)
end
end
test_json_pass3
--
test_json_pass3
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("pass3.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("pass3.json", parse_json.is_parsed = True)
parse_json.parse_content
assert ("pass3.json",parse_json.is_valid)
end
end
@@ -69,412 +68,420 @@ feature -- Tests Pass
utf: UTF_CONVERTER
s: READABLE_STRING_32
do
s := {STRING_32} "{ %"nihaoma%": %"你好吗\t?%" }"
s := {STRING_32} "{ %"nihaoma%": %"你好吗\t?%" }"
parse_json := new_json_parser (utf.string_32_to_utf_8_string_8 (s))
json_value := parse_json.parse_json
assert ("utf8.pass1.json", parse_json.is_parsed = True)
if attached {JSON_OBJECT} json_value as jo and then attached {JSON_STRING} jo.item ("nihaoma") as js then
assert ("utf8.nihaoma", js.unescaped_string_32.same_string ({STRING_32} "你好吗%T?"))
parse_json.parse_content
assert ("utf8.pass1.json", parse_json.is_valid)
if attached {JSON_OBJECT} parse_json.parsed_json_value as jo and then attached {JSON_STRING} jo.item ("nihaoma") as js then
assert ("utf8.nihaoma", js.unescaped_string_32.same_string ({STRING_32} "你好吗%T?"))
else
assert ("utf8.nihaoma", False)
end
end
end
end
feature -- Tests Failures
test_json_fail1
--
test_json_fail1
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail1.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail1.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail1.json", parse_json.is_valid = False)
end
end
test_json_fail2
--
test_json_fail2
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail2.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail2.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail2.json",parse_json.is_valid = False)
end
end
test_json_fail3
--
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail3.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail3.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail3.json",parse_json.is_valid = False)
end
end
test_json_fail4
--
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail4.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail4.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail4.json",parse_json.is_valid = False)
end
end
test_json_fail5
--
test_json_fail5
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail5.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail5.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail5.json",parse_json.is_valid = False)
end
end
test_json_fail6
--
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail6.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail6.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail6.json",parse_json.is_valid = False )
end
end
test_json_fail7
--
test_json_fail7
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail7.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail7.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail7.json",parse_json.is_valid = False)
end
end
test_json_fail8
--
test_json_fail8
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail8.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail8.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail8.json",parse_json.is_valid = False )
end
end
test_json_fail9
--
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail9.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail9.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail9.json",parse_json.is_valid = False)
end
end
test_json_fail10
--
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail10.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail10.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail10.json",parse_json.is_valid = False)
end
end
test_json_fail11
--
test_json_fail11
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail11.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail11.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail11.json",parse_json.is_valid = False)
end
end
test_json_fail12
--
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail12.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail12.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail12.json",parse_json.is_valid = False)
end
end
test_json_fail13
--
test_json_fail13
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail13.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail13.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail13.json",parse_json.is_valid = False)
end
end
test_json_fail14
--
test_json_fail14
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail14.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail14.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail14.json",parse_json.is_valid = False)
end
end
test_json_fail15
--
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail15.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail15.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail15.json",parse_json.is_valid = False)
end
end
test_json_fail16
--
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail16.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail16.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail16.json",parse_json.is_valid = False)
end
end
test_json_fail17
--
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail17.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail17.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail17.json",parse_json.is_valid = False)
end
end
test_json_fail18
--
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail18.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail18.json", parse_json.is_parsed = True)
parse_json.parse_content
assert ("fail18.json",parse_json.is_valid = True)
end
end
test_json_fail19
--
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail19.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail19.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail19.json",parse_json.is_valid = False)
end
end
test_json_fail20
--
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail20.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail20.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail20.json",parse_json.is_valid = False)
end
end
test_json_fail21
--
test_json_fail21
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail21.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail21.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail21.json",parse_json.is_valid = False)
end
end
test_json_fail22
--
test_json_fail22
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail22.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail22.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail22.json",parse_json.is_valid = False)
end
end
test_json_fail23
--
test_json_fail23
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail23.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail23.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail23.json",parse_json.is_valid = False)
end
end
test_json_fail24
--
test_json_fail24
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail24.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail24.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail24.json",parse_json.is_valid = False)
end
end
test_json_fail25
--
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail25.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail25.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail25.json",parse_json.is_valid = False)
end
end
test_json_fail26
--
test_json_fail26
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail26.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail26.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail26.json",parse_json.is_valid = False)
end
end
test_json_fail27
--
test_json_fail27
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail27.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail27.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail27.json",parse_json.is_valid = False)
end
end
test_json_fail28
--
test_json_fail28
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail28.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail28.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail28.json",parse_json.is_valid = False)
end
end
test_json_fail29
--
test_json_fail29
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail29.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail29.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail29.json",parse_json.is_valid = False )
end
end
test_json_fail30
--
test_json_fail30
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail30.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail30.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail30.json",parse_json.is_valid = False)
end
end
test_json_fail31
--
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail31.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail31.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail31.json",parse_json.is_valid = False)
end
end
test_json_fail32
--
test_json_fail32
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail32.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail32.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail32.json",parse_json.is_valid = False)
end
end
test_json_fail33
--
test_json_fail33
--
local
parse_json: like new_json_parser
do
if attached json_file_from ("fail33.json") as json_file then
parse_json := new_json_parser (json_file)
json_value := parse_json.parse_json
assert ("fail33.json", parse_json.is_parsed = False)
parse_json.parse_content
assert ("fail33.json",parse_json.is_valid = False)
end
end
@@ -482,19 +489,16 @@ feature -- JSON_FROM_FILE
file_reader: JSON_FILE_READER
json_value: detachable JSON_VALUE
json_file_from (fn: STRING): detachable STRING
json_file_from (fn: READABLE_STRING_GENERAL): detachable STRING
local
f: RAW_FILE
l_path: STRING
test_dir: STRING
l_path: PATH
test_dir: PATH
i: INTEGER
do
test_dir := (create {EXECUTION_ENVIRONMENT}).current_working_directory
test_dir.append_character ((create {OPERATING_ENVIRONMENT}).directory_separator)
l_path := test_dir + fn
create f.make_with_name (l_path)
test_dir := (create {EXECUTION_ENVIRONMENT}).current_working_path
l_path := test_dir.extended (fn)
create f.make_with_path (l_path)
if f.exists then
-- Found json file
else
@@ -505,26 +509,27 @@ feature -- JSON_FROM_FILE
until
i = 0
loop
test_dir.append_character ('.')
test_dir.append_character ('.')
test_dir.append_character ((create {OPERATING_ENVIRONMENT}).directory_separator)
test_dir := test_dir.extended ("..")
i := i - 1
end
l_path := test_dir + fn
l_path := test_dir.extended (fn)
end
create f.make_with_name (l_path)
create f.make_with_path (l_path)
if f.exists then
Result := file_reader.read_json_from (l_path)
Result := file_reader.read_json_from (l_path.name)
end
assert ("File contains json data", Result /= Void)
end
assert ("File contains json data", Result /= Void)
end
new_json_parser (a_string: STRING): JSON_PARSER
do
create Result.make_parser (a_string)
create Result.make_with_string (a_string)
end
invariant
file_reader /= Void
end

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<system xmlns="http://www.eiffel.com/developers/xml/configuration-1-8-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eiffel.com/developers/xml/configuration-1-8-0 http://www.eiffel.com/developers/xml/configuration-1-8-0.xsd" name="test_suite" uuid="EA141B17-6A21-4781-8B5F-E9939BAE968A">
<system xmlns="http://www.eiffel.com/developers/xml/configuration-1-13-0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.eiffel.com/developers/xml/configuration-1-13-0 http://www.eiffel.com/developers/xml/configuration-1-13-0.xsd" name="test_suite" uuid="EA141B17-6A21-4781-8B5F-E9939BAE968A">
<target name="test_suite">
<root cluster="test_suite" class="APPLICATION" feature="make"/>
<file_rule>
@@ -7,11 +7,11 @@
<exclude>/CVS$</exclude>
<exclude>/.svn$</exclude>
</file_rule>
<option warning="true" is_attached_by_default="true" void_safety="all" syntax="standard">
<option warning="true" full_class_checking="false" is_attached_by_default="true" void_safety="transitional" syntax="standard">
<assertions precondition="true" postcondition="true" check="true" invariant="true" loop="true" supplier_precondition="true"/>
</option>
<library name="base" location="$ISE_LIBRARY\library\base\base-safe.ecf"/>
<library name="json" location="..\..\..\library\json-safe.ecf"/>
<library name="json" location="..\..\..\library\json-safe.ecf" readonly="false"/>
<library name="testing" location="$ISE_LIBRARY\library\testing\testing-safe.ecf"/>
<cluster name="test_suite" location=".\" recursive="true"/>
</target>