Example usage for com.fasterxml.jackson.core JsonFactory createGenerator

List of usage examples for com.fasterxml.jackson.core JsonFactory createGenerator

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonFactory createGenerator.

Prototype

public JsonGenerator createGenerator(Writer out) throws IOException 

Source Link

Document

Method for constructing JSON generator for writing JSON content using specified Writer.

Usage

From source file:de.alexkamp.sandbox.ChrootSandbox.java

public ChrootSandbox(JsonFactory factory, String host, int port, SandboxData data) {
    this.factory = new JsonFactory();

    this.data = data;

    try {//from ww  w. j a  v a2 s  .c o m
        socket = new Socket(host, port);
        this.sender = factory.createGenerator(socket.getOutputStream());
    } catch (IOException ex) {
        throw new SandboxException(ex);
    }

    this.reader = new Thread(this);
    this.reader.start();
    try {
        connect();
    } catch (IOException ex) {
        throw new SandboxException(ex);
    }
}

From source file:org.eclipse.winery.repository.resources.entitytypes.relationshiptypes.VisualAppearanceResource.java

@GET
@RestDoc(methodDescription = "@return JSON object to be used at jsPlumb.registerConnectionType('NAME', <data>)")
@Produces(MediaType.APPLICATION_JSON)/*  w ww. j  a v  a 2  s. c  o  m*/
public Response getConnectionTypeForJsPlumbData() {
    JsonFactory jsonFactory = new JsonFactory();
    StringWriter sw = new StringWriter();
    try {
        JsonGenerator jg = jsonFactory.createGenerator(sw);
        jg.writeStartObject();

        jg.writeFieldName("connector");
        jg.writeString("Flowchart");

        jg.writeFieldName("paintStyle");
        jg.writeStartObject();
        jg.writeFieldName("lineWidth");
        jg.writeNumber(this.getLineWidth());
        jg.writeFieldName("strokeStyle");
        jg.writeObject(this.getColor());
        String dash = this.getDash();
        if (!StringUtils.isEmpty(dash)) {
            String dashStyle = null;
            switch (dash) {
            case "dotted":
                dashStyle = "1 5";
                break;
            case "dotted2":
                dashStyle = "3 4";
                break;
            case "plain":
                // default works
                // otherwise, "1 0" can be used
                break;
            }
            if (dashStyle != null) {
                jg.writeStringField("dashstyle", dashStyle);
            }
        }
        jg.writeEndObject();

        jg.writeFieldName("hoverPaintStyle");
        jg.writeStartObject();
        jg.writeFieldName("strokeStyle");
        jg.writeObject(this.getHoverColor());
        jg.writeEndObject();

        // BEGIN: Overlays

        jg.writeFieldName("overlays");
        jg.writeStartArray();

        // source arrow head
        String head = this.getSourceArrowHead();
        if (!head.equals("none")) {
            jg.writeStartArray();
            jg.writeString(head);

            jg.writeStartObject();

            jg.writeFieldName("location");
            jg.writeNumber(0);

            // arrow should point towards the node and not away from it
            jg.writeFieldName("direction");
            jg.writeNumber(-1);

            jg.writeFieldName("width");
            jg.writeNumber(20);

            jg.writeFieldName("length");
            jg.writeNumber(12);

            jg.writeEndObject();
            jg.writeEndArray();
        }

        // target arrow head
        head = this.getTargetArrowHead();
        if (!head.equals("none")) {
            jg.writeStartArray();
            jg.writeString(head);
            jg.writeStartObject();
            jg.writeFieldName("location");
            jg.writeNumber(1);
            jg.writeFieldName("width");
            jg.writeNumber(20);
            jg.writeFieldName("length");
            jg.writeNumber(12);
            jg.writeEndObject();
            jg.writeEndArray();
        }

        // Type in brackets on the arrow
        jg.writeStartArray();
        jg.writeString("Label");
        jg.writeStartObject();
        jg.writeStringField("id", "label");
        //jg.writeStringField("label", "(" + ((RelationshipTypeResource) this.res).getName() + ")");
        jg.writeStringField("label", "");
        jg.writeStringField("cssClass", "relationshipTypeLabel");
        jg.writeFieldName("location");
        jg.writeNumber(0.5);
        jg.writeEndObject();
        jg.writeEndArray();

        jg.writeEndArray();

        // END: Overlays

        jg.writeEndObject();

        jg.close();
    } catch (Exception e) {
        VisualAppearanceResource.logger.error(e.getMessage(), e);
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e).build();
    }
    String res = sw.toString();
    return Response.ok(res).build();
}

From source file:TDS.Shared.Messages.MessageJson.java

public String create(ContextType contextType, List<String> contexts) throws ReturnStatusException {
    MessageContextType messageContextType = null;
    StringWriter sw = new StringWriter();
    JsonFactory jsonFactory = new JsonFactory();
    JsonGenerator jsonWriter;//from  ww w.  ja  va 2 s  . co m

    try {
        if (_messageSystem != null) {
            messageContextType = _messageSystem.getMessageContextType(contextType);
        }

        if (messageContextType == null)
            return "{}";

        jsonWriter = jsonFactory.createGenerator(sw);
        jsonWriter.writeStartObject();
        jsonWriter.writeStringField("c_l", _language); // "c_l": _language
        jsonWriter.writeFieldName("c_a"); // "c_a" :
        jsonWriter.writeStartArray(); // [

        for (String context : contexts) {
            MessageContext messageContext = messageContextType.getContext(context);
            writeContextElement(messageContext, jsonWriter);
        }

        jsonWriter.writeEndArray(); // ]
        jsonWriter.writeEndObject(); // }

        jsonWriter.close();
        sw.close();
    } catch (IOException e) {
        ReturnStatus rs = new ReturnStatus("failed", "Serialization failed: " + e.getMessage());
        throw new ReturnStatusException(rs);
    }

    return sw.getBuffer().toString();
}

From source file:msearch.filmlisten.MSFilmlisteSchreiben.java

public void filmlisteSchreibenJson(String datei, ListeFilme listeFilme) {
    MSLog.systemMeldung("Filme schreiben (" + listeFilme.size() + " Filme) :");
    File file = new File(datei);
    File dir = new File(file.getParent());
    if (!dir.exists()) {
        if (!dir.mkdirs()) {
            MSLog.fehlerMeldung(915236478, MSLog.FEHLER_ART_PROG,
                    "MSearchIoXmlFilmlisteSchreiben.xmlSchreibenStart",
                    "Kann den Pfad nicht anlegen: " + dir.toString());
        }/*from  w  w  w  .j a va2 s  .com*/
    }
    MSLog.systemMeldung("   --> Start Schreiben nach: " + datei);
    try {
        String sender = "", thema = "";
        JsonFactory jsonF = new JsonFactory();
        JsonGenerator jg;
        if (datei.endsWith(MSConst.FORMAT_XZ)) {
            LZMA2Options options = new LZMA2Options();
            XZOutputStream out = new XZOutputStream(new FileOutputStream(file), options);
            jg = jsonF.createGenerator(out);
        } else if (datei.endsWith(MSConst.FORMAT_BZ2)) {
            bZip2CompressorOutputStream = new BZip2CompressorOutputStream(new FileOutputStream(file),
                    9 /*Blocksize: 1 - 9*/);
            jg = jsonF.createGenerator(bZip2CompressorOutputStream, JsonEncoding.UTF8);
        } else if (datei.endsWith(MSConst.FORMAT_ZIP)) {
            zipOutputStream = new ZipOutputStream(new FileOutputStream(file));
            ZipEntry entry = new ZipEntry(MSConst.XML_DATEI_FILME);
            zipOutputStream.putNextEntry(entry);
            jg = jsonF.createGenerator(zipOutputStream, JsonEncoding.UTF8);
        } else {
            jg = jsonF.createGenerator(new File(datei), JsonEncoding.UTF8);
        }
        jg.useDefaultPrettyPrinter(); // enable indentation just to make debug/testing easier
        jg.writeStartObject();
        // Infos zur Filmliste
        jg.writeArrayFieldStart(ListeFilme.FILMLISTE);
        for (int i = 0; i < ListeFilme.MAX_ELEM; ++i) {
            jg.writeString(listeFilme.metaDaten[i]);
        }
        jg.writeEndArray();
        // Infos der Felder in der Filmliste
        jg.writeArrayFieldStart(ListeFilme.FILMLISTE);
        for (int i = 0; i < DatenFilm.COLUMN_NAMES_JSON.length; ++i) {
            jg.writeString(DatenFilm.COLUMN_NAMES[DatenFilm.COLUMN_NAMES_JSON[i]]);
        }
        jg.writeEndArray();
        //Filme schreiben
        ListIterator<DatenFilm> iterator;
        DatenFilm datenFilm;
        iterator = listeFilme.listIterator();
        while (iterator.hasNext()) {
            datenFilm = iterator.next();
            jg.writeArrayFieldStart(DatenFilm.FILME_);
            for (int i = 0; i < DatenFilm.COLUMN_NAMES_JSON.length; ++i) {
                int m = DatenFilm.COLUMN_NAMES_JSON[i];
                if (m == DatenFilm.FILM_SENDER_NR) {
                    if (datenFilm.arr[m].equals(sender)) {
                        jg.writeString("");
                    } else {
                        sender = datenFilm.arr[m];
                        jg.writeString(datenFilm.arr[m]);
                    }
                } else if (m == DatenFilm.FILM_THEMA_NR) {
                    if (datenFilm.arr[m].equals(thema)) {
                        jg.writeString("");
                    } else {
                        thema = datenFilm.arr[m];
                        jg.writeString(datenFilm.arr[m]);
                    }
                } else {
                    jg.writeString(datenFilm.arr[m]);
                }
            }
            jg.writeEndArray();
        }
        jg.writeEndObject();
        jg.close();
        MSLog.systemMeldung("   --> geschrieben!");
    } catch (Exception ex) {
        MSLog.fehlerMeldung(846930145, MSLog.FEHLER_ART_PROG, "IoXmlSchreiben.FilmeSchreiben", ex,
                "nach: " + datei);
    }
}

From source file:msearch.io.MSFilmlisteSchreiben.java

public void filmlisteSchreibenJson(String datei, ListeFilme listeFilme) {
    MSLog.systemMeldung("Filme Schreiben (" + listeFilme.size() + " Filme) :");
    File file = new File(datei);
    File dir = new File(file.getParent());
    if (!dir.exists()) {
        if (!dir.mkdirs()) {
            MSLog.fehlerMeldung(915236478, MSLog.FEHLER_ART_PROG,
                    "MSearchIoXmlFilmlisteSchreiben.xmlSchreibenStart",
                    "Kann den Pfad nicht anlegen: " + dir.toString());
        }//from w w w.  j  a v a  2 s. c o  m
    }
    MSLog.systemMeldung("   --> Start Schreiben nach: " + datei);
    try {
        String sender = "", thema = "";
        JsonFactory jsonF = new JsonFactory();
        JsonGenerator jg;
        if (datei.endsWith(MSConst.FORMAT_XZ)) {
            LZMA2Options options = new LZMA2Options();
            XZOutputStream out = new XZOutputStream(new FileOutputStream(file), options);
            jg = jsonF.createGenerator(out);
        } else if (datei.endsWith(MSConst.FORMAT_BZ2)) {
            bZip2CompressorOutputStream = new BZip2CompressorOutputStream(new FileOutputStream(file),
                    9 /*Blocksize: 1 - 9*/);
            jg = jsonF.createGenerator(bZip2CompressorOutputStream, JsonEncoding.UTF8);
        } else if (datei.endsWith(MSConst.FORMAT_ZIP)) {
            zipOutputStream = new ZipOutputStream(new FileOutputStream(file));
            ZipEntry entry = new ZipEntry(MSConst.XML_DATEI_FILME);
            zipOutputStream.putNextEntry(entry);
            jg = jsonF.createGenerator(zipOutputStream, JsonEncoding.UTF8);
        } else {
            jg = jsonF.createGenerator(new File(datei), JsonEncoding.UTF8);
        }
        jg.useDefaultPrettyPrinter(); // enable indentation just to make debug/testing easier
        jg.writeStartObject();
        // Infos zur Filmliste
        jg.writeArrayFieldStart(ListeFilme.FILMLISTE);
        for (int i = 0; i < ListeFilme.MAX_ELEM; ++i) {
            jg.writeString(listeFilme.metaDaten[i]);
        }
        jg.writeEndArray();
        // Infos der Felder in der Filmliste
        jg.writeArrayFieldStart(ListeFilme.FILMLISTE);
        for (int i = 0; i < DatenFilm.COLUMN_NAMES_JSON.length; ++i) {
            jg.writeString(DatenFilm.COLUMN_NAMES[DatenFilm.COLUMN_NAMES_JSON[i]]);
        }
        jg.writeEndArray();
        //Filme schreiben
        ListIterator<DatenFilm> iterator;
        DatenFilm datenFilm;
        iterator = listeFilme.listIterator();
        while (iterator.hasNext()) {
            datenFilm = iterator.next();
            jg.writeArrayFieldStart(DatenFilm.FILME_);
            for (int i = 0; i < DatenFilm.COLUMN_NAMES_JSON.length; ++i) {
                int m = DatenFilm.COLUMN_NAMES_JSON[i];
                if (m == DatenFilm.FILM_SENDER_NR) {
                    if (datenFilm.arr[m].equals(sender)) {
                        jg.writeString("");
                    } else {
                        sender = datenFilm.arr[m];
                        jg.writeString(datenFilm.arr[m]);
                    }
                } else if (m == DatenFilm.FILM_THEMA_NR) {
                    if (datenFilm.arr[m].equals(thema)) {
                        jg.writeString("");
                    } else {
                        thema = datenFilm.arr[m];
                        jg.writeString(datenFilm.arr[m]);
                    }
                } else {
                    jg.writeString(datenFilm.arr[m]);
                }
            }
            jg.writeEndArray();
        }
        jg.writeEndObject();
        jg.close();
        MSLog.systemMeldung("   --> geschrieben!");
    } catch (Exception ex) {
        MSLog.fehlerMeldung(846930145, MSLog.FEHLER_ART_PROG, "IoXmlSchreiben.FilmeSchreiben", ex,
                "nach: " + datei);
    }
}

