Conversation
libdevcore/JSON.h
Outdated
| /// \param _json [out] resulting JSON object | ||
| /// \param _errs [out] Formatted error messages | ||
| /// \return \c true if the document was successfully parsed, \c false if an error occurred. | ||
| inline bool parse(Json::CharReaderBuilder& _builder, const std::string &_input, Json::Value *_json, std::string *_errs) |
There was a problem hiding this comment.
Instead of pointers can we use references?
There was a problem hiding this comment.
I also thought about this. The good thing with pointers are, that the direction of read/write is more visible - but thats mainly somehow just a matter of taste. Of course this would only make sense, if it's not only used in libdevcore/JSON.h.
There was a problem hiding this comment.
Our general guideline is to only use a pointer if the pointer can be nullptr and references otherwise.
There was a problem hiding this comment.
Ok. I will just change _json to be a reference.
|
Most of the helpers were inline in I might be wrong about this, any opinions @chriseth @aarlt ? |
libsolc/libsolc.cpp
Outdated
| string compileMulti(string const& _input, bool _optimize, CStyleReadFileCallback _readCallback = nullptr) | ||
| { | ||
| Json::Reader reader; | ||
| std::string errs; |
There was a problem hiding this comment.
The std namespace is imported in this file.
There was a problem hiding this comment.
Also it could be just errors to avoid abbreviations.
There was a problem hiding this comment.
Sure! I really love explicitly written namespaces - but I will change my coding style for you. But errs should really be renamed to errors.
libsolc/libsolc.cpp
Outdated
| std::string errs; | ||
| Json::Value input; | ||
| if (!reader.parse(_input, input, false)) | ||
| if (!dev::jsonParseStrict(_input, &input, &errs)) |
There was a problem hiding this comment.
The dev namespace is imported in this file.
| Json::Value input; | ||
| Json::Reader reader; | ||
|
|
||
| std::string errs; |
test/Metadata.cpp
Outdated
| { | ||
| Json::Value metadata; | ||
| if (!Json::Reader().parse(_metadata, metadata, false)) | ||
| if (!dev::jsonParseStrict(_metadata, &metadata)) |
There was a problem hiding this comment.
This file is within the dev namespace.
test/RPCSession.cpp
Outdated
| Json::Value config; | ||
| BOOST_REQUIRE(Json::Reader().parse(c_configString, config)); | ||
| for (auto const& account: _accounts) | ||
| BOOST_REQUIRE(dev::jsonParseStrict(c_configString, &config)); |
There was a problem hiding this comment.
Similarly, this file imports the dev namespace.
test/libdevcore/JSON.cpp
Outdated
|
|
||
| // comments are allowed in strict-mode? - that's strange... | ||
| BOOST_CHECK( | ||
| dev::jsonParseStrict("{\"1\":3, // awesome comment\n\"2\":\"2\",\"3\":{\"3.1\":\"3.1\",\"3.2\":5}}", &json, |
There was a problem hiding this comment.
In these files please follow the coding style, roughly:
BOOST_CHECK(dev::jsonParseStrict(
"{\"1\":3, // awesome comment\n\"2\":\"2\",\"3\":{\"3.1\":\"3.1\",\"3.2\":5}}",
&errors
));
There was a problem hiding this comment.
Yes, thats much better.
| "\n but got:\n" << generatedDocumentation.toStyledString() | ||
| ); | ||
| dev::jsonParseStrict(_expectedDocumentationString, &expectedDocumentation); | ||
| BOOST_CHECK_MESSAGE(expectedDocumentation == generatedDocumentation, |
There was a problem hiding this comment.
Please do not change the coding style here.
| BOOST_CHECK(containsError(result, "JSONError", "Input is not a JSON object.")); | ||
| BOOST_CHECK(!containsError(result, "JSONError", "* Line 1, Column 1\n Syntax error: value, object or array expected.\n")); | ||
| BOOST_CHECK(containsError(result, "JSONError", "* Line 1, Column 1\n A valid JSON document must be either an array or an object value.\n")); | ||
| BOOST_CHECK(!containsError(result, "JSONError","* Line 1, Column 1\n Syntax error: value, object or array expected.\n")); |
There was a problem hiding this comment.
Please also avoid changing the coding style in the lines above.
It's definitely on the edge to get moved out of the header. I would like to move it. |
libdevcore/JSON.h
Outdated
| { | ||
|
|
||
| /// StreamWriterBuilder that can be constructed with specific settings | ||
| class StreamWriterBuilder : public Json::StreamWriterBuilder { |
There was a problem hiding this comment.
Please don't put a space in front of : (except in x ? y : z).
libdevcore/JSON.h
Outdated
| /// StreamWriterBuilder that can be constructed with specific settings | ||
| class StreamWriterBuilder : public Json::StreamWriterBuilder { | ||
| public: | ||
| explicit StreamWriterBuilder(const std::map<std::string, std::string> &_settings) |
There was a problem hiding this comment.
Please put the const after the type
There was a problem hiding this comment.
As far as I know const T& is just an alternative notation of T& const. I know that this notation is sometimes preferred, but why exactly?
libdevcore/JSON.h
Outdated
| /// \param _input JSON input string | ||
| /// \param _builder StreamWriterBuilder that is used to create new Json::StreamWriter | ||
| /// \return serialized json object | ||
| inline std::string print(Json::Value const &_input, Json::StreamWriterBuilder &_builder) { |
There was a problem hiding this comment.
Please put { on its own line and move & to the left: Json::Value const& _input
libdevcore/JSON.h
Outdated
| inline std::string jsonPrettyPrint(Json::Value const& _input) | ||
| { | ||
| return Json::StyledWriter().write(_input); | ||
| static std::map<std::string, std::string> settings{{"indentation", " "}}; |
There was a problem hiding this comment.
Do we need this local variable?
There was a problem hiding this comment.
To what local variable you're referring to? The static settings object is used to configure the static StreamWriterBuilder.
libdevcore/JSON.h
Outdated
| }; | ||
|
|
||
| /// CharReaderBuilder with strict-mode settings | ||
| class StrictModeCharReaderBuilder : public Json::CharReaderBuilder { |
There was a problem hiding this comment.
I think those classes are better suited in an anonymous namespace in the .cpp file - they are only used by the functions in this header, right?
There was a problem hiding this comment.
Yep, currently it's just used by the functions in header. I thought that it could make sense to allow others to use those classes. But I think that will probably never used, because we can already parse and print JSON objects, that are the most important operations. I moved it to the .cpp file.
b21c97d to
b75e3a1
Compare
|
@axic @chriseth Why is there that error? Is that related to my changes? |
|
I'm running a small cleanup on the coding style and will force-push to your branch, so please do not overwrite later. |
b75e3a1 to
c3bc0c7
Compare
| /// \param _json [out] resulting JSON object | ||
| /// \param _errs [out] Formatted error messages | ||
| /// \return \c true if the document was successfully parsed, \c false if an error occurred. | ||
| bool jsonParse(std::string const& _input, Json::Value& _json, std::string* _errs = nullptr); |
There was a problem hiding this comment.
Every use cases uses strict mode - do we want to keep this non-strict helper?
There was a problem hiding this comment.
Good question.. will we need to parse non-strict JSON in the future?
There was a problem hiding this comment.
I don't think so, though it's small we can keep it.
|
From the strict mode documentation: Perhaps it makes sense adding two more tests: comments and invalid UTF-8 strings. |
|
@axic I already included a test to check the comments and I was really surprised that they where allowed. Now I just took a look to the void Reader::skipCommentTokens(Token& token) {
if (features_.allowComments_) {
do {
readToken(token);
} while (token.type_ == tokenComment);
} else {
readToken(token);
}
}It looks like that they just disable the collection of comments in there AST. So there will be no error generated. Probably Regarding |
|
@chriseth Wow, I didn't knew that a force-push to my branch is possible. Good to know. |
We should submit a patch for "super strict", pedantic or "canonical json" mode :)
Ok - I assumed the parser would fail on an invalid UTF-8 sequence. I'll add such a test case anyway so we'll trigger a failure once/if they implement it. |
c3bc0c7 to
c4ed18b
Compare
c4ed18b to
0f29ac4
Compare
see issue #3341 & #3355