use of com.fasterxml.jackson.databind.ObjectReader in project jackson-databind by FasterXML.
the class TestJDKSerialization method testEnumHandlers.
// for [databind#899]
public void testEnumHandlers() throws IOException {
ObjectMapper mapper = new ObjectMapper();
// ensure we have serializers and/or deserializers, first
String json = mapper.writerFor(EnumPOJO.class).writeValueAsString(new EnumPOJO());
EnumPOJO result = mapper.readerFor(EnumPOJO.class).readValue(json);
assertNotNull(result);
// and then use JDK serialization to freeze/thaw objects
byte[] bytes = jdkSerialize(mapper);
ObjectMapper mapper2 = jdkDeserialize(bytes);
assertNotNull(mapper2);
bytes = jdkSerialize(mapper.readerFor(EnumPOJO.class));
ObjectReader r = jdkDeserialize(bytes);
assertNotNull(r);
/* 14-Aug-2015, tatu: Looks like pre-loading JsonSerializer is problematic
* at this point; comment out for now. Try to fix later on.
*/
bytes = jdkSerialize(mapper.writerFor(EnumPOJO.class));
ObjectWriter w = jdkDeserialize(bytes);
assertNotNull(w);
// plus, ensure objects are usable:
String json2 = w.writeValueAsString(new EnumPOJO());
assertEquals(json, json2);
EnumPOJO result2 = r.readValue(json2);
assertNotNull(result2);
}
use of com.fasterxml.jackson.databind.ObjectReader in project midpoint by Evolveum.
the class JsonValueParser method asDomElement.
Element asDomElement() throws IOException {
ObjectMapper mapper = (ObjectMapper) parser.getCodec();
ObjectReader r = mapper.readerFor(Document.class);
return ((Document) r.readValue(node)).getDocumentElement();
}
use of com.fasterxml.jackson.databind.ObjectReader in project midpoint by Evolveum.
the class JsonValueParser method parse.
@Override
public T parse(QName typeName, XNodeProcessorEvaluationMode mode) throws SchemaException {
ObjectMapper mapper = (ObjectMapper) parser.getCodec();
Class clazz = XsdTypeMapper.toJavaType(typeName);
ObjectReader r = mapper.readerFor(clazz);
try {
return r.readValue(node);
// TODO implement COMPAT mode
} catch (IOException e) {
throw new SchemaException("Cannot parse value: " + e.getMessage(), e);
}
}
use of com.fasterxml.jackson.databind.ObjectReader in project JMRI by JMRI.
the class JsonServer method handleClient.
// Handle communication to a client through inStream and outStream
@Override
public void handleClient(DataInputStream inStream, DataOutputStream outStream) throws IOException {
ObjectReader reader = this.mapper.reader();
JsonClientHandler handler = new JsonClientHandler(new JsonConnection(outStream));
// Start by sending a welcome message
handler.onMessage(JsonClientHandler.HELLO_MSG);
while (true) {
try {
handler.onMessage(reader.readTree((InputStream) inStream));
// Read the command from the client
} catch (IOException e) {
// attempt to close the connection and throw the exception
handler.dispose();
throw e;
} catch (NoSuchElementException nse) {
// so break out of the loop
break;
}
}
handler.dispose();
}
use of com.fasterxml.jackson.databind.ObjectReader in project JMRI by JMRI.
the class JsonClientTrafficController method receiveLoop.
@Override
public void receiveLoop() {
log.debug("receiveLoop starts");
ObjectReader reader = this.mapper.reader();
while (true) {
try {
JsonNode root = reader.readTree((InputStream) this.istream);
String type = root.path(TYPE).asText();
JsonNode data = root.path(DATA);
log.debug("Processing {} with {}", type, data);
if (type.equals(GOODBYE)) {
log.info("Connection closing from server.");
break;
} else if (type.equals(HELLO)) {
this.receiveHello(data);
} else if (type.equals(LOCALE)) {
this.receiveHello(data);
} else if (type.equals(PONG)) {
// silently ignore
} else if (!data.isMissingNode() || (root.isArray() && ((ArrayNode) root).size() > 0)) {
// process replies with a data node or non-empty arrays
JsonClientReply reply = new JsonClientReply(root);
Runnable r = new RcvNotifier(reply, mLastSender, this);
try {
SwingUtilities.invokeAndWait(r);
} catch (InterruptedException e) {
log.error("Exception notifying listeners of reply: {}", e.getMessage(), e);
} catch (InvocationTargetException e) {
log.error("Exception notifying listeners of reply: {}", e.getMessage(), e);
}
}
} catch (IOException e) {
this.rcvException = true;
reportReceiveLoopException(e);
break;
} catch (NoSuchElementException nse) {
// so break out of the loop
break;
}
}
ConnectionStatus.instance().setConnectionState(this.controller.getCurrentPortName(), ConnectionStatus.CONNECTION_DOWN);
log.error("Exit from rcv loop");
this.recovery();
}
Aggregations