Boon 简明教程

Boon - Quick Guide

Boon - Overview

Boon 是基于 Java 的简单 JSON 工具包。您可以使用 Boon JSON 以高效、更快速的方式对 JSON 数据进行编码或解码。

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

Boon 的功能如下所述 −

The features of Boon are explained below −

  1. Fast − Boon JSON is faster at Object Serialization, enabling JSON Expression and JSON Parsing as compared to Jackson.

  2. Lightweight − It have very few classes and provides the necessary functionalities like encode/decode Object mapping.

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

  4. No public tree model − End user view is data binding view.

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

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

  7. No dependency − No external library dependency. Can be independently included.

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

Boon - Environment Setup

在本章中,我们将了解 Boon 的本地环境设置,以及如何在 Windows 2000/XP、Windows 95/98/ME 等系统上设置 Boon 的路径。我们还将了解一些流行的 Java 编辑器以及如何下载 Boon 存档。

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

如果您仍然愿意为 Java 编程语言设置环境,那么本章将指导您如何在机器上下载和设置 Java。请遵循以下步骤来设置环境。

If you are still willing 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 可从链接 www.oracle.com/java/technologies/oracle-java-archive-downloads.html 免费获取。因此,可根据操作系统下载版本。

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

按照说明下载 Java 并运行 .exe 以在您的计算机上安装 Java。安装计算机上的 Java 后,您需要设置环境变量以指向正确的安装目录 −

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

我们假设已在 c:\Program Files\java\jdk 目录中安装 Java −

We are assuming that you have installed Java in c:\Program Files\java\jdk directory −

  1. Right-click on 'My Computer' and select 'Properties'.

  2. Click on the 'Environment variables' button under the 'Advanced' tab.

  3. 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:\WINDOWS\SYSTEM32', then change your path to read 'C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin'.

Path for Windows 95/98/ME

我们假设已在 c:\Program Files\java\jdk 目录中安装 Java −

We are assuming that you have installed Java in c:\Program Files\java\jdk directory −

  1. Edit the 'C:\autoexec.bat' file and add the following line at the end − 'SET PATH=%PATH%;C:\Program Files\java\jdk\bin'

Path for Linux, UNIX, Solaris, FreeBSD

环境变量 PATH 应设置为指向已安装 Java 二进制文件的位置。如果你在这方面遇到问题,请参阅 shell 文档。

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.

例如,如果您用 bash 作为您的 shell,则您将向您 '.bashrc: export PATH=/path/to/java:$PATH' 的末尾添加以下行

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

要编写 Java 程序,您需要一个文本编辑器。市场中有许多高级集成开发环境可用。但目前,您可以考虑以下选项之一 −

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 −

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

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

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

Download Boon Archive

Maven Repository - Boon 下载最新版本的 Boon jar 文件。此文件位于 https://mvnrepository.com/artifact/io.fastjson/boon 。在此教程中,下载 boon-0.34.jar 文件并将其复制到 C:\> boon 文件夹中。

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

BOON 环境变量设置为指向计算机上存储 Boon jar 的基本目录位置。假设我们已将 boon-0.34.jar 提取到各个操作系统的 Boon 文件夹中,如下所示。

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

CLASSPATH 环境变量设置为指向 Boon jar 所在位置。假设已将 boon-0.34.jar 文件存储到各个操作系统的 Boon 文件夹中,如下所示。

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%\boon-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 是 Boon 库的主要 Actor 类。ObjectMapper 类提供用于读写 JSON 的功能,可以从基本的 POJO(纯旧 Java 对象)读写或写到基本 POJO,也可以从通用的 JSON 树模型(JsonNode)读写或写到通用 JSON 树模型,以及用于执行转换的相关功能。

ObjectMapper is the main actor class of Boon library. ObjectMapper class provides functionality 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 functionality for performing conversions.

它还具有高度的可定制性,既适用于不同样式的 JSON 内容,又支持更高级的对象概念,例如多态性和对象标识。

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

以下示例使用 ObjectMapper 类将 JSON 字符串解析为 Student 对象。

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;

public class BoonTester {
   public 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;
   public Student(){}
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public int getAge() {
      return age;
   }
   public void setAge(int age) {
      this.age = age;
   }
   public String toString(){
      return "Student [ name: "+name+", age: "+ age+ " ]";
   }
}

Output

输出如下:

The output is mentioned below −

Student [ name: Mahesh, age: 21 ]

Boon - To Map

ObjectMapper 类还可用于解析 JSON 到 Map 对象,而不是 POJO 对象。

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

Example

以下示例使用 ObjectMapper 类解析 JSON 字符串到 Map 对象。

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;

public class BoonTester {
   public 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 类可用于从不同的源解析 json。它可以使用以下源解析 JSON。

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

  1. byte Array

  2. char Array

  3. File

  4. Reader classes

  5. Input Stream classes

  6. String

Example

以下示例使用 ObjectMapper 类将 JSON 字符数组解析为 Map 对象。

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;

public class BoonTester {
   public 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 类可用于从对象生成 json 字符串。

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

Example

以下示例使用 ObjectMapper 类从 Student 对象生成 JSON 字符串。

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;

public class BoonTester {
   public 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 {
   public String name;
   public int age;
   public 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 类可用于从 Map 生成 JSON 字符串。

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

Example

以下示例正在使用 ObjectMapper 类从 Map 对象生成 JSON 字符串。

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;

public class BoonTester {
   public 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 类可用于处理 JSON 中不同的日期格式。该类可用于解析/生成日期的长版本。

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

以下示例使用 ObjectMapper 类从长版本生成日期字符串。

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;

public class BoonTester {
   public 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 {
   public String name;
   public int age;
   public Date dateOfBirth;

   public 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 类可以用于处理 JSON 中不同的日期格式。它可以用来解析/生成字符串版本的日期。

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

以下示例使用 ObjectMapper 类从字符串版本生成日期字符串。

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;

public class BoonTester {
   public 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 {
   public String name;
   public int age;
   public Date dateOfBirth;

   public 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 类可以用于处理 JSON 中不同的日期格式。它也可以用于生成日期对象。ObjectMapper 默认生成长毫秒版本的 Date。通过使用 JsonFactory.createUseJSONDates() 方法返回的 ObjectMapper,我们可以获取解析期间的字符串版本日期。

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 milliseconds version. Using ObjectMapper returned by JsonFactory.createUseJSONDates() method, we can get a string version of date during parsing.

Example

以下示例正在使用 ObjectMapper 类通过解析 JSON 生成 Date 字符串。

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;

public class BoonTester {
   public 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 {
   public String name;
   public int age;
   public Date dateOfBirth;
   public 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 在字段级别用于标记要忽略的属性或属性列表。

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

Example - @JsonIgnore

以下示例适用于 @JsonIgnore −

Following example is for @JsonIgnore −

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

public class BoonTester {
   public 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 {
   public int id;
   @JsonIgnore
   public String systemId;
   public int rollNo;
   public 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 用于包含具有 null/空或默认值 的属性。默认情况下,Boon 在序列化/反序列化期间会忽略此类属性。

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

Example - @JsonInclude

以下示例适用于 @JsonInclude −

Following example is for @JsonInclude −

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

public class BoonTester {
   public 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 {
   public int id;
   @JsonInclude
   public 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 用于控制值是否被序列化。

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

Example - @JsonView

以下是有关 @JsonView 的示例:

Following example is for @JsonView −

import org.boon.json.JsonSerializer;
import org.boon.json.JsonSerializerFactory;
import org.boon.json.annotations.JsonViews;

public class BoonTester {
   public static void main(String args[]) {
      JsonSerializer serializerPublic = new JsonSerializerFactory()
         .useAnnotations()
         .setView( "public" )
         .create();

      JsonSerializer serializerInternal = new JsonSerializerFactory()
         .useAnnotations()
         .setView( "internal" )
         .create();

      Student student = new Student(1,"Mark", 20);
      String jsonString = serializerPublic.serialize( student ).toString();
      System.out.println(jsonString);
      jsonString = serializerInternal.serialize( student ).toString();
      System.out.println(jsonString);
   }
}
class Student {
   public int id;
   public String name;
   @JsonViews( ignoreWithViews = {"public"}, includeWithViews = {"internal"})
   public 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 用于将非标准的 getter/setter 方法标记为与 json 属性一起使用。

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

Example - @JsonProperty

以下是有关 @JsonProperty 的示例:

Following example is for @JsonProperty −

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

public class BoonTester {
   public 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")
   public int getTheId() {
      return id;
   }
   @JsonProperty("id")
   public void setTheId(int id) {
      this.id = id;
   }
}

Output

执行后,您将收到以下输出:

Upon execution, you will receive the following output −

{"id":1}