BIND 10 trac3239, updated. fde55e700932b537b9306833150fb21dd4fe2b85 [3239] Convert JSON string literals containing integers to integer type automatically
BIND 10 source code commits
bind10-changes at lists.isc.org
Wed Dec 4 11:24:52 UTC 2013
The branch, trac3239 has been updated
via fde55e700932b537b9306833150fb21dd4fe2b85 (commit)
via 90a38997c16cb4f438fe03142dc3644a046ed81f (commit)
from 135e8dd110e5df75658cf5bf2e95bc76372f6fe5 (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
commit fde55e700932b537b9306833150fb21dd4fe2b85
Author: Mukund Sivaraman <muks at isc.org>
Date: Wed Dec 4 16:51:20 2013 +0530
[3239] Convert JSON string literals containing integers to integer type automatically
commit 90a38997c16cb4f438fe03142dc3644a046ed81f
Author: Mukund Sivaraman <muks at isc.org>
Date: Wed Dec 4 16:37:25 2013 +0530
[3239] Wrap more long lines
-----------------------------------------------------------------------
Summary of changes:
src/lib/cc/data.cc | 28 ++++++++++++++++++++++------
src/lib/cc/tests/data_unittests.cc | 21 ++++++++++++++++++++-
2 files changed, 42 insertions(+), 7 deletions(-)
-----------------------------------------------------------------------
diff --git a/src/lib/cc/data.cc b/src/lib/cc/data.cc
index a06ccdd..0d3ccb5 100644
--- a/src/lib/cc/data.cc
+++ b/src/lib/cc/data.cc
@@ -305,10 +305,13 @@ skipTo(std::istream& in, const std::string& file, int& line,
}
return (c);
} else {
- throwJSONError(std::string("'") + std::string(1, c) + "' read, one of \"" + chars + "\" expected", file, line, pos);
+ throwJSONError(std::string("'") + std::string(1, c) +
+ "' read, one of \"" + chars + "\" expected",
+ file, line, pos);
}
}
- throwJSONError(std::string("EOF read, one of \"") + chars + "\" expected", file, line, pos);
+ throwJSONError(std::string("EOF read, one of \"") + chars + "\" expected",
+ file, line, pos);
return (c); // shouldn't reach here, but some compilers require it
}
@@ -427,7 +430,8 @@ fromStringstreamBool(std::istream& in, const std::string& file,
} else if (boost::iequals(word, "False")) {
return (Element::create(false));
} else {
- throwJSONError(std::string("Bad boolean value: ") + word, file, line, pos);
+ throwJSONError(std::string("Bad boolean value: ") + word,
+ file, line, pos);
// above is a throw shortcurt, return empty is never reached
return (ElementPtr());
}
@@ -450,7 +454,16 @@ ElementPtr
fromStringstreamString(std::istream& in, const std::string& file, int& line,
int& pos)
{
- return (Element::create(strFromStringstream(in, file, line, pos)));
+ const std::string& str = strFromStringstream(in, file, line, pos);
+ // Try to parse the string as an integer (that was quoted), so that
+ // literals like "600" are handled as integers. This is not strictly
+ // JSON, but it has been requested as a usability feature (see
+ // #3239).
+ try {
+ return (Element::create(boost::lexical_cast<int64_t>(str)));
+ } catch (const boost::bad_lexical_cast&) {
+ return (Element::create(str));
+ }
}
ElementPtr
@@ -483,7 +496,8 @@ fromStringstreamMap(std::istream& in, const std::string& file, int& line,
skipChars(in, WHITESPACE, line, pos);
int c = in.peek();
if (c == EOF) {
- throwJSONError(std::string("Unterminated map, <string> or } expected"), file, line, pos);
+ throwJSONError(std::string("Unterminated map, <string> or } expected"),
+ file, line, pos);
} else if (c == '}') {
// empty map, skip closing curly
in.ignore();
@@ -630,7 +644,9 @@ Element::fromJSON(std::istream& in, const std::string& file, int& line,
case EOF:
break;
default:
- throwJSONError(std::string("error: unexpected character ") + std::string(1, c), file, line, pos);
+ throwJSONError(std::string("error: unexpected character ") +
+ std::string(1, c),
+ file, line, pos);
break;
}
}
diff --git a/src/lib/cc/tests/data_unittests.cc b/src/lib/cc/tests/data_unittests.cc
index 96e18ad..45c8c53 100644
--- a/src/lib/cc/tests/data_unittests.cc
+++ b/src/lib/cc/tests/data_unittests.cc
@@ -696,7 +696,7 @@ efs(const std::string& str) {
TEST(Element, equals) {
EXPECT_EQ(*efs("1"), *efs("1"));
EXPECT_NE(*efs("1"), *efs("2"));
- EXPECT_NE(*efs("1"), *efs("\"1\""));
+ EXPECT_EQ(*efs("1"), *efs("\"1\""));
EXPECT_NE(*efs("1"), *efs("[]"));
EXPECT_NE(*efs("1"), *efs("True"));
EXPECT_NE(*efs("1"), *efs("{}"));
@@ -758,6 +758,25 @@ TEST(Element, equals) {
EXPECT_EQ(*efs("null"), *Element::create());
}
+TEST(Element, stringContainingInteger) {
+ // String literals like "42" are meant to be handled as
+ // integers. This is not strictly JSON, but it has been requested as
+ // a usability feature (see #3239).
+ EXPECT_EQ(*efs("42"), *efs("42"));
+ EXPECT_EQ(*efs("42"), *efs("\"42\""));
+ EXPECT_NE(*efs("42"), *efs("\"42trailingchars\""));
+
+ // Check types
+ EXPECT_EQ(Element::integer, efs("42")->getType());
+ EXPECT_EQ(Element::integer, efs("\"42\"")->getType());
+ EXPECT_EQ(Element::string, efs("\"42trailingchars\"")->getType());
+
+ // "42e10" is a string literal containing a floating-point
+ // representation. It won't be automatically converted from string
+ // to real for now.
+ EXPECT_EQ(Element::string, efs("\"42e10\"")->getType());
+}
+
TEST(Element, removeIdentical) {
ElementPtr a = Element::createMap();
ConstElementPtr b = Element::createMap();
More information about the bind10-changes
mailing list