Search in sources :

Example 1 with DataFormatDetector

use of com.fasterxml.jackson.core.format.DataFormatDetector in project jackson-core by FasterXML.

the class TestJsonFormatDetection method testSimpleValidObject.

public void testSimpleValidObject() throws Exception {
    JsonFactory jsonF = new JsonFactory();
    DataFormatDetector detector = new DataFormatDetector(jsonF);
    final String JSON = "{  \"field\" : true }";
    DataFormatMatcher matcher = detector.findFormat(new ByteArrayInputStream(JSON.getBytes("UTF-8")));
    // should have match
    assertTrue(matcher.hasMatch());
    assertEquals("JSON", matcher.getMatchedFormatName());
    assertSame(jsonF, matcher.getMatch());
    // no "certain" match with JSON, but solid:
    assertEquals(MatchStrength.SOLID_MATCH, matcher.getMatchStrength());
    // and thus:
    JsonParser jp = matcher.createParserWithMatch();
    assertToken(JsonToken.START_OBJECT, jp.nextToken());
    assertToken(JsonToken.FIELD_NAME, jp.nextToken());
    assertEquals("field", jp.getCurrentName());
    assertToken(JsonToken.VALUE_TRUE, jp.nextToken());
    assertToken(JsonToken.END_OBJECT, jp.nextToken());
    assertNull(jp.nextToken());
    jp.close();
}
Also used : DataFormatDetector(com.fasterxml.jackson.core.format.DataFormatDetector) DataFormatMatcher(com.fasterxml.jackson.core.format.DataFormatMatcher)

Example 2 with DataFormatDetector

use of com.fasterxml.jackson.core.format.DataFormatDetector in project jackson-core by FasterXML.

the class TestJsonFormatDetection method testSimpleInvalid.

public void testSimpleInvalid() throws Exception {
    DataFormatDetector detector = new DataFormatDetector(new JsonFactory());
    final String NON_JSON = "<root />";
    DataFormatMatcher matcher = detector.findFormat(new ByteArrayInputStream(NON_JSON.getBytes("UTF-8")));
    // should not have match
    assertFalse(matcher.hasMatch());
    // and thus:
    assertEquals(MatchStrength.INCONCLUSIVE, matcher.getMatchStrength());
    // also:
    assertNull(matcher.createParserWithMatch());
}
Also used : DataFormatDetector(com.fasterxml.jackson.core.format.DataFormatDetector) DataFormatMatcher(com.fasterxml.jackson.core.format.DataFormatMatcher)

Example 3 with DataFormatDetector

use of com.fasterxml.jackson.core.format.DataFormatDetector in project jackson-core by FasterXML.

the class TestJsonFormatDetection method testSimpleValidString.

/**
     * While JSON String is not a strong match alone, it should
     * be detected unless some better match is available
     */
public void testSimpleValidString() throws Exception {
    JsonFactory jsonF = new JsonFactory();
    DataFormatDetector detector = new DataFormatDetector(jsonF);
    final String JSON = "\"JSON!\"";
    DataFormatMatcher matcher = detector.findFormat(new ByteArrayInputStream(JSON.getBytes("UTF-8")));
    // should have match
    assertTrue(matcher.hasMatch());
    assertEquals("JSON", matcher.getMatchedFormatName());
    assertSame(jsonF, matcher.getMatch());
    assertEquals(MatchStrength.WEAK_MATCH, matcher.getMatchStrength());
    JsonParser jp = matcher.createParserWithMatch();
    assertToken(JsonToken.VALUE_STRING, jp.nextToken());
    assertEquals("JSON!", jp.getText());
    assertNull(jp.nextToken());
    jp.close();
}
Also used : DataFormatDetector(com.fasterxml.jackson.core.format.DataFormatDetector) DataFormatMatcher(com.fasterxml.jackson.core.format.DataFormatMatcher)

Example 4 with DataFormatDetector

use of com.fasterxml.jackson.core.format.DataFormatDetector in project divolte-collector by divolte.

the class MincodeFactoryTest method testDataDetector.

@Test
public void testDataDetector() throws Exception {
    final JsonFactory factory = mincodeMapper.getFactory();
    final DataFormatDetector detector = new DataFormatDetector(factory);
    assertEquals(factory.getFormatName(), detector.findFormat(MINCODE.getBytes(StandardCharsets.UTF_8)).getMatchedFormatName());
    // Zero-length mincode is invalid.
    assertFalse(detector.findFormat(new byte[0]).hasMatch());
    // 'z' is not a valid record type.
    assertFalse(detector.findFormat("zoop".getBytes(StandardCharsets.UTF_8)).hasMatch());
    // '.' is a valid record type, but not as the first record.
    assertFalse(detector.findFormat(".".getBytes(StandardCharsets.UTF_8)).hasMatch());
}
Also used : DataFormatDetector(com.fasterxml.jackson.core.format.DataFormatDetector) JsonFactory(com.fasterxml.jackson.core.JsonFactory) Test(org.junit.Test)

Example 5 with DataFormatDetector

use of com.fasterxml.jackson.core.format.DataFormatDetector in project jackson-core by FasterXML.

the class TestJsonFormatDetection method testSimpleValidArray.

public void testSimpleValidArray() throws Exception {
    JsonFactory jsonF = new JsonFactory();
    DataFormatDetector detector = new DataFormatDetector(jsonF);
    final String ARRAY_JSON = "[ 1, 2 ]";
    DataFormatMatcher matcher = detector.findFormat(new ByteArrayInputStream(ARRAY_JSON.getBytes("UTF-8")));
    // should have match
    assertTrue(matcher.hasMatch());
    assertEquals("JSON", matcher.getMatchedFormatName());
    assertSame(jsonF, matcher.getMatch());
    // no "certain" match with JSON, but solid:
    assertEquals(MatchStrength.SOLID_MATCH, matcher.getMatchStrength());
    // and thus:
    JsonParser jp = matcher.createParserWithMatch();
    assertToken(JsonToken.START_ARRAY, jp.nextToken());
    assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
    assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
    assertToken(JsonToken.END_ARRAY, jp.nextToken());
    assertNull(jp.nextToken());
    jp.close();
}
Also used : DataFormatDetector(com.fasterxml.jackson.core.format.DataFormatDetector) DataFormatMatcher(com.fasterxml.jackson.core.format.DataFormatMatcher)

Aggregations

DataFormatDetector (com.fasterxml.jackson.core.format.DataFormatDetector)6 DataFormatMatcher (com.fasterxml.jackson.core.format.DataFormatMatcher)5 Test (org.junit.Test)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1