From source file:org.forgerock.openig.migrate.Main.java

void execute(OutputStream stream) throws Exception {

    // Pre-parse the original files with JSON-Simple parser (which is more lenient than Jackson's one)
    File source = new File(sources.get(0));
    JSONParser parser = new JSONParser();
    JSONObject object = (JSONObject) parser.parse(new FileReader(source));

    // Then serialize again the structure (should clean up the JSON)
    StringWriter writer = new StringWriter();
    object.writeJSONString(writer);//from   w  w  w .  j  ava  2  s .c  om

    // Load migration actions to apply in order
    List<Action> actions = loadActions();

    // Parse the cleaned-up content with Jackson this time
    JsonFactory factory = new JsonFactory();
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode node = (ObjectNode) mapper.readTree(writer.toString());

    // Apply migration actions
    for (Action action : actions) {
        node = action.migrate(node);
    }

    // Serialize the migrated content on the given stream (with pretty printer)
    DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter();
    prettyPrinter.indentArraysWith(DefaultPrettyPrinter.Lf2SpacesIndenter.instance);
    mapper.writeTree(factory.createGenerator(stream).setPrettyPrinter(prettyPrinter), node);
}

From source file:tds.student.web.controls.dummy.GlobalJavascriptWriter.java

public void writeCustomSettings() throws IOException {
    StringWriter sw = new StringWriter();
    JsonFactory jsonFactory = new JsonFactory();
    JsonGenerator jsonWriter = jsonFactory.createGenerator(sw);

    jsonWriter.writeStartObject();/*  w w w  .ja v a  2s .  co  m*/

    // TODO Shajib:
    // jsonWriter.writeBooleanField ("showExceptions",
    // DebugSettings.showClientExceptions());
    jsonWriter.writeBooleanField("ignoreForbiddenApps", DebugSettings.ignoreForbiddenApps());
    jsonWriter.writeBooleanField("ignoreBrowserChecks", DebugSettings.ignoreBrowserChecks());
    jsonWriter.writeEndObject();
    jsonWriter.close();
    _writer.write(String.format("TDS.Debug = %s; ", sw.toString()));
    _writer.write("\n\r");
}

From source file:tds.student.web.controls.dummy.GlobalJavascriptWriter.java

public void writeBrowserInfo() throws JsonGenerationException, IOException {
    StringWriter sw = new StringWriter();
    JsonFactory jsonFactory = new JsonFactory();
    JsonGenerator jsonWriter = jsonFactory.createGenerator(sw);

    jsonWriter.writeStartObject();// {

    // browser info
    BrowserParser browser = new BrowserParser();
    jsonWriter.writeStringField("userAgent", browser.getUserAgent());
    jsonWriter.writeStringField("osLabel", browser.getOSFullName());
    jsonWriter.writeStringField("osName", String.valueOf(browser.getOsName()));
    jsonWriter.writeNumberField("osVersion", browser.getVersion());
    jsonWriter.writeStringField("architecture", browser.getHardwareArchitecture());
    jsonWriter.writeStringField("name", browser.getName());
    jsonWriter.writeNumberField("version", browser.getVersion());
    jsonWriter.writeBooleanField("isSecure", browser.isSecureBrowser());

    String label;/*from  w  w  w. j  av a 2  s  . c  o m*/
    if (browser.isSecureBrowser())
        label = String.format("Secure v%s", "" + browser.getVersion());
    else
        label = String.format("%s v%s", browser.getName(), "" + browser.getVersion());
    jsonWriter.writeStringField("label", label);

    jsonWriter.writeEndObject(); // }
    jsonWriter.close();
    // TODO shiva: do we need to do sw.getBuffer () below.
    _writer.write(String.format("TDS.BrowserInfo = %s; ", sw.getBuffer().toString()));
    _writer.write("\n\r");
}