by kyberias · 7y ago · 0 comments
In every application, any dependency to JSON should be minimized, contained and preferably eliminated.
What's wrong with writing "initializers" which are serializers/deserializers? If you're looking for automatic file format to C++ class object, why settle for JSON (whether it's this library or JSONCpp) why not use Thrift or Protocol Buffers?
- Thrift: https://thrift.apache.org/ - Protocol Buffers: https://developers.google.com/protocol-buffers/
Because I'm not writing code in a vacuum, and JSON is what everyone else is using. JSON is chosen for simplicity & interoperability.
This has carried me for 99% of all use cases along with a try/catch that barfs and an array specialization that makes a vector:
template<typename T> T get(const char* key) { return boost::lexical_cast<T>( json.GetObject(key) ); }
The reality is that if I see some value of a type I wasn't expecting, something probably went wrong or changed in the protocol anyways.
The idea that types can replace code is just broken. The type system is not a programming language. Or at least, as proven by C++/Haskell/..., not a remotely usable one.
It's obviously not simple - no two parsers have the same behaviour!
This is the exact nonsense that is happening all over the place, and why we need 512GB ram in workstations to compile something.
I can't help but think that some basic knowledge about libraries is missing.
The JSON obsession is that everything under the moon supports it- and it's a nice easy structure which can be converted to a dynamic or statically typed object with no issues. Plus it's very human readable, so it's great for debugging.
If you sit there and ONLY use C++, ok why bother? But many systems need to talk to different systems these days. I can setup a webapi that can communicate with my angular typescript, C#, matlab all through nice easy JSON. Each language just converts it to the object and I'm good to go.
The question is why are you so against such a widely accepted format of communication?
Of course there are other data transport protocols, XML and ProtoBuf to name a couple off the top of my head, but those are extremely heavyweight.
If you want to use JSON for config files, again, they are easy to make human readable and parse. Grab an off the shelf parser and support whatever scenario users may need. Sure we have yaml and ini, or whatever key=value config format your project creates, but you'll be much more on your own with how deep you want to support various value types.
As far as your complaint or desire for JSON to be minimized or eliminated as a philosophy, I'm not sure why this is the top comment about a very well made and loved C++ library to parse JSON. Are you hoping your comment will change people's minds about what library to use or whether their system needs JSON at all? I don't really understand your goal.
Hence, our embedded C++ backend can now easily accommodate and return JSON to the front end using this library.
Plus, what data format do you propose for things like configuration files, etc? No matter what format you choose, you are going to need a parser of that format for C/C++.
TOML has inline and multiline forms for most objects.
This is my entire ini parser for making a std::map<std::string, std::string>. I cast/parse the values at the use site:
std::regex section_test("\\[(.*?)\\]");
std::regex value_test("([\\w\\.]+)\\s*=\\s*([^\\+]+(?!\\+{3}))");
while(-1 != getline((char**)&buf, &len, file))
{
std::string line(buf);
trim(line);
if(!line.size() || line[0] == '#') continue;
std::smatch match;
if(std::regex_search(line, match, section_test))
current_section = match[1].str();
else if(std::regex_search(line, match, value_test))
{
std::string key(match[1].str());
std::string value(match[2].str());
info("INIReader: %s::%s = %s\n",
current_section.c_str(), key.c_str(), value.c_str());
config[current_section][key] = value;
}
}We got used to this, but results are pretty ugly. We get people confused about delimiter order, broken copy-paste, and so on. For the next project, I’d recommend something which can support complex structures. A schema validation would be nice, too.
I put this code in a standalone cpp file so that it compiles to its own object in parallel with the rest of the system. I've never had issues with it with regards to build times.
how do you suggest interaction with $standardised_protocol which uses JSON in that case ?
And how to make the transformation? Using your own setup. No point in expressing (duplicating!) your own data structure definitions in a random library's DDL (which, apart from the inflicted duplication, will not fit very well since it doesn't know your project).
I don't understand, doing this step is exactly why you need to have a library such as the one which is being discussed here. There's no in-language way in C++ to go from a string that looks like `{ "foo": [1,2,3] }` to `struct { int foo[3]; }`.
I would just specify your case as a data item:
ARRAY_ITEM("foo", foo, INT, 3)
And that's really all, specification-wise. Here ARRAY_ITEM is a simple macro that calculates the offset of the array within the struct (need to give the struct a name!), so that results maybe in { .srcfield="foo", .typekind=TYPE_ARRAY, .arraylength=3, .basetype=INT, offset=offsetof(MyStruct, foo) } or something like that. One can compute the length of the C array at compile time and verify that the lengths match, so there will be absolutely no danger of making a wrong data item.The corresponding code that handles the data items of kind ARRAY_ITEM would be something like this:
case TYPE_ARRAY: {
char *store = add_offset(&myStruct, spec->offset);
int elemSize = typeKindToElementSize[spec->typekind];
JsonArray *jsonArray = get_json_array(jsonObject, spec->srcfield);
for (int i = 0; i < spec->arraylength; i++) {
JsonValue *jsonValue = get_json_array_element(jsonArray, i);
/* This makes sure the dynamic type of jsonValue matches, then
does the appropriate conversion and stores the thing. We also
want to check errors, either now or at the end. */
store_json_value(jsonValue, spec->basetype, store + (i * elemSize));
}
}
You'll need <10, maybe 5, of these cases, and basically it's all code that you will test once and then it just works without surprises. This is less than a day of work initially, will have very low maintenance, will work exactly with the type representations you choose and be easy to change. The other aspect is you're now free from a library that you don't understand, will give you Modern C++ headaches, and will be slow to compile.I'm likely to write a new version of this for every new project, to avoid dependencies and to allow for change.
Since JSON only has basic data types, it's easy to just parse and retrieve objects, similar to using XML tree parsing libraries back in the day.
Of the myriads of existing transfer or serialization formats,
* JSON is still the only secure by default one (if you ignore the two later updates, which made it insecure),
* JSON is by far the easiest to parse (small and secure, no references),
* is natively supported by Javascript.
It has some minor design mistakes, and is not binary (such as msgpack, which is therefore faster), but still is a sane and proper default. Esp. it does not support objects and other extensions, with its initializer problems on MITM attacks, and is properly ended. Unlike XML, YAML, BJSON, BSON, ... which do have major problems, or msgpack, protobuf, ... which do have minor problems.
In every application, any dependency to XML should be minimized, contained and preferably eliminated.
And enough people put it into eval that several companies started prepending while(1); to their JSON messages. Don't blindly trust user input no matter what format it comes in.
> JSON is by far the easiest to parse (small and secure, no references),
We all know its definition fits on a business card, which is the reason we have at least seven different specifications[1] on the details omitted from the card.
while(1); existed to work around a browser vulnerability where the same-origin policy could be bypassed by including cross-origin JSON as a <script>. Nothing to do with eval.
How can a data format be insecure?
> JSON is by far the easiest to parse (small and secure, no references),
It's relatively easy to parse (while not as easy as a sane well-specified version of CSV would be). But it also offers no structural integrity, which means that you need to augment the parsing code with terribly ugly validation code. (I don't really use JSON frequently, but the attempts at providing a principled validation framework on top of JSON that I've seen were... unsatisfying).
> In every application, any dependency to XML should be minimized, contained and preferably eliminated.
That's missing the point. It's not about JSON vs XML vs whatever. It's that application logic shouldn't be operating on transport data formats. And even if JSON has a simple representation as runtime data objects (in most scripting languages), that representation is pretty far from ideal compared to a representation tailored towards your specific application logic.
If it tries to be too featureful, then it requires parsers to be very flexible. Malicious input can trick a too-flexible parser into doing things that the developer of whatever function called the parser probably didn't want to happen.
For example, YAML:
https://metacpan.org/pod/Cpanel::JSON::XS#SECURITY-CONSIDERA...
which just misses details on stack-overflows on overlarge nesting levels, or denial of service attacks on overlarge strings, arrays or maps.
Better formats which prepended sizes do have an advantage here, such as msgpack. But msgpack has no CRC or digest verification to detect missing or cut-off tails. JSON (its secure 1st RFC 4627) must be properly nested, it does not need this. From the 2nd RFC 7159 on it became insecure, and the 3rd RFC 8259 is merely a joke, as it didn't fix the known issues, only removed a harmless feature.
Wrong. CSV is horrible to parse with its string quoting rules. JSON accepts only utf8 and is not misleaded by \, nor ",". Done both, JSON is much simpler and can represent nested structures, arrays or maps. CSV importers are mostly broken, and should be left to Excel folks only.
> It's that application logic shouldn't be operating on transport data formats
Nobody is talking about application logic here but you. JSON is one of the best transport data formats, and this library makes it much easier to encode/decode from/to JSON and C++.
We are not talking about javascript's hack to prefer JSON over full objects. Of course is an internal representation always better than an external, esp. such a simplified one. But for transports simple ones are preferred over complicated serialized ones, because then you can easily do MITM stack-overflows or abusing side effects on creating arbitrary objects.
If you didn't do a compilers class and you want a simple language to play around with for lexing/parsing, JSON works great. Here's the core of a probably correct JSON lexer, albeit a super inefficient one, that I whipped up in <110 lines of Python a few years ago out of curiosity[1].
By comparison, check out the state machine transitions at the heart of the cpython implementation of the csv module[2]. It's not really a fair comparison (my JSON lexer is written in Python and uses regexes, the csv parser is written in C and does not use regexes) but even ignoring how nicely the csv parser handles different csv dialects, I still find it strictly more complex.
[1]: https://github.com/chucksmash/jsonish/blob/master/jsonish/to...
[2]: https://github.com/python/cpython/blob/41c57b335330ff48af098...
> Wrong. CSV is horrible to parse with its string quoting rules.
I made a reasonable attempt at preventing exactly this misunderstanding, but I guess if people want to misread you they will.
> Nobody is talking about application logic here but you.
The application logic (I'm including internal data representation here) is the meat of all efforts, so you should absolutely be considering it, instead of needlessly putting isolated efforts in optimization of side actors. Parsing JSON should IMHO be a total side concern, and using an oversized library like the one we're talking about fails to acknowledge that. Such a library is either a wasted effort (if its data types aren't used throughout the application), or else (if they are) use of the library leads to massive problems down the road.
Note that the quote is talking about a well-specified CSV, not any CSV in general. A well-specified CSV would indeed be fairly easy to parse.
XML external entities allow for arbitrary file inclusion: https://en.wikipedia.org/wiki/XML_external_entity_attack
You can make a badly configured XML parser allocate memory until it crashes: https://en.wikipedia.org/wiki/Billion_laughs_attack
You cannot host user-generated xmls on a domain without making yourself vulnerable to cross site scripting attacks. Browsers will happily execute any javascript you include: https://stackoverflow.com/questions/384639/how-to-include-ja...
Often you have control over the sending and receiving parts of an API and can design the transport format to fit your application logic.
For example, I am currently using a GraphQL API and the data structures I get from this API are exactly what I need in my application.
Of course, it's not limited to JSON. For applications, it should be a tiny insignificant detail what format is used for serialization. Be it JSON or some superior format, such as XML.
It depends on the extant to which your program needs to communicate/interoperate with other programs...in particular ones without the same programming language (no Java RMI).
Under a "Unix" philosophy, this happens quite a bit.