use of com.fasterxml.jackson.core.format.DataFormatMatcher 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();
}
use of com.fasterxml.jackson.core.format.DataFormatMatcher 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());
}
use of com.fasterxml.jackson.core.format.DataFormatMatcher 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();
}
use of com.fasterxml.jackson.core.format.DataFormatMatcher 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();
}
use of com.fasterxml.jackson.core.format.DataFormatMatcher in project divolte-collector by divolte.
the class MincodeParserSamplesTest method testMincodeFormatDetector.
@Test
public void testMincodeFormatDetector() throws Exception {
final DataFormatDetector detector = new DataFormatDetector(factory);
final DataFormatMatcher format = detector.findFormat(this.sampleMincode.getBytes(StandardCharsets.UTF_8));
assertEquals(factory.getFormatName(), format.getMatchedFormatName());
}
Aggregations