Search in sources :

Example 11 with JsonGenerationException

use of com.fasterxml.jackson.core.JsonGenerationException in project uPortal by Jasig.

the class JacksonColumnMapper method toNonNullValue.

@Override
public final String toNonNullValue(Object value) {
    try {
        final Class<? extends Object> type = value.getClass();
        final ObjectWriter typeWriter = typedObjectWriters.getUnchecked(type);
        final String valueAsString = typeWriter.writeValueAsString(value);
        return objectWriter.writeValueAsString(new JsonWrapper(type, valueAsString));
    } catch (JsonGenerationException e) {
        throw new IllegalArgumentException("Could not write to JSON: " + value, e);
    } catch (JsonMappingException e) {
        throw new IllegalArgumentException("Could not write to JSON: " + value, e);
    } catch (IOException e) {
        throw new IllegalArgumentException("Could not write to JSON: " + value, e);
    }
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) IOException(java.io.IOException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException)

Example 12 with JsonGenerationException

use of com.fasterxml.jackson.core.JsonGenerationException in project camel by apache.

the class MultiSelectPicklistSerializer method serialize.

@Override
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
    // get Picklist enum element class from array class
    Class<?> arrayClass = value.getClass();
    final Class<?> aClass = arrayClass.getComponentType();
    try {
        Method getterMethod = aClass.getMethod("value");
        final int length = Array.getLength(value);
        // construct a string of form value1;value2;...
        final StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < length; i++) {
            buffer.append((String) getterMethod.invoke(Array.get(value, i)));
            if (i < (length - 1)) {
                buffer.append(';');
            }
        }
        jgen.writeString(buffer.toString());
    } catch (Exception e) {
        throw new JsonGenerationException(String.format("Exception writing pick list value %s of type %s: %s", value, value.getClass().getName(), e.getMessage()), jgen);
    }
}
Also used : Method(java.lang.reflect.Method) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) IOException(java.io.IOException)

Example 13 with JsonGenerationException

use of com.fasterxml.jackson.core.JsonGenerationException in project camel by apache.

the class StringMultiSelectPicklistSerializer method serialize.

@Override
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
    try {
        String[] a = (String[]) value;
        final int length = a.length;
        // construct a string of form value1;value2;...
        final StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < length; i++) {
            buffer.append(a[i].trim());
            if (i < (length - 1)) {
                buffer.append(';');
            }
        }
        jgen.writeString(buffer.toString());
    } catch (Exception e) {
        throw new JsonGenerationException(String.format("Exception writing pick list value %s of type %s: %s", value, value.getClass().getName(), e.getMessage()), jgen);
    }
}
Also used : JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) IOException(java.io.IOException)

Example 14 with JsonGenerationException

use of com.fasterxml.jackson.core.JsonGenerationException in project cloudstack by apache.

the class CSJacksonAnnotationTest method test.

@Test
@Ignore
public void test() {
    ObjectMapper mapper = new ObjectMapper();
    JaxbAnnotationModule jaxbModule = new JaxbAnnotationModule();
    jaxbModule.setPriority(Priority.SECONDARY);
    mapper.registerModule(jaxbModule);
    mapper.registerModule(new CSJacksonAnnotationModule());
    StringWriter writer = new StringWriter();
    TestVO vo = new TestVO(1000, "name");
    vo.names = new ArrayList<String>();
    vo.names.add("name1");
    vo.names.add("name2");
    vo.values = new HashMap<String, Long>();
    vo.values.put("key1", 1000l);
    vo.values.put("key2", 2000l);
    vo.vo2.name = "testvoname2";
    vo.pods = "abcde";
    try {
        mapper.writeValue(writer, vo);
    } catch (JsonGenerationException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.print(writer.getBuffer().toString());
}
Also used : JaxbAnnotationModule(com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule) StringWriter(java.io.StringWriter) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) IOException(java.io.IOException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 15 with JsonGenerationException

use of com.fasterxml.jackson.core.JsonGenerationException in project JMRI by JMRI.

the class PanelServlet method getJsonPanel.

@Override
protected String getJsonPanel(String name) {
    log.debug("Getting {} for {}", getPanelType(), name);
    try {
        PanelEditor editor = (PanelEditor) getEditor(name);
        ObjectNode root = this.mapper.createObjectNode();
        ObjectNode panel = root.putObject("panel");
        JFrame frame = editor.getTargetFrame();
        panel.put("name", name);
        panel.put("height", frame.getContentPane().getHeight());
        panel.put("width", frame.getContentPane().getWidth());
        panel.put("panelheight", frame.getContentPane().getHeight());
        panel.put("panelwidth", frame.getContentPane().getWidth());
        panel.put("showtooltips", editor.showTooltip());
        panel.put("controlling", editor.allControlling());
        if (editor.getBackgroundColor() != null) {
            ObjectNode color = panel.putObject("backgroundColor");
            color.put("red", editor.getBackgroundColor().getRed());
            color.put("green", editor.getBackgroundColor().getGreen());
            color.put("blue", editor.getBackgroundColor().getBlue());
        }
        // include contents
        log.debug("N elements: {}", editor.getContents().size());
        for (Positionable sub : editor.getContents()) {
            try {
            // TODO: get all panel contents as JSON
            // I tried using JavaBean Introspection to simply build the contents using Jackson Databindings,
            // but when a panel element has a reference to the panel or to itself as a property, this leads
            // to infinite recursion
            } catch (Exception ex) {
                log.error("Error storing panel element: " + ex, ex);
            }
        }
        return this.mapper.writeValueAsString(root);
    } catch (NullPointerException ex) {
        log.warn("Requested Panel [" + name + "] does not exist.");
        return "ERROR Requested panel [" + name + "] does not exist.";
    } catch (JsonGenerationException e) {
        log.error("Error generating JSON", e);
        return "ERROR " + e.getLocalizedMessage();
    } catch (JsonMappingException e) {
        log.error("Error mapping JSON", e);
        return "ERROR " + e.getLocalizedMessage();
    } catch (IOException e) {
        log.error("IOException", e);
        return "ERROR " + e.getLocalizedMessage();
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JFrame(javax.swing.JFrame) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Positionable(jmri.jmrit.display.Positionable) IOException(java.io.IOException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) PanelEditor(jmri.jmrit.display.panelEditor.PanelEditor) IOException(java.io.IOException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException)

Aggregations

JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)17 IOException (java.io.IOException)9 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)8 Test (org.junit.Test)3 RequestFailedException (com.cribbstechnologies.clients.mandrill.exception.RequestFailedException)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 BaseMandrillRequest (com.cribbstechnologies.clients.mandrill.model.BaseMandrillRequest)1 MandrillError (com.cribbstechnologies.clients.mandrill.model.MandrillError)1 BaseMandrillStringResponse (com.cribbstechnologies.clients.mandrill.model.response.BaseMandrillStringResponse)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 InvalidFormatException (com.fasterxml.jackson.databind.exc.InvalidFormatException)1 PropertyBindingException (com.fasterxml.jackson.databind.exc.PropertyBindingException)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 JaxbAnnotationModule (com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule)1 JsonLdError (com.github.jsonldjava.core.JsonLdError)1 ErrorMessage (io.dropwizard.jersey.errors.ErrorMessage)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 StringWriter (java.io.StringWriter)1