com.eclipsesource.json.Json类的使用及代码示例

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

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

Json介绍

[英]This class serves as the entry point to the minimal-json API.

To parse a given JSON input, use the parse() methods like in this example:

JsonObject object = Json.parse(string).asObject();

To create a JSON data structure to be serialized, use the methods value(), array(), and object(). For example, the following snippet will produce the JSON string {"foo": 23, "bar": true}:

String string = Json.object().add("foo", 23).add("bar", true).toString();

To create a JSON array from a given Java array, you can use one of the array() methods with varargs parameters:

String[] names = ... 
JsonArray array = Json.array(names);

[中]此类充当最小json API的入口点。
要解析给定的JSON输入,请使用以下示例中的parse()方法:

JsonObject object = Json.parse(string).asObject();

要创建要序列化的JSON数据结构,请使用方法value()array()object()。例如,以下代码段将生成JSON字符串*{“foo”:23,“bar”:true}*:

String string = Json.object().add("foo", 23).add("bar", true).toString();

要从给定的Java数组创建JSON数组,可以使用带有varargs参数的array()方法之一:

String[] names = ... 
JsonArray array = Json.array(names);

代码示例

代码示例来源: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

private static JsonValue extractSimpleName(JsonObject caliperResults) {
 String name = caliperResults.get("run").asObject().get("benchmarkName").asString();
 return Json.value(name.replaceFirst(".*\\.", ""));
}

代码示例来源:origin: wnameless/json-flattener

private void setUnflattenedValue(JsonObject flattened, String key,
  JsonValue currentVal, String objKey, Integer aryIdx) {
 JsonValue val = flattened.get(key);
 if (objKey != null) {
  if (val.isArray()) {
   JsonValue jsonArray = Json.array();
   for (JsonValue arrayVal : val.asArray()) {
    jsonArray.asArray().add(
      Json.parse(newJsonUnflattener(arrayVal.toString()).unflatten()));
   }
   currentVal.asObject().add(objKey, jsonArray);
  } else {
   currentVal.asObject().add(objKey, val);
  }
 } else { // aryIdx != null
  assureJsonArraySize(currentVal.asArray(), aryIdx);
  currentVal.asArray().set(aryIdx, val);
 }
}

代码示例来源:origin: ZencashOfficial/zencash-swing-wallet-ui

public static JsonObject parseJsonObject(Reader r)
  throws IOException
{
  try
  {
    return Json.parse(r).asObject();
  } catch (RuntimeException rte)
  {
    throw new IOException(rte);
  }
}

代码示例来源: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: com.github.wnameless/json-flattener

private JsonValue findOrCreateJsonObject(JsonValue currentVal, String objKey,
  Integer aryIdx) {
 if (objKey != null) {
  JsonObject jsonObj = currentVal.asObject();
  if (jsonObj.get(objKey) == null) {
   JsonValue obj = Json.object();
   jsonObj.add(objKey, obj);
   return obj;
  }
  return jsonObj.get(objKey);
 } else { // aryIdx != null
  JsonArray jsonAry = currentVal.asArray();
  if (jsonAry.size() <= aryIdx || jsonAry.get(aryIdx).equals(Json.NULL)) {
   JsonValue obj = Json.object();
   assureJsonArraySize(jsonAry, aryIdx);
   jsonAry.set(aryIdx, obj);
   return obj;
  }
  return jsonAry.get(aryIdx);
 }
}

代码示例来源:origin: com.github.wnameless/json-flattener

private JsonValue findOrCreateJsonArray(JsonValue currentVal, String objKey,
  Integer aryIdx) {
 if (objKey != null) {
  JsonObject jsonObj = currentVal.asObject();
  if (jsonObj.get(objKey) == null) {
   JsonValue ary = Json.array();
   jsonObj.add(objKey, ary);
   return ary;
  }
  return jsonObj.get(objKey);
 } else { // aryIdx != null
  JsonArray jsonAry = currentVal.asArray();
  if (jsonAry.size() <= aryIdx || jsonAry.get(aryIdx).equals(Json.NULL)) {
   JsonValue ary = Json.array();
   assureJsonArraySize(jsonAry, aryIdx);
   jsonAry.set(aryIdx, ary);
   return ary;
  }
  return jsonAry.get(aryIdx);
 }
}

代码示例来源:origin: io.thorntail/fraction-metadata

@SuppressWarnings({"unchecked", "rawtypes"})
public FractionListParser(InputStream fractionListJson) throws IOException {
  try (InputStreamReader reader = new InputStreamReader(fractionListJson)) {
    Json.parse(reader).asArray().forEach(entry -> {
      JsonObject fraction = entry.asObject();
      FractionDescriptor fd = getFractionDescriptor(fraction);
      addDependencies(fraction, fd);
    });
  }
}

