How to Install Boon and Configuring it with Java Application?
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
// Java Program to Illustrate Boon ObjectMapper Functionality // by parsing JSON into objects // Using the Boon ObjectMapper class // Importing JsonFactory and ObjectMapper classes // from org.boon package import org.boon.json.JsonFactory; import org.boon.json.ObjectMapper; // Class 1 // Main class public class MyBoon { // Main driver method public static void main(String args[]) { // Setting up ObjectMapper ObjectMapper mapper = JsonFactory.create(); // Creating json string to be parse String jsonString = "{\"empDesignation\":\"SDE\",\"empName\":\"Sam\", \"empDesignation\":100}" ; // json will be mapped to object Employee employee = mapper.readValue(jsonString, Employee. class ); // Print and display employee credentials System.out.println(employee); } } // Class 2 // Helper class // Getter and Setter Class for Employee class Employee { // Member variables of this class private int empID; private String empName; private String empDesignation; // Constructor of this class public Employee() {} // Method 1 // To get employee ID public int getempID() { return empID; } // Method 2 // To allocate temporary ID of the employee public void setempID( int empID) { // This keyword refers to current object itself this .empID = empID; } // Method 3 // To get employee name public String getempName() { return empName; } // Method 4 // To set employee name public void setempName(String empName) { this .empName = empName; } // Method 5 // To get designation of the employee public String getDesignation() { return empDesignation; } // Method 6 // To set temporary designation of employee public void setempDesignation(String empDesignation) { this .empDesignation = empDesignation; } // Method 7 public String toString() { return "Employee [ empID: " + empID + ", empName: " + empName + ", empDesignation: " + empDesignation + " ]" ; } } |
Output:
Employee [ empID: 100, empName: Sam, empDesignation: SDE]