Search in sources :

Example 26 with ObjectReader

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);
}
Also used : ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 27 with ObjectReader

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();
}
Also used : ObjectReader(com.fasterxml.jackson.databind.ObjectReader) Document(org.w3c.dom.Document) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 28 with ObjectReader

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);
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 29 with ObjectReader

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();
}
Also used : JsonClientHandler(jmri.server.json.JsonClientHandler) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) IOException(java.io.IOException) JsonConnection(jmri.server.json.JsonConnection) NoSuchElementException(java.util.NoSuchElementException)

Example 30 with ObjectReader

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();
}
Also used : ObjectReader(com.fasterxml.jackson.databind.ObjectReader) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

ObjectReader (com.fasterxml.jackson.databind.ObjectReader)32 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)13 Test (org.junit.Test)12 IOException (java.io.IOException)6 JavaType (com.fasterxml.jackson.databind.JavaType)5 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)4 PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)2 Method (java.lang.reflect.Method)2 NoSuchElementException (java.util.NoSuchElementException)2 AclEntry (org.apache.hadoop.fs.permission.AclEntry)2 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 InvalidFormatException (com.fasterxml.jackson.databind.exc.InvalidFormatException)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 FilterProvider (com.fasterxml.jackson.databind.ser.FilterProvider)1 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)1 AvroFactory (com.fasterxml.jackson.dataformat.avro.AvroFactory)1 AvroSchema (com.fasterxml.jackson.dataformat.avro.AvroSchema)1