[Java] [jackson] Corresponds to the trailing comma (trailing comma) when parsing JSON.

When parsing JSON in Java, I'm wondering if a library called jackson is often used. (Aside from the complaint that Java doesn't have a standard library that handles JSON !? Only EE8 is supported?)

I will write about how to use this jackson to parse JSON in the so-called "ketsu comma" and "end comma".

It doesn't seem to be an official release.

First of all, the official release of jackson at the moment (2017/6/22) is 2.8 series, and it seems that 2.8 series can not support the trailing comma. It seems that the option ALLOW_MISSING_VALUES can be used to the extent that it is regrettable, but it cannot be said that it corresponds to the so-called trailing comma.

Ending comma (ketsu comma, trailing comma)

ES5 and recent Java (which I forgot for some time) now ignore trailing commas.

js


let obj = {
        key1: 1,
        key2: 2,
    };
let arr = [
        1,
        2,
    ];

Java


String[] ss = new String[] {
        "a",
        "b",
    };
//・ ・ ・
enum KIND {
    A,
    B,
}

You can write something like this.

(Although I haven't investigated what is going on with JSON officially)

If there is a trailing comma in JSON parsing with normal jackson, a syntax error will occur, but Actually, I tried to find out if it could be handled with Option.

jackson support

Looking at the issue below, it seems to correspond.

Add JsonParser.ALLOW_TRAILING_COMMA to work for Arrays and Objects

However, it seems that official support has not been made yet, and it seems that it can be realized by using 2.9.0.pr system.

If it is maven, write as follows.

pom.xml


<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.0.pr4</version>
</dependency>

After using 2.9.0 series, it seems that it can be supported by setting ALLOW_TRAILING_COMMA of ObjectMapper as follows.

ObjectMapper mapper = new ObjectMapper().configure(JsonParser.Feature.ALLOW_TRAILING_COMMA, true);
Object obj = mapper.readValue(json, Object.class);

It doesn't come out much even if I google it with Ketsukanma.

I personally called it "Ketsukamma", but it doesn't come out much even if I google it. Is it a "last comma" in Japanese? However, even if I googled with "jackson trailing comma", this countermeasure did not come out easily. Finally, when I googled "jackson trailing comma", it came out.

When checking "Ketsu comma", google with "trailing comma"! That is my personal lesson this time.

Recommended Posts

[Java] [jackson] Corresponds to the trailing comma (trailing comma) when parsing JSON.
Convert Java enum enums and JSON to and from Jackson
[Java] Convert JSON to Java and Java to JSON-How to use GSON and Jackson-
9 Corresponds to the return value
[Java] JSON communication with jackson
JSON in Java and Jackson Part 1 Return JSON from the server
Failed to create the Java Virtual Machine when installing Eclipse.
12 Corresponds to the while statement
Input to the Java console
I tried to translate the error message when executing Eclipse (Java)
How to solve the unknown error when using slf4j in Java
[java8] To understand the Stream API
Welcome to the Java Library Swamp! !!
Parsing the COTOHA API in Java
The road from JavaScript to Java
[Java] Output the result of ffprobe -show_streams in JSON and map it to an object with Jackson
Update JAVA to the latest version to 1.8.0_144 (when downloading from the web and updating)
Sample code to serialize and deserialize Java Enum enums and JSON in Jackson
[Java small story] Monitor when a value is added to the List
How to find the total number of pages when paging in Java
I want to get the IP address when connecting to Wi-Fi in Java
[Java] How to use the File class
Introduction to java for the first time # 2
[Java] How to use the hasNext function
Java SE8 Silver ~ The Road to Pass ~
[Java] How to use the HashMap class
About the procedure for java to work
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
[Java] How to set the Date time to 00:00:00
[Java] How to get the current directory
[Processing × Java] How to use the class
How to install the legacy version [Java]
How to get the date in java
Output of the book "Introduction to Java"
[Processing × Java] How to use the function
Precautions when migrating from VB6.0 to JAVA
I went to the Java Women's Club # 1
[Java] Color the standard output to the terminal
[Java] How to use the Calendar class
How to assemble JSON directly in Jackson
[Promotion of Ruby comprehension (1)] When switching from Java to Ruby, first understand the difference.
Make the JSON of the snake case correspond to the field of the camel case in Java (JVM)