use of com.fasterxml.jackson.databind.ObjectWriter in project jackson-databind by FasterXML.
the class ObjectWriterTestBase method test.
protected void test(ObjectMapper mapper, String desc1, T1 inputValue1, Class<? extends T1> inputClass1, String desc2, T2 inputValue2, Class<? extends T2> inputClass2) throws Exception {
final int REPS;
{
final byte[] input1 = mapper.writeValueAsBytes(inputValue1);
final byte[] input2 = mapper.writeValueAsBytes(inputValue2);
// Let's try to guestimate suitable size, N megs of output
REPS = (int) ((double) (targetSizeMegs() * 1000 * 1000) / (double) input1.length);
System.out.printf("Read %d bytes to bind (%d as array); will do %d repetitions\n", input1.length, input2.length, REPS);
}
final ObjectWriter writer0 = mapper.writer().with(SerializationFeature.EAGER_SERIALIZER_FETCH);
final ObjectWriter writer1 = writer0.forType(inputClass1);
final ObjectWriter writer2 = writer0.forType(inputClass2);
int i = 0;
int roundsDone = 0;
final int TYPES = 2;
// Skip first 5 seconds
long startMeasure = System.currentTimeMillis() + 5000L;
System.out.print("Warming up");
final double[] timesMsec = new double[TYPES];
while (true) {
final int round = (i % TYPES);
final boolean lf = (++i % TYPES) == 0;
String msg;
ObjectWriter writer;
Object value;
switch(round) {
case 0:
msg = desc1;
writer = writer1;
value = inputValue1;
break;
case 1:
msg = desc2;
writer = writer2;
value = inputValue2;
break;
default:
throw new Error();
}
double msecs = testSer(REPS, value, writer);
// skip first N seconds to let results stabilize
if (startMeasure > 0L) {
if ((round != 0) || (System.currentTimeMillis() < startMeasure)) {
System.out.print(".");
continue;
}
startMeasure = 0L;
System.out.println();
System.out.println("Starting measurements...");
Thread.sleep(250L);
System.out.println();
}
timesMsec[round] += msecs;
if ((i % 17) == 0) {
System.out.println("[GC]");
Thread.sleep(100L);
System.gc();
Thread.sleep(100L);
}
System.out.printf("Test '%s' [hash: 0x%s] -> %.1f msecs\n", msg, hash, msecs);
Thread.sleep(50L);
if (!lf) {
continue;
}
if ((++roundsDone % 3) == 0) {
double den = (double) roundsDone;
System.out.printf("Averages after %d rounds (%s/%s): %.1f / %.1f msecs\n", roundsDone, desc1, desc2, timesMsec[0] / den, timesMsec[1] / den);
}
System.out.println();
}
}
use of com.fasterxml.jackson.databind.ObjectWriter in project series-rest-api by 52North.
the class BaseController method writeExceptionResponse.
private void writeExceptionResponse(WebException e, HttpServletResponse response, HttpStatus status) {
final String logMessage = "An exception occured.";
if (status == HttpStatus.INTERNAL_SERVER_ERROR) {
LOGGER.error(logMessage, e);
} else {
LOGGER.debug(logMessage, e);
}
// TODO consider using a 'suppress_response_codes=true' parameter and always return 200 OK
response.setStatus(status.value());
response.setContentType(MimeType.APPLICATION_JSON.getMimeType());
ObjectMapper objectMapper = createObjectMapper();
ObjectWriter writer = objectMapper.writerFor(ExceptionResponse.class);
ExceptionResponse exceptionResponse = ExceptionResponse.createExceptionResponse(e, status);
try (OutputStream outputStream = response.getOutputStream()) {
writer.writeValue(outputStream, exceptionResponse);
} catch (IOException ioe) {
LOGGER.error("Could not process error message.", ioe);
}
}
use of com.fasterxml.jackson.databind.ObjectWriter in project drill by apache.
the class TestRepeated method listOfList.
//
// @Test
// public void repeatedMap() {
//
// /**
// * We're going to try to create an object that looks like:
// *
// * {
// * a: [
// * {x: 1, y: 2}
// * {x: 2, y: 1}
// * ]
// * }
// *
// */
// MapVector v = new MapVector("", allocator);
// ComplexWriter writer = new ComplexWriterImpl("col", v);
//
// MapWriter map = writer.rootAsMap();
//
// map.start();
// ListWriter list = map.list("a");
// MapWriter inner = list.map();
//
// IntHolder holder = new IntHolder();
// IntWriter xCol = inner.integer("x");
// IntWriter yCol = inner.integer("y");
//
// inner.start();
//
// holder.value = 1;
// xCol.write(holder);
// holder.value = 2;
// yCol.write(holder);
//
// inner.end();
//
// inner.start();
//
// holder.value = 2;
// xCol.write(holder);
// holder.value = 1;
// yCol.write(holder);
//
// inner.end();
//
// IntWriter numCol = map.integer("nums");
// holder.value = 14;
// numCol.write(holder);
//
// map.end();
//
//
// assertTrue(writer.ok());
//
// System.out.println(v.getAccessor().getObject(0));
//
// }
@Test
public void listOfList() throws Exception {
/**
* We're going to try to create an object that looks like:
*
* {
* a: [
* [1,2,3],
* [2,3,4]
* ],
* nums: 14,
* b: [
* { c: 1 },
* { c: 2 , x: 15}
* ]
* }
*
*/
final MapVector mapVector = new MapVector("", allocator, null);
final ComplexWriterImpl writer = new ComplexWriterImpl("col", mapVector);
writer.allocate();
{
final MapWriter map = writer.rootAsMap();
final ListWriter list = map.list("a");
list.startList();
final ListWriter innerList = list.list();
final IntWriter innerInt = innerList.integer();
innerList.startList();
final IntHolder holder = new IntHolder();
holder.value = 1;
innerInt.write(holder);
holder.value = 2;
innerInt.write(holder);
holder.value = 3;
innerInt.write(holder);
innerList.endList();
innerList.startList();
holder.value = 4;
innerInt.write(holder);
holder.value = 5;
innerInt.write(holder);
innerList.endList();
list.endList();
final IntWriter numCol = map.integer("nums");
holder.value = 14;
numCol.write(holder);
final MapWriter repeatedMap = map.list("b").map();
repeatedMap.start();
holder.value = 1;
repeatedMap.integer("c").write(holder);
repeatedMap.end();
repeatedMap.start();
holder.value = 2;
repeatedMap.integer("c").write(holder);
final BigIntHolder h = new BigIntHolder();
h.value = 15;
repeatedMap.bigInt("x").write(h);
repeatedMap.end();
map.end();
}
{
writer.setPosition(1);
final MapWriter map = writer.rootAsMap();
final ListWriter list = map.list("a");
list.startList();
final ListWriter innerList = list.list();
final IntWriter innerInt = innerList.integer();
innerList.startList();
final IntHolder holder = new IntHolder();
holder.value = -1;
innerInt.write(holder);
holder.value = -2;
innerInt.write(holder);
holder.value = -3;
innerInt.write(holder);
innerList.endList();
innerList.startList();
holder.value = -4;
innerInt.write(holder);
holder.value = -5;
innerInt.write(holder);
innerList.endList();
list.endList();
final IntWriter numCol = map.integer("nums");
holder.value = -28;
numCol.write(holder);
final MapWriter repeatedMap = map.list("b").map();
repeatedMap.start();
holder.value = -1;
repeatedMap.integer("c").write(holder);
repeatedMap.end();
repeatedMap.start();
holder.value = -2;
repeatedMap.integer("c").write(holder);
final BigIntHolder h = new BigIntHolder();
h.value = -30;
repeatedMap.bigInt("x").write(h);
repeatedMap.end();
map.end();
}
final ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
System.out.println("Map of Object[0]: " + ow.writeValueAsString(mapVector.getAccessor().getObject(0)));
System.out.println("Map of Object[1]: " + ow.writeValueAsString(mapVector.getAccessor().getObject(1)));
final ByteArrayOutputStream stream = new ByteArrayOutputStream();
final JsonWriter jsonWriter = new JsonWriter(stream, true, true);
final FieldReader reader = mapVector.getChild("col", MapVector.class).getReader();
reader.setPosition(0);
jsonWriter.write(reader);
reader.setPosition(1);
jsonWriter.write(reader);
System.out.print("Json Read: ");
System.out.println(new String(stream.toByteArray(), Charsets.UTF_8));
writer.close();
}
use of com.fasterxml.jackson.databind.ObjectWriter in project uPortal by Jasig.
the class JpaStatisticalSummaryTest method testStorelessUnivariateStatistic.
public void testStorelessUnivariateStatistic(StorelessUnivariateStatistic sus, double expected) throws Exception {
assertEquals(expected, sus.getResult(), 0.1);
final ObjectMapper mapper = new ObjectMapper();
mapper.findAndRegisterModules();
//Configure Jackson to just use fields
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
mapper.setVisibility(PropertyAccessor.GETTER, Visibility.NONE);
mapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.NONE);
mapper.setVisibility(PropertyAccessor.SETTER, Visibility.NONE);
mapper.setVisibility(PropertyAccessor.CREATOR, Visibility.NONE);
mapper.addMixInAnnotations(Object.class, IgnoreTypeMixIn.class);
final FilterProvider filters = new SimpleFilterProvider().addFilter("storedDataFilter", SimpleBeanPropertyFilter.serializeAllExcept("storedData"));
final ObjectWriter ssWriter = mapper.writer(filters);
final ObjectReader ssReader = mapper.reader(sus.getClass());
final String susString = ssWriter.writeValueAsString(sus);
System.out.println(susString);
final StorelessUnivariateStatistic newSus = ssReader.readValue(susString);
assertEquals(expected, newSus.getResult(), 0.1);
}
use of com.fasterxml.jackson.databind.ObjectWriter in project uPortal by Jasig.
the class AnalyticsIncorporationComponentEventSerializationTest method testMixinNoCopy.
@Test
public void testMixinNoCopy() throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.findAndRegisterModules();
mapper.addMixInAnnotations(Object.class, PortletRenderExecutionEventFilterMixIn.class);
final FilterProvider filterProvider = new SimpleFilterProvider().addFilter(PortletRenderExecutionEventFilterMixIn.FILTER_NAME, SimpleBeanPropertyFilter.filterOutAllExcept("fname", "executionTimeNano", "parameters"));
final ObjectWriter portletEventWriter = mapper.writer(filterProvider);
final String result = portletEventWriter.writeValueAsString(createEvent());
assertEquals("{\"@c\":\".PortletRenderExecutionEvent\",\"fname\":\"fname1\",\"executionTimeNano\":123450000,\"parameters\":{}}", result);
}
Aggregations