/// <exception cref="System.IO.IOException"/> private static com.fasterxml.jackson.core.format.MatchStrength tryMatch(com.fasterxml.jackson.core.format.InputAccessor acc, string matchStr, com.fasterxml.jackson.core.format.MatchStrength fullMatchStrength ) { for (int i = 0; i < len; ++i) { if (!acc.hasMoreBytes()) { return com.fasterxml.jackson.core.format.MatchStrength.INCONCLUSIVE; } if (acc.nextByte() != matchStr[i]) { return com.fasterxml.jackson.core.format.MatchStrength.NO_MATCH; } } return fullMatchStrength; }
/// <exception cref="System.IO.IOException"/> private static int skipSpace(com.fasterxml.jackson.core.format.InputAccessor acc) { if (!acc.hasMoreBytes()) { return -1; } return skipSpace(acc, acc.nextByte()); }
/// <exception cref="System.IO.IOException"/> private static int skipSpace(com.fasterxml.jackson.core.format.InputAccessor acc, byte b) { while (true) { int ch = (int)b & unchecked((int)(0xFF)); if (!(ch == ' ' || ch == '\r' || ch == '\n' || ch == '\t')) { return ch; } if (!acc.hasMoreBytes()) { return -1; } b = acc.nextByte(); ch = (int)b & unchecked((int)(0xFF)); } }
/* /********************************************************** /* Encoding detection for data format auto-detection /********************************************************** */ /// <summary> /// Current implementation is not as thorough as other functionality /// ( /// <see cref="ByteSourceJsonBootstrapper"/> /// ); /// supports UTF-8, for example. But it should work, for now, and can /// be improved as necessary. /// </summary> /// <exception cref="System.IO.IOException"/> public static com.fasterxml.jackson.core.format.MatchStrength hasJSONFormat(com.fasterxml.jackson.core.format.InputAccessor acc) { // Ideally we should see "[" or "{"; but if not, we'll accept double-quote (String) // in future could also consider accepting non-standard matches? if (!acc.hasMoreBytes()) { return com.fasterxml.jackson.core.format.MatchStrength.INCONCLUSIVE; } byte b = acc.nextByte(); // Very first thing, a UTF-8 BOM? if (b == UTF8_BOM_1) { // yes, looks like UTF-8 BOM if (!acc.hasMoreBytes()) { return com.fasterxml.jackson.core.format.MatchStrength.INCONCLUSIVE; } if (acc.nextByte() != UTF8_BOM_2) { return com.fasterxml.jackson.core.format.MatchStrength.NO_MATCH; } if (!acc.hasMoreBytes()) { return com.fasterxml.jackson.core.format.MatchStrength.INCONCLUSIVE; } if (acc.nextByte() != UTF8_BOM_3) { return com.fasterxml.jackson.core.format.MatchStrength.NO_MATCH; } if (!acc.hasMoreBytes()) { return com.fasterxml.jackson.core.format.MatchStrength.INCONCLUSIVE; } b = acc.nextByte(); } // Then possible leading space int ch = skipSpace(acc, b); if (ch < 0) { return com.fasterxml.jackson.core.format.MatchStrength.INCONCLUSIVE; } // First, let's see if it looks like a structured type: if (ch == '{') { // JSON object? // Ideally we need to find either double-quote or closing bracket ch = skipSpace(acc); if (ch < 0) { return com.fasterxml.jackson.core.format.MatchStrength.INCONCLUSIVE; } if (ch == '"' || ch == '}') { return com.fasterxml.jackson.core.format.MatchStrength.SOLID_MATCH; } // ... should we allow non-standard? Let's not yet... can add if need be return com.fasterxml.jackson.core.format.MatchStrength.NO_MATCH; } com.fasterxml.jackson.core.format.MatchStrength strength; if (ch == '[') { ch = skipSpace(acc); if (ch < 0) { return com.fasterxml.jackson.core.format.MatchStrength.INCONCLUSIVE; } // closing brackets is easy; but for now, let's also accept opening... if (ch == ']' || ch == '[') { return com.fasterxml.jackson.core.format.MatchStrength.SOLID_MATCH; } return com.fasterxml.jackson.core.format.MatchStrength.SOLID_MATCH; } else { // plain old value is not very convincing... strength = com.fasterxml.jackson.core.format.MatchStrength.WEAK_MATCH; } if (ch == '"') { // string value return strength; } if (ch <= '9' && ch >= '0') { // number return strength; } if (ch == '-') { // negative number ch = skipSpace(acc); if (ch < 0) { return com.fasterxml.jackson.core.format.MatchStrength.INCONCLUSIVE; } return (ch <= '9' && ch >= '0') ? strength : com.fasterxml.jackson.core.format.MatchStrength .NO_MATCH; } // or one of literals if (ch == 'n') { // null return tryMatch(acc, "ull", strength); } if (ch == 't') { // true return tryMatch(acc, "rue", strength); } if (ch == 'f') { // false return tryMatch(acc, "alse", strength); } return com.fasterxml.jackson.core.format.MatchStrength.NO_MATCH; }