3 ms·
I generally prefer using JSON in my interactions, where possible. That's because it is lightweight, and 99.9% of the data I'm transferring is scalar. JSON is b
by ChrisMarshallNY 5y ago
I generally prefer using JSON in my interactions, where possible.
That's because it is lightweight, and 99.9% of the data I'm transferring is scalar. JSON is basically "implied" for scalar types.
The good thing (if you want to call it "good") about XML, is Schema.
Schema is a "rock hard" contract. It is definite, empirical, unambiguous. When I am looking at an API, and it has a Schema, then I know that I can figure out exactly what shape the data will take/emit.
If possible, I still use JSON for the exchange. I just use the XML to figure out the specifics in the exchange.
I've been working in XML forever, it seems. I've done ONVIF stuff, which is SOAP/WSDL-based, and even XSLT.
I still like JSON.
Mostly because I don't have to deal with Schema.
Over the years, I've gotten fairly good at developing XML Schemas, but I have never gotten "used" to it, and still have to look everything up.
If I develop APIs, I will often do an XML variant, alongside the JSON, because that forces me to write a Schema. Doing this, helps me to "code review" my schema, and also gives me a very convenient automated test hook.
But, boy, I still hate XML Schema...
- tored 5y agoOne thing I'm guilty of is designing a document format as JSON, thus the need to formally verify it. Passing data works well as JSON but documents works better as XML where you can use XML Schema.
- ChrisMarshallNY 5y agoThis is where I would also develop an XML variant, even if it is never to be used, simply so that I can have a Schema to go along with it. I'll start with a data structure/class, in the code, then use a built-in transformer, to turn it into JSON or XML. That way, I know that the "kernel" of the data is the same, between them. Even though JSON is a "natural" for scalar types, it can get weird, there. For example, let's say we have a float: { "kids": 1.0 } It needs to be parsed as a float, because we could have: { "kids": 2.3 } But I often see these sent as: { "kids": 1 } or { "kids": "1.0" } or even { "kids": "1" } And I have differing results, based on the parser.
- matja 5y agoEven with integers (comes up often for large numeric IDs): JSON.stringify(JSON.parse('{"kids":10000000000000001}')) -> '{"kids":10000000000000000}' How do JSON schemas handle "must be an integer", and having to pass a string to get the necessary value?
- tored 5y agoI think you can use a string type with a regexp pattern.
- Someone 5y agoThat’s not an issue with json, but with json libraries. For better or (IMO) for worse {“kids”: 12345678901234567890123456789012345678901234567890} is perfectly legal json.
- matja 5y agoIf every JSON library actively chooses not to implement the JSON spec, that sounds like an issue with JSON to me.
- HelloNurse 5y agoMore precisely, an issue with different literal types instead of one like in XML. In XML, every attribute or text fragment is a string, to be POSSIBLY parsed as a string, integer, date etc. according to what applications choose to do (usually according to an explicit schema). In JSON, YAML, etc. there are at least integers, strings and floating point numbers, introducing arbitrary representation choices (1, 1.0, "1", "1.0") and implementation artifacts (e.g. numerical limits).
- matja 5y agoSeparate types is fine, but JSON doesn't specify a minimum range or precision of the number type that a implementation must have, so the number type is unreliable (and string has to be used instead). To handle any number, a JSON library needs to use an arbitrary-precision number type, which no common one does because of the huge performance hit. So, no better than XML really :D