Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If a requirement of your program is that the input files be well-formatted and not mixed-type, then I would recommend replacing your JSON and XML impls with the following:</p> <ol> <li><p>JSON - simply look for an opening '{' as one of the first chars in the file; this is an invalid format for any other files (except maybe script depending on your format). If you find '{' as the first char, its a JSON. This saves on processing the entire file with GSON.</p></li> <li><p>XML - look for file header; well-formed XML files cannot even have space before this header; it must appear immediately. Again, no reason to try and ingest the entire thing just to catch an exception.</p></li> <li><p>PROPERTIES - in the same vein, I would check the first line and make sure it has =\n format. If they do, you are good to go.</p></li> <li><p>SCRIPT - I am not sure the format of your scripting language, but you get the idea.</p></li> </ol> <p>All told, doing cheap/quick checks if your requirements are well-defined is the way to go here. If you require a JSON file to be all JSON and the first char you encounter reading the file is '{' then I'd say that is a JSON file and not EMPTY, XML or PROPERTIES (again, excluding SCRIPT because I don't know the format).</p> <p>Then you can rewind the input stream and give it to your parsing library to read (this is where <a href="http://docs.oracle.com/javase/7/docs/api/java/io/PushbackInputStream.html" rel="nofollow">PushbackInputStreams</a> can come handy)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload