com.eclipsesource.json.JsonObject.add()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(11.1k)|赞(0)|评价(0)|浏览(153)

本文整理了Java中com.eclipsesource.json.JsonObject.add()方法的一些代码示例,展示了JsonObject.add()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JsonObject.add()方法的具体详情如下:
包路径:com.eclipsesource.json.JsonObject
类名称:JsonObject
方法名:add

JsonObject.add介绍

[英]Appends a new member to the end of this object, with the specified name and the JSON representation of the specified double value.

This method does not prevent duplicate names. Calling this method with a name that already exists in the object will append another member with the same name. In order to replace existing members, use the method set(name, value) instead. However, add is much faster than set (because it does not need to search for existing members). Therefore add should be preferred when constructing new objects.
[中]使用指定的名称和指定的double值的JSON表示形式将新成员追加到此对象的末尾。
此方法不防止名称重复。使用对象中已存在的名称调用此方法将附加具有相同名称的另一个成员。要替换现有成员,请改用set(name, value)方法。但是,addset快得多(因为它不需要搜索现有成员)。因此,在构造新对象时,应首选“添加”。

代码示例

代码示例来源:origin: Vedenin/useful-java-links

public static void main(String[] args) throws IOException {
    // convert Java to writer
    JsonObject root = Json.object().add("message", "Hi").add(
        "place", Json.object().add("name", "World!")
    );
    StringWriter writer = new StringWriter();
    root.writeTo(writer);
    String json = writer.toString();
    System.out.println(json);

    System.out.println();
    // convert writer to Java
    JsonObject obj = Json.parse(json).asObject();
    String message = obj.get("message").asString();
    String name = obj.get("place").asObject().get("name").asString();
    System.out.println(message + " " + name);
  }
}

代码示例来源:origin: ralfstx/minimal-json

@Override
public void endObjectValue(JsonObject object, String name) {
 object.add(name, value);
}

代码示例来源:origin: ralfstx/minimal-json

@Override
protected void setUp() throws IOException {
 jsonObject = new JsonObject();
 for (int index = 0; index < size; index++) {
  String name = Integer.toHexString(index);
  jsonObject.add(name, index);
 }
}

代码示例来源:origin: ralfstx/minimal-json

private static JsonObject transformResults(JsonObject caliperResults) {
 return new JsonObject().add("name", extractSimpleName(caliperResults))
             .add("details", extractEnvironment(caliperResults))
             .add("measurements", extractMeasurements(caliperResults));
}

代码示例来源:origin: ralfstx/minimal-json

/**
 * Appends a new member to the end of this object, with the specified name and the JSON
 * representation of the specified <code>boolean</code> value.
 * <p>
 * This method <strong>does not prevent duplicate names</strong>. Calling this method with a name
 * that already exists in the object will append another member with the same name. In order to
 * replace existing members, use the method <code>set(name, value)</code> instead. However,
 * <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to
 * search for existing members). Therefore <em>add</em> should be preferred when constructing new
 * objects.
 * </p>
 *
 * @param name
 *          the name of the member to add
 * @param value
 *          the value of the member to add
 * @return the object itself, to enable method chaining
 */
public JsonObject add(String name, boolean value) {
 add(name, Json.value(value));
 return this;
}

代码示例来源:origin: ralfstx/minimal-json

/**
 * Appends a new member to the end of this object, with the specified name and the JSON
 * representation of the specified string.
 * <p>
 * This method <strong>does not prevent duplicate names</strong>. Calling this method with a name
 * that already exists in the object will append another member with the same name. In order to
 * replace existing members, use the method <code>set(name, value)</code> instead. However,
 * <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to
 * search for existing members). Therefore <em>add</em> should be preferred when constructing new
 * objects.
 * </p>
 *
 * @param name
 *          the name of the member to add
 * @param value
 *          the value of the member to add
 * @return the object itself, to enable method chaining
 */
public JsonObject add(String name, String value) {
 add(name, Json.value(value));
 return this;
}

代码示例来源:origin: ralfstx/minimal-json

/**
 * Appends a new member to the end of this object, with the specified name and the JSON
 * representation of the specified <code>double</code> value.
 * <p>
 * This method <strong>does not prevent duplicate names</strong>. Calling this method with a name
 * that already exists in the object will append another member with the same name. In order to
 * replace existing members, use the method <code>set(name, value)</code> instead. However,
 * <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to
 * search for existing members). Therefore <em>add</em> should be preferred when constructing new
 * objects.
 * </p>
 *
 * @param name
 *          the name of the member to add
 * @param value
 *          the value of the member to add
 * @return the object itself, to enable method chaining
 */
public JsonObject add(String name, double value) {
 add(name, Json.value(value));
 return this;
}

代码示例来源:origin: ralfstx/minimal-json

/**
 * Appends a new member to the end of this object, with the specified name and the JSON
 * representation of the specified <code>long</code> value.
 * <p>
 * This method <strong>does not prevent duplicate names</strong>. Calling this method with a name
 * that already exists in the object will append another member with the same name. In order to
 * replace existing members, use the method <code>set(name, value)</code> instead. However,
 * <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to
 * search for existing members). Therefore <em>add</em> should be preferred when constructing new
 * objects.
 * </p>
 *
 * @param name
 *          the name of the member to add
 * @param value
 *          the value of the member to add
 * @return the object itself, to enable method chaining
 */
public JsonObject add(String name, long value) {
 add(name, Json.value(value));
 return this;
}

代码示例来源:origin: ralfstx/minimal-json

/**
 * Appends a new member to the end of this object, with the specified name and the JSON
 * representation of the specified <code>int</code> value.
 * <p>
 * This method <strong>does not prevent duplicate names</strong>. Calling this method with a name
 * that already exists in the object will append another member with the same name. In order to
 * replace existing members, use the method <code>set(name, value)</code> instead. However,
 * <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to
 * search for existing members). Therefore <em>add</em> should be preferred when constructing new
 * objects.
 * </p>
 *
 * @param name
 *          the name of the member to add
 * @param value
 *          the value of the member to add
 * @return the object itself, to enable method chaining
 */
public JsonObject add(String name, int value) {
 add(name, Json.value(value));
 return this;
}

代码示例来源:origin: ralfstx/minimal-json

/**
 * Appends a new member to the end of this object, with the specified name and the JSON
 * representation of the specified <code>float</code> value.
 * <p>
 * This method <strong>does not prevent duplicate names</strong>. Calling this method with a name
 * that already exists in the object will append another member with the same name. In order to
 * replace existing members, use the method <code>set(name, value)</code> instead. However,
 * <strong> <em>add</em> is much faster than <em>set</em></strong> (because it does not need to
 * search for existing members). Therefore <em>add</em> should be preferred when constructing new
 * objects.
 * </p>
 *
 * @param name
 *          the name of the member to add
 * @param value
 *          the value of the member to add
 * @return the object itself, to enable method chaining
 */
public JsonObject add(String name, float value) {
 add(name, Json.value(value));
 return this;
}

代码示例来源:origin: ralfstx/minimal-json

private static JsonValue extractEnvironment(JsonObject caliperResults) {
 JsonObject details = caliperResults.get("environment").asObject().get("propertyMap").asObject();
 details.add("benchmark.classname", extractBenchmarkName(caliperResults));
 details.add("benchmark.executionTime", extractTimestamp(caliperResults));
 return details;
}

代码示例来源:origin: ralfstx/minimal-json

private static JsonObject extractMeasurement(JsonObject measurement) {
 JsonObject times = measurement.get("v").asObject()
                .get("measurementSetMap").asObject()
                .get("TIME").asObject();
 return new JsonObject().add("variables", measurement.get("k").asObject().get("variables"))
             .add("units", times.get("unitNames"))
             .add("values", extractTimes(times.get("measurements").asArray()));
}

代码示例来源:origin: Netflix/spectator

private JsonObject toJson(Map<String, String> tags) {
 final JsonObject obj = new JsonObject();
 for (Map.Entry<String, String> entry : tags.entrySet()) {
  obj.add(entry.getKey(), entry.getValue());
 }
 return obj;
}

代码示例来源:origin: eclipse/leshan

@Override
public void visit(WriteRequest request) {
  o.add("kind", "write");
  o.add("contentFormat", request.getContentFormat().getCode());
  o.add("mode", request.isPartialUpdateRequest() ? "UPDATE" : "REPLACE");
  o.add("node", LwM2mNodeSerDes.jSerialize(request.getNode()));
}

代码示例来源:origin: org.eclipse.leshan/leshan-server-cluster

@Override
  public void visit(ReadRequest request) {
    o.add("kind", "read");
    if (request.getContentFormat() != null)
      o.add("contentFormat", request.getContentFormat().getCode());
  }
});

代码示例来源:origin: eclipse/leshan

private void sendError(String ticket, String message) {
  try (Jedis j = pool.getResource()) {
    JsonObject m = Json.object();
    m.add("ticket", ticket);
    JsonObject err = Json.object();
    err.add("errorMessage", message);
    m.add("err", err);
    j.publish(RESPONSE_CHANNEL, m.toString());
  }
}

代码示例来源:origin: eclipse/leshan

@Override
public void visit(CreateRequest request) {
  o.add("kind", "create");
  o.add("contentFormat", request.getContentFormat().getCode());
  if (request.getInstanceId() != null)
    o.add("instanceId", request.getInstanceId());
  JsonArray resources = new JsonArray();
  for (LwM2mResource resource : request.getResources()) {
    resources.add(LwM2mNodeSerDes.jSerialize(resource));
  }
  o.add("resources", resources);
}

代码示例来源:origin: org.eclipse.ditto/ditto-json

@Override
protected String createStringRepresentation() {
  final com.eclipsesource.json.JsonObject minJsonObject = new com.eclipsesource.json.JsonObject();
  fields.values().forEach(field -> minJsonObject.add(field.getKeyName(), JsonFactory.convert(field.getValue())));
  return minJsonObject.toString();
}

代码示例来源:origin: eclipse/leshan

private void sendResponse(String ticket, LwM2mResponse response) {
  if (response instanceof ObserveResponse) {
    Observation observation = ((ObserveResponse) response).getObservation();
    observatioIdToTicket.put(new KeyId(observation.getId()), ticket);
  }
  try (Jedis j = pool.getResource()) {
    JsonObject m = Json.object();
    m.add("ticket", ticket);
    m.add("rep", ResponseSerDes.jSerialize(response));
    j.publish(RESPONSE_CHANNEL, m.toString());
  }
}

代码示例来源:origin: fabienrenaud/java-json-benchmark

@Override
public com.eclipsesource.json.JsonValue minimaljson(Users obj) throws IOException {
  com.eclipsesource.json.JsonObject jso = com.eclipsesource.json.Json.object();
  if (obj.users != null) {
    com.eclipsesource.json.JsonArray jsarr = (com.eclipsesource.json.JsonArray) com.eclipsesource.json.Json.array();
    for (User u : obj.users) {
      jsarr.add(minimaljson(u));
    }
    jso.add("users", jsarr);
  }
  return jso;
}

相关文章