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

The following examples show how to use org.codehaus.jackson.JsonGenerator#close() . 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: DetectedLanguageWriter.java    From stanbol-freeling with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void writeTo(Collection<Language> detected, Class<?> type, Type genericType, Annotation[] annotations,
        MediaType mediaType, MultivaluedMap<String,Object> httpHeaders, OutputStream entityStream)
        throws IOException, WebApplicationException {
    JsonGenerator jg = getJsonFactory().createJsonGenerator(entityStream);
    jg.writeStartArray();
    for(Language lang : detected){
        jg.writeStartObject();
        jg.writeStringField("lang", lang.getLang());
        if(lang.getProb() > 0){
            jg.writeNumberField("prob", lang.getProb());
        }
        jg.writeEndObject();
    }
    jg.writeEndArray();
    jg.close();
}
 
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: 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 5
Source File: SiteToSiteReceiver.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
public TransactionCompletion receiveFiles() throws IOException {
    Transaction transaction = siteToSiteClient.createTransaction(TransferDirection.RECEIVE);
    JsonGenerator jsonGenerator = new JsonFactory().createJsonGenerator(output);
    jsonGenerator.writeStartArray();
    DataPacket dataPacket;
    while ((dataPacket = transaction.receive()) != null) {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeFieldName("attributes");
        jsonGenerator.writeStartObject();
        Map<String, String> attributes = dataPacket.getAttributes();
        if (attributes != null) {
            for (Map.Entry<String, String> stringStringEntry : attributes.entrySet()) {
                jsonGenerator.writeStringField(stringStringEntry.getKey(), stringStringEntry.getValue());
            }
        }
        jsonGenerator.writeEndObject();
        InputStream data = dataPacket.getData();
        if (data != null) {
            jsonGenerator.writeBinaryField("data", IOUtils.toByteArray(data));
        }
        jsonGenerator.writeEndObject();
    }
    jsonGenerator.writeEndArray();
    jsonGenerator.close();
    transaction.confirm();
    return transaction.complete();
}
 
Example 6
Source File: JSONUtil.java    From jstorm with Apache License 2.0 5 votes vote down vote up
/**
 * POJO对象转换为JSON
 * @param pojo
 * @return
 */
public String formatPOJO2JSON(Object pojo) {
	StringWriter stringWriter = new StringWriter();
	String json = "";
	try {
		JsonGenerator gen = new JsonFactory()
				.createJsonGenerator(stringWriter);
		MAPPER.writeValue(gen, pojo);
		gen.close();
		json = stringWriter.toString();
	} catch (Exception e) {
		LOGGER.error(pojo.getClass().getName() + "转json出错", e);
	}
	return json;
}
 
Example 7
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 8
Source File: JsonMapper.java    From ReactiveLab with Apache License 2.0 5 votes vote down vote up
static String toJson(HystrixThreadPoolMetrics threadPoolMetrics) throws IOException {
    HystrixThreadPoolKey key = threadPoolMetrics.getThreadPoolKey();

    StringWriter jsonString = new StringWriter();
    JsonGenerator json = jsonFactory.createJsonGenerator(jsonString);
    json.writeStartObject();

    json.writeStringField("type", "HystrixThreadPool");
    json.writeStringField("name", key.name());
    json.writeNumberField("currentTime", System.currentTimeMillis());

    json.writeNumberField("currentActiveCount", threadPoolMetrics.getCurrentActiveCount().intValue());
    json.writeNumberField("currentCompletedTaskCount", threadPoolMetrics.getCurrentCompletedTaskCount().longValue());
    json.writeNumberField("currentCorePoolSize", threadPoolMetrics.getCurrentCorePoolSize().intValue());
    json.writeNumberField("currentLargestPoolSize", threadPoolMetrics.getCurrentLargestPoolSize().intValue());
    json.writeNumberField("currentMaximumPoolSize", threadPoolMetrics.getCurrentMaximumPoolSize().intValue());
    json.writeNumberField("currentPoolSize", threadPoolMetrics.getCurrentPoolSize().intValue());
    json.writeNumberField("currentQueueSize", threadPoolMetrics.getCurrentQueueSize().intValue());
    json.writeNumberField("currentTaskCount", threadPoolMetrics.getCurrentTaskCount().longValue());
    json.writeNumberField("rollingCountThreadsExecuted", threadPoolMetrics.getRollingCountThreadsExecuted());
    json.writeNumberField("rollingMaxActiveThreads", threadPoolMetrics.getRollingMaxActiveThreads());

    json.writeNumberField("propertyValue_queueSizeRejectionThreshold", threadPoolMetrics.getProperties().queueSizeRejectionThreshold().get());
    json.writeNumberField("propertyValue_metricsRollingStatisticalWindowInMilliseconds", threadPoolMetrics.getProperties().metricsRollingStatisticalWindowInMilliseconds().get());

    json.writeNumberField("reportingHosts", 1); // this will get summed across all instances in a cluster

    json.writeEndObject();
    json.close();

    return jsonString.getBuffer().toString();
}
 
Example 9
Source File: ResponseBuilder.java    From ReactiveLab with Apache License 2.0 5 votes vote down vote up
public static ByteArrayOutputStream buildTestResponse(BackendResponse responseA, BackendResponse responseB,
        BackendResponse responseC, BackendResponse responseD, BackendResponse responseE) {
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(bos);
        generateResponse(responseA, responseB, responseC, responseD, responseE, jsonGenerator);
        jsonGenerator.close();
        return bos;
    } catch (Exception e) {
        throw new RuntimeException("failed to generated response", e);
    }
}
 
Example 10
Source File: ResponseBuilder.java    From ReactiveLab with Apache License 2.0 5 votes vote down vote up
public static void writeTestResponse(Writer writer, BackendResponse responseA, BackendResponse responseB,
        BackendResponse responseC, BackendResponse responseD, BackendResponse responseE, Action0 onComplete) {
    try {
        JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(writer);
        generateResponse(responseA, responseB, responseC, responseD, responseE, jsonGenerator);
        if (onComplete != null) {
            onComplete.call();
        }
        jsonGenerator.close();
    } catch (Exception e) {
        throw new RuntimeException("failed to generated response", e);
    }

}
 
Example 11
Source File: JSONUtil.java    From jstorm with Apache License 2.0 5 votes vote down vote up
/**
 * 将map转化为json
 * 
 * @param map
 * @return
 */
public String formatMap2JSON(Map<String, Object> map) {
	StringWriter stringWriter = new StringWriter();
	String json = "";
	try {
		JsonGenerator gen = new JsonFactory()
				.createJsonGenerator(stringWriter);
		MAPPER.writeValue(gen, map);
		gen.close();
		json = stringWriter.toString();
	} catch (Exception e) {
		LOGGER.error("", e);
	}
	return json;
}
 
Example 12
Source File: JsonStorage.java    From spork with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void putNext(Tuple t) throws IOException {
    // Build a ByteArrayOutputStream to write the JSON into
    ByteArrayOutputStream baos = new ByteArrayOutputStream(BUF_SIZE);
    // Build the generator
    JsonGenerator json =
        jsonFactory.createJsonGenerator(baos, JsonEncoding.UTF8);

    // Write the beginning of the top level tuple object
    json.writeStartObject();
    
    ResourceFieldSchema[] fields = schema.getFields();
    for (int i = 0; i < fields.length; i++) {
        int tupleLength = t.size();
        //write col if exists in tuple, null otherwise
        if (i < tupleLength) {
            writeField(json, fields[i], t.get(i));
        } else {
            writeField(json, fields[i], null);
        }
    }
    json.writeEndObject();
    json.close();

    // Hand a null key and our string to Hadoop
    try {
        writer.write(null, new Text(baos.toByteArray()));
    } catch (InterruptedException ie) {
        throw new IOException(ie);
    }
}
 
Example 13
Source File: Anonymizer.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private void anonymizeTopology() throws Exception {
  if (anonymizeTopology) {
    System.out.println("Anonymizing topology file: " + inputTopologyPath);
    ClusterTopologyReader reader = null;
    JsonGenerator outGen = null;
    Configuration conf = getConf();

    try {
      // create a generator
      outGen = createJsonGenerator(conf, outputTopologyPath);

      // define the input cluster topology reader
      reader = new ClusterTopologyReader(inputTopologyPath, conf);
      
      // read the plain unanonymized logged job
      LoggedNetworkTopology job = reader.get();
      
      // write it via an anonymizing channel
      outGen.writeObject(job);
      
      System.out.println("Anonymized topology file: " + outputTopologyPath);
    } finally {
      if (outGen != null) {
        outGen.close();
      }
    }
  }
}
 
Example 14
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 15
Source File: Anonymizer.java    From big-c with Apache License 2.0 5 votes vote down vote up
private void anonymizeTopology() throws Exception {
  if (anonymizeTopology) {
    System.out.println("Anonymizing topology file: " + inputTopologyPath);
    ClusterTopologyReader reader = null;
    JsonGenerator outGen = null;
    Configuration conf = getConf();

    try {
      // create a generator
      outGen = createJsonGenerator(conf, outputTopologyPath);

      // define the input cluster topology reader
      reader = new ClusterTopologyReader(inputTopologyPath, conf);
      
      // read the plain unanonymized logged job
      LoggedNetworkTopology job = reader.get();
      
      // write it via an anonymizing channel
      outGen.writeObject(job);
      
      System.out.println("Anonymized topology file: " + outputTopologyPath);
    } finally {
      if (outGen != null) {
        outGen.close();
      }
    }
  }
}
 
Example 16
Source File: ServiceResponseBuilder.java    From WSPerfLab with Apache License 2.0 5 votes vote down vote up
public static ByteArrayOutputStream buildTestBResponse(JsonFactory jsonFactory, BackendResponse response)
            throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(bos);

    jsonGenerator.writeStartObject();

    // delay values of each response
    jsonGenerator.writeArrayFieldStart("delay");
    writeTuple(jsonGenerator, "a", response.getDelay());
    jsonGenerator.writeEndArray();

    // itemSize values of each response
    jsonGenerator.writeArrayFieldStart("itemSize");
    writeTuple(jsonGenerator, "a", response.getItemSize());
    jsonGenerator.writeEndArray();

    // numItems values of each response
    jsonGenerator.writeArrayFieldStart("numItems");
    writeTuple(jsonGenerator, "a", response.getNumItems());
    jsonGenerator.writeEndArray();

    // all items from responses
    jsonGenerator.writeArrayFieldStart("items");
    addItemsFromResponse(jsonGenerator, response);
    jsonGenerator.writeEndArray();

    jsonGenerator.writeEndObject();
    jsonGenerator.close();

    return bos;
}
 
Example 17
Source File: Anonymizer.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private void anonymizeTrace() throws Exception {
  if (anonymizeTrace) {
    System.out.println("Anonymizing trace file: " + inputTracePath);
    JobTraceReader reader = null;
    JsonGenerator outGen = null;
    Configuration conf = getConf();
    
    try {
      // create a generator
      outGen = createJsonGenerator(conf, outputTracePath);

      // define the input trace reader
      reader = new JobTraceReader(inputTracePath, conf);
      
      // read the plain unanonymized logged job
      LoggedJob job = reader.getNext();
      
      while (job != null) {
        // write it via an anonymizing channel
        outGen.writeObject(job);
        // read the next job
        job = reader.getNext();
      }
      
      System.out.println("Anonymized trace file: " + outputTracePath);
    } finally {
      if (outGen != null) {
        outGen.close();
      }
      if (reader != null) {
        reader.close();
      }
    }
  }
}
 
Example 18
Source File: Log4Json.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Build a JSON entry from the parameters. This is public for testing.
 *
 * @param writer destination
 * @param loggerName logger name
 * @param timeStamp time_t value
 * @param level level string
 * @param threadName name of the thread
 * @param message rendered message
 * @param ti nullable thrown information
 * @return the writer
 * @throws IOException on any problem
 */
