use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class ManualReadPerfWithMedia method main.
public static void main(String[] args) throws Exception {
if (args.length != 0) {
System.err.println("Usage: java ...");
System.exit(1);
}
MediaItem.Content content = new MediaItem.Content();
content.setTitle("Performance micro-benchmark, to be run manually");
content.addPerson("William");
content.addPerson("Robert");
content.setWidth(900);
content.setHeight(120);
content.setBitrate(256000);
content.setDuration(3600 * 1000L);
content.setCopyright("none");
content.setPlayer(MediaItem.Player.FLASH);
content.setUri("http://whatever.biz");
MediaItem input = new MediaItem(content);
input.addPhoto(new MediaItem.Photo("http://a.com", "title1", 200, 100, MediaItem.Size.LARGE));
input.addPhoto(new MediaItem.Photo("http://b.org", "title2", 640, 480, MediaItem.Size.SMALL));
final JsonFactory f = new JsonFactory();
final String jsonStr = input.asJsonString(f);
final byte[] json = jsonStr.getBytes("UTF-8");
new ManualReadPerfWithMedia(f, jsonStr).test("String", "char[]", json.length);
}
use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class NonStandardParserFeaturesTest method _testSingleQuotesDefault.
/**
* Test to verify that the default parser settings do not
* accept single-quotes for String values (field names,
* textual values)
*/
private void _testSingleQuotesDefault(int mode) throws Exception {
JsonFactory f = new JsonFactory();
// First, let's see that by default they are not allowed
String JSON = "[ 'text' ]";
JsonParser p = createParser(f, mode, JSON);
assertToken(JsonToken.START_ARRAY, p.nextToken());
try {
p.nextToken();
fail("Expected exception");
} catch (JsonParseException e) {
verifyException(e, "Unexpected character ('''");
} finally {
p.close();
}
JSON = "{ 'a':1 }";
p = createParser(f, mode, JSON);
assertToken(JsonToken.START_OBJECT, p.nextToken());
try {
p.nextToken();
fail("Expected exception");
} catch (JsonParseException e) {
verifyException(e, "Unexpected character ('''");
} finally {
p.close();
}
}
use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class NonStandardParserFeaturesTest method _testAllowNaN.
private void _testAllowNaN(int mode) throws Exception {
final String JSON = "[ NaN]";
JsonFactory f = new JsonFactory();
assertFalse(f.isEnabled(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS));
// without enabling, should get an exception
JsonParser p = createParser(f, mode, JSON);
assertToken(JsonToken.START_ARRAY, p.nextToken());
try {
p.nextToken();
fail("Expected exception");
} catch (Exception e) {
verifyException(e, "non-standard");
} finally {
p.close();
}
// we can enable it dynamically (impl detail)
f = f.rebuild().enable(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS).build();
p = createParser(f, mode, JSON);
assertToken(JsonToken.START_ARRAY, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
double d = p.getDoubleValue();
assertTrue(Double.isNaN(d));
assertEquals("NaN", p.getText());
// [Issue#98]
try {
/*BigDecimal dec =*/
p.getDecimalValue();
fail("Should fail when trying to access NaN as BigDecimal");
} catch (NumberFormatException e) {
verifyException(e, "can not be represented as BigDecimal");
}
assertToken(JsonToken.END_ARRAY, p.nextToken());
p.close();
// finally, should also work with skipping
f = f.rebuild().enable(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS).build();
p = createParser(f, mode, JSON);
assertToken(JsonToken.START_ARRAY, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertToken(JsonToken.END_ARRAY, p.nextToken());
p.close();
}
use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class NonStandardParserFeaturesTest method _testLargeUnquoted.
/*
/****************************************************************
/* Secondary test methods
/****************************************************************
*/
private void _testLargeUnquoted(int mode) throws Exception {
StringBuilder sb = new StringBuilder(5000);
sb.append("[\n");
// final int REPS = 2000;
final int REPS = 1050;
for (int i = 0; i < REPS; ++i) {
if (i > 0) {
sb.append(',');
if ((i & 7) == 0) {
sb.append('\n');
}
}
sb.append("{");
sb.append("abc").append(i & 127).append(':');
sb.append((i & 1) != 0);
sb.append("}\n");
}
sb.append("]");
String JSON = sb.toString();
JsonFactory f = JsonFactory.builder().enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES).build();
JsonParser p = createParser(f, mode, JSON);
assertToken(JsonToken.START_ARRAY, p.nextToken());
for (int i = 0; i < REPS; ++i) {
assertToken(JsonToken.START_OBJECT, p.nextToken());
assertToken(JsonToken.FIELD_NAME, p.nextToken());
assertEquals("abc" + (i & 127), p.currentName());
assertToken(((i & 1) != 0) ? JsonToken.VALUE_TRUE : JsonToken.VALUE_FALSE, p.nextToken());
assertToken(JsonToken.END_OBJECT, p.nextToken());
}
assertToken(JsonToken.END_ARRAY, p.nextToken());
p.close();
}
use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class NonStandardParserFeaturesTest method _testAllowInf.
private void _testAllowInf(int mode) throws Exception {
final String JSON = "[ -INF, +INF, +Infinity, Infinity, -Infinity ]";
JsonFactory f = new JsonFactory();
assertFalse(f.isEnabled(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS));
// without enabling, should get an exception
JsonParser p = createParser(f, mode, JSON);
assertToken(JsonToken.START_ARRAY, p.nextToken());
try {
p.nextToken();
fail("Expected exception");
} catch (Exception e) {
verifyException(e, "Non-standard token '-INF'");
} finally {
p.close();
}
f = f.rebuild().enable(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS).build();
p = createParser(f, mode, JSON);
assertToken(JsonToken.START_ARRAY, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
double d = p.getDoubleValue();
assertEquals("-INF", p.getText());
assertTrue(Double.isInfinite(d));
assertTrue(d == Double.NEGATIVE_INFINITY);
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
d = p.getDoubleValue();
assertEquals("+INF", p.getText());
assertTrue(Double.isInfinite(d));
assertTrue(d == Double.POSITIVE_INFINITY);
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
d = p.getDoubleValue();
assertEquals("+Infinity", p.getText());
assertTrue(Double.isInfinite(d));
assertTrue(d == Double.POSITIVE_INFINITY);
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
d = p.getDoubleValue();
assertEquals("Infinity", p.getText());
assertTrue(Double.isInfinite(d));
assertTrue(d == Double.POSITIVE_INFINITY);
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
d = p.getDoubleValue();
assertEquals("-Infinity", p.getText());
assertTrue(Double.isInfinite(d));
assertTrue(d == Double.NEGATIVE_INFINITY);
assertToken(JsonToken.END_ARRAY, p.nextToken());
p.close();
// finally, should also work with skipping
f = f.rebuild().enable(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS).build();
p = createParser(f, mode, JSON);
assertToken(JsonToken.START_ARRAY, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
assertToken(JsonToken.END_ARRAY, p.nextToken());
p.close();
}
Aggregations