在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:ralfstx/minimal-json开源软件地址:https://github.com/ralfstx/minimal-json开源编程语言:Java 93.9%开源软件介绍:minimal-json [UNMAINTAINED]I'm glad to see that minimal-json is still used, however, I can't maintain this project anymore. If anyone actively maintains a fork that follows the ideas of minimal-json, I'm happy to link to this repository here. Thank you. A fast and minimal JSON parser and writer for Java. It's not an object mapper, but a bare-bones library that aims at being
Minimal-json is fully covered by unit tests, and field-tested by the Eclipse RAP project and others (see below). The JAR contains a valid OSGi bundle manifest and can be used in OSGi environments without modifications. UsageThe class Parse JSONYou can parse JSON from a JsonValue value = Json.parse(string); JSON valuesJSON values are represented by the type if (value.isString()) {
String string = value.asString();
// ...
} else if (value.isArray()) {
JsonArray array = value.asArray();
// ...
} JSON arraysThe method JsonArray array = Json.parse(reader).asArray();
String name = array.get(0).asString();
int quantity = array.get(1).asInt(); You can also iterate over the elements of a for (JsonValue value : jsonArray) {
// ...
} JSON objectsSimilar to JsonObject object = Json.parse(input).asObject();
String name = object.get("name").asString();
int quantity = object.get("quantity").asInt(); There are also shorthand methods like String name = object.getString("name", "Unknown");
int age = object.getInt("quantity", 1); You can also iterate over the members of a JSON object: for (Member member : jsonObject) {
String name = member.getName();
JsonValue value = member.getValue();
// ...
} Example: Extract nested contentsLet's take the following JSON as an example: {
"order": 4711,
"items": [
{
"name": "NE555 Timer IC",
"cat-id": "645723",
"quantity": 10
},
{
"name": "LM358N OpAmp IC",
"cat-id": "764525",
"quantity": 2
}
]
} The following snippet extracts the names and quantities of all items: JsonArray items = Json.parse(json).asObject().get("items").asArray();
for (JsonValue item : items) {
String name = item.asObject().getString("name", "Unknown Item");
int quantity = item.asObject().getInt("quantity", 1);
...
} Create JSON valuesThe entrypoint class JsonValue name = Json.value("Alice");
JsonValue points = Json.value(23); And there are methods for creating empty arrays and objects as well.
Use these together with JsonObject user = Json.object().add("name", "Alice").add("points", 23);
// -> {"name": "Alice", "points": 23}
JsonArray user = Json.array().add("Bob").add(42);
// -> ["Bob", 42] You can also create JSON arrays conveniently from Java arrays such as String[] javaNames = {"Alice", "Bob"};
JsonArray jsonNames = Json.array(names); Modify JSON arrays and objectsYou can replace or remove array elements based on their index. The index must be valid. jsonArray.set(1, 24);
jsonArray.remove(1); Likewise, members of JSON objects can be modified by their name. If the name does not exist, jsonObject.set("quantity", 24);
jsonObject.remove("quantity");
jsonObject.merge(otherObject); Output JSONThe String json = jsonValue.toString();
jsonValue.writeTo(writer); Both methods accept an additonal parameter to enable formatted output: String json = jsonValue.toString(WriterConfig.PRETTY_PRINT);
jsonValue.writeTo(writer, WriterConfig.PRETTY_PRINT); For more details, have a look at the JavaDoc. ConcurrencyThe JSON structures in this library ( Iterators will throw a PerformanceI've spent days benchmarking and tuning minimal-json's reading and writing performance. You can see from the charts below that it performs quite reasonably. But damn, it's not quite as fast as Jackson! However, given that Jackson is a very complex machine compared to minimal-json, I think for the minimalism of this parser, the results are quite good. Below is the result of a performance comparison with other parsers, namely org.json 20141113, Gson 2.3.1, Jackson 2.5.0, and JSON.simple 1.1.1. In this benchmark, two example JSON texts are parsed into a Java object and then serialized to JSON again. All benchmarks can be found in com.eclipsesource.json.performancetest. rap.json, ~ 30kB caliper.json, ~ 83kB Who is using minimal-json?Here are some projects that use minimal-json:
IncludeYou can include minimal-json from Maven Central by adding this dependency to your <dependency>
<groupId>com.eclipsesource.minimal-json</groupId>
<artifactId>minimal-json</artifactId>
<version>0.9.5</version>
</dependency> Development snapshots are available on oss.sonatype.org. BuildTo build minimal-json on your machine, checkout the repository,
A continuous integration build is running at Travis-CI. LicenseThe code is available under the terms of the MIT License. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论