-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Discussion: How to structure the parsing function families #623
Comments
For the conditional parse consider a std::pair and/or std::optional or your own std::optional lookalike. |
You could also add a function nlohmann::json obj;
if (!nlohmann::str2json("{}", &obj)) printf("parse error");
if (!nlohmann::str2json("{}", nullptr)) printf("parse error"); // same as above but only validates the string Alternatively you could add a valid/invalid variable to the nlohmann::json obj = nlohmann::str2json("{}"));
if (!obj.is_valid()) printf("parse error"); This way you would have something different than yet another parse function. |
I personally prefer the pointer version the most because it is so obvious that if I pass nullptr as an argument then I don't wish to store the parsed json structure anywhere but I am merely validating the string. |
Note that With |
It would a too Cish solution in my opinion, for better or worse, we use c++
here.
…On Jun 17, 2017 7:42 PM, "Erich Erstu" ***@***.***> wrote:
You could also add a function bool nlohmann::str2json(const char *,
nlohmann::json *)
nlohmann::json obj;if (!nlohmann::str2json("{}", &obj)) printf("parse error");if (!nlohmann::str2json("{}", nullptr)) printf("parse error"); // same as above but only validates the string
Alternatively you could add a valid/invalid variable to the nlohmann::json
class. It would work out like this:
nlohmann::json obj = nlohmann::str2json("{}"));
if (!obj.is_valid()) printf("parse error");
This way you would have something different than yet another *parse*
function.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#623 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AH6jVM8xqTw62H_84jHdte4_yDTjF0zDks5sFA_-gaJpZM4N9NYh>
.
|
I am a very practical person and C is a very practical language. I don't think we should have bullshit syntactic sugar crust all over the place just to make the code look more C++ for the sake of it. Less is more. Sure the |
One more thing is that since your solution is just a single header (which is what I love about nlohmann::json btw) you should avoid bloating the code with too many border use cases. if you want to satisfy any possible potential need of any programmer then you'd better develop a full blown library :P |
Some comments:
|
lol, you're a true C programmer. Note that I don't think that's bad. C is
converging towards C++.
2017-06-17 21:07 GMT+02:00 Erich Erstu <[email protected]>:
… @nlohmann <https://github.com/nlohmann>
ok these are fair points you brought out. I like the idea of keeping it
consistent with the STL library.
maybe nothrow <http://en.cppreference.com/w/cpp/memory/new/nothrow> would
be of any help? not sure if this can be used here though.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#623 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AH6jVMaF293UFEfLU_FawMjGngp3TfKlks5sFCPrgaJpZM4N9NYh>
.
|
We now rely on implicit conversions to an input_adapter object in the parse/accept functions.
I want to discuss a restructuring of the way input is parsed.
For this, we need to consider three dimensions:
1. Inputs
We currently accept inputs with the following types:
const char []
)const char*
orconst uint8_t*
)std::ifstream
,std::stringstream
, ...)std::vector<char>
orstd::vector<uint8_t>
)The input handling has been abstracted into a class
input_adapter
which offers a unified interface to get a single character or return multiple characters as astd::string
.2. File formats
We currently support the following inputs:
3. What to do with the input
For JSON, we support the generation of a
basic_json
object from any inputs using theparse()
function family. Withoperator<<
we also allow to sequentially parse multiple JSON values from the same input stream. In addition, we may pass a callback function that allows to react on certain parse events.For CBOR and MessagePack, we also suppor the generation of a
basic_json
object.In either case, a parse error yields an exception. There is currently no way to indicate an error without throwing (or calling
abort
if exceptions are switched off).Just supporting all input types for all file formats would need 5 x 3 = 15 functions. If we then want to add support for callbacks, make exceptions optional, allow to provide a JSON object reference to the parser as target, or anything else, we may quickly end up in dozens of similar functions. This is madness.
Instead, I would like to discuss the following approach. We implement a parser class that is exposed to the user that can be constructed with any of the input types listed above. Then, the user can decided what to do by configuring this parser object and by calling its member functions. Having a parser object, we may, for instance, decide to create an object that describes a parse error which is available to the user after parsing. So instead of throwing, we can alternativeley allow to query this object afterward, just like
errno
, but nicer.I would like to still offer the static parse functions for JSON so the user can write
json j = json::parse("[1,2,3]");
. Removing these functions would be too confusing, even for a 3.0.0 release. But these functions would then just offer the default behavior (no callback, throwing exceptions). Anything fancy should be moved to the described parser class. Meaning, I would like to offer the simplest use cases with the old interface, but allow for more complex scenarios without an explosion of functions.What do you think?
The text was updated successfully, but these errors were encountered: