All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pro_crafting.tools.jsonpretty.JsonPrettyResource Maven / Gradle / Ivy

package com.pro_crafting.tools.jsonpretty;

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.StreamingOutput;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * No Rest Api. This is just supposed as output medium for the prettifyed json.
 */
@ApplicationScoped
@Path("jsonpretty")
public class JsonPrettyResource {

    @Inject
    ObjectMapper mapper;

    @POST
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.TEXT_HTML)
    public StreamingOutput prettify(InputStream json) {
        // The form is transmitted as text/plain enctype
        // in the form entryname=jsonstring
        // We have to filter out anything before the =,
        // so that jackson can read just the json string

        // Read the first 5 bytes
        // containing json=
        try {
            json.read(new byte[5], 0, 5);
        } catch (IOException ex) {
            return output -> output.write(asBytes("Unable to read json"));
        }

        JsonParser parser;
        try {
            parser = mapper.getFactory().createParser(json);
        } catch (IOException e) {
            return output -> output.write(asBytes("Invalid JSON, Reason: " + e.getMessage()));
        }

        return output -> {
            output.write(asBytes("
"));

            try (JsonGenerator generator = mapper.getFactory().createGenerator(output, JsonEncoding.UTF8))
            {
                generator.setPrettyPrinter(new DefaultPrettyPrinter());
                while (parser.nextToken() != null) {
                    generator.copyCurrentEvent(parser);
                }
            } catch (Exception e) {
                output.write(asBytes("Invalid JSON, Reason: " + e.getMessage()));
            }

            parser.close();

            output.write(asBytes("
")); }; } private byte[] asBytes(String value) { return value.getBytes(StandardCharsets.UTF_8); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy