Search in sources :

Example 81 with JsonMappingException

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonMappingException in project jackson-databind by FasterXML.

the class TestInferredMutators method testDeserializationInference.

// for #195
public void testDeserializationInference() throws Exception {
    final String JSON = "{\"x\":2}";
    ObjectMapper mapper = new ObjectMapper();
    // First: default case, inference enabled:
    assertTrue(mapper.isEnabled(MapperFeature.INFER_PROPERTY_MUTATORS));
    Point p = mapper.readValue(JSON, Point.class);
    assertEquals(2, p.x);
    // but without it, should fail:
    mapper = new ObjectMapper();
    mapper.disable(MapperFeature.INFER_PROPERTY_MUTATORS);
    try {
        p = mapper.readValue(JSON, Point.class);
        fail("Should not succeeed");
    } catch (JsonMappingException e) {
        verifyException(e, "unrecognized field \"x\"");
    }
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 82 with JsonMappingException

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonMappingException in project hadoop by apache.

the class JobSubmitter method readTokensFromFiles.

@SuppressWarnings("unchecked")
private void readTokensFromFiles(Configuration conf, Credentials credentials) throws IOException {
    // add tokens and secrets coming from a token storage file
    String binaryTokenFilename = conf.get(MRJobConfig.MAPREDUCE_JOB_CREDENTIALS_BINARY);
    if (binaryTokenFilename != null) {
        Credentials binary = Credentials.readTokenStorageFile(FileSystem.getLocal(conf).makeQualified(new Path(binaryTokenFilename)), conf);
        credentials.addAll(binary);
    }
    // add secret keys coming from a json file
    String tokensFileName = conf.get("mapreduce.job.credentials.json");
    if (tokensFileName != null) {
        LOG.info("loading user's secret keys from " + tokensFileName);
        String localFileName = new Path(tokensFileName).toUri().getPath();
        try {
            // read JSON
            Map<String, String> nm = READER.readValue(new File(localFileName));
            for (Map.Entry<String, String> ent : nm.entrySet()) {
                credentials.addSecretKey(new Text(ent.getKey()), ent.getValue().getBytes(Charsets.UTF_8));
            }
        } catch (JsonMappingException | JsonParseException e) {
            LOG.warn("couldn't parse Token Cache JSON file with user secret keys");
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Text(org.apache.hadoop.io.Text) JsonParseException(com.fasterxml.jackson.core.JsonParseException) File(java.io.File) Map(java.util.Map) Credentials(org.apache.hadoop.security.Credentials)

Example 83 with JsonMappingException

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonMappingException in project OpenAM by OpenRock.

the class ConditionTypesResource method jsonify.

/**
     * Transforms a subclass of {@link EntitlementCondition} in to a JsonSchema representation.
     * This schema is then combined with the Condition's name (taken as the resourceId) and all this is
     * compiled together into a new {@link JsonValue} object until "title" and "config" fields respectively.
     *
     * @param conditionClass The class whose schema to produce.
     * @param resourceId The ID of the resource to return
     * @return A JsonValue containing the schema of the EntitlementCondition
     */
private JsonValue jsonify(Class<? extends EntitlementCondition> conditionClass, String resourceId, boolean logical) {
    try {
        final JsonSchema schema = mapper.generateJsonSchema(conditionClass);
        //this will remove the 'name' attribute from those conditions which incorporate it unnecessarily
        final JsonNode node = schema.getSchemaNode().get("properties");
        if (node instanceof ObjectNode) {
            final ObjectNode alter = (ObjectNode) node;
            alter.remove("name");
        }
        return JsonValue.json(JsonValue.object(JsonValue.field(JSON_OBJ_TITLE, resourceId), JsonValue.field(JSON_OBJ_LOGICAL, logical), JsonValue.field(JSON_OBJ_CONFIG, schema)));
    } catch (JsonMappingException e) {
        if (debug.errorEnabled()) {
            debug.error("ConditionTypesResource :: JSONIFY - Error applying " + "jsonification to the Condition class representation.", e);
        }
        return null;
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonSchema(com.fasterxml.jackson.databind.jsonschema.JsonSchema) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 84 with JsonMappingException

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonMappingException in project camel by apache.

the class MultiSelectPicklistDeserializer method deserialize.

@Override
public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    // validate enum class
    if (enumClass == null) {
        throw new JsonMappingException(jp, "Unable to parse unknown pick-list type");
    }
    final String listValue = jp.getText();
    try {
        // parse the string of the form value1;value2;...
        final String[] value = listValue.split(";");
        final int length = value.length;
        final Object resultArray = Array.newInstance(enumClass, length);
        for (int i = 0; i < length; i++) {
            // use factory method to create object
            Array.set(resultArray, i, factoryMethod.invoke(null, value[i].trim()));
        }
        return resultArray;
    } catch (Exception e) {
        throw new JsonParseException(jp, "Exception reading multi-select pick list value", jp.getCurrentLocation());
    }
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Example 85 with JsonMappingException

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonMappingException 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

JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)185 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)93 IOException (java.io.IOException)80 JsonParseException (com.fasterxml.jackson.core.JsonParseException)57 Test (org.junit.Test)45 ATTest (org.jboss.eap.additional.testsuite.annotations.ATTest)33 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)24 ArrayList (java.util.ArrayList)16 Map (java.util.Map)16 JsonNode (com.fasterxml.jackson.databind.JsonNode)15 File (java.io.File)15 HashMap (java.util.HashMap)15 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)13 InputStream (java.io.InputStream)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)10 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 List (java.util.List)8 Writer (org.alfresco.rest.framework.jacksonextensions.JacksonHelper.Writer)7 Test (org.junit.jupiter.api.Test)6