If the encoding of that string is known to you (or is ASCII or UTF), then looking at the very first char of that String should be enough.
If the String starts
For JSON you also have to strip whitespace before you look at the "first" char (if the String you receive may contain additional whitespace).
While it is legal for JSON data structures to begin with null
, true
, false
you can avoid those situations if you already know a bit about your data structures.
So, basically you could check if the 1st char is a <
and in that case treat it as XML. In other cases treat it as JSON and let jackson fire some exceptions if it is not legal JSON syntax.