How to Install Boon and Configuring it with Java Application?
Last Updated :
14 Sep, 2021
Boon is a tool based on java used to efficiently encode or decode JSON in a fast way. Boon is a less known Java JSON API, Boon is also being used as the standard JSON API in Groovy. Boon is one of the general-purpose toolkits for working with data easily. Boon contains ObjectMapper which can parse JSON into custom objects or Java Maps.
We can use the Boon JSON parser by including the Boon JAR file in your Java application. We can do these either by adding Boon as a Maven dependency to your Java application or by downloading the Boon JAR from the central Maven repository and include it manually in the classpath of your Java application. We will cover all this in upcoming topics.
There are 2 methods for setting up Boon which are listed below and later discussed in-depth with visual aids. It is as follows:
- Downloading The Boon JAR File
- Boon as Maven Dependency
Method 1: Downloading The Boon JAR File
Step 1: Download Boon Archive
1.1 Visit the given link : https://mvnrepository.com/artifact/io.fastjson/boon , Now you will see various versions of Boon.
1.2 Click on the latest version.
1.3 Then Click on View All. (This will display a list of various Boon files)
1.4 Click on boon-0.34.jar , jar file will be downloaded
1.5 Create a folder named boon in C drive
1.6 Paste the boon jar file in it and done now we can proceed to set up Boon.
Step 2: Setting Boon Environment
2.1 Click Start and open ‘Edit the system environment variables’.
2.2 Click on Environment variables -> Under System Variables click on New option and then add variable name as BOON and in variable value as the path to the Boon folder and click ok.
2.3 Setting classpath variable by clicking on New to add CLASSPATH and then set variable name as CLASSPATH and variable value as %CLASSPATH%;%boon%\boon-0.34.jar;.; and click OK.
Method 2: Boon as Maven Dependency
Note: This method can be used only when use have created a Maven Project.
2.1 Open pom.xml file (Sample of pom.xml is given below)
<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>BoonTest</groupId>
<artifactId>BoonTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>15</release>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
</project>
2.2 Under <dependencies> tag add the below code
<dependency>
<groupId>io.fastjson</groupId>
<artifactId>boon</artifactId>
<version>0.34</version>
</dependency>
Now, pom.xml we look something like this.
<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>BoonTest</groupId>
<artifactId>BoonTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>15</release>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Included Boon Dependency here-->
<dependency>
<groupId>io.fastjson</groupId>
<artifactId>boon</artifactId>
<version>0.34</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
</project>
Note: Make sure you use the right version number in version tag.
Boon – ObjectMapper
Now we have successfully set up Boon in our system. Its time knows more about Boon functionality. Once we have installed Boon you can start parsing JSON into objects using the Boon ObjectMapper class. ObjectMapper class provides functionality for reading and writing JSON, either to and from Plain Old Java Objects or to and from a general-purpose JSON Tree Model.
Example
Java
import org.boon.json.JsonFactory;
import org.boon.json.ObjectMapper;
public class MyBoon {
public static void main(String args[])
{
ObjectMapper mapper = JsonFactory.create();
String jsonString
= "{\"empDesignation\":\"SDE\",\"empName\":\"Sam\", \"empDesignation\":100}" ;
Employee employee
= mapper.readValue(jsonString, Employee. class );
System.out.println(employee);
}
}
class Employee {
private int empID;
private String empName;
private String empDesignation;
public Employee() {}
public int getempID() { return empID; }
public void setempID( int empID)
{
this .empID = empID;
}
public String getempName() { return empName; }
public void setempName(String empName)
{
this .empName = empName;
}
public String getDesignation()
{
return empDesignation;
}
public void setempDesignation(String empDesignation)
{
this .empDesignation = empDesignation;
}
public String toString()
{
return "Employee [ empID: " + empID
+ ", empName: " + empName
+ ", empDesignation: " + empDesignation + " ]" ;
}
}
|
Output:
Employee [ empID: 100, empName: Sam, empDesignation: SDE]
Share your thoughts in the comments
Please Login to comment...