Java Code Examples for com.fasterxml.jackson.core.JsonGenerator#setCodec()

The following examples show how to use com.fasterxml.jackson.core.JsonGenerator#setCodec() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UCartographer.java    From ure with MIT License 6 votes vote down vote up
/**
 * Persist an object to disk.  This will most likely be an area or region, but in theory you could
 * write anything that serializes properly.
 * @param object
 * @param filename
 */
public void persist(Object object, String filename) {
    String path = commander.savePath();
    if (commander.config.isPersistentAreas()) {
        File dir = new File(path);
        dir.mkdirs();
        log.info("saving file " + path + filename);
        File file = new File(path + filename);
        try (
                FileOutputStream stream = new FileOutputStream(file);
                GZIPOutputStream gzip = new GZIPOutputStream(stream)
        ) {
            JsonFactory jfactory = new JsonFactory();
            JsonGenerator jGenerator = jfactory
                    .createGenerator(gzip, JsonEncoding.UTF8);
            jGenerator.setCodec(objectMapper);
            jGenerator.writeObject(object);
            jGenerator.close();
        } catch (IOException e) {
            throw new RuntimeException("Couldn't persist object " + object.toString(), e);
        }
    }
}
 
Example 2
Source File: UVaultSet.java    From ure with MIT License 6 votes vote down vote up
public void persist(String absoluteFilepath) {
    File file = new File(absoluteFilepath);
    try (
            FileOutputStream stream = new FileOutputStream(file);
            //GZIPOutputStream gzip = new GZIPOutputStream(stream)
    ) {
        JsonFactory jfactory = new JsonFactory();
        JsonGenerator jGenerator = jfactory
                .createGenerator(stream, JsonEncoding.UTF8);
        jGenerator.setCodec(new ObjectMapper());
        jGenerator.writeObject(this);
        jGenerator.close();
    } catch (IOException e) {
        throw new RuntimeException("Couldn't persist object " + toString(), e);
    }
}
 
