类com.fasterxml.jackson.core.format.MatchStrength源码实例Demo

下面列出了怎么用com.fasterxml.jackson.core.format.MatchStrength的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: lams   文件: MappingJsonFactory.java
/**
 * Sub-classes need to override this method
 */
@Override
public MatchStrength hasFormat(InputAccessor acc) throws IOException
{
    if (getClass() == MappingJsonFactory.class) {
        return hasJSONFormat(acc);
    }
    return null;
}
 
源代码2 项目: lams   文件: ByteSourceJsonBootstrapper.java
private static MatchStrength tryMatch(InputAccessor acc, String matchStr, MatchStrength fullMatchStrength)
    throws IOException
{
    for (int i = 0, len = matchStr.length(); i < len; ++i) {
        if (!acc.hasMoreBytes()) {
            return MatchStrength.INCONCLUSIVE;
        }
        if (acc.nextByte() != matchStr.charAt(i)) {
            return MatchStrength.NO_MATCH;
        }
    }
    return fullMatchStrength;
}
 
源代码3 项目: lams   文件: JsonFactory.java
/**
 * Convenience method for trying to determine whether input via given accessor
 * is of format type supported by this factory.
 */
public MatchStrength hasFormat(InputAccessor acc) throws IOException
{
    // since we can't keep this abstract, only implement for "vanilla" instance
    if (getClass() == JsonFactory.class) {
        return hasJSONFormat(acc);
    }
    return null;
}
 
源代码4 项目: openbd-core   文件: ByteSourceJsonBootstrapper.java
private static MatchStrength tryMatch(InputAccessor acc, String matchStr, MatchStrength fullMatchStrength)
    throws IOException
{
    for (int i = 0, len = matchStr.length(); i < len; ++i) {
        if (!acc.hasMoreBytes()) {
            return MatchStrength.INCONCLUSIVE;
        }
        if (acc.nextByte() != matchStr.charAt(i)) {
            return MatchStrength.NO_MATCH;
        }
    }
    return fullMatchStrength;
}
 
源代码5 项目: openbd-core   文件: JsonFactory.java
/**
 * Convenience method for trying to determine whether input via given accessor
 * is of format type supported by this factory.
 */
public MatchStrength hasFormat(InputAccessor acc) throws IOException
{
    // since we can't keep this abstract, only implement for "vanilla" instance
    if (getClass() == JsonFactory.class) {
        return hasJSONFormat(acc);
    }
    return null;
}
 
源代码6 项目: divolte-collector   文件: MincodeFactory.java
@Override
public MatchStrength hasFormat(final InputAccessor acc) throws IOException {
    final MatchStrength matchStrength;
    if (acc.hasMoreBytes()) {
        final byte b = acc.nextByte();
        // First byte has to be one of the record types, possibly capital denoting a root object.
        // (Only '.' cannot appear at the start of Mincode.)
        switch (b) {
            case '(':
            case 'a':
            case 's':
            case 't':
            case 'f':
            case 'n':
            case 'd':
            case 'j':
                matchStrength = MatchStrength.SOLID_MATCH;
                break;
            default:
                matchStrength = MatchStrength.NO_MATCH;
        }
    } else {
        // Zero-length isn't supported.
        matchStrength = MatchStrength.NO_MATCH;
    }
    return matchStrength;
}
 
源代码7 项目: jackson-dataformat-hocon   文件: HoconFactory.java
/**
 * Sub-classes need to override this method (as of 1.8)
 */
@Override
public MatchStrength hasFormat(InputAccessor acc) throws IOException
{
    if (!acc.hasMoreBytes()) {
        return 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 MatchStrength.INCONCLUSIVE;
        }
        if (acc.nextByte() != UTF8_BOM_2) {
            return MatchStrength.NO_MATCH;
        }
        if (!acc.hasMoreBytes()) {
            return MatchStrength.INCONCLUSIVE;
        }
        if (acc.nextByte() != UTF8_BOM_3) {
            return MatchStrength.NO_MATCH;
        }
        if (!acc.hasMoreBytes()) {
            return MatchStrength.INCONCLUSIVE;
        }
        b = acc.nextByte();
    }
    if (b == '{' || Character.isLetter((char) b) || Character.isDigit((char) b)) {
        return MatchStrength.WEAK_MATCH;
    }
    return MatchStrength.INCONCLUSIVE;
}
 
源代码8 项目: lams   文件: ByteSourceJsonBootstrapper.java
/**
 * Current implementation is not as thorough as other functionality
 * ({@link com.fasterxml.jackson.core.json.ByteSourceJsonBootstrapper}); 
 * supports UTF-8, for example. But it should work, for now, and can
 * be improved as necessary.
 */
public static MatchStrength hasJSONFormat(InputAccessor acc) throws IOException
{
    // 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 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 MatchStrength.INCONCLUSIVE;
        }
        if (acc.nextByte() != UTF8_BOM_2) {
            return MatchStrength.NO_MATCH;
        }
        if (!acc.hasMoreBytes()) {
            return MatchStrength.INCONCLUSIVE;
        }
        if (acc.nextByte() != UTF8_BOM_3) {
            return MatchStrength.NO_MATCH;
        }
        if (!acc.hasMoreBytes()) {
            return MatchStrength.INCONCLUSIVE;
        }
        b = acc.nextByte();
    }
    // Then possible leading space
    int ch = skipSpace(acc, b);
    if (ch < 0) {
        return 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 MatchStrength.INCONCLUSIVE;
        }
        if (ch == '"' || ch == '}') {
            return MatchStrength.SOLID_MATCH;
        }
        // ... should we allow non-standard? Let's not yet... can add if need be
        return MatchStrength.NO_MATCH;
    }
    MatchStrength strength;
    
    if (ch == '[') {
        ch = skipSpace(acc);
        if (ch < 0) {
            return MatchStrength.INCONCLUSIVE;
        }
        // closing brackets is easy; but for now, let's also accept opening...
        if (ch == ']' || ch == '[') {
            return MatchStrength.SOLID_MATCH;
        }
        return MatchStrength.SOLID_MATCH;
    } else {
        // plain old value is not very convincing...
        strength = 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 MatchStrength.INCONCLUSIVE;
        }
        return (ch <= '9' && ch >= '0') ? strength : 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 MatchStrength.NO_MATCH;
}
 
源代码9 项目: lams   文件: JsonFactory.java
/**
 * Helper method that can be called to determine if content accessed
 * using given accessor seems to be JSON content.
 */
protected MatchStrength hasJSONFormat(InputAccessor acc) throws IOException
{
    return ByteSourceJsonBootstrapper.hasJSONFormat(acc);
}
 
源代码10 项目: vespa   文件: FormatInputStream.java
/**
 * Creates a single data input stream from either file or InputStream depending on which one is present. Preference
 * for file if both present. Additionally also detects input data format of the result stream, throws
 * IllegalArgumentException if unable to determine data format.
 *
 * @param stream              InputStream of the data if present
 * @param inputFile           path to file to use as input
 * @param addRootElementToXml to add vespafeed root element around the input data stream
 * @throws IOException        on errors
 */
public FormatInputStream(InputStream stream, Optional<String> inputFile, boolean addRootElementToXml)
        throws IOException {
    DataFormatDetector dataFormatDetector = new DataFormatDetector(new JsonFactory(), new XmlFactory());
    DataFormatMatcher formatMatcher;

    if (inputFile.isPresent()) {
        try (FileInputStream fileInputStream = new FileInputStream(inputFile.get())) {
            formatMatcher = dataFormatDetector.findFormat(fileInputStream);
        }
        inputStream = new FileInputStream(inputFile.get());

    } else {
        if (stream.available() == 0)
            System.out.println("No data in stream yet and no file specified, waiting for data.");

        inputStream = stream.markSupported() ? stream : new BufferedInputStream(stream);
        inputStream.mark(DataFormatDetector.DEFAULT_MAX_INPUT_LOOKAHEAD);
        formatMatcher = dataFormatDetector.findFormat(inputStream);
        inputStream.reset();
    }

    if (addRootElementToXml) {
        inputStream = addVespafeedTag(inputStream);
        format = Format.XML;
        return;
    }

    if (formatMatcher.getMatchStrength() == MatchStrength.INCONCLUSIVE
        || formatMatcher.getMatchStrength() == MatchStrength.NO_MATCH) {
        throw new IllegalArgumentException("Could not detect input format");
    }

    switch (formatMatcher.getMatchedFormatName().toLowerCase()) {
        case "json":
            format = Format.JSON;
            break;
        case "xml":
            format = Format.XML;
            break;
        default:
            throw new IllegalArgumentException("Unknown data format");
    }
}
 
源代码11 项目: openbd-core   文件: ByteSourceJsonBootstrapper.java
/**
 * Current implementation is not as thorough as other functionality
 * ({@link com.fasterxml.jackson.core.json.ByteSourceJsonBootstrapper}); 
 * supports UTF-8, for example. But it should work, for now, and can
 * be improved as necessary.
 */
public static MatchStrength hasJSONFormat(InputAccessor acc) throws IOException
{
    // 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 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 MatchStrength.INCONCLUSIVE;
        }
        if (acc.nextByte() != UTF8_BOM_2) {
            return MatchStrength.NO_MATCH;
        }
        if (!acc.hasMoreBytes()) {
            return MatchStrength.INCONCLUSIVE;
        }
        if (acc.nextByte() != UTF8_BOM_3) {
            return MatchStrength.NO_MATCH;
        }
        if (!acc.hasMoreBytes()) {
            return MatchStrength.INCONCLUSIVE;
        }
        b = acc.nextByte();
    }
    // Then possible leading space
    int ch = skipSpace(acc, b);
    if (ch < 0) {
        return 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 MatchStrength.INCONCLUSIVE;
        }
        if (ch == '"' || ch == '}') {
            return MatchStrength.SOLID_MATCH;
        }
        // ... should we allow non-standard? Let's not yet... can add if need be
        return MatchStrength.NO_MATCH;
    }
    MatchStrength strength;
    
    if (ch == '[') {
        ch = skipSpace(acc);
        if (ch < 0) {
            return MatchStrength.INCONCLUSIVE;
        }
        // closing brackets is easy; but for now, let's also accept opening...
        if (ch == ']' || ch == '[') {
            return MatchStrength.SOLID_MATCH;
        }
        return MatchStrength.SOLID_MATCH;
    } else {
        // plain old value is not very convincing...
        strength = 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 MatchStrength.INCONCLUSIVE;
        }
        return (ch <= '9' && ch >= '0') ? strength : 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 MatchStrength.NO_MATCH;
}
 
源代码12 项目: openbd-core   文件: JsonFactory.java
/**
 * Helper method that can be called to determine if content accessed
 * using given accessor seems to be JSON content.
 */
protected MatchStrength hasJSONFormat(InputAccessor acc) throws IOException
{
    return ByteSourceJsonBootstrapper.hasJSONFormat(acc);
}
 
 类方法
 同包方法