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

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

Introduction

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

Prototype

@Deprecated
public JsonGenerator createJsonGenerator(File f, JsonEncoding enc) throws IOException 

Source Link

Document

Method for constructing JSON generator for writing JSON content to specified file, overwriting contents it might have (or creating it if such file does not yet exist).

Usage

From source file:nl.bneijt.javapjson.JavapJsonMojo.java

public void execute() throws MojoExecutionException {
    JsonFactory jsonFactory = new JsonFactory();
    if (!outputDirectory.exists()) {
        throw new MojoExecutionException(
                "No build output directory found. Was looking at \"" + outputDirectory + "\"");
    }//from  www. j a  v  a  2 s. co m

    String jsonDirectory = buildDirectory.getPath() + File.separator + "javap-json";
    File jsonDirectoryFile = new File(jsonDirectory);
    if (!jsonDirectoryFile.exists()) {
        if (!jsonDirectoryFile.mkdir()) {
            throw new MojoExecutionException(
                    "Could not create output directory \"" + jsonDirectoryFile.getPath() + "\"");
        }
        getLog().debug("Created output directory \"" + jsonDirectoryFile.getPath() + "\"");
    }

    for (File classFile : FileUtils.listFiles(outputDirectory, new SuffixFileFilter(CLASS_EXTENSION),
            TrueFileFilter.INSTANCE)) {
        String output = runJavap(classFile);
        JavapLOutput parseL = JavapParser.parseL(output);

        File outputFile = new File(jsonDirectory + File.separator + "current.json");

        try {
            JsonGenerator jsonOutput = jsonFactory.createJsonGenerator(outputFile, JsonEncoding.UTF8);
            parseL.toJsonOnto(jsonOutput);

            //Move file into correct position
            File classDirectory = classFile.getParentFile();
            String classFileName = classFile.getName();
            String restOfDirectory = classDirectory.getPath().substring(outputDirectory.getPath().length());
            File jsonOutputFileDirectory = new File(jsonDirectory + restOfDirectory);
            File jsonOutputFile = new File(jsonOutputFileDirectory.getPath() + File.separator
                    + classFileName.substring(0, classFileName.length() - ".class".length()) + ".json");
            if (!jsonOutputFileDirectory.exists())
                FileUtils.forceMkdir(jsonOutputFileDirectory);
            if (jsonOutputFile.exists()) {
                FileUtils.deleteQuietly(jsonOutputFile);
            }
            FileUtils.moveFile(outputFile, jsonOutputFile);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            throw new MojoExecutionException("Unable to serialize javap output to Json", e);
        }
    }

}

From source file:com.rhfung.P2PDictionary.DataConnection.java

private byte[] GetEntryMetadataAsJson(DataEntry entry) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    JsonFactory jsonFactory = new JsonFactory(); // or, for data binding, org.codehaus.jackson.mapper.MappingJsonFactory 
    JsonGenerator jg;/*  w ww . ja va  2 s.c  o  m*/
    try {
        jg = jsonFactory.createJsonGenerator(stream, JsonEncoding.UTF8);
        WriteJSONForEntry(jg, entry);
        jg.flush();
    } catch (JsonGenerationException ex) {
        return new byte[0];

    } catch (IOException e) {
        // TODO Auto-generated catch block
        return new byte[0];
    } // or Stream, Reader

    return stream.toByteArray();
}

From source file:com.rhfung.P2PDictionary.DataConnection.java

private byte[] GetDictionaryAsJson() {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    JsonFactory jsonFactory = new JsonFactory(); // or, for data binding, org.codehaus.jackson.mapper.MappingJsonFactory 
    JsonGenerator jg;/*from  w ww . j  av a 2s.  c  o  m*/
    try {
        jg = jsonFactory.createJsonGenerator(stream, JsonEncoding.UTF8);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        return new byte[0];
    } // or Stream, Reader

    List<DataEntry> entries; // make a local copy for access without worry about changes thereafter
    this.dataLock.readLock().lock();
    try {
        entries = new Vector<DataEntry>(this.data.size());
        //entries.AddRange(this.data.Values.Where(x => x.subscribed));
        entries.addAll(this.data.values());
    } finally {
        this.dataLock.readLock().unlock();
    }

    // write count of data entries
    //writer.write(DATA_NAMESPACE + "/\t" + this.local_uid + "\t0\tRW\t" + entries.size() + "\t" + this.local_uid + NEWLINE) ;
    try {
        jg.writeStartObject();
        jg.writeObjectField("size", entries.size());
        jg.writeObjectField("localid", this.local_uid);

        jg.writeArrayFieldStart("keys");

        // write each data entry, converting simple data immediately
        // (pretend i don't know about these non-subscribed entries)
        for (DataEntry d : entries) {
            WriteJSONForEntry(jg, d);
        }

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

        jg.close();
    } catch (JsonGenerationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return stream.toByteArray();
}

From source file:org.jbpm.designer.bpmn2.impl.Bpmn2JsonMarshaller.java

public String marshall(Definitions def, String preProcessingData) throws IOException {
    DroolsPackageImpl.init();//from   www . jav a2  s.c o  m
    BpsimPackageImpl.init();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JsonFactory f = new JsonFactory();
    JsonGenerator generator = f.createJsonGenerator(baos, JsonEncoding.UTF8);
    if (def.getRelationships() != null && def.getRelationships().size() > 0) {
        // current support for single relationship
        Relationship relationship = def.getRelationships().get(0);
        for (ExtensionAttributeValue extattrval : relationship.getExtensionValues()) {
            FeatureMap extensionElements = extattrval.getValue();
            @SuppressWarnings("unchecked")
            List<BPSimDataType> bpsimExtensions = (List<BPSimDataType>) extensionElements
                    .get(BpsimPackage.Literals.DOCUMENT_ROOT__BP_SIM_DATA, true);
            if (bpsimExtensions != null && bpsimExtensions.size() > 0) {
                BPSimDataType processAnalysis = bpsimExtensions.get(0);
                if (processAnalysis.getScenario() != null && processAnalysis.getScenario().size() > 0) {
                    _simulationScenario = processAnalysis.getScenario().get(0);
                }
            }
        }
    }
    if (preProcessingData == null || preProcessingData.length() < 1) {
        preProcessingData = "ReadOnlyService";
    }

    // this is a temp way to determine if
    // coordinate system changes are necessary
    String bpmn2Exporter = def.getExporter();
    String bpmn2ExporterVersion = def.getExporterVersion();
    boolean haveExporter = bpmn2Exporter != null && bpmn2ExporterVersion != null;
    if (_simulationScenario != null && !haveExporter) {
        coordianteManipulation = false;
    }

    marshallDefinitions(def, generator, preProcessingData);
    generator.close();

    return baos.toString("UTF-8");
}