Skip to content

Repository files navigation

Amarok 's Json For Java (AJ4J)

简体中文

What is AJ4J?

Amarok's Json For Java is a Json parsing library written in Java, which can accurately process Json and internally infer the type obtained by Json.

Processon Json, Json5, and write to the files!

Why AJ4J?

Google Gson : Gson is great!, but Gson sometimes handles type inference incorrectly, and it's easy to get into the trouble of type agnosticism when you use JsonObject directly. Most of the time this is fine, but I am writing something based on Json parsing that I urgently need to know if the user has entered the correct type and pre-infer the type to throw the correct false positive. Constantly trying to check only increases the performance overhead of the runtime, giving the user the feeling that "this software is stuck", so I think type inference should be done at parsing time.

Doing so may make parsing slower, but it's equivalent. And Gson still doesn't seem to be able to handle Json5.

Jackson : Jackson is really my best bet! Not only is it fast, but it can also handle Json5! I also prioritize Jackson in addition to Gson when dealing with some personal projects.

But Jackson's size made my post-package program look bloated. In some of the software that is required to be lighter, I will try to consider other libraries.

FoxSuma Json for Java: I packaged it in my Minecraft Mod Lib because the library is cool (it supports Json5 processing!), but it smells a bit personal and just as easy to get into type-agnostic trouble as Gson. Consistently holding a JsonNode's data type is a better option than by internal inference (or relying on the brain).

Let's go start

Maven Central

repositories {
mavenCentral()
}
dependencies {
implementation "club.snowlyicewolf:amarok-json-for-java:1.7.6"
}

Github Packet

Chilk me go to Github Package

JitPack

Chilk me go to JitPack IO

Amarok Maven

Groove:

repositories {
maven {
name = "Amarok Maven"
url = "http://maven.snowlyicewolf.club"
allowInsecureProtocol = true // Append this if Gradle 7.4+
}
}
dependencies {
implementation "club.someoneice.json:amarok-json-for-java:1.7.6"
}

KotlinDSL:

repositories {
maven {
name ="Amarok Maven"
url = uri("http://maven.snowlyicewolf.club")
isAllowInsecureProtocol true
}
}
dependencies {
implementation("club.someoneice.json:amarok-json-for-java:1.7.6")
}

From Json

Set up our Json File, Json String, and then instance the JSON. Any you can use Json or Json5, but don't use Json Handler to processor Json5.

JSONjson = JSON.json; // For Json, OrJSONjson5 = JSON.json5; // For Json or Json5

Then parse get JsonNode:

// Processor to ArrayNode or MapNode which your Json started。JsonNodenode = json.parse(file);
// Parse to Array or Map (Maybe empty)ArrayNodearrayNode = json.tryPullArrayOrEmpty(node);
MapNodemapNode = json.tryPullObjectOrEmpty(node);

The default JsonNode type is:
Null, String, Int, Float, Double, Boolean, Map, Array, Number, Long, Other

JsonNode can fetch the internal content directly, or it can get the type through the internal inference of the node:

JsonNodenode = JsonNode.NULL;
System.out.println(node.getType());
// It a null node.

To Json

To Json by method JsonBuilder#asString(JsonNode) :String ,then JsonNode will changed to Json String. And the method prettyPrint can make our Json String pretty.

Json5Bean allow the use of stream input and annotation input, you need to implement Json5Builder first, and then obtain ArrayBean and MapBean to start importing the task stream.

publicclassMain {
publicstaticvoidmain(String[] args) {
Filefile2 = newFile("./NewJsonFile.json5");
if (!file2.exists() || !file2.isFile()) file2.createNewFile();
// Core of builderJson5Builderbuilder = newJson5Builder();
// Array builderJson5Builder.ArrayBeanarrayBean = builder.getArrayBean();
// Map builderJson5Builder.ObjectBeanmapBean = builder.getObjectBean();
arrayBean.add(newStringNode("Test"));
// Yes! Another linearrayBean.enterLine();
arrayBean.add(newStringNode("This is another Test"));
mapBean.put("test", newStringNode("Test"));
// And you can make a comment for your Json5.mapBean.addNote("This is a test note");
mapBean.put("newTest", newStringNode("This is another Test"));
MapNodemap = newMapNode();
map.put("testInMap", newStringNode("mapTest"));
// Build a HashMap into the Map Builder.mapBean.put("nodeMap", map);
// And put the Map Builder into the Array Builder.arrayBean.addBean(mapBean);
// Finally, push our Builder.builder.put(arrayBean);
// Now! We can get our Json String.Stringdata = builder.build();
// Check it out.System.out.println(data);
// Save the text to file.OutputStreamoutputStream = Files.newOutputStream(file2.toPath());
outputStream.write(data.getBytes());
outputStream.close();
}
}

Free Software!

Amarok's Json4J is a Free Software, as defined in the Pineapple License: if you want to sell the software you license, you should embed it in your labor, not use it directly. Even if this isn't defined as Free Software to some extent - the author doesn't care about the minutiae, if you insist on selling it.

You can freely fork or contribute to Amarok's Json4J or even provide a better name to get it up and running on the Community Relay Network faster!

About

轻量,且强大的Java实现的Json解析库,为你提供最轻且最精准的解析,且支持流式输出Json。

Topics

Resources

Stars

6 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages