Search in sources :

Example 36 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project keywhiz by square.

the class CliModule method generalMapper.

@Provides
public ObjectMapper generalMapper() {
    /**
     * Customizes ObjectMapper for common settings.
     *
     * @param objectMapper to be customized
     * @return customized input factory
     */
    ObjectMapper objectMapper = Jackson.newObjectMapper();
    objectMapper.registerModule(new Jdk8Module());
    objectMapper.registerModules(new JavaTimeModule());
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    return objectMapper;
}
Also used : Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Provides(com.google.inject.Provides)

Example 37 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project android-async-http by loopj.

the class CustomCASample method getResponseHandler.

@Override
public ResponseHandlerInterface getResponseHandler() {
    return new BaseJsonHttpResponseHandler<SampleJSON>() {

        @Override
        public void onStart() {
            clearOutputs();
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, SampleJSON response) {
            debugHeaders(LOG_TAG, headers);
            debugStatusCode(LOG_TAG, statusCode);
            if (response != null) {
                debugResponse(LOG_TAG, rawJsonResponse);
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, SampleJSON errorResponse) {
            debugHeaders(LOG_TAG, headers);
            debugStatusCode(LOG_TAG, statusCode);
            debugThrowable(LOG_TAG, throwable);
            if (errorResponse != null) {
                debugResponse(LOG_TAG, rawJsonData);
            }
        }

        @Override
        protected SampleJSON parseResponse(String rawJsonData, boolean isFailure) throws Throwable {
            return new ObjectMapper().readValues(new JsonFactory().createParser(rawJsonData), SampleJSON.class).next();
        }
    };
}
Also used : SampleJSON(com.loopj.android.http.sample.util.SampleJSON) JsonFactory(com.fasterxml.jackson.core.JsonFactory) BaseJsonHttpResponseHandler(com.loopj.android.http.BaseJsonHttpResponseHandler) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 38 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project camel by apache.

the class ConnectorMojo method updateVersionInCamelConnectorJSon.

private void updateVersionInCamelConnectorJSon(String qualifier, String version) throws MojoExecutionException {
    File file = new File(classesDirectory, "camel-connector.json");
    if (file.exists()) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            Map dto = mapper.readValue(file, Map.class);
            if (version != null) {
                String existingVersion = (String) dto.get(qualifier);
                if (existingVersion == null || !existingVersion.equals(version)) {
                    dto.put(qualifier, version);
                    // update file
                    mapper.writerWithDefaultPrettyPrinter().writeValue(file, dto);
                    // project root folder
                    File root = classesDirectory.getParentFile().getParentFile();
                    // update source file also
                    file = new File(root, "src/main/resources/camel-connector.json");
                    if (file.exists()) {
                        getLog().info("Updating " + qualifier + " to " + version + " in " + file);
                        mapper.writerWithDefaultPrettyPrinter().writeValue(file, dto);
                    }
                }
            }
        } catch (Exception e) {
            throw new MojoExecutionException("Error in camel-connector-maven-plugin", e);
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) File(java.io.File) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Example 39 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project camel by apache.

the class ConnectorMojo method createArchive.

@Override
public File createArchive() throws MojoExecutionException {
    // project root folder
    File root = classesDirectory.getParentFile().getParentFile();
    String gitUrl = null;
    // find the component dependency and get its .json file
    File file = new File(classesDirectory, "camel-connector.json");
    if (file.exists()) {
        // updating to use correct project version in camel-connector.json
        String version = getProject().getVersion();
        updateVersionInCamelConnectorJSon("version", version);
        try {
            ObjectMapper mapper = new ObjectMapper();
            Map dto = mapper.readValue(file, Map.class);
            if (includeGitUrl) {
                gitUrl = embedGitUrlInCamelConnectorJSon(mapper, dto);
            }
            File schema = embedCamelComponentSchema(file);
            if (schema != null) {
                String json = FileHelper.loadText(new FileInputStream(schema));
                List<Map<String, String>> rows = JSonSchemaHelper.parseJsonSchema("component", json, false);
                String header = buildComponentHeaderSchema(rows, dto, gitUrl);
                getLog().debug(header);
                rows = JSonSchemaHelper.parseJsonSchema("componentProperties", json, true);
                String componentOptions = buildComponentOptionsSchema(rows, dto);
                getLog().debug(componentOptions);
                rows = JSonSchemaHelper.parseJsonSchema("properties", json, true);
                String endpointOptions = buildEndpointOptionsSchema(rows, dto);
                getLog().debug(endpointOptions);
                // generate the json file
                StringBuilder jsonSchema = new StringBuilder();
                jsonSchema.append("{\n");
                jsonSchema.append(header);
                jsonSchema.append(componentOptions);
                jsonSchema.append(endpointOptions);
                jsonSchema.append("}\n");
                String newJson = jsonSchema.toString();
                // parse ourselves
                rows = JSonSchemaHelper.parseJsonSchema("component", newJson, false);
                String newScheme = getOption(rows, "scheme");
                // write the json file to the target directory as if camel apt would do it
                String javaType = (String) dto.get("javaType");
                String dir = javaType.substring(0, javaType.lastIndexOf("."));
                dir = dir.replace('.', '/');
                File subDir = new File(classesDirectory, dir);
                String name = newScheme + ".json";
                File out = new File(subDir, name);
                FileOutputStream fos = new FileOutputStream(out, false);
                fos.write(newJson.getBytes());
                fos.close();
                // also write the file in the root folder so its easier to find that for tooling
                out = new File(classesDirectory, "camel-connector-schema.json");
                fos = new FileOutputStream(out, false);
                fos.write(newJson.getBytes());
                fos.close();
                if (generateToSources) {
                    // copy the file into the sources as well
                    File from = new File(classesDirectory, "camel-connector-schema.json");
                    File to = new File(root, "src/main/resources/camel-connector-schema.json");
                    FileHelper.copyFile(from, to);
                }
            }
        // build json schema for component that only has the selectable options
        } catch (Exception e) {
            throw new MojoExecutionException("Error in camel-connector-maven-plugin", e);
        }
    }
    return super.createArchive();
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Example 40 with ObjectMapper

use of com.fasterxml.jackson.databind.ObjectMapper in project camel by apache.

the class ZooKeeperGroup method close.

/**
     * Close/end the cache
     *
     * @throws IOException errors
     */
@Override
public void close() throws IOException {
    LOG.debug(this + ".close, connected:" + connected);
    if (started.compareAndSet(true, false)) {
        client.getConnectionStateListenable().removeListener(connectionStateListener);
        executorService.shutdownNow();
        try {
            executorService.awaitTermination(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            throw (IOException) new InterruptedIOException().initCause(e);
        }
        try {
            doUpdate(null);
            if (isConnected()) {
                callListeners(GroupListener.GroupEvent.DISCONNECTED);
            }
        } catch (Exception e) {
            handleException(e);
        }
        listeners.clear();
        mapper.getTypeFactory().clearCache();
        mapper = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        client.clearWatcherReferences(childrenWatcher);
        client.clearWatcherReferences(dataWatcher);
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) InterruptedIOException(java.io.InterruptedIOException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1706 Test (org.junit.Test)641 JsonNode (com.fasterxml.jackson.databind.JsonNode)270 IOException (java.io.IOException)238 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)183 DefaultObjectMapper (io.druid.jackson.DefaultObjectMapper)127 Map (java.util.Map)124 HashMap (java.util.HashMap)119 ArrayList (java.util.ArrayList)81 File (java.io.File)72 InputStream (java.io.InputStream)65 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)55 JCodeModel (com.sun.codemodel.JCodeModel)52 List (java.util.List)49 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)47 JPackage (com.sun.codemodel.JPackage)44 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)41 JsonException (jmri.server.json.JsonException)41 JType (com.sun.codemodel.JType)38 Test (org.testng.annotations.Test)37