summaryrefslogtreecommitdiff
path: root/src/json_ui.cc
diff options
context:
space:
mode:
authorDan Rosén <dan.rosen@gu.se>2019-10-22 21:14:32 +0200
committerDan Rosén <dan.rosen@gu.se>2019-10-22 21:14:38 +0200
commitc792986768a2f3d42fc6113fb8b5459fdbf5a0fc (patch)
tree8873a06fe769ba38e9af1ed99e499dd3815e57ce /src/json_ui.cc
parent3c34de7fe7db607fe2c0519471b8d3c4935ce717 (diff)
Teach JSON parser negative numbers
Diffstat (limited to 'src/json_ui.cc')
-rw-r--r--src/json_ui.cc11
1 files changed, 8 insertions, 3 deletions
diff --git a/src/json_ui.cc b/src/json_ui.cc
index ebb3b7bf..a0294f3e 100644
--- a/src/json_ui.cc
+++ b/src/json_ui.cc
@@ -257,7 +257,7 @@ void JsonUI::set_on_key(OnKeyCallback callback)
using JsonArray = Vector<Value>;
using JsonObject = HashMap<String, Value>;
-static bool is_digit(char c) { return c >= '0' and c <= '9'; }
+static bool is_digit_or_minus(char c) { return (c >= '0' and c <= '9') or c == '-'; }
struct JsonResult { Value value; const char* new_pos; };
@@ -266,10 +266,10 @@ JsonResult parse_json(const char* pos, const char* end)
if (not skip_while(pos, end, is_blank))
return {};
- if (is_digit(*pos))
+ if (is_digit_or_minus(*pos))
{
auto digit_end = pos;
- skip_while(digit_end, end, is_digit);
+ skip_while(digit_end, end, is_digit_or_minus);
return { Value{str_to_int({pos, digit_end})}, digit_end };
}
if (end - pos > 4 and StringView{pos, pos+4} == "true")
@@ -524,6 +524,11 @@ UnitTest test_json_parser{[]()
}
{
+ auto value = parse_json("-1").value;
+ kak_assert(value.as<int>() == -1);
+ }
+
+ {
auto value = parse_json("{}").value;
kak_assert(value and value.is_a<JsonObject>());
kak_assert(value.as<JsonObject>().empty());