Search in sources :

Example 76 with ObjectWriter

use of com.fasterxml.jackson.databind.ObjectWriter in project grakn by graknlabs.

the class SystemController method getMetrics.

@GET
@Path("/metrics")
private String getMetrics(Request request, Response response) throws IOException {
    response.header(CACHE_CONTROL, "must-revalidate,no-cache,no-store");
    response.status(HttpServletResponse.SC_OK);
    Optional<String> format = Optional.ofNullable(request.queryParams(FORMAT));
    String dFormat = format.orElse(JSON);
    switch(dFormat) {
        case PROMETHEUS:
            // Prometheus format for the metrics
            response.type(PROMETHEUS_CONTENT_TYPE);
            final Writer writer1 = new StringWriter();
            TextFormat.write004(writer1, this.prometheusRegistry.metricFamilySamples());
            return writer1.toString();
        case JSON:
            // Json/Dropwizard format
            response.type(APPLICATION_JSON);
            final ObjectWriter writer = mapper.writer();
            try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
                writer.writeValue(output, this.metricRegistry);
                return new String(output.toByteArray(), "UTF-8");
            }
        default:
            throw GraknServerException.requestInvalidParameter(FORMAT, dFormat);
    }
}
Also used : StringWriter(java.io.StringWriter) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 77 with ObjectWriter

use of com.fasterxml.jackson.databind.ObjectWriter in project graphhopper by graphhopper.

the class GHBaseServlet method writeJson.

protected void writeJson(HttpServletRequest req, HttpServletResponse res, JsonNode json) throws IOException {
    String type = getParam(req, "type", "json");
    res.setCharacterEncoding("UTF-8");
    final boolean indent = getBooleanParam(req, "debug", false) || getBooleanParam(req, "pretty", false);
    ObjectWriter objectWriter = indent ? objectMapper.writer().with(SerializationFeature.INDENT_OUTPUT) : objectMapper.writer();
    if ("jsonp".equals(type)) {
        res.setContentType("application/javascript");
        if (!jsonpAllowed) {
            writeError(res, SC_BAD_REQUEST, "Server is not configured to allow jsonp!");
            return;
        }
        String callbackName = getParam(req, "callback", null);
        if (callbackName == null) {
            writeError(res, SC_BAD_REQUEST, "No callback provided, necessary if type=jsonp");
            return;
        }
        writeResponse(res, callbackName + "(" + objectWriter.writeValueAsString(json) + ")");
    } else {
        res.setContentType("application/json");
        writeResponse(res, objectWriter.writeValueAsString(json));
    }
}
Also used : ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter)

Example 78 with ObjectWriter

use of com.fasterxml.jackson.databind.ObjectWriter in project cxf by apache.

the class Java2SwaggerMojo method generateSwaggerPayLoad.

private void generateSwaggerPayLoad() throws MojoExecutionException {
    if (outputFile == null && project != null) {
        // Put the json in target/generated/json
        // put the yaml in target/generated/yaml
        String name = null;
        if (outputFileName != null) {
            name = outputFileName;
        } else if (resourceClasses.size() == 1) {
            name = resourceClasses.iterator().next().getSimpleName();
        } else {
            name = "swagger";
        }
        outputFile = (project.getBuild().getDirectory() + "/generated/" + payload.toLowerCase() + "/" + name + "." + payload.toLowerCase()).replace("/", File.separator);
    }
    BufferedWriter writer = null;
    try {
        FileUtils.mkDir(new File(outputFile).getParentFile());
        writer = new BufferedWriter(new FileWriter(outputFile));
        if ("json".equals(this.payload)) {
            ObjectWriter jsonWriter = mapper.writer(new DefaultPrettyPrinter());
            writer.write(jsonWriter.writeValueAsString(swagger));
        } else if ("yaml".equals(this.payload)) {
            writer.write(Yaml.pretty().writeValueAsString(swagger));
        }
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } finally {
        try {
            if (writer != null) {
                writer.close();
            }
        } catch (IOException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }
    // with the enclosing project
    if (attachSwagger && outputFile != null) {
        File jsonFile = new File(outputFile);
        if (jsonFile.exists()) {
            if (classifier != null) {
                projectHelper.attachArtifact(project, payload.toLowerCase(), classifier, jsonFile);
            } else {
                projectHelper.attachArtifact(project, payload.toLowerCase(), jsonFile);
            }
        }
    }
}
Also used : DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileWriter(java.io.FileWriter) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) IOException(java.io.IOException) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 79 with ObjectWriter

use of com.fasterxml.jackson.databind.ObjectWriter in project ORCID-Source by ORCID.

the class OpenIDController method getOpenIDDiscovery.

/**
 * Expose the openid discovery information
 *
 * @param request
 * @return
 * @throws JsonProcessingException
 */
@CrossOrigin
@RequestMapping(value = "/.well-known/openid-configuration", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public String getOpenIDDiscovery(HttpServletRequest request) throws JsonProcessingException {
    ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
    String json = ow.writeValueAsString(openIDConnectDiscoveryService.getConfig());
    return json;
}
Also used : ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) CrossOrigin(org.springframework.web.bind.annotation.CrossOrigin) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 80 with ObjectWriter

use of com.fasterxml.jackson.databind.ObjectWriter in project drill by axbaretto.

the class TestParsePhysicalPlan method parseSimplePlan.

@Test
public void parseSimplePlan() throws Exception {
    DrillConfig c = DrillConfig.create();
    ScanResult scanResult = ClassPathScanner.fromPrescan(c);
    LogicalPlanPersistence lpp = new LogicalPlanPersistence(c, scanResult);
    PhysicalPlanReader reader = new PhysicalPlanReader(c, scanResult, lpp, CoordinationProtos.DrillbitEndpoint.getDefaultInstance(), null);
    ObjectReader r = lpp.getMapper().reader(PhysicalPlan.class);
    ObjectWriter writer = lpp.getMapper().writer();
    PhysicalPlan plan = reader.readPhysicalPlan(Files.toString(DrillFileUtils.getResourceAsFile("/physical_test1.json"), Charsets.UTF_8));
    String unparse = plan.unparse(writer);
// System.out.println(unparse);
}
Also used : ScanResult(org.apache.drill.common.scanner.persistence.ScanResult) PhysicalPlan(org.apache.drill.exec.physical.PhysicalPlan) DrillConfig(org.apache.drill.common.config.DrillConfig) PhysicalPlanReader(org.apache.drill.exec.planner.PhysicalPlanReader) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) LogicalPlanPersistence(org.apache.drill.common.config.LogicalPlanPersistence) Test(org.junit.Test) ExecTest(org.apache.drill.exec.ExecTest) PlannerTest(org.apache.drill.categories.PlannerTest)

Aggregations

ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)124 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)53 IOException (java.io.IOException)28 Test (org.junit.Test)27 File (java.io.File)13 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)12 JavaType (com.fasterxml.jackson.databind.JavaType)10 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)10 ArrayList (java.util.ArrayList)10 FileOutputStream (java.io.FileOutputStream)7 Map (java.util.Map)7 JCommander (com.beust.jcommander.JCommander)6 ParameterException (com.beust.jcommander.ParameterException)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 RateLimiter (com.google.common.util.concurrent.RateLimiter)6 FileInputStream (java.io.FileInputStream)6 OutputStream (java.io.OutputStream)6 Writer (java.io.Writer)6 HashMap (java.util.HashMap)6 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)5