Java Code Examples for org.codehaus.jackson.JsonGenerator#useDefaultPrettyPrinter()

The following examples show how to use org.codehaus.jackson.JsonGenerator#useDefaultPrettyPrinter() . 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: StatePool.java    From hadoop with Apache License 2.0 6 votes vote down vote up
private void write(DataOutput out) throws IOException {
  // This is just a JSON experiment
  System.out.println("Dumping the StatePool's in JSON format.");
  ObjectMapper outMapper = new ObjectMapper();
  outMapper.configure(
      SerializationConfig.Feature.CAN_OVERRIDE_ACCESS_MODIFIERS, true);
  // define a module
  SimpleModule module = new SimpleModule("State Serializer",  
      new Version(0, 1, 1, "FINAL"));
  // add the state serializer
  //module.addSerializer(State.class, new StateSerializer());

  // register the module with the object-mapper
  outMapper.registerModule(module);

  JsonFactory outFactory = outMapper.getJsonFactory();
  JsonGenerator jGen = 
    outFactory.createJsonGenerator((DataOutputStream)out, JsonEncoding.UTF8);
  jGen.useDefaultPrettyPrinter();

  jGen.writeObject(this);
  jGen.close();
}
 
Example 2
Source File: Anonymizer.java    From hadoop with Apache License 2.0 6 votes vote down vote up
private JsonGenerator createJsonGenerator(Configuration conf, Path path) 
throws IOException {
  FileSystem outFS = path.getFileSystem(conf);
  CompressionCodec codec =
    new CompressionCodecFactory(conf).getCodec(path);
  OutputStream output;
  Compressor compressor = null;
  if (codec != null) {
    compressor = CodecPool.getCompressor(codec);
    output = codec.createOutputStream(outFS.create(path), compressor);
  } else {
    output = outFS.create(path);
  }

  JsonGenerator outGen = outFactory.createJsonGenerator(output, 
                                                        JsonEncoding.UTF8);
  outGen.useDefaultPrettyPrinter();
  
  return outGen;
}
 
Example 3
Source File: StatePool.java    From big-c with Apache License 2.0 6 votes vote down vote up
private void write(DataOutput out) throws IOException {
  // This is just a JSON experiment
  System.out.println("Dumping the StatePool's in JSON format.");
  ObjectMapper outMapper = new ObjectMapper();
  outMapper.configure(
      SerializationConfig.Feature.CAN_OVERRIDE_ACCESS_MODIFIERS, true);
  // define a module
  SimpleModule module = new SimpleModule("State Serializer",  
      new Version(0, 1, 1, "FINAL"));
  // add the state serializer
  //module.addSerializer(State.class, new StateSerializer());

  // register the module with the object-mapper
  outMapper.registerModule(module);

  JsonFactory outFactory = outMapper.getJsonFactory();
  JsonGenerator jGen = 
    outFactory.createJsonGenerator((DataOutputStream)out, JsonEncoding.UTF8);
  jGen.useDefaultPrettyPrinter();

  jGen.writeObject(this);
  jGen.close();
}
 
Example 4
Source File: Anonymizer.java    From big-c with Apache License 2.0 6 votes vote down vote up
private JsonGenerator createJsonGenerator(Configuration conf, Path path) 
throws IOException {
  FileSystem outFS = path.getFileSystem(conf);
  CompressionCodec codec =
    new CompressionCodecFactory(conf).getCodec(path);
  OutputStream output;
  Compressor compressor = null;
  if (codec != null) {
    compressor = CodecPool.getCompressor(codec);
    output = codec.createOutputStream(outFS.create(path), compressor);
  } else {
    output = outFS.create(path);
  }

  JsonGenerator outGen = outFactory.createJsonGenerator(output, 
                                                        JsonEncoding.UTF8);
  outGen.useDefaultPrettyPrinter();
  
  return outGen;
}
 
Example 5
Source File: AvroJson.java    From brooklin with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Convert the HashMap {@code _info} into an AvroSchema
 * This involves using the Avro Parser which will ensure the entire
 * schema is properly formatted
 *
 * If the Schema is not formatted properly, this function will throw an
 * error
 */
public Schema toSchema() throws SchemaGenerationException {
  try {
    ObjectMapper mapper = new ObjectMapper();
    JsonFactory factory = new JsonFactory();
    StringWriter writer = new StringWriter();
    JsonGenerator jgen = factory.createJsonGenerator(writer);
    jgen.useDefaultPrettyPrinter();
    mapper.writeValue(jgen, _info);
    String schema = writer.getBuffer().toString();

    return Schema.parse(schema);
  } catch (IOException e) {
    throw new SchemaGenerationException("Failed to generate Schema from AvroJson", e);
  }
}
 
