Skip to content

Commit 7cdadb4

Browse files
committed
Fix #30
1 parent 706db51 commit 7cdadb4

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

src/jsony.nim

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ proc parseHook*(s: string, i: var int, v: var SomeFloat) =
127127
eatSpace(s, i)
128128
let chars = parseutils.parseFloat(s, f, i)
129129
if chars == 0:
130+
echo s[i - 10 .. i + 10]
130131
error("Failed to parse a float.", i)
131132
i += chars
132133
v = f
@@ -516,16 +517,13 @@ proc parseHook*(s: string, i: var int, v: var JsonNode) =
516517
elif data == "false":
517518
v = newJBool(false)
518519
elif data.len > 0 and data[0] in {'0'..'9', '-', '+'}:
519-
if "." in data:
520+
try:
521+
v = newJInt(parseInt(data))
522+
except ValueError:
520523
try:
521524
v = newJFloat(parseFloat(data))
522525
except ValueError:
523-
error("Invalid integer.", i)
524-
else:
525-
try:
526-
v = newJInt(parseInt(data))
527-
except ValueError:
528-
error("Invalid float.", i)
526+
error("Invalid number.", i)
529527
else:
530528
error("Unexpected.", i)
531529

tests/test_json_in_json.nim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,9 @@ block:
2727

2828
var foo = Entry()
2929
doAssert toJson(foo) == """{"name":"","data":null}"""
30+
31+
block:
32+
# https://github.com/treeform/jsony/issues/30
33+
let s = r"""[9e-8]"""
34+
echo fromJson(s)
35+
doAssert $fromJson(s) == "[9e-008]"

tests/test_numbers.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ block:
4343
doAssert " -1.34E3 ".fromJson(float64) == -1.34E3
4444
doAssert " -1.34E3 ".fromJson(float64) == -1.34E3
4545

46+
doAssert "9e-8".fromJson(float64) == 9e-8
47+
4648
block:
4749
doAssert "[1, 2, 3]".fromJson(seq[int]) == @[1, 2, 3]
4850
doAssert """["hi", "bye", "maybe"]""".fromJson(seq[string]) ==

0 commit comments

Comments
 (0)