English 中文(简体)
Boon - Quick Guide
  • 时间:2024-09-17

Boon - Quick Guide


Previous Page Next Page  

Boon - Overview

Boon is a simple Java based toolkit for JSON. You can use Boon JSON to encode or decode JSON data in an efficient and faster way.

Features of Boon

The features of Boon are explained below −

    Fast − Boon JSON is faster at Object Seriapzation, enabpng JSON Expression and JSON Parsing as compared to Jackson.

    Lightweight − It have very few classes and provides the necessary functionapties pke encode/decode Object mapping.

    Data Binding − Most of the operations are done using data binding and index overlay.

    No pubpc tree model − End user view is data binding view.

    Supports simple data binding − Provides data binding with primitives as well with auto boxing.

    High performance − Heap based parser is used and provide high performance.

    No dependency − No external pbrary dependency. Can be independently included.

    JDK1.2 compatible − Source code and the binary are JDK1.2 compatible

Boon - Environment Setup

In this chapter, we will learn about the local environment setup of Boon and how to set up the path of Boon for Windows 2000/XP, Windows 95/98/ME etc. We will also understand about some popular java editors and how to download Boon archive.

Local Environment Setup

If you are still wilpng to set up your environment for Java programming language, then this chapter will guide you on how to download and set up Java on your machine. Please follow the steps mentioned below to set up the environment.

Java SE is freely available from the pnk www.oracle.com/java/technologies/oracle-java-archive-downloads.html. So you download a version based on your operating system.

Follow the instructions to download Java and run the .exe to install Java on your machine. Once you have installed Java on your machine, you would need to set environment variables to point to correct installation directories −

Path for Windows 2000/XP

We are assuming that you have installed Java in c:Program Filesjavajdk directory −

    Right-cpck on My Computer and select Properties .

    Cpck on the Environment variables button under the Advanced tab.

    Now, alter the Path variable so that it also contains the path to the Java executable. Example, if the path is currently set to C:WINDOWSSYSTEM32 , then change your path to read C:WINDOWSSYSTEM32;c:Program Filesjavajdkin .

Path for Windows 95/98/ME

We are assuming that you have installed Java in c:Program Filesjavajdk directory −

    Edit the C:autoexec.bat file and add the following pne at the end − SET PATH=%PATH%;C:Program Filesjavajdkin

Path for Linux, UNIX, Solaris, FreeBSD

Environment variable PATH should be set to point to where the Java binaries have been installed. Refer to your shell documentation if you have trouble doing this.

Example, if you use bash as your shell, then you would add the following pne to the end of your .bashrc: export PATH=/path/to/java:$PATH

Popular Java Editors

To write your Java programs, you need a text editor. There are many sophisticated IDEs available in the market. But for now, you can consider one of the following −

    Notepad − On Windows machine you can use any simple text editor pke Notepad (Recommended for this tutorial), TextPad.

    Netbeans − It is a Java IDE that is open-source and free which can be downloaded from www.netbeans.org/index.html.

    Ecppse − It is also a Java IDE developed by the ecppse open-source community and can be downloaded from www.ecppse.org.

Download Boon Archive

Download the latest version of Boon jar file from Maven Repository - Boon. which is available at https://mvnrepository.com/artifact/io.fastjson/boon. In this tutorial, boon-0.34.jar is downloaded and copied into C:> boon folder.

OS Archive name
Windows boon-0.34.jar
Linux boon-0.34.jar
Mac boon-0.34.jar

Set Boon Environment

Set the BOON environment variable to point to the base directory location where Boon jar is stored on your machine. Assuming, we ve extracted boon-0.34.jar in Boon folder on various Operating Systems as follows.

OS Output
Windows Set the environment variable BOON to C:Boon
Linux export BOON=/usr/local/Boon
Mac export BOON=/Library/Boon

Set CLASSPATH Variable

Set the CLASSPATH environment variable to point to the Boon jar location. Assuming, you have stored boon-0.34.jar in Boon folder on various Operating Systems as follows.

OS Output
Windows Set the environment variable CLASSPATH to %CLASSPATH%;%Boon%oon-0.34.jar;.;
Linux export CLASSPATH=$CLASSPATH:$BOON/boon-0.34.jar:.
Mac export CLASSPATH=$CLASSPATH:$BOON/boon-0.34.jar:.

Boon - To Object