Example 3
Source File: UCommander.java    From ure with MIT License 6 votes vote down vote up
public void persistPlayer() {
    if (player.area().getLabel().equals("vaulted"))
        return;
    log.debug("Persisting player " + player.getName() + "...");
    player.saveStateData();
    String path = savePath();
    File file = new File(path + "player");
    try (
            FileOutputStream stream = new FileOutputStream(file);
            //GZIPOutputStream gzip = new GZIPOutputStream(stream)
    ) {
        JsonFactory jfactory = new JsonFactory();
        JsonGenerator jGenerator = jfactory.createGenerator(stream, JsonEncoding.UTF8);
        jGenerator.setCodec(objectMapper);
        jGenerator.writeObject(player);
        jGenerator.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 4
Source File: LandedModal.java    From ure with MIT License 6 votes vote down vote up
void saveScaper(ULandscaper scaper, String filename) {
    String path = commander.savePath();
    File file = new File(path + filename);
    try (
            FileOutputStream stream = new FileOutputStream(file);
    ) {
        JsonFactory jfactory = new JsonFactory();
        JsonGenerator jGenerator = jfactory
                .createGenerator(stream, JsonEncoding.UTF8);
        jGenerator.setCodec(objectMapper);
        jGenerator.writeObject(scaper);
        jGenerator.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 5
Source File: PartitionStrategyParser.java    From kite with Apache License 2.0 6 votes vote down vote up
public static String toString(PartitionStrategy strategy, boolean pretty) {
  StringWriter writer = new StringWriter();
  JsonGenerator gen;
  try {
    gen = new JsonFactory().createGenerator(writer);
    if (pretty) {
      gen.useDefaultPrettyPrinter();
    }
    gen.setCodec(new ObjectMapper());
    gen.writeTree(toJson(strategy));
    gen.close();
  } catch (IOException e) {
    throw new DatasetIOException("Cannot write to JSON generator", e);
  }
  return writer.toString();
}
 
Example 6
Source File: ColumnMappingParser.java    From kite with Apache License 2.0 6 votes vote down vote up
public static String toString(ColumnMapping mapping, boolean pretty) {
  StringWriter writer = new StringWriter();
  JsonGenerator gen;
  try {
    gen = new JsonFactory().createGenerator(writer);
    if (pretty) {
      gen.useDefaultPrettyPrinter();
    }
    gen.setCodec(new ObjectMapper());
    gen.writeTree(toJson(mapping));
    gen.close();
  } catch (IOException e) {
    throw new DatasetIOException("Cannot write to JSON generator", e);
  }
  return writer.toString();
}
 
Example 7
Source File: GlyphedModal.java    From ure with MIT License 5 votes vote down vote up
void writeJson(ArrayList<Icon> icons, String filename) {
    log.info("writing " + filename);
    File file = new File(commander.config.getResourcePath() + "icons/" + filename);
    try (FileOutputStream stream = new FileOutputStream(file);) {
        JsonFactory jfactory = new JsonFactory();
        JsonGenerator jGenerator = jfactory.createGenerator(stream, JsonEncoding.UTF8);
        jGenerator.setCodec(objectMapper);
        jGenerator.writeObject(icons);
        jGenerator.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 8
Source File: SecuritySchemeSerializerTest.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
@Test
void givenSecurityScheme_whenItIsSerializingToJson_thenEnumTypesShouldPrintedLowercase() throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();

    Writer jsonWriter = new StringWriter();
    JsonGenerator jsonGenerator = new JsonFactory().createGenerator(jsonWriter);
    jsonGenerator.setCodec(objectMapper);

    SerializerProvider provider = objectMapper.getSerializerProvider();

    new SecuritySchemeSerializer().serialize(getDummyScheme(), jsonGenerator, provider);
    jsonGenerator.flush();

    assertEquals("{\"type\":\"http\",\"description\":\"desc\",\"name\":\"name\",\"$ref\":\"#/components/securitySchemes/ref\",\"in\":\"cookie\",\"scheme\":\"scheme\",\"bearerFormat\":\"format\",\"flows\":{},\"openIdConnectUrl\":\"url\",\"extensions\":{}}", jsonWriter.toString());
}
 
Example 9
Source File: JavaxServletPartSerializer.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(Part value, JsonGenerator gen, SerializerProvider provider) throws IOException {
  ObjectCodec preservedCodec = ((TokenBuffer) gen).asParser().getCodec();
  // set codec as null to avoid recursive dead loop
  // JsonGenerator is instantiated for each serialization, so there should be no thread safe issue
  gen.setCodec(null);
  gen.writeObject(value);
  gen.setCodec(preservedCodec);
}
 
Example 10
Source File: GeneralUtils.java    From template-compiler with Apache License 2.0 5 votes vote down vote up
/**
 * Formats the {@code node} as a string using the pretty printer.
 */
public static String jsonPretty(JsonNode node) throws IOException {
  StringBuilder buf = new StringBuilder();
  JsonGenerator gen = JSON_FACTORY.createGenerator(new StringBuilderWriter(buf));
  gen.useDefaultPrettyPrinter();
  gen.setCodec(JsonUtils.getMapper());
  gen.writeTree(node);
  return buf.toString();
}
 
Example 11
Source File: JsonEntitySerializer.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(final Object entity, final OutputStream out) throws IOException {
    final JsonFactory factory = new JsonFactory();
    final JsonGenerator generator = factory.createGenerator(out);
    generator.setCodec(jsonCodec);
    generator.writeObject(entity);
    generator.flush();
}
 
Example 12
Source File: ExportServiceImpl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
protected JsonGenerator getJsonGenerator( File ephermal ) throws IOException {
    //TODO:shouldn't the below be UTF-16?

    JsonGenerator jg = jsonFactory.createJsonGenerator( ephermal, JsonEncoding.UTF8 );
    jg.setPrettyPrinter( new DefaultPrettyPrinter(  ) );
    jg.setCodec( new ObjectMapper() );
    return jg;
}
 
Example 13
Source File: ExportingToolBase.java    From usergrid with Apache License 2.0 5 votes vote down vote up
protected JsonGenerator getJsonGenerator( File outFile ) throws IOException {
    PrintWriter out = new PrintWriter( outFile, "UTF-8" );
    JsonGenerator jg = jsonFactory.createJsonGenerator( out );
    jg.setPrettyPrinter( new DefaultPrettyPrinter() );
    jg.setCodec( new ObjectMapper() );
    return jg;
}
 
Example 14
Source File: ColumnMappingParser.java    From kite with Apache License 2.0 5 votes vote down vote up
public static String toString(FieldMapping mapping) {
  StringWriter writer = new StringWriter();
  JsonGenerator gen;
  try {
    gen = new JsonFactory().createGenerator(writer);
    gen.setCodec(new ObjectMapper());
    gen.writeTree(toJson(mapping));
    gen.close();
  } catch (IOException e) {
    throw new DatasetIOException("Cannot write to JSON generator", e);
  }
  return writer.toString();
}
 
Example 15
Source File: CommonAPI.java    From data-prep with Apache License 2.0 4 votes vote down vote up
/**
 * Describe the supported error codes.
 *
 * @param output the http response.
 */
@RequestMapping(value = "/api/errors", method = RequestMethod.GET, produces = APPLICATION_JSON_VALUE)
@ApiOperation(value = "Get all supported errors.", notes = "Returns the list of all supported errors.")
@Timed
public void listErrors(final OutputStream output) throws IOException {

    LOG.debug("Listing supported error codes");

    JsonFactory factory = new JsonFactory();
    JsonGenerator generator = factory.createGenerator(output);
    generator.setCodec(mapper);

    // start the errors array
    generator.writeStartArray();

    // write the direct known errors
    writeErrorsFromEnum(generator, CommonErrorCodes.values());
    writeErrorsFromEnum(generator, APIErrorCodes.values());

    // get dataset api errors
    HystrixCommand<InputStream> datasetErrors = getCommand(ErrorList.class, GenericCommand.DATASET_GROUP, DATASET);
    try (InputStream errorsInput = datasetErrors.execute()) {
        writeErrorsFromApi(generator, errorsInput);
    }

    // get preparation api errors
    HystrixCommand<InputStream> preparationErrors =
            getCommand(ErrorList.class, GenericCommand.PREPARATION_GROUP, PREPARATION);
    try (InputStream errorsInput = preparationErrors.execute()) {
        writeErrorsFromApi(generator, errorsInput);
    }

    // get transformation api errors
    HystrixCommand<InputStream> transformationErrors =
            getCommand(ErrorList.class, GenericCommand.TRANSFORM_GROUP, TRANSFORMATION);
    try (InputStream errorsInput = transformationErrors.execute()) {
        writeErrorsFromApi(generator, errorsInput);
    }

    // close the errors array
    generator.writeEndArray();
    generator.flush();
}