Example 6
Source File: TestHistograms.java    From hadoop with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
  final Configuration conf = new Configuration();
  final FileSystem lfs = FileSystem.getLocal(conf);

  for (String arg : args) {
    Path filePath = new Path(arg).makeQualified(lfs);
    String fileName = filePath.getName();
    if (fileName.startsWith("input")) {
      LoggedDiscreteCDF newResult = histogramFileToCDF(filePath, lfs);
      String testName = fileName.substring("input".length());
      Path goldFilePath = new Path(filePath.getParent(), "gold"+testName);

      ObjectMapper mapper = new ObjectMapper();
      JsonFactory factory = mapper.getJsonFactory();
      FSDataOutputStream ostream = lfs.create(goldFilePath, true);
      JsonGenerator gen = factory.createJsonGenerator(ostream,
          JsonEncoding.UTF8);
      gen.useDefaultPrettyPrinter();
      
      gen.writeObject(newResult);
      
      gen.close();
    } else {
      System.err.println("Input file not started with \"input\". File "+fileName+" skipped.");
    }
  }
}
 
Example 7
Source File: TestHistograms.java    From big-c with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
  final Configuration conf = new Configuration();
  final FileSystem lfs = FileSystem.getLocal(conf);

  for (String arg : args) {
    Path filePath = new Path(arg).makeQualified(lfs);
    String fileName = filePath.getName();
    if (fileName.startsWith("input")) {
      LoggedDiscreteCDF newResult = histogramFileToCDF(filePath, lfs);
      String testName = fileName.substring("input".length());
      Path goldFilePath = new Path(filePath.getParent(), "gold"+testName);

      ObjectMapper mapper = new ObjectMapper();
      JsonFactory factory = mapper.getJsonFactory();
      FSDataOutputStream ostream = lfs.create(goldFilePath, true);
      JsonGenerator gen = factory.createJsonGenerator(ostream,
          JsonEncoding.UTF8);
      gen.useDefaultPrettyPrinter();
      
      gen.writeObject(newResult);
      
      gen.close();
    } else {
      System.err.println("Input file not started with \"input\". File "+fileName+" skipped.");
    }
  }
}
 
Example 8
Source File: JsonUtils.java    From fountain with Apache License 2.0 5 votes vote down vote up
private void writeValueWithConf(JsonGenerator jgen, Object value,
		JsonSerialize.Inclusion inc) throws IOException,
		JsonGenerationException, JsonMappingException {
	
	SerializationConfig cfg = copySerializationConfig();
	cfg = cfg.withSerializationInclusion(inc);
	
	// [JACKSON-96]: allow enabling pretty printing for ObjectMapper
	// directly
	if (cfg.isEnabled(SerializationConfig.Feature.INDENT_OUTPUT)) {
		jgen.useDefaultPrettyPrinter();
	}
	// [JACKSON-282]: consider Closeable
	if (cfg.isEnabled(SerializationConfig.Feature.CLOSE_CLOSEABLE)
			&& (value instanceof Closeable)) {
		configAndWriteCloseable(jgen, value, cfg);
		return;
	}
	boolean closed = false;
	try {
		_serializerProvider.serializeValue(cfg, jgen, value,
				_serializerFactory);
		closed = true;
		jgen.close();
	} finally {
		/*
		 * won't try to close twice; also, must catch exception (so it
		 * will not mask exception that is pending)
		 */
		if (!closed) {
			try {
				jgen.close();
			} catch (IOException ioe) {
			}
		}
	}
}
 
Example 9
Source File: TestHistograms.java    From RDFS with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
  final Configuration conf = new Configuration();
  final FileSystem lfs = FileSystem.getLocal(conf);

  for (String arg : args) {
    Path filePath = new Path(arg).makeQualified(lfs);
    String fileName = filePath.getName();
    if (fileName.startsWith("input")) {
      LoggedDiscreteCDF newResult = histogramFileToCDF(filePath, lfs);
      String testName = fileName.substring("input".length());
      Path goldFilePath = new Path(filePath.getParent(), "gold"+testName);

      ObjectMapper mapper = new ObjectMapper();
      JsonFactory factory = mapper.getJsonFactory();
      FSDataOutputStream ostream = lfs.create(goldFilePath, true);
      JsonGenerator gen = factory.createJsonGenerator(ostream,
          JsonEncoding.UTF8);
      gen.useDefaultPrettyPrinter();
      
      gen.writeObject(newResult);
      
      gen.close();
    } else {
      System.err.println("Input file not started with \"input\". File "+fileName+" skipped.");
    }
  }
}
 
Example 10
Source File: InstanceJsonMapper.java    From ensemble-clustering with MIT License 5 votes vote down vote up
public static String toJson(Instance inst, boolean prettyPrint) 
	throws JsonMappingException, JsonGenerationException, IOException {
    
	StringWriter writer = new StringWriter();
    JsonGenerator generator = factory.createJsonGenerator(writer);
    
    if (prettyPrint) {
        generator.useDefaultPrettyPrinter();
    }
    
    mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    mapper.writeValue(generator, inst);
    return writer.toString();
}
 
Example 11
Source File: ClusterJsonMapper.java    From ensemble-clustering with MIT License 5 votes vote down vote up
public static String toJson(Cluster cluster, boolean prettyPrint) 
	throws JsonMappingException, JsonGenerationException, IOException {
    
	StringWriter writer = new StringWriter();
    JsonGenerator generator = factory.createJsonGenerator(writer);
    
    if (prettyPrint) {
        generator.useDefaultPrettyPrinter();
    }
    
    mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    mapper.writeValue(generator, cluster);
    return writer.toString();
}