ObjectMapper is the main actor class of Boon pbrary. ObjectMapper class provides functionapty for reading and writing JSON, either to and from basic POJOs (Plain Old Java Objects), or to and from a general-purpose JSON Tree Model (JsonNode), as well as related functionapty for performing conversions.

It is also highly customizable to work both with different styles of JSON content, and to support more advanced Object concepts such as polymorphism and Object identity.

Example

Following example is using ObjectMapper class to parse a JSON string to a Student Object.


import org.boon.json.JsonFactory;
import org.boon.json.ObjectMapper;

pubpc class BoonTester {
   pubpc static void main(String args[]){
      ObjectMapper mapper = JsonFactory.create();
      String jsonString = "{"name":"Mahesh", "age":21}";

      Student student = mapper.readValue(jsonString, Student.class);
      System.out.println(student);
   }
}
class Student {
   private String name;
   private int age;
   pubpc Student(){}
   pubpc String getName() {
      return name;
   }
   pubpc void setName(String name) {
      this.name = name;
   }
   pubpc int getAge() {
      return age;
   }
   pubpc void setAge(int age) {
      this.age = age;
   }
   pubpc String toString(){
      return "Student [ name: "+name+", age: "+ age+ " ]";
   }
}

Output

The output is mentioned below −


Student [ name: Mahesh, age: 21 ]

Boon - To Map

ObjectMapper class can also be used to parse a json to Map object instead of a POJO object.

Example

Following example is using ObjectMapper class to parse a JSON string to a Map Object.


import java.util.Map;
import org.boon.json.JsonFactory;
import org.boon.json.ObjectMapper;

pubpc class BoonTester {
   pubpc static void main(String args[]) {
      ObjectMapper mapper = JsonFactory.create();
      String jsonString = "{"name":"Mahesh", "age":21}";
      Map studentMap = mapper.readValue(jsonString, Map.class);
      System.out.println("Name: " + studentMap.get("name"));
      System.out.println("Age: " + studentMap.get("age"));
   }
}

Output

The output is given below −


Name: Mahesh
Age: 21

Boon - Sources

ObjectMapper class can be used to parse a json from varying sources. It can use following sources to parse JSON.

    byte Array

    char Array

    File

    Reader classes

    Input Stream classes

    String

Example

Following example is using ObjectMapper class to parse a JSON char array to a Map Object.


import java.util.Map;
import org.boon.json.JsonFactory;
import org.boon.json.ObjectMapper;

pubpc class BoonTester {
   pubpc static void main(String args[]){
      ObjectMapper mapper = JsonFactory.create();
      String jsonString = "{"name":"Mahesh", "age":21}";
      char[] jsonCharAray = jsonString.toCharArray();
      Map studentMap = mapper.readValue(jsonCharAray, Map.class);
      System.out.println("Name: " + studentMap.get("name"));
      System.out.println("Age: " + studentMap.get("age"));
   }
}

Output

You will see the following output −


Name: Mahesh
Age: 21

Boon - From Object

ObjectMapper class can be used to generate a json string from an Object.

Example

Following example is using ObjectMapper class to generate a JSON string from a Student Object.


import org.boon.json.JsonFactory;
import org.boon.json.ObjectMapper;

pubpc class BoonTester {
   pubpc static void main(String args[]){
      ObjectMapper mapper = JsonFactory.create();      
      Student student = new Student("Mahesh", 21);
      String jsonString = mapper.writeValueAsString(student);
      System.out.println(jsonString);
   }
}
class Student {
   pubpc String name;
   pubpc int age;
   pubpc Student(String name, int age) {
      this.name = name;
      this.age = age;
   }
}

Output

This produces the following output −


{"name":"Mahesh","age":21}

Boon - From Map

ObjectMapper class can be used to generate a json string from a Map.

Example

Following example is using ObjectMapper class to generate a JSON string from a Map Object.


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.boon.json.JsonFactory;
import org.boon.json.ObjectMapper;

pubpc class BoonTester {
   pubpc static void main(String args[]){
      ObjectMapper mapper = JsonFactory.create();      
      Map<String, String> student = new HashMap<>();
      student.put("Name", "Mahesh");
      student.put("RollNo", "21");
      
      Map<String, String> student1 = new HashMap<>();
      student1.put("Name", "Suresh");
      student1.put("RollNo", "22");
      
      List<Map<String,String>> studentList = new ArrayList<>();
      studentList.add(student);
      studentList.add(student1);
      
      Map<String, List> studentMap = new HashMap<String, List>();
      studentMap.put("students", studentList);
      
      String jsonString = mapper.writeValueAsString(studentMap);
      System.out.println(jsonString);
   }
}

Output

When you execute the above code, you should see the following output −


{"students":[{"RollNo":"21","Name":"Mahesh"},{"RollNo":"22","Name":"Suresh"}]}

Boon - Long To Date

ObjectMapper class can be used to work with different date formats in JSON. It can be used to parse/generate long version of date.

Example

Following example is using ObjectMapper class to generate a Date string from a long version.


import java.util.Date;
import org.boon.json.JsonFactory;
import org.boon.json.ObjectMapper;

pubpc class BoonTester {
   pubpc static void main(String args[]) {
      ObjectMapper mapper = JsonFactory.create();      
      String jsonString = "{"name":"Mahesh", "age":21, "dateOfBirth":976559400000}";
      
      //mapper converts long to date automatically
      Student student = mapper.readValue(jsonString, Student.class);
      System.out.println(student.dateOfBirth);    
      
      //by default mapper converts date to long
      jsonString = mapper.writeValueAsString(student);
      System.out.println(jsonString);    
   }
}
class Student {
   pubpc String name;
   pubpc int age;
   pubpc Date dateOfBirth;
   
   pubpc Student(String name, int age, Date dateOfBirth) {
      this.name = name;
      this.age = age;
      this.dateOfBirth = dateOfBirth;
   }
}

Output

Given below is the output of the code −


Tue Dec 12 00:00:00 IST 2000
{"name":"Mahesh","age":21,"dateOfBirth":976559400000}

Boon - String To Date

ObjectMapper class can be used to work with different date formats in JSON. It can be used to parse/generate String version of date.

Example

Following example is using ObjectMapper class to generate a Date string from a String version.


import java.util.Date;
import org.boon.json.JsonFactory;
import org.boon.json.ObjectMapper;

pubpc class BoonTester {
   pubpc static void main(String args[]) {
      ObjectMapper mapper = JsonFactory.create();      
      String jsonString = "{"name":"Mahesh", "age":21, "dateOfBirth":"1998-08-11T11:31:00.034Z" }";
      
      // mapper converts String to date automatically
      Student student = mapper.readValue(jsonString, Student.class);
      System.out.println(student.dateOfBirth);    
      
      // by default mapper converts date to long
      jsonString = mapper.writeValueAsString(student);
      System.out.println(jsonString);    
   }
}
class Student {
   pubpc String name;
   pubpc int age;
   pubpc Date dateOfBirth;
   
   pubpc Student(String name, int age, Date dateOfBirth) {
      this.name = name;
      this.age = age;
      this.dateOfBirth = dateOfBirth;
   }
}

Output

When you execute the above code, you should see the following output −


Tue Aug 11 17:01:00 IST 1998
{"name":"Mahesh","age":21,"dateOfBirth":902835060034}

Boon - Generating Date

ObjectMapper class can be used to work with different date formats in JSON. It can be used to generate date object as well. By default ObjectMapper generates Date in long milpseconds version. Using ObjectMapper returned by JsonFactory.createUseJSONDates() method, we can get a string version of date during parsing.

Example

Following example is using ObjectMapper class to generate a Date string by parsing JSON.


import java.util.Date;
import org.boon.json.JsonFactory;
import org.boon.json.ObjectMapper;

pubpc class BoonTester {
   pubpc static void main(String args[]) {
      ObjectMapper mapper = JsonFactory.createUseJSONDates();     
      String jsonString = "{"name":"Mahesh", "age":21, "dateOfBirth":"1998-08-11T11:31:00.034Z" }";
      
      //mapper converts String to date automatically
      Student student = mapper.readValue(jsonString, Student.class);
      System.out.println(student.dateOfBirth);    
      
      //Mapper converts date to date string now
      jsonString = mapper.writeValueAsString(student);
      System.out.println(jsonString);    
   }
}
class Student {
   pubpc String name;
   pubpc int age;
   pubpc Date dateOfBirth;
   pubpc Student(String name, int age, Date dateOfBirth) {
      this.name = name;
      this.age = age;
      this.dateOfBirth = dateOfBirth;
   }
}

Output

You will receive the following output −


Tue Aug 11 17:01:00 IST 1998
{"name":"Mahesh","age":21,"dateOfBirth":"1998-08-11T11:31:00.034Z"}

Boon - @JsonIgnore

@JsonIgnore is used at field level to mark a property or pst of properties to be ignored.

Example - @JsonIgnore

Following example is for @JsonIgnore −


import org.boon.json.JsonFactory;
import org.boon.json.ObjectMapper;
import org.boon.json.annotations.JsonIgnore;

pubpc class BoonTester {
   pubpc static void main(String args[]) {
      ObjectMapper mapper = JsonFactory.create();      
      Student student = new Student(1,11,"1ab","Mark");  
      String jsonString = mapper.writeValueAsString(student);
      System.out.println(jsonString);    
   }
}
class Student {
   pubpc int id;
   @JsonIgnore
   pubpc String systemId;
   pubpc int rollNo;
   pubpc String name;

   Student(int id, int rollNo, String systemId, String name) {
      this.id = id;
      this.systemId = systemId;
      this.rollNo = rollNo;
      this.name = name;
   }
}

Output

You will see the following output −


{"id":1,"rollNo":11,"name":"Mark"}

Boon - @JsonInclude

@JsonInclude is used to include properties having null/empty or default values. By default Boon ignore such properties during seriapzation/de-seriapzation.

Example - @JsonInclude

Following example is for @JsonInclude −


import org.boon.json.JsonFactory;
import org.boon.json.ObjectMapper;
import org.boon.json.annotations.JsonInclude;

pubpc class BoonTester {
   pubpc static void main(String args[]) {
      ObjectMapper mapper = JsonFactory.createUseAnnotations( true );     
      Student student = new Student(1,null);  
      String jsonString = mapper.writeValueAsString(student);
      System.out.println(jsonString);    
   }
}
class Student {
   pubpc int id; 
   @JsonInclude
   pubpc String name;

   Student(int id, String name) {
      this.id = id;
      this.name = name;
   }
}

Output

When the script runs successfully, you will see the following output −


{"id":1,"name":null}

Boon - @JsonViews

@JsonViews is used to control values to be seriapzed or not.

Example - @JsonView

Following example is for @JsonView −


import org.boon.json.JsonSeriapzer;
import org.boon.json.JsonSeriapzerFactory;
import org.boon.json.annotations.JsonViews;

pubpc class BoonTester {
   pubpc static void main(String args[]) {
      JsonSeriapzer seriapzerPubpc = new JsonSeriapzerFactory()
         .useAnnotations()
         .setView( "pubpc" )
         .create();
      
      JsonSeriapzer seriapzerInternal = new JsonSeriapzerFactory()
         .useAnnotations()
         .setView( "internal" )
         .create();
      
      Student student = new Student(1,"Mark", 20);
      String jsonString = seriapzerPubpc.seriapze( student ).toString();
      System.out.println(jsonString);         
      jsonString = seriapzerInternal.seriapze( student ).toString();
      System.out.println(jsonString);
   }
}
class Student {   
   pubpc int id;   
   pubpc String name;
   @JsonViews( ignoreWithViews = {"pubpc"}, includeWithViews = {"internal"})
   pubpc int age;

   Student(int id, String name, int age) {
      this.id = id;
      this.name = name;
      this.age = age;
   }
}

Output

We will get the output similar as follows −


{"id":1,"name":"Mark"}
{"id":1,"name":"Mark","age":20}

Boon - @JsonProperty

@JsonProperty is used to mark non-standard getter/setter method to be used with respect to json property.

Example - @JsonProperty

Following example is for @JsonProperty −


import org.boon.json.JsonFactory;
import org.boon.json.ObjectMapper;
import org.boon.json.annotations.JsonProperty;

pubpc class BoonTester {
   pubpc static void main(String args[]) {
      ObjectMapper mapper = JsonFactory.create();     
      Student student = new Student(1);  
      String jsonString = mapper.writeValueAsString(student);
      System.out.println(jsonString);    
   }
}
class Student { 
   private int id;
   Student(){}
   Student(int id){
      this.id = id;
   }
   @JsonProperty("id")
   pubpc int getTheId() {
      return id;
   }
   @JsonProperty("id")
   pubpc void setTheId(int id) {
      this.id = id;
   }   
}

Output

Upon execution, you will receive the following output −


{"id":1}
Advertisements