public Writer toJson(final Writer writer,
                     final String loggerName,
                     final long timeStamp,
                     final String level,
                     final String threadName,
                     final String message,
                     final ThrowableInformation ti) throws IOException {
  JsonGenerator json = factory.createJsonGenerator(writer);
  json.writeStartObject();
  json.writeStringField(NAME, loggerName);
  json.writeNumberField(TIME, timeStamp);
  Date date = new Date(timeStamp);
  json.writeStringField(DATE, dateFormat.format(date));
  json.writeStringField(LEVEL, level);
  json.writeStringField(THREAD, threadName);
  json.writeStringField(MESSAGE, message);
  if (ti != null) {
    //there is some throwable info, but if the log event has been sent over the wire,
    //there may not be a throwable inside it, just a summary.
    Throwable thrown = ti.getThrowable();
    String eclass = (thrown != null) ?
        thrown.getClass().getName()
        : "";
    json.writeStringField(EXCEPTION_CLASS, eclass);
    String[] stackTrace = ti.getThrowableStrRep();
    json.writeArrayFieldStart(STACK);
    for (String row : stackTrace) {
      json.writeString(row);
    }
    json.writeEndArray();
  }
  json.writeEndObject();
  json.flush();
  json.close();
  return writer;
}
 
Example 19
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 20
Source File: ServiceResponseBuilder.java    From WSPerfLab with Apache License 2.0 4 votes vote down vote up
public static ByteArrayOutputStream buildTestAResponse(JsonFactory jsonFactory,
        BackendResponse responseA, BackendResponse responseB,
        BackendResponse responseC, BackendResponse responseD,
        BackendResponse responseE) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(bos);

    jsonGenerator.writeStartObject();
    // multiplication of C, D, E responseKey
    jsonGenerator.writeNumberField("responseKey", responseC.getResponseKey() + responseD.getResponseKey() +
            responseE.getResponseKey());

    // delay values of each response
    jsonGenerator.writeArrayFieldStart("delay");
    writeTuple(jsonGenerator, "a", responseA.getDelay());
    writeTuple(jsonGenerator, "b", responseB.getDelay());
    writeTuple(jsonGenerator, "c", responseC.getDelay());
    writeTuple(jsonGenerator, "d", responseD.getDelay());
    writeTuple(jsonGenerator, "e", responseE.getDelay());
    jsonGenerator.writeEndArray();

    // itemSize values of each response
    jsonGenerator.writeArrayFieldStart("itemSize");
    writeTuple(jsonGenerator, "a", responseA.getItemSize());
    writeTuple(jsonGenerator, "b", responseB.getItemSize());
    writeTuple(jsonGenerator, "c", responseC.getItemSize());
    writeTuple(jsonGenerator, "d", responseD.getItemSize());
    writeTuple(jsonGenerator, "e", responseE.getItemSize());
    jsonGenerator.writeEndArray();

    // numItems values of each response
    jsonGenerator.writeArrayFieldStart("numItems");
    writeTuple(jsonGenerator, "a", responseA.getNumItems());
    writeTuple(jsonGenerator, "b", responseB.getNumItems());
    writeTuple(jsonGenerator, "c", responseC.getNumItems());
    writeTuple(jsonGenerator, "d", responseD.getNumItems());
    writeTuple(jsonGenerator, "e", responseE.getNumItems());
    jsonGenerator.writeEndArray();

    // all items from responses
    jsonGenerator.writeArrayFieldStart("items");
    addItemsFromResponse(jsonGenerator, responseA);
    addItemsFromResponse(jsonGenerator, responseB);
    addItemsFromResponse(jsonGenerator, responseC);
    addItemsFromResponse(jsonGenerator, responseD);
    addItemsFromResponse(jsonGenerator, responseE);
    jsonGenerator.writeEndArray();

    jsonGenerator.writeEndObject();
    jsonGenerator.close();

    return bos;
}