# 12.3 Four Implementations of Json

JFinal officially provides four implementations of the Json abstract class: JFinalJson, FastJson, Jackson, and MixedJson, which can meet most requirements.

# 1. JFinalJson

JFinalJson is the earliest implementation provided by JFinal. The most important feature of this implementation is that when converting a JFinal Model, it first gets the Map attrs attribute from the Model and then converts this Map object. Even if you have generated getter methods in your Model, they will not be called during conversion.

Several reasons for converting Model.attrs rather than using getter methods are:

A: Supports the conversion of multi-table join query results.

B: Early JFinal users did not generate getter methods for Model.

Note: JFinalJson only supports object-to-JSON-string conversion and does not support the reverse JSON-string-to-object conversion. You can use MixedJson to support reverse conversion: me.setJsonFactory(new MixedJsonFactory());.

# Advanced Features

  • Convert Model and Record field names to camel case:

    JFinalJson.setModelAndRecordFieldNameToCamelCase();
    
    1
  • Skip null value fields during conversion:

    JFinalJson.setSkipNullValueField(true);
    
    1
  • Add custom addToJson extension methods:

    JFinalJson.addToJson(Timestamp.class, (value, depth, ret) -> {
        ret.addLong(((Timestamp)value).getTime());
    });
    
    1
    2
    3

# 2. FastJson

FastJson is a wrapper around the third-party FastJson library. This implementation heavily depends on the getter methods of Model and Java beans. You can configure various FastJson conversion parameters according to its official documentation.

To use FastJson, you need to add its dependency:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.68</version>
</dependency>
1
2
3
4
5

# 3. Jackson

This implementation is similar to FastJson and is a wrapper around the third-party Jackson library.

To use Jackson, you need to add its dependency:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.11.0</version>
</dependency>
1
2
3
4
5

# 4. MixedJson

MixedJson is another wrapper around JFinalJson and FastJson. It uses JFinalJson for Object-to-JSON-string conversion and FastJson for JSON-string-to-Object conversion.

This implementation combines the advantages of both JFinalJson and FastJson. The former does not support JSON-string-to-Object conversion, while the latter does not support the conversion of dynamic fields in join table SQL queries.

To use MixedJson, you need to add the FastJson dependency:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.68</version>
</dependency>
1
2
3
4
5
Last Updated: 9/22/2023, 7:48:43 AM