<commit_before>= JSON

A short document about simple manipulation of JSON using the Java API for JSON Processing 1.1. (See also JSON on https://en.wikipedia.org/wiki/JSON[Wikipedia] for a good introduction, https://github.com/oliviercailloux/java-course/blob/master/JSON-B.adoc[JSON-B] for (de-)serialization.)

== Dependency
If you use Java SE, you need to add the http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22javax.json%22[javax JSON API] to your dependencies.

.Specify dependencies in your `pom.xml`
[source,xml]
----
<dependency>
	<groupId>javax.json</groupId>
	<artifactId>javax.json-api</artifactId>
	<version>1.1</version>
</dependency>
----

If you use Java EE, this is already provided by your application server.

== Example use
You may now use the API. Check https://javaee.github.io/tutorial/jsonp.html[here] for a tutorial.

.Obtain a json object from a String
[source,java]
----
public JsonObject asJson(String data) {
	JsonObject json;
	try (JsonReader jr = Json.createReader(new StringReader(data))) {
		json = jr.readObject();
	}
}
----

.Pretty print a json value
[source,java]
----
public String asPrettyString(JsonValue json) {
	StringWriter stringWriter = new StringWriter();
	JsonWriterFactory writerFactory = Json.createWriterFactory(ImmutableMap.of(JsonGenerator.PRETTY_PRINTING, true));
	try (JsonWriter jsonWriter = writerFactory.createWriter(stringWriter)) {
		jsonWriter.write(json);
	}
	return stringWriter.toString();
}
----

This example uses https://github.com/google/guava[Guava] to create the `Map` object. (Inspiration: https://stackoverflow.com/a/32500882[SO].)

== References
* Java API for JSON Processing 1.1 (the specs, https://jcp.org/en/jsr/detail?id=374[JSR 374], are exclusively the https://docs.oracle.com/javaee/7/api/javax/json/package-summary.html[javadoc] of the API)
<commit_msg>Specs link<commit_after>= JSON

A short document about simple manipulation of JSON using the Java API for JSON Processing 1.1. (See also JSON on https://en.wikipedia.org/wiki/JSON[Wikipedia] for a good introduction, https://github.com/oliviercailloux/java-course/blob/master/JSON-B.adoc[JSON-B] for (de-)serialization.)

== Dependency
If you use Java SE, you need to add the http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22javax.json%22[javax JSON API] to your dependencies.

.Specify dependencies in your `pom.xml`
[source,xml]
----
<dependency>
	<groupId>javax.json</groupId>
	<artifactId>javax.json-api</artifactId>
	<version>1.1</version>
</dependency>
----

If you use Java EE, this is already provided by your application server.

== Example use
You may now use the API. Check https://javaee.github.io/tutorial/jsonp.html[here] for a tutorial.

.Obtain a json object from a String
[source,java]
----
public JsonObject asJson(String data) {
	JsonObject json;
	try (JsonReader jr = Json.createReader(new StringReader(data))) {
		json = jr.readObject();
	}
}
----

.Pretty print a json value
[source,java]
----
public String asPrettyString(JsonValue json) {
	StringWriter stringWriter = new StringWriter();
	JsonWriterFactory writerFactory = Json.createWriterFactory(ImmutableMap.of(JsonGenerator.PRETTY_PRINTING, true));
	try (JsonWriter jsonWriter = writerFactory.createWriter(stringWriter)) {
		jsonWriter.write(json);
	}
	return stringWriter.toString();
}
----

This example uses https://github.com/google/guava[Guava] to create the `Map` object. (Inspiration: https://stackoverflow.com/a/32500882[SO].)

== References
* Java API for JSON Processing 1.1 https://docs.oracle.com/javaee/7/api/javax/json/package-summary.html[javadoc] (specs: https://jcp.org/en/jsr/detail?id=374[JSR 374], but nothing more than the javadoc here)

{'fraction_non_alphanumeric': 0.13456823753745573, 'fraction_numerical': 0.016344320348678835, 'mean_word_length': 4.719626168224299, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 14, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6549693', 'n_tokens_mistral': 1250, 'n_tokens_neox': 1180, 'n_words': 319}