Home > other >  Cannot parse a simple empty JSON array with Jackson
Cannot parse a simple empty JSON array with Jackson

Time:10-23

I'm trying to play with the Jackson package in a very simple custom-layout java project:

├── main.java
└── pom.xml

And here are the contents of both files:

main.java

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

class Main {
  static String tags = "[]";
  public static void main(String[] args) {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode root;
    root = mapper.readTree(tags);
  }
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>my-group</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0-SNAPSHOT</version>
 
  <name>my-app</name>
  <dependencies>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.13.0</version>
    </dependency>
  </dependencies>

  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  
  <build>
    <sourceDirectory>${project.basedir}</sourceDirectory>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.3.0</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib/</classpathPrefix>
              <mainClass>Main</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Now, when I run mvn package it gives me this error:

... main.java:[9,27] unreported exception com.fasterxml.jackson.core.JsonProcessingException; must be caught or declared to be thrown

When I first had this error, I had a bigger JSON string to test with, then I suspected the issue might be a parsing one, so I simplified things by replacing it with just a simple empty array [] as shown in the code, with no luck.

How can I fix this error?

CodePudding user response:

Thanks to @huy's comment, the message is descriptive enough and it was a matter of enclosing the parsing function in a try ... catch clause.

The updated code would be:

main.java

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;

class Main {
  static String tags = "[]";
  public static void main(String[] args) {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode root;
    try {
      root = mapper.readTree(tags);
      System.out.println(root.toString());
    } catch (JsonProcessingException e) {}
  }
}

CodePudding user response:

public JsonNode readTree(String content)
                  throws IOException,
                         JsonProcessingException

You should handle exception with try/catch or throws in main method.

  • Related