jeljer te Wies wrote:
The problem is that I can’t figure out how to check what protocol is
used. (the only thing i can figure out is to just try to transform it
with every known “protocol” until it doesn’t return an error anymore.
Silly wabbit! Just take a look at the JSON, XML and SOAP files you are
(or would be) getting. Pretty easy for a human to tell at a distance
which. And not hard to do a decent job in your server code.
XML, strictly formatted, always starts with a specific line that says
it’s XML. Here’s three examples I got by randomly searching the web:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml version="1.0" encoding=UTF-8"?>
<?xml version="1.0"?>
Must be the first line. Therefore the first six characters in the whole
file should be '<?xml '. So like this:
if (datastring.substr(0, 6) == '<?xml ') …
sorry I’m writing in JS today.
XML, sloppily formatted, might blow this off. At any rate, it consists
of just like HTML. So the first character (after maybe some
whitespace) should be <. You’re not supposed to do sloppy XML, but my
boss does. If you have to accept sloppy xml, like this:
if (datastring.search(/^\s*</) >= 0) …
SOAP is a dialect of XML, I don’t know much about it but probably
there’s some clues close to the start; look at some files and/or read
some SOAP docs.
JSON usually starts with an object at the root, which almost always
starts with a quoted fieldname like this:
{“qleft”:5.5,“qright”:-22.01}
IF it doesn’t, chances are it has an array at the root like this:
[{“qleft”:5.5,“qright”:-22.01},{“qleft”:6.5, “qright”:-202.01}]
If your JSON is tight, like it often is, you can just look for one of
these two like this:
if (datastring.substr(0, 2) == ‘{"’ || datastring.substr(0, 1) == ‘[’)
…
In fact, if you look at your protocol, it’s very possible it’ll ALWAYS
start with an object at top, even simpler. so you can blow off starting
[s.
But of course you can legally stick in spaces so you could use this
regex:
if (datastring…search(/^\s*[[|{]/) >= 0) …
easy!