@Override
  public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException {
    gen.writeStartObject();
    gen.writeStringField(TYPE_FIELD, type);
    gen.writeStringField(ID_FIELD, id);
    gen.writeStringField(MPEG4URL_FIELD, this.mpeg4Url);

    if (parseMode != null) {
      gen.writeStringField(PARSEMODE_FIELD, this.parseMode);
    }
    if (disableWebPagePreview != null) {
      gen.writeBooleanField(DISABLEWEBPAGEPREVIEW_FIELD, this.disableWebPagePreview);
    }
    if (mpeg4Width != null) {
      gen.writeNumberField(MPEG4WIDTH_FIELD, this.mpeg4Width);
    }
    if (mpeg4Height != null) {
      gen.writeNumberField(MPEG4HEIGHT_FIELD, this.mpeg4Height);
    }
    if (thumbUrl != null) {
      gen.writeStringField(THUMBURL_FIELD, this.thumbUrl);
    }
    if (title != null) {
      gen.writeStringField(TITLE_FIELD, this.title);
    }
    if (caption != null) {
      gen.writeStringField(CAPTION_FIELD, this.caption);
    }
    if (messageText != null) {
      gen.writeStringField(MESSAGETEXT_FIELD, this.messageText);
    }

    gen.writeEndObject();
    gen.flush();
  }
  public void serialize(SqlSession sql, JsonGenerator jg)
      throws JsonGenerationException, IOException, IllegalTypeException {
    Map<String, GroupBean> sortedGroups = GroupsIE.getSortedGroups(sql, groupResolver);
    GroupsMapper mapper = sql.getMapper(GroupsMapper.class);

    jg.writeStartArray();
    for (Map.Entry<String, GroupBean> entry : sortedGroups.entrySet()) {
      List<GroupElementBean> members = mapper.getMembers(entry.getValue().getId());

      jg.writeStartObject();
      jg.writeStringField("groupPath", entry.getKey());
      jg.writeFieldName("members");
      jg.writeStartArray();
      for (GroupElementBean member : members) {
        jg.writeStartObject();
        jg.writeNumberField("entity", member.getElementId());
        jg.writeNumberField("groupId", member.getGroupId());
        jg.writeBinaryField("contents", member.getContents());
        jg.writeEndObject();
      }
      jg.writeEndArray();
      jg.writeEndObject();
    }
    jg.writeEndArray();
  }
    @Override
    public void serialize(TMessage value, JsonGenerator gen, SerializerProvider provider)
        throws IOException {
      gen.writeStartObject();

      gen.writeStringField("name", value.name);
      gen.writeNumberField("type", value.type);
      gen.writeNumberField("seqid", value.seqid);

      gen.writeEndObject();
    }
 @Override
 public void serialize(
     final Rectangle2D value, final JsonGenerator gen, final SerializerProvider sP)
     throws IOException, JsonProcessingException {
   gen.writeStartObject();
   gen.writeNumberField("X", value.getX());
   gen.writeNumberField("Y", value.getY());
   gen.writeNumberField("Width", value.getWidth());
   gen.writeNumberField("Height", value.getHeight());
   gen.writeEndObject();
 }
 private void safelyWriteNumberField(
     JsonGenerator json, String name, Func0<Long> metricGenerator) throws IOException {
   try {
     json.writeNumberField(name, metricGenerator.call());
   } catch (NoSuchFieldError error) {
     logger.error(
         "While publishing Hystrix metrics stream, error looking up eventType for : {}.  Please check that all Hystrix versions are the same!",
         name);
     json.writeNumberField(name, 0L);
   }
 }
 private void writeValue(String field, Object value, JsonGenerator jg) throws IOException {
   if (value instanceof String) jg.writeStringField(field, (String) value);
   else if (value instanceof Long) jg.writeNumberField(field, (Long) value);
   else if (value instanceof Integer) jg.writeNumberField(field, (Integer) value);
   else if (value instanceof Float) jg.writeNumberField(field, (Float) value);
   else if (value instanceof Double) jg.writeNumberField(field, (Double) value);
   else if (value instanceof Boolean) jg.writeBooleanField(field, (Boolean) value);
   else if (value instanceof Iterable<?>) writeIterableToField(field, (Iterable<?>) value, jg);
   else if (value instanceof MapWrapper<?, ?>)
     writeMapWrapperToField(field, (MapWrapper<?, ?>) value, jg);
   else jg.writeStringField(field, "Unknown");
   jg.flush();
 }
  private void writeQueryDetails(JsonGenerator jg, Query query) throws IOException {
    if (query.getView() != 20) {
      jg.writeStringField("id", query.getId());
      System.out.print("{ id: " + query.getId());
      jg.writeStringField("cql", query.toCQL());
      System.out.print(", cql: " + query.toCQL());
      if (query.toJSON() != null) jg.writeStringField("json", query.toJSON().toString());
      jg.writeStringField("within", query.getWithin());
      System.out.print(", within: " + query.getWithin());
      jg.writeNumberField("number", query.getLimit());
      System.out.print(", number: " + String.valueOf(query.getLimit()));
      jg.writeNumberField("offset", query.getSkip());
      System.out.print(", offset: " + String.valueOf(query.getSkip()));
    }
    jg.writeStringField("filter", query.getFilter());
    System.out.print(", filter: " + query.getFilter());
    jg.writeNumberField("view", query.getView());
    System.out.print(", view: " + String.valueOf(query.getView()));

    String group = query.getGroup();
    if (group != null && group.length() > 0) {
      jg.writeStringField("group", group);
      System.out.print(", group: " + group);
    }

    if (query.getView() != 20) {
      String sort = query.getSort();
      if (sort != null && sort.length() > 0) {
        jg.writeStringField("sort", sort);
        System.out.print(", sort: " + sort);
      }

      String order = query.getOrder();
      if (order != null && order.length() > 0) {
        jg.writeStringField("order", order);
        System.out.print(", order: " + order);
      }

      String docPid = query.getDocpid();
      if (docPid != null && docPid.length() > 0) {
        jg.writeStringField("docpid", docPid);
        System.out.print(", docpid: " + docPid);
      }
      System.out.print(", count: " + String.valueOf(query.isCountQuery()) + " }");
      System.out.println("");
    }

    jg.writeStringField("cypher", query.toCypher());
    jg.flush();
  }
    @Override
    public void serialize(Record record, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonProcessingException {
      jgen.writeStartObject();
      jgen.writeStringField("id", record.getId());
      jgen.writeStringField("type", record.getType());
      jgen.writeStringField("creationDate", record.getTime().toString());

      jgen.writeObjectFieldStart("fields");
      for (Map.Entry<String, Field> entry : record.getFieldsEntrySet()) {
        // retrieve event field
        String fieldName = entry.getKey();
        Field field = entry.getValue();
        Object fieldValue = field.getRawValue();
        String fieldType = field.getType().toString();

        // dump event field as record attribute

        try {
          switch (fieldType.toLowerCase()) {
            case "string":
              jgen.writeStringField(fieldName, (String) fieldValue);
              break;
            case "integer":
              jgen.writeNumberField(fieldName, (int) fieldValue);
              break;
            case "long":
              jgen.writeNumberField(fieldName, (long) fieldValue);
              break;
            case "float":
              jgen.writeNumberField(fieldName, (float) fieldValue);
              break;
            case "double":
              jgen.writeNumberField(fieldName, (double) fieldValue);
              break;
            case "boolean":
              jgen.writeBooleanField(fieldName, (boolean) fieldValue);
              break;
            default:
              jgen.writeObjectField(fieldName, fieldValue);
              break;
          }
        } catch (Exception ex) {
          logger.debug("exception while serializing field {}", field);
        }
      }
      jgen.writeEndObject();
      jgen.writeEndObject();
    }
  @GET("/backend/aggregate/points")
  String getPoints(String content) throws IOException {
    logger.debug("getPoints(): content={}", content);
    PointsRequest request = ObjectMappers.readRequiredValue(mapper, content, PointsRequest.class);
    List<AggregatePoint> points = aggregateDao.readAggregates(request.getFrom(), request.getTo());

    StringBuilder sb = new StringBuilder();
    JsonGenerator jg = mapper.getFactory().createGenerator(CharStreams.asWriter(sb));
    jg.writeStartObject();
    jg.writeArrayFieldStart("points");
    for (AggregatePoint point : points) {
      jg.writeStartArray();
      jg.writeNumber(point.getCaptureTime());
      long durationAverage;
      if (point.getTraceCount() == 0) {
        durationAverage = 0;
      } else {
        durationAverage = point.getDurationTotal() / point.getTraceCount();
      }
      jg.writeNumber(durationAverage / 1000000000.0);
      jg.writeNumber(point.getTraceCount());
      jg.writeEndArray();
    }
    jg.writeEndArray();
    jg.writeNumberField("fixedAggregateIntervalSeconds", fixedAggregateIntervalSeconds);
    jg.writeEndObject();
    jg.close();
    return sb.toString();
  }
 private void writeApplication(JsonGenerator jgen, Application application) throws IOException {
   jgen.writeStartObject();
   jgen.writeStringField("applicationName", application.getName());
   jgen.writeStringField("serviceType", application.getServiceType().getDesc());
   jgen.writeNumberField("code", application.getServiceTypeCode());
   jgen.writeEndObject();
 }
 @Override
 public void serialize(
     final Point value, final JsonGenerator gen, final SerializerProvider sP)
     throws IOException, JsonProcessingException {
   gen.writeStartObject();
   gen.writeNumberField("X", value.x);
   gen.writeNumberField("Y", value.y);
   gen.writeEndObject();
 }
  /**
   * "hotspots": [{
   *
   * <p>"id": "test_1",
   *
   * <p>"anchor": { "geolocation": { "lat": 52.3729, "lon": 4.93 } },
   *
   * <p>"text": { "title": "The Layar Office", "description": "The Location of the Layar Office",
   * "footnote": "Powered by Layar" },
   *
   * <p>"imageURL": "http:\/\/custom.layar.nl\/layarimage.jpeg", }
   *
   * <p>]
   *
   * <p>See http://layar.com/documentation/browser/api/getpois-response/hotspots/
   *
   * @param generator
   * @param hotspots2
   * @throws IOException
   * @throws JsonGenerationException
   */
  private void createHotspots(JsonGenerator generator) throws JsonGenerationException, IOException {
    generator.writeFieldName("hotspots");
    generator.writeStartArray();
    for (Hotspot poi : this.hotspots) {

      generator.writeStartObject();
      generator.writeStringField("id", poi.id);

      // generator.writeFieldName("actions");
      // generator.writeStartArray();
      // if (layarPOI.actions != null) {
      // for (final LayarAction layarAction : layarPOI.actions) {
      // layarAction.toJSON(generator);
      // }
      // }
      // generator.writeEndArray();

      generator.writeObjectFieldStart("anchor");
      generator.writeObjectFieldStart("geolocation");
      generator.writeNumberField("lat", poi.lat);
      generator.writeNumberField("lon", poi.lon);
      generator.writeNumberField("alt", poi.alt);
      generator.writeEndObject();
      generator.writeEndObject();

      // generator.writeNumberField("distance", layarPOI.distance);
      // generator.writeNumberField("type", layarPOI.type);
      // generator.writeStringField("title", layarPOI.title);
      generator.writeObjectFieldStart("text");
      generator.writeStringField("title", poi.title);
      generator.writeStringField("description", poi.description);
      generator.writeStringField("footnote", "Service URL: ...");
      generator.writeEndObject();

      generator.writeStringField("attribution", poi.attribution);
      if (poi.imageURL != null) {
        generator.writeStringField("imageURL", poi.imageURL.toString());
      } else {
        generator.writeNullField("imageURL");
      }
      generator.writeEndObject();
    }
    generator.writeEndArray();
  }
  @Override
  public String handleRequest(ExecutionJobVertex jobVertex, Map<String, String> params)
      throws Exception {
    StringWriter writer = new StringWriter();
    JsonGenerator gen = JsonFactory.jacksonFactory.createGenerator(writer);

    gen.writeStartObject();
    gen.writeStringField("id", jobVertex.getJobVertexId().toString());
    gen.writeNumberField("parallelism", jobVertex.getParallelism());

    gen.writeArrayFieldStart("subtasks");

    int num = 0;
    for (ExecutionVertex vertex : jobVertex.getTaskVertices()) {

      InstanceConnectionInfo location = vertex.getCurrentAssignedResourceLocation();
      String locationString = location == null ? "(unassigned)" : location.getHostname();

      gen.writeStartObject();

      gen.writeNumberField("subtask", num++);
      gen.writeNumberField("attempt", vertex.getCurrentExecutionAttempt().getAttemptNumber());
      gen.writeStringField("host", locationString);

      StringifiedAccumulatorResult[] accs =
          vertex.getCurrentExecutionAttempt().getUserAccumulatorsStringified();
      gen.writeArrayFieldStart("user-accumulators");
      for (StringifiedAccumulatorResult acc : accs) {
        gen.writeStartObject();
        gen.writeStringField("name", acc.getName());
        gen.writeStringField("type", acc.getType());
        gen.writeStringField("value", acc.getValue());
        gen.writeEndObject();
      }
      gen.writeEndArray();

      gen.writeEndObject();
    }
    gen.writeEndArray();

    gen.writeEndObject();
    gen.close();
    return writer.toString();
  }
 private void addServiceData(JsonGenerator json, ServiceInstance localService) throws IOException {
   json.writeObjectFieldStart("origin");
   json.writeStringField("host", localService.getHost());
   json.writeNumberField("port", localService.getPort());
   json.writeStringField("serviceId", localService.getServiceId());
   if (this.properties.isSendId()) {
     json.writeStringField("id", this.context.getId());
   }
   json.writeEndObject();
 }
 private void _serialise(final HyperLogLogPlus hyperLogLogPlus, final JsonGenerator jsonGenerator)
     throws IOException {
   jsonGenerator.writeObjectFieldStart("hyperLogLogPlus");
   jsonGenerator.writeObjectField(
       HyperLogLogPlusJsonConstants.HYPER_LOG_LOG_PLUS_SKETCH_BYTES_FIELD,
       hyperLogLogPlus.getBytes());
   jsonGenerator.writeNumberField(
       HyperLogLogPlusJsonConstants.CARDINALITY_FIELD, hyperLogLogPlus.cardinality());
   jsonGenerator.writeEndObject();
 }
 @Override
 public void serialize(HealthCheck healthCheck, JsonGenerator jgen, SerializerProvider provider)
     throws IOException {
   jgen.writeStartObject();
   jgen.writeNumberField("interval", healthCheck.getInterval());
   jgen.writeStringField("unit", healthCheck.getUnit().toString());
   jgen.writeBooleanField("enabled", healthCheck.isEnabled());
   jgen.writeObjectField("request", healthCheck.getRequest());
   jgen.writeObjectField("expectation", healthCheck.getExpectation());
   jgen.writeEndObject();
 }
  @Override
  public void serialize(
      ApplicationScatterScanResult value, JsonGenerator jgen, SerializerProvider provider)
      throws IOException, JsonProcessingException {
    jgen.writeStartObject();
    Application application = value.getApplication();
    jgen.writeStringField("applicationName", application.getName());
    jgen.writeNumberField("applicationServiceType", application.getServiceTypeCode());

    jgen.writeFieldName("scatter");
    jgen.writeObject(value.getScatterScanResult());

    jgen.writeEndObject();
  }
  @Override
  public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException {
    gen.writeStartObject();
    gen.writeStringField(TYPE_FIELD, type);
    gen.writeStringField(ID_FIELD, id);
    gen.writeStringField(PHOTOURL_FIELD, this.photoUrl);
    if (mimeType != null) {
      gen.writeStringField(MIMETYPE_FIELD, this.mimeType);
    }
    if (photoWidth != null) {
      gen.writeNumberField(PHOTOWIDTH_FIELD, this.photoWidth);
    }
    if (photoHeight != null) {
      gen.writeNumberField(PHOTOHEIGHT_FIELD, this.photoHeight);
    }
    if (thumbUrl != null) {
      gen.writeStringField(THUMBURL_FIELD, this.thumbUrl);
    }
    if (description != null) {
      gen.writeStringField(DESCRIPTION_FIELD, this.description);
    }
    if (title != null) {
      gen.writeStringField(TITLE_FIELD, this.title);
    }
    if (caption != null) {
      gen.writeStringField(CAPTION_FIELD, this.caption);
    }
    if (replyMarkup != null) {
      gen.writeObjectField(REPLY_MARKUP_FIELD, replyMarkup);
    }
    if (inputMessageContent != null) {
      gen.writeObjectField(INPUTMESSAGECONTENT_FIELD, inputMessageContent);
    }

    gen.writeEndObject();
    gen.flush();
  }
  @Test
  public void writeJSONViaLowerlevelLibs() throws IOException {
    final StringWriter writer = new StringWriter();

    final ObjectMapper mapper = new ObjectMapper();

    final JsonGenerator jgen = mapper.getFactory().createGenerator(writer);

    jgen.writeStartObject();

    jgen.writeStringField(
        "odata.type", "Microsoft.Test.OData.Services.AstoriaDefaultService.Customer");

    jgen.writeStringField("*****@*****.**", "Edm.String");
    jgen.writeStringField("Name", "A name");

    jgen.writeStringField("*****@*****.**", "Edm.Int32");
    jgen.writeNumberField("CustomerId", 0);

    jgen.writeArrayFieldStart("BackupContactInfo");

    jgen.writeStartObject();

    jgen.writeArrayFieldStart("AlternativeNames");
    jgen.writeString("myname");
    jgen.writeEndArray();

    jgen.writeArrayFieldStart("EmailBag");
    jgen.writeString("*****@*****.**");
    jgen.writeEndArray();

    jgen.writeObjectFieldStart("ContactAlias");
    jgen.writeStringField(
        "odata.type", "Microsoft.Test.OData.Services.AstoriaDefaultService.Aliases");
    jgen.writeArrayFieldStart("AlternativeNames");
    jgen.writeString("myAlternativeName");
    jgen.writeEndArray();
    jgen.writeEndObject();

    jgen.writeEndObject();

    jgen.writeEndArray();

    jgen.writeEndObject();

    jgen.flush();

    assertFalse(writer.toString().isEmpty());
  }
  /**
   * Saves the configuration in a file so it can be loaded if the user wants to use the
   * configuration again.
   */
  public void saveConfig() {
    final JsonFactory jsonFactory = new JsonFactory();
    try {
      LOG.info("Saving configuration in " + this.outDirName);

      final DateFormat dateFmt = new SimpleDateFormat("yyyyMMdd-HHmmss");
      final Date date = new Date();
      final String d = dateFmt.format(date);

      final File file = new File(this.outDirName + "/config" + d + ".json");
      final JsonGenerator jg = jsonFactory.createGenerator(file, JsonEncoding.UTF8);
      jg.writeStartObject();
      if (this.inputFileName != null) {
        jg.writeStringField(IN_FILE_KEY, this.inputFileName);
        jg.writeStringField(SPLIT_PATTERN_KEY, this.splitPattern);
      } else {
        jg.writeStringField(IN_FILE_DIR_KEY, this.inputFileDirName);
      }

      jg.writeStringField(OUT_FILE_KEY, this.outDirName);
      jg.writeStringField(EXEC_LOC_KEY, this.execPath);
      jg.writeNumberField(NUM_PROCESSES_KEY, this.numberOfThreads);
      jg.writeBooleanField(STATS_KEY, this.makeStats);
      jg.writeStringField(ARGUMENT_KEY, this.argument);
      jg.writeNumberField(NUM_HEADER_KEY, this.numberOfHeaderLines);
      jg.writeBooleanField(EXTERNAL_MERGE_KEY, this.mergeMethod == 2);
      jg.writeBooleanField(CUSTOM_MERGE_KEY, this.mergeMethod == 1);
      jg.writeBooleanField(DEFAULT_MERGE_KEY, this.mergeMethod == 0);
      jg.writeBooleanField(OUTPUT_FMT_KEY, this.outputFmt);
      jg.writeEndObject();
      jg.close();
      LOG.info("Configuration saved");
    } catch (IOException e) {
      LOG.error("saveConfig: " + e.getMessage());
    }
  }
Exemple #21
0
 @Override
 public void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException {
   gen.writeStartObject();
   gen.writeStringField(FILEID_FIELD, fileId);
   gen.writeObjectField(THUMB_FIELD, thumb);
   if (fileName != null) {
     gen.writeStringField(FILENAME_FIELD, fileName);
   }
   if (mimeType != null) {
     gen.writeStringField(MIMETYPE_FIELD, mimeType);
   }
   if (fileSize != null) {
     gen.writeNumberField(FILESIZE_FIELD, fileSize);
   }
   gen.writeEndObject();
   gen.flush();
 }
 private void executeQuery(JsonGenerator jg, Query query) throws IOException {
   query.setStart(System.currentTimeMillis());
   try (Transaction ignored = database.beginTx();
       Result result = database.execute(query.toCypher())) {
     query.setEnd(System.currentTimeMillis());
     jg.writeNumberField("duration", query.getDuration());
     if (!query.isCountQuery()) jg.writeArrayFieldStart(query.getResultHeader());
     while (result.hasNext()) {
       Map<String, Object> row = result.next();
       if (!query.isCountQuery()) jg.writeStartObject();
       for (Entry<String, Object> column : row.entrySet()) {
         writeField(column, jg);
       }
       if (!query.isCountQuery()) jg.writeEndObject();
     }
     if (!query.isCountQuery()) jg.writeEndArray();
   }
 }
  public void toJSON(final JsonGenerator generator) throws IOException {
    generator.writeStartObject();

    /*
     * mandatory:
     */
    generator.writeStringField("layer", this.layer);
    if (this.errorCode != CODE_ERROR && this.hotspots.size() < 1) {
      this.errorString = "No POI found. Please increase the search range to try again.";
      this.errorCode = CODE_ERROR;
    }
    generator.writeStringField("errorString", this.errorString);
    generator.writeNumberField("errorCode", this.errorCode);

    // paging is not implemented yet
    generator.writeBooleanField("morePages", this.morePages);
    if (this.nextPageKey != null) {
      generator.writeStringField("nextPageKey", this.nextPageKey);
    } else {
      generator.writeNullField("nextPageKey");
    }
    // generator.writeNumberField("refreshInterval", 3600);
    // generator.writeNumberField("refreshDistance", 50);
    // generator.writeBooleanField("fullRefresh", false);
    // actions for the entire layer:
    // http://layar.com/documentation/browser/api/getpois-response/actions/
    if (this.errorCode == CODE_OK)
      this.showMessage = "You got " + this.hotspots.size() + " hits in the area around you!";
    generator.writeStringField("showMessage", this.showMessage);
    // deletedHotspots
    // animations
    // showDialog: title, description, iconURL, actions
    generator.writeNullField("biwStyle");

    /*
     * hotspots (mandatory):
     */
    createHotspots(generator);

    generator.writeEndObject();
  }
  @Test
  public void testCreateGenerator_writeRaw() throws Exception {

    EbeanServer server = Ebean.getDefaultServer();

    StringWriter writer = new StringWriter();
    JsonContext json = server.json();
    JsonGenerator generator = json.createGenerator(writer);

    // test that we can write anything via writeRaw()
    generator.writeRaw("START");
    generator.writeStartArray();
    generator.writeStartObject();
    generator.writeNumberField("count", 12);
    generator.writeEndObject();
    generator.writeEndArray();
    generator.writeRaw("END");
    generator.close();

    assertEquals("START[{\"count\":12}]END", writer.toString());
  }
  @Override
  public String handleRequest(ExecutionJobVertex jobVertex, Map<String, String> params)
      throws Exception {
    // Build a map that groups tasks by TaskManager
    Map<String, List<ExecutionVertex>> taskManagerVertices = new HashMap<>();

    for (ExecutionVertex vertex : jobVertex.getTaskVertices()) {
      TaskManagerLocation location = vertex.getCurrentAssignedResourceLocation();
      String taskManager =
          location == null ? "(unassigned)" : location.getHostname() + ":" + location.dataPort();

      List<ExecutionVertex> vertices = taskManagerVertices.get(taskManager);

      if (vertices == null) {
        vertices = new ArrayList<ExecutionVertex>();
        taskManagerVertices.put(taskManager, vertices);
      }

      vertices.add(vertex);
    }

    // Build JSON response
    final long now = System.currentTimeMillis();

    StringWriter writer = new StringWriter();
    JsonGenerator gen = JsonFactory.jacksonFactory.createGenerator(writer);

    gen.writeStartObject();

    gen.writeStringField("id", jobVertex.getJobVertexId().toString());
    gen.writeStringField("name", jobVertex.getJobVertex().getName());
    gen.writeNumberField("now", now);

    gen.writeArrayFieldStart("taskmanagers");
    for (Entry<String, List<ExecutionVertex>> entry : taskManagerVertices.entrySet()) {
      String host = entry.getKey();
      List<ExecutionVertex> taskVertices = entry.getValue();

      int[] tasksPerState = new int[ExecutionState.values().length];

      long startTime = Long.MAX_VALUE;
      long endTime = 0;
      boolean allFinished = true;

      LongCounter tmReadBytes = new LongCounter();
      LongCounter tmWriteBytes = new LongCounter();
      LongCounter tmReadRecords = new LongCounter();
      LongCounter tmWriteRecords = new LongCounter();

      for (ExecutionVertex vertex : taskVertices) {
        final ExecutionState state = vertex.getExecutionState();
        tasksPerState[state.ordinal()]++;

        // take the earliest start time
        long started = vertex.getStateTimestamp(ExecutionState.DEPLOYING);
        if (started > 0) {
          startTime = Math.min(startTime, started);
        }

        allFinished &= state.isTerminal();
        endTime = Math.max(endTime, vertex.getStateTimestamp(state));

        Map<AccumulatorRegistry.Metric, Accumulator<?, ?>> metrics =
            vertex.getCurrentExecutionAttempt().getFlinkAccumulators();

        if (metrics != null) {
          LongCounter readBytes =
              (LongCounter) metrics.get(AccumulatorRegistry.Metric.NUM_BYTES_IN);
          tmReadBytes.merge(readBytes);

          LongCounter writeBytes =
              (LongCounter) metrics.get(AccumulatorRegistry.Metric.NUM_BYTES_OUT);
          tmWriteBytes.merge(writeBytes);

          LongCounter readRecords =
              (LongCounter) metrics.get(AccumulatorRegistry.Metric.NUM_RECORDS_IN);
          tmReadRecords.merge(readRecords);

          LongCounter writeRecords =
              (LongCounter) metrics.get(AccumulatorRegistry.Metric.NUM_RECORDS_OUT);
          tmWriteRecords.merge(writeRecords);
        }
      }

      long duration;
      if (startTime < Long.MAX_VALUE) {
        if (allFinished) {
          duration = endTime - startTime;
        } else {
          endTime = -1L;
          duration = now - startTime;
        }
      } else {
        startTime = -1L;
        endTime = -1L;
        duration = -1L;
      }

      ExecutionState jobVertexState =
          ExecutionJobVertex.getAggregateJobVertexState(tasksPerState, taskVertices.size());

      gen.writeStartObject();

      gen.writeStringField("host", host);
      gen.writeStringField("status", jobVertexState.name());

      gen.writeNumberField("start-time", startTime);
      gen.writeNumberField("end-time", endTime);
      gen.writeNumberField("duration", duration);

      gen.writeObjectFieldStart("metrics");
      gen.writeNumberField("read-bytes", tmReadBytes.getLocalValuePrimitive());
      gen.writeNumberField("write-bytes", tmWriteBytes.getLocalValuePrimitive());
      gen.writeNumberField("read-records", tmReadRecords.getLocalValuePrimitive());
      gen.writeNumberField("write-records", tmWriteRecords.getLocalValuePrimitive());
      gen.writeEndObject();

      gen.writeObjectFieldStart("status-counts");
      for (ExecutionState state : ExecutionState.values()) {
        gen.writeNumberField(state.name(), tasksPerState[state.ordinal()]);
      }
      gen.writeEndObject();

      gen.writeEndObject();
    }
    gen.writeEndArray();

    gen.writeEndObject();

    gen.close();
    return writer.toString();
  }
  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws IOException, ServletException {
    String sid = req.getParameter("sid");
    String gameId = req.getParameter("game");

    HanabiUser user = s.getUserBySession(sid);
    HanabiGame game = s.getGame(gameId);

    JsonGenerator out = new JsonFactory().createJsonGenerator(resp.getOutputStream());

    out.writeStartObject();
    out.writeStringField("id", gameId);
    out.writeStringField("nextEvent", Integer.toString(game.events.getNextId()));
    out.writeNumberField("drawPile", game.drawPile.size());
    out.writeNumberField("hintsLeft", game.hintsLeft);
    out.writeNumberField("errorsMade", game.errorsMade);
    out.writeNumberField("turnNumber", game.turn);
    out.writeStringField("activePlayer", Integer.toString(game.activeSeat));

    out.writeFieldName("seats");
    out.writeStartArray();
    for (int i = 0; i < game.seats.size(); i++) {
      HanabiGame.Seat seat = game.seats.get(i);
      out.writeStartObject();
      out.writeStringField("seat", Integer.toString(i));
      out.writeStringField("playerName", seat.user.name);
      if (seat.user == user) {
        out.writeBooleanField("isYou", true);
      }
      out.writeFieldName("hand");
      out.writeStartArray();
      for (HanabiGame.Card c : seat.hand) {
        if (seat.user == user) {
          out.writeString("unknown");
        } else {
          out.writeString(c.toString());
        }
      }
      out.writeEndArray();
      out.writeEndObject();
    }
    out.writeEndArray(); // end seats

    out.writeFieldName("piles");
    out.writeStartObject();
    for (int i = 0; i < HanabiGame.SUIT_COUNT; i++) {
      HanabiGame.Card c = game.getPileTopCard(i);
      if (c != null) {
        out.writeStringField(c.getSuitName(), c.toString());
      }
    }
    out.writeEndObject(); // end piles

    out.writeFieldName("discards");
    out.writeStartArray();
    for (HanabiGame.Card c : game.discards) {
      out.writeString(c.toString());
    }
    out.writeEndArray();

    out.writeFieldName("hints");
    out.writeStartArray();
    for (HanabiGame.Hint h : game.hints) {
      out.writeStartObject();
      out.writeNumberField("turn", h.whenGiven);
      out.writeStringField("from", Integer.toString(h.from));
      out.writeStringField("to", Integer.toString(h.to));
      out.writeStringField("hintType", h.type.name());
      out.writeStringField("hint", h.getHintString());

      HanabiGame.Seat targetSeat = game.seats.get(h.to);
      out.writeFieldName("applies");
      out.writeStartArray();
      for (int slot = 0; slot < targetSeat.hand.size(); slot++) {
        if (targetSeat.whenReceived.get(slot).intValue() > h.whenGiven) {
          out.writeString("");
        } else if (h.affirms(targetSeat.hand.get(slot))) {
          out.writeString("Y");
        } else {
          out.writeString("N");
        }
      }
      out.writeEndArray();
      out.writeEndObject();
    }
    out.writeEndArray(); // end hints

    out.writeEndObject(); // end game
    out.close();
  }
  @Override
  public String handleJsonRequest(
      Map<String, String> pathParams, Map<String, String> queryParams, ActorGateway jobManager)
      throws Exception {
    try {
      if (jobManager != null) {
        // whether one task manager's metrics are requested, or all task manager, we
        // return them in an array. This avoids unnecessary code complexity.
        // If only one task manager is requested, we only fetch one task manager metrics.
        final List<Instance> instances = new ArrayList<>();
        if (pathParams.containsKey(TASK_MANAGER_ID_KEY)) {
          try {
            InstanceID instanceID =
                new InstanceID(StringUtils.hexStringToByte(pathParams.get(TASK_MANAGER_ID_KEY)));
            Future<Object> future =
                jobManager.ask(
                    new JobManagerMessages.RequestTaskManagerInstance(instanceID), timeout);
            TaskManagerInstance instance = (TaskManagerInstance) Await.result(future, timeout);
            if (instance.instance().nonEmpty()) {
              instances.add(instance.instance().get());
            }
          }
          // this means the id string was invalid. Keep the list empty.
          catch (IllegalArgumentException e) {
            // do nothing.
          }
        } else {
          Future<Object> future =
              jobManager.ask(JobManagerMessages.getRequestRegisteredTaskManagers(), timeout);
          RegisteredTaskManagers taskManagers =
              (RegisteredTaskManagers) Await.result(future, timeout);
          instances.addAll(taskManagers.asJavaCollection());
        }

        StringWriter writer = new StringWriter();
        JsonGenerator gen = JsonFactory.jacksonFactory.createGenerator(writer);

        gen.writeStartObject();
        gen.writeArrayFieldStart("taskmanagers");

        for (Instance instance : instances) {
          gen.writeStartObject();
          gen.writeStringField("id", instance.getId().toString());
          gen.writeStringField("path", instance.getActorGateway().path());
          gen.writeNumberField("dataPort", instance.getTaskManagerLocation().dataPort());
          gen.writeNumberField("timeSinceLastHeartbeat", instance.getLastHeartBeat());
          gen.writeNumberField("slotsNumber", instance.getTotalNumberOfSlots());
          gen.writeNumberField("freeSlots", instance.getNumberOfAvailableSlots());
          gen.writeNumberField("cpuCores", instance.getResources().getNumberOfCPUCores());
          gen.writeNumberField("physicalMemory", instance.getResources().getSizeOfPhysicalMemory());
          gen.writeNumberField("freeMemory", instance.getResources().getSizeOfJvmHeap());
          gen.writeNumberField("managedMemory", instance.getResources().getSizeOfManagedMemory());

          // only send metrics when only one task manager requests them.
          if (pathParams.containsKey(TASK_MANAGER_ID_KEY)) {
            byte[] report = instance.getLastMetricsReport();
            if (report != null) {
              gen.writeFieldName("metrics");
              gen.writeRawValue(new String(report, "utf-8"));
            }
          }

          gen.writeEndObject();
        }

        gen.writeEndArray();
        gen.writeEndObject();

        gen.close();
        return writer.toString();
      } else {
        throw new Exception("No connection to the leading JobManager.");
      }
    } catch (Exception e) {
      throw new RuntimeException("Failed to fetch list of all task managers: " + e.getMessage(), e);
    }
  }
    private String getCommandJson(final HystrixCommandMetrics commandMetrics) throws IOException {
      HystrixCommandKey key = commandMetrics.getCommandKey();
      HystrixCircuitBreaker circuitBreaker = HystrixCircuitBreaker.Factory.getInstance(key);

      StringWriter jsonString = new StringWriter();
      JsonGenerator json = jsonFactory.createGenerator(jsonString);

      json.writeStartObject();
      json.writeStringField("type", "HystrixCommand");
      json.writeStringField("name", key.name());
      json.writeStringField("group", commandMetrics.getCommandGroup().name());
      json.writeNumberField("currentTime", System.currentTimeMillis());

      // circuit breaker
      if (circuitBreaker == null) {
        // circuit breaker is disabled and thus never open
        json.writeBooleanField("isCircuitBreakerOpen", false);
      } else {
        json.writeBooleanField("isCircuitBreakerOpen", circuitBreaker.isOpen());
      }
      HealthCounts healthCounts = commandMetrics.getHealthCounts();
      json.writeNumberField("errorPercentage", healthCounts.getErrorPercentage());
      json.writeNumberField("errorCount", healthCounts.getErrorCount());
      json.writeNumberField("requestCount", healthCounts.getTotalRequests());

      // rolling counters
      safelyWriteNumberField(
          json,
          "rollingCountBadRequests",
          new Func0<Long>() {
            @Override
            public Long call() {
              return commandMetrics.getRollingCount(HystrixEventType.BAD_REQUEST);
            }
          });
      safelyWriteNumberField(
          json,
          "rollingCountCollapsedRequests",
          new Func0<Long>() {
            @Override
            public Long call() {
              return commandMetrics.getRollingCount(HystrixEventType.COLLAPSED);
            }
          });
      safelyWriteNumberField(
          json,
          "rollingCountEmit",
          new Func0<Long>() {
            @Override
            public Long call() {
              return commandMetrics.getRollingCount(HystrixEventType.EMIT);
            }
          });
      safelyWriteNumberField(
          json,
          "rollingCountExceptionsThrown",
          new Func0<Long>() {
            @Override
            public Long call() {
              return commandMetrics.getRollingCount(HystrixEventType.EXCEPTION_THROWN);
            }
          });
      safelyWriteNumberField(
          json,
          "rollingCountFailure",
          new Func0<Long>() {
            @Override
            public Long call() {
              return commandMetrics.getRollingCount(HystrixEventType.FAILURE);
            }
          });
      safelyWriteNumberField(
          json,
          "rollingCountFallbackEmit",
          new Func0<Long>() {
            @Override
            public Long call() {
              return commandMetrics.getRollingCount(HystrixEventType.FALLBACK_EMIT);
            }
          });
      safelyWriteNumberField(
          json,
          "rollingCountFallbackFailure",
          new Func0<Long>() {
            @Override
            public Long call() {
              return commandMetrics.getRollingCount(HystrixEventType.FALLBACK_FAILURE);
            }
          });
      safelyWriteNumberField(
          json,
          "rollingCountFallbackMissing",
          new Func0<Long>() {
            @Override
            public Long call() {
              return commandMetrics.getRollingCount(HystrixEventType.FALLBACK_MISSING);
            }
          });
      safelyWriteNumberField(
          json,
          "rollingCountFallbackRejection",
          new Func0<Long>() {
            @Override
            public Long call() {
              return commandMetrics.getRollingCount(HystrixEventType.FALLBACK_REJECTION);
            }
          });
      safelyWriteNumberField(
          json,
          "rollingCountFallbackSuccess",
          new Func0<Long>() {
            @Override
            public Long call() {
              return commandMetrics.getRollingCount(HystrixEventType.FALLBACK_SUCCESS);
            }
          });
      safelyWriteNumberField(
          json,
          "rollingCountResponsesFromCache",
          new Func0<Long>() {
            @Override
            public Long call() {
              return commandMetrics.getRollingCount(HystrixEventType.RESPONSE_FROM_CACHE);
            }
          });
      safelyWriteNumberField(
          json,
          "rollingCountSemaphoreRejected",
          new Func0<Long>() {
            @Override
            public Long call() {
              return commandMetrics.getRollingCount(HystrixEventType.SEMAPHORE_REJECTED);
            }
          });
      safelyWriteNumberField(
          json,
          "rollingCountShortCircuited",
          new Func0<Long>() {
            @Override
            public Long call() {
              return commandMetrics.getRollingCount(HystrixEventType.SHORT_CIRCUITED);
            }
          });
      safelyWriteNumberField(
          json,
          "rollingCountSuccess",
          new Func0<Long>() {
            @Override
            public Long call() {
              return commandMetrics.getRollingCount(HystrixEventType.SUCCESS);
            }
          });
      safelyWriteNumberField(
          json,
          "rollingCountThreadPoolRejected",
          new Func0<Long>() {
            @Override
            public Long call() {
              return commandMetrics.getRollingCount(HystrixEventType.THREAD_POOL_REJECTED);
            }
          });
      safelyWriteNumberField(
          json,
          "rollingCountTimeout",
          new Func0<Long>() {
            @Override
            public Long call() {
              return commandMetrics.getRollingCount(HystrixEventType.TIMEOUT);
            }
          });

      json.writeNumberField(
          "currentConcurrentExecutionCount", commandMetrics.getCurrentConcurrentExecutionCount());
      json.writeNumberField(
          "rollingMaxConcurrentExecutionCount", commandMetrics.getRollingMaxConcurrentExecutions());

      // latency percentiles
      json.writeNumberField("latencyExecute_mean", commandMetrics.getExecutionTimeMean());
      json.writeObjectFieldStart("latencyExecute");
      json.writeNumberField("0", commandMetrics.getExecutionTimePercentile(0));
      json.writeNumberField("25", commandMetrics.getExecutionTimePercentile(25));
      json.writeNumberField("50", commandMetrics.getExecutionTimePercentile(50));
      json.writeNumberField("75", commandMetrics.getExecutionTimePercentile(75));
      json.writeNumberField("90", commandMetrics.getExecutionTimePercentile(90));
      json.writeNumberField("95", commandMetrics.getExecutionTimePercentile(95));
      json.writeNumberField("99", commandMetrics.getExecutionTimePercentile(99));
      json.writeNumberField("99.5", commandMetrics.getExecutionTimePercentile(99.5));
      json.writeNumberField("100", commandMetrics.getExecutionTimePercentile(100));
      json.writeEndObject();
      //
      json.writeNumberField("latencyTotal_mean", commandMetrics.getTotalTimeMean());
      json.writeObjectFieldStart("latencyTotal");
      json.writeNumberField("0", commandMetrics.getTotalTimePercentile(0));
      json.writeNumberField("25", commandMetrics.getTotalTimePercentile(25));
      json.writeNumberField("50", commandMetrics.getTotalTimePercentile(50));
      json.writeNumberField("75", commandMetrics.getTotalTimePercentile(75));
      json.writeNumberField("90", commandMetrics.getTotalTimePercentile(90));
      json.writeNumberField("95", commandMetrics.getTotalTimePercentile(95));
      json.writeNumberField("99", commandMetrics.getTotalTimePercentile(99));
      json.writeNumberField("99.5", commandMetrics.getTotalTimePercentile(99.5));
      json.writeNumberField("100", commandMetrics.getTotalTimePercentile(100));
      json.writeEndObject();

      // property values for reporting what is actually seen by the command rather than what was set
      // somewhere
      HystrixCommandProperties commandProperties = commandMetrics.getProperties();

      json.writeNumberField(
          "propertyValue_circuitBreakerRequestVolumeThreshold",
          commandProperties.circuitBreakerRequestVolumeThreshold().get());
      json.writeNumberField(
          "propertyValue_circuitBreakerSleepWindowInMilliseconds",
          commandProperties.circuitBreakerSleepWindowInMilliseconds().get());
      json.writeNumberField(
          "propertyValue_circuitBreakerErrorThresholdPercentage",
          commandProperties.circuitBreakerErrorThresholdPercentage().get());
      json.writeBooleanField(
          "propertyValue_circuitBreakerForceOpen",
          commandProperties.circuitBreakerForceOpen().get());
      json.writeBooleanField(
          "propertyValue_circuitBreakerForceClosed",
          commandProperties.circuitBreakerForceClosed().get());
      json.writeBooleanField(
          "propertyValue_circuitBreakerEnabled", commandProperties.circuitBreakerEnabled().get());

      json.writeStringField(
          "propertyValue_executionIsolationStrategy",
          commandProperties.executionIsolationStrategy().get().name());
      json.writeNumberField(
          "propertyValue_executionIsolationThreadTimeoutInMilliseconds",
          commandProperties.executionTimeoutInMilliseconds().get());
      json.writeNumberField(
          "propertyValue_executionTimeoutInMilliseconds",
          commandProperties.executionTimeoutInMilliseconds().get());
      json.writeBooleanField(
          "propertyValue_executionIsolationThreadInterruptOnTimeout",
          commandProperties.executionIsolationThreadInterruptOnTimeout().get());
      json.writeStringField(
          "propertyValue_executionIsolationThreadPoolKeyOverride",
          commandProperties.executionIsolationThreadPoolKeyOverride().get());
      json.writeNumberField(
          "propertyValue_executionIsolationSemaphoreMaxConcurrentRequests",
          commandProperties.executionIsolationSemaphoreMaxConcurrentRequests().get());
      json.writeNumberField(
          "propertyValue_fallbackIsolationSemaphoreMaxConcurrentRequests",
          commandProperties.fallbackIsolationSemaphoreMaxConcurrentRequests().get());

      /*
       * The following are commented out as these rarely change and are verbose for streaming for something people don't change.
       * We could perhaps allow a property or request argument to include these.
       */

      //                    json.put("propertyValue_metricsRollingPercentileEnabled",
      // commandProperties.metricsRollingPercentileEnabled().get());
      //                    json.put("propertyValue_metricsRollingPercentileBucketSize",
      // commandProperties.metricsRollingPercentileBucketSize().get());
      //                    json.put("propertyValue_metricsRollingPercentileWindow",
      // commandProperties.metricsRollingPercentileWindowInMilliseconds().get());
      //                    json.put("propertyValue_metricsRollingPercentileWindowBuckets",
      // commandProperties.metricsRollingPercentileWindowBuckets().get());
      //                    json.put("propertyValue_metricsRollingStatisticalWindowBuckets",
      // commandProperties.metricsRollingStatisticalWindowBuckets().get());
      json.writeNumberField(
          "propertyValue_metricsRollingStatisticalWindowInMilliseconds",
          commandProperties.metricsRollingStatisticalWindowInMilliseconds().get());

      json.writeBooleanField(
          "propertyValue_requestCacheEnabled", commandProperties.requestCacheEnabled().get());
      json.writeBooleanField(
          "propertyValue_requestLogEnabled", commandProperties.requestLogEnabled().get());

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

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

      return jsonString.getBuffer().toString();
    }
    private String getCollapserJson(final HystrixCollapserMetrics collapserMetrics)
        throws IOException {
      HystrixCollapserKey key = collapserMetrics.getCollapserKey();
      StringWriter jsonString = new StringWriter();
      JsonGenerator json = jsonFactory.createJsonGenerator(jsonString);
      json.writeStartObject();

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

      safelyWriteNumberField(
          json,
          "rollingCountRequestsBatched",
          new Func0<Long>() {
            @Override
            public Long call() {
              return collapserMetrics.getRollingCount(HystrixEventType.Collapser.ADDED_TO_BATCH);
            }
          });
      safelyWriteNumberField(
          json,
          "rollingCountBatches",
          new Func0<Long>() {
            @Override
            public Long call() {
              return collapserMetrics.getRollingCount(HystrixEventType.Collapser.BATCH_EXECUTED);
            }
          });
      safelyWriteNumberField(
          json,
          "rollingCountResponsesFromCache",
          new Func0<Long>() {
            @Override
            public Long call() {
              return collapserMetrics.getRollingCount(
                  HystrixEventType.Collapser.RESPONSE_FROM_CACHE);
            }
          });

      // batch size percentiles
      json.writeNumberField("batchSize_mean", collapserMetrics.getBatchSizeMean());
      json.writeObjectFieldStart("batchSize");
      json.writeNumberField("25", collapserMetrics.getBatchSizePercentile(25));
      json.writeNumberField("50", collapserMetrics.getBatchSizePercentile(50));
      json.writeNumberField("75", collapserMetrics.getBatchSizePercentile(75));
      json.writeNumberField("90", collapserMetrics.getBatchSizePercentile(90));
      json.writeNumberField("95", collapserMetrics.getBatchSizePercentile(95));
      json.writeNumberField("99", collapserMetrics.getBatchSizePercentile(99));
      json.writeNumberField("99.5", collapserMetrics.getBatchSizePercentile(99.5));
      json.writeNumberField("100", collapserMetrics.getBatchSizePercentile(100));
      json.writeEndObject();

      // shard size percentiles (commented-out for now)
      // json.writeNumberField("shardSize_mean", collapserMetrics.getShardSizeMean());
      // json.writeObjectFieldStart("shardSize");
      // json.writeNumberField("25", collapserMetrics.getShardSizePercentile(25));
      // json.writeNumberField("50", collapserMetrics.getShardSizePercentile(50));
      // json.writeNumberField("75", collapserMetrics.getShardSizePercentile(75));
      // json.writeNumberField("90", collapserMetrics.getShardSizePercentile(90));
      // json.writeNumberField("95", collapserMetrics.getShardSizePercentile(95));
      // json.writeNumberField("99", collapserMetrics.getShardSizePercentile(99));
      // json.writeNumberField("99.5", collapserMetrics.getShardSizePercentile(99.5));
      // json.writeNumberField("100", collapserMetrics.getShardSizePercentile(100));
      // json.writeEndObject();

      // json.writeNumberField("propertyValue_metricsRollingStatisticalWindowInMilliseconds",
      // collapserMetrics.getProperties().metricsRollingStatisticalWindowInMilliseconds().get());
      json.writeBooleanField(
          "propertyValue_requestCacheEnabled",
          collapserMetrics.getProperties().requestCacheEnabled().get());
      json.writeNumberField(
          "propertyValue_maxRequestsInBatch",
          collapserMetrics.getProperties().maxRequestsInBatch().get());
      json.writeNumberField(
          "propertyValue_timerDelayInMilliseconds",
          collapserMetrics.getProperties().timerDelayInMilliseconds().get());

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

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

      return jsonString.getBuffer().toString();
    }
    private String getThreadPoolJson(final 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());
      safelyWriteNumberField(
          json,
          "rollingCountThreadsExecuted",
          new Func0<Long>() {
            @Override
            public Long call() {
              return threadPoolMetrics.getRollingCount(HystrixEventType.ThreadPool.EXECUTED);
            }
          });
      json.writeNumberField(
          "rollingMaxActiveThreads", threadPoolMetrics.getRollingMaxActiveThreads());
      safelyWriteNumberField(
          json,
          "rollingCountCommandRejections",
          new Func0<Long>() {
            @Override
            public Long call() {
              return threadPoolMetrics.getRollingCount(HystrixEventType.ThreadPool.REJECTED);
            }
          });

      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();
    }