代码示例来源:origin: com.github.wnameless/json-flattener

private JsonArray unflattenArray(JsonArray array) {
 JsonArray unflattenArray = Json.array().asArray();
 for (JsonValue value : array) {
  if (value.isArray()) {
   unflattenArray.add(unflattenArray(value.asArray()));
  } else if (value.isObject()) {
   unflattenArray.add(Json.parse(new JsonUnflattener(value.toString())
     .withSeparator(separator).unflatten()));
  } else {
   unflattenArray.add(value);
  }
 }
 return unflattenArray;
}

代码示例来源:origin: UniversaBlockchain/universa

@Test
public void testPreRequisites() throws Exception {
  JsonObject object = Json.parse("{\"name\":\"test\"}").asObject();
  String name = object.get("name").asString();
  assertEquals("test", name);
}

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

@Override
public Users minimaljson(Reader reader) throws IOException {
  com.eclipsesource.json.JsonObject jso = com.eclipsesource.json.Json.parse(reader).asObject();
  com.eclipsesource.json.JsonValue v;
  Users uc = new Users();
  if ((v = jso.get("users")) != null) {
    com.eclipsesource.json.JsonArray jsarr = v.asArray();
    uc.users = new ArrayList<>();
    for (com.eclipsesource.json.JsonValue vi : jsarr) {
      uc.users.add(minimaljsonUser(vi.asObject()));
    }
  }
  return uc;
}

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

@Override
public Object readFromString(String string) throws IOException {
 return Json.parse(string);
}

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

private void createJsonFile() throws IOException {
 JsonObject caliperJson = Json.parse(readFromFile(resultsFile)).asObject();
 String resultsJson = new CaliperResultsPreprocessor(caliperJson).getResults().toString();
 writeToFile(resultsJson, resultsFile);
}

代码示例来源: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: de.ruedigermoeller/kontraktor-http

public static String getVersion(String nodeModulePath) throws IOException {
  File pack = new File(nodeModulePath,"package.json");
  if ( pack.exists() ) {
    String version = null;
    JsonObject pjson = Json.parse(new FileReader(pack)).asObject();
    return pjson.getString("version", null);
  }
  return null;
}

代码示例来源: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;
}

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

@Override
public JsonObject jSerialize(X509Certificate certificate) {
  final JsonObject o = Json.object();
  // add pubkey info
  o.add("pubkey", publicKeySerDes.jSerialize(certificate.getPublicKey()));
  // Get certificate (DER encoding)
  try {
    o.add("b64Der", Base64.encodeBase64String(certificate.getEncoded()));
  } catch (CertificateEncodingException e) {
    throw new RuntimeException(e);
  }
  return o;
}

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

private static com.eclipsesource.json.JsonArray tryToReadMinimalJsonArrayFrom(final String jsonString) {
  try {
    final com.eclipsesource.json.JsonValue parsedJsonString = Json.parse(jsonString);
    return parsedJsonString.asArray();
  } catch (final ParseException | UnsupportedOperationException | StackOverflowError e) {
    throw JsonParseException.newBuilder()
        .message("Failed to create JSON array from string!")
        .cause(e)
        .build();
  }
}

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

public static byte[] serialize(Observation obs) {
  JsonObject o = Json.object();
  o.set("request", Hex.encodeHexString(serializer.serializeRequest(obs.getRequest()).bytes));
  if (obs.getContext() != null)
    o.set("peer", EndpointContextSerDes.serialize(obs.getContext()));
  else
    o.set("peer", EndpointContextSerDes.serialize(obs.getRequest().getDestinationContext()));
  if (obs.getRequest().getUserContext() != null) {
    JsonObject ctxObject = Json.object();
    for (Entry<String, String> e : obs.getRequest().getUserContext().entrySet()) {
      ctxObject.set(e.getKey(), e.getValue());
    }
    o.set("context", ctxObject);
  }
  return o.toString().getBytes();
}

代码示例来源:origin: dernasherbrezon/r2cloud

@Override
public ModelAndView doPost(IHTTPSession session) {
  JsonValue request = Json.parse(WebServer.getRequestBody(session));
  if (!request.isObject()) {
    return new BadRequest("expected object");
  }
  String username = ((JsonObject) request).getString("username", null);
  String password = ((JsonObject) request).getString("password", null);
  return doLogin(auth, username, password);
}

相关文章

微信公众号

最新文章

更多