FASTJSON 2
is an extremely performant and easy-to-use Java JSON library.
FASTJSON 2
is an important upgrade of the FASTJSON
project. Compared with FASTJSON 1, the performance has been greatly improved and the security issues of the autoType function due to compatibility and whitelisting have been solved.JDK 11
/ JDK 17
, optimized for compact string
, support Record, support GraalVM Native-Image
JSONPath
support, supporting SQL:2016 JSONPath syntaxAndroid 8+
, a set of APIs for client and serverKotlin
https://alibaba.github.io/fastjson2/kotlin_cnJSON Schema
https://alibaba.github.io/fastjson2/json_schema_cn In fastjson v2
, groupId
is different from 1.x
, it is com.alibaba.fastjson2
:
Maven
:
< dependency >
< groupId >com.alibaba.fastjson2</ groupId >
< artifactId >fastjson2</ artifactId >
< version >2.0.53</ version >
</ dependency >
Gradle
:
dependencies {
implementation ' com.alibaba.fastjson2:fastjson2:2.0.53 '
}
You can view the latest available version at maven.org.
Fastjson v1
compatible module If you originally used the fastjson 1.2.x
version, you can use the compatibility package. The compatibility package cannot guarantee 100% compatibility. Please test and verify it carefully and provide timely feedback if you find any problems.
Maven
:
< dependency >
< groupId >com.alibaba</ groupId >
< artifactId >fastjson</ artifactId >
< version >2.0.53</ version >
</ dependency >
Gradle
:
dependencies {
implementation ' com.alibaba:fastjson:2.0.53 '
}
Fastjson Kotlin
integration module If the project uses Kotlin
, you can use the fastjson-kotlin
module and adopt the features of kotlin
.
Maven
: < dependency >
< groupId >com.alibaba.fastjson2</ groupId >
< artifactId >fastjson2-kotlin</ artifactId >
< version >2.0.53</ version >
</ dependency >
Add the standard library (kotlin-stdlib) and reflection library (kotlin-reflect) as appropriate. If you use a data class (data class) and pass parameters through the constructor, add the reflection library.
< dependency >
< groupId >org.jetbrains.kotlin</ groupId >
< artifactId >kotlin-stdlib</ artifactId >
< version >${kotlin-version}</ version >
</ dependency >
< dependency >
< groupId >org.jetbrains.kotlin</ groupId >
< artifactId >kotlin-reflect</ artifactId >
< version >${kotlin-version}</ version >
</ dependency >
Kotlin Gradle
: dependencies {
implementation( " com.alibaba.fastjson2:fastjson2-kotlin:2.0.53 " )
}
dependencies {
implementation( " org.jetbrains.kotlin:kotlin-stdlib: $kotlin_version " )
implementation( " org.jetbrains.kotlin:kotlin-reflect: $kotlin_version " )
}
Fastjson Extension
extension module If the project uses a framework such as SpringFramework
, you can use the fastjson-extension
module. For usage, refer to SpringFramework Support.
Maven
:
< dependency >
< groupId >com.alibaba.fastjson2</ groupId >
< artifactId >fastjson2-extension-spring5</ artifactId >
< version >2.0.53</ version >
</ dependency >
< dependency >
< groupId >com.alibaba.fastjson2</ groupId >
< artifactId >fastjson2-extension-spring6</ artifactId >
< version >2.0.53</ version >
</ dependency >
Gradle
:
dependencies {
implementation ' com.alibaba.fastjson2:fastjson2-extension-spring5:2.0.53 '
}
dependencies {
implementation ' com.alibaba.fastjson2:fastjson2-extension-spring6:2.0.53 '
}
In fastjson v2
, package
is different from 1.x
, it is com.alibaba.fastjson2
. If you were using fastjson1
before, in most cases you can just change the package name.
JSON
into JSONObject
Java
:
String text = "..." ;
JSONObject data = JSON . parseObject ( text );
byte [] bytes = ...;
JSONObject data = JSON . parseObject ( bytes );
Kotlin
:
import com.alibaba.fastjson2.*
val text = .. . // String
val data = text.parseObject()
val bytes = .. . // ByteArray
val data = bytes.parseObject() // JSONObject
JSON
into JSONArray
Java
:
String text = "..." ;
JSONArray data = JSON . parseArray ( text );
Kotlin
:
import com.alibaba.fastjson2.*
val text = .. . // String
val data = text.parseArray() // JSONArray
JSON
into Java
objects Java
:
String text = "..." ;
User data = JSON . parseObject ( text , User . class );
Kotlin
:
import com.alibaba.fastjson2.*
val text = .. . // String
val data = text.to< User >() // User
val data = text.parseObject< User >() // User
Java
objects to JSON
Java
:
Object data = "..." ;
String text = JSON . toJSONString ( data );
byte [] text = JSON . toJSONBytes ( data );
Kotlin
:
import com.alibaba.fastjson2.*
val data = .. . // Any
val text = text.toJSONString() // String
val bytes = text.toJSONByteArray() // ByteArray
JSONObject
and JSONArray
String text = "{ " id " : 2, " name " : " fastjson2 " }" ;
JSONObject obj = JSON . parseObject ( text );
int id = obj . getIntValue ( "id" );
String name = obj . getString ( "name" );
String text = "[2, " fastjson2 " ]" ;
JSONArray array = JSON . parseArray ( text );
int id = array . getIntValue ( 0 );
String name = array . getString ( 1 );
JavaBean
Java
:
JSONArray array = ...
JSONObject obj = ...
User user = array . getObject ( 0 , User . class );
User user = obj . getObject ( "key" , User . class );
Kotlin
:
val array = .. . // JSONArray
val obj = .. . // JSONObject
val user = array.to< User >( 0 )
val user = obj.to< User >( " key " )
JavaBean
Java
:
JSONArray array = ...
JSONObject obj = ...
User user = obj . toJavaObject ( User . class );
List < User > users = array . toJavaList ( User . class );
Kotlin
:
val array = .. . // JSONArray
val obj = .. . // JSONObject
val user = obj.to< User >() // User
val users = array.toList< User >() // List<User>
JavaBean
objects to JSON
Java
:
class User {
public int id ;
public String name ;
}
User user = new User ();
user . id = 2 ;
user . name = "FastJson2" ;
String text = JSON . toJSONString ( user );
byte [] bytes = JSON . toJSONBytes ( user );
Kotlin
:
class User (
var id : Int ,
var name : String
)
val user = User ()
user.id = 2
user.name = " FastJson2 "
val text = user.toJSONString() // String
val bytes = user.toJSONByteArray() // ByteArray
Serialization result:
{
"id" : 2 ,
"name" : " FastJson2 "
}
JSONB
JavaBean
objects JSONB
User user = ...;
byte [] bytes = JSONB . toBytes ( user );
byte [] bytes = JSONB . toBytes ( user , JSONWriter . Feature . BeanToArray );
JSONB
data into JavaBean
byte [] bytes = ...
User user = JSONB . parseObject ( bytes , User . class );
User user = JSONB . parseObject ( bytes , User . class , JSONReader . Feature . SupportBeanArrayMapping );
JSONPath
JSONPath
to read some data String text = ...;
JSONPath path = JSONPath . of ( "$.id" ); // 缓存起来重复使用能提升性能
JSONReader parser = JSONReader . of ( text );
Object result = path . extract ( parser );
JSONPath
to read partial byte[]
data byte [] bytes = ...;
JSONPath path = JSONPath . of ( "$.id" ); // 缓存起来重复使用能提升性能
JSONReader parser = JSONReader . of ( bytes );
Object result = path . extract ( parser );
JSONPath
to read partial byte[]
data byte [] bytes = ...;
JSONPath path = JSONPath . of ( "$.id" ); // 缓存起来重复使用能提升性能
JSONReader parser = JSONReader . ofJSONB ( bytes ); // 注意这里使用ofJSONB方法
Object result = path . extract ( parser );