{"id":13487125,"url":"https://github.com/alibaba/fastjson2","last_synced_at":"2025-05-12T05:32:42.619Z","repository":{"id":36974886,"uuid":"482425877","full_name":"alibaba/fastjson2","owner":"alibaba","description":"🚄 FASTJSON2 is a Java JSON  library with excellent performance.","archived":false,"fork":false,"pushed_at":"2025-05-12T02:27:45.000Z","size":130384,"stargazers_count":4026,"open_issues_count":539,"forks_count":526,"subscribers_count":33,"default_branch":"main","last_synced_at":"2025-05-12T02:46:03.507Z","etag":null,"topics":["android","big-data","deserialization","fastjson","fastjson2","graal","graalvm-native-image","high-performance","java","java-json","json","json-deserialization","json-parser","json-path","json-serialization","json-serializer","jsonb","serialization"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alibaba.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-04-17T04:27:24.000Z","updated_at":"2025-05-12T02:27:49.000Z","dependencies_parsed_at":"2024-11-05T21:39:26.701Z","dependency_job_id":"16655c96-ef1e-4349-a3d0-2ed0eaacdd20","html_url":"https://github.com/alibaba/fastjson2","commit_stats":{"total_commits":4156,"total_committers":92,"mean_commits":45.17391304347826,"dds":"0.28897978825794035","last_synced_commit":"4986288cf22591436e1130b45e4fdb508a0dda92"},"previous_names":[],"tags_count":227,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alibaba%2Ffastjson2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alibaba%2Ffastjson2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alibaba%2Ffastjson2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alibaba%2Ffastjson2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alibaba","download_url":"https://codeload.github.com/alibaba/fastjson2/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253672734,"owners_count":21945482,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","big-data","deserialization","fastjson","fastjson2","graal","graalvm-native-image","high-performance","java","java-json","json","json-deserialization","json-parser","json-path","json-serialization","json-serializer","jsonb","serialization"],"created_at":"2024-07-31T18:00:55.639Z","updated_at":"2025-05-12T05:32:42.590Z","avatar_url":"https://github.com/alibaba.png","language":"Java","funding_links":[],"categories":["Java","Uncategorized","Java 程序设计","二、核心功能库（解决具体开发问题）"],"sub_categories":["Uncategorized","网络服务_其他","2. JSON处理"],"readme":"[![Java CI](https://img.shields.io/github/actions/workflow/status/alibaba/fastjson2/ci.yaml?branch=main\u0026logo=github\u0026logoColor=white)](https://github.com/alibaba/fastjson2/actions/workflows/ci.yaml)\n[![Codecov](https://img.shields.io/codecov/c/github/alibaba/fastjson2/main?logo=codecov\u0026logoColor=white)](https://codecov.io/gh/alibaba/fastjson2/branch/main)\n[![Maven Central](https://img.shields.io/maven-central/v/com.alibaba.fastjson2/fastjson2?logo=apache-maven\u0026logoColor=white)](https://search.maven.org/artifact/com.alibaba.fastjson2/fastjson2)\n[![GitHub release](https://img.shields.io/github/release/alibaba/fastjson2)](https://github.com/alibaba/fastjson2/releases)\n[![Java support](https://img.shields.io/badge/Java-8+-green?logo=java\u0026logoColor=white)](https://openjdk.java.net/)\n[![License](https://img.shields.io/github/license/alibaba/fastjson2?color=4D7A97\u0026logo=apache)](https://www.apache.org/licenses/LICENSE-2.0.html)\n[![Gitpod Ready-to-Code](https://img.shields.io/badge/Gitpod-ready--to--code-green?label=gitpod\u0026logo=gitpod\u0026logoColor=white)](https://gitpod.io/#https://github.com/alibaba/fastjson2)\n[![Last SNAPSHOT](https://img.shields.io/nexus/snapshots/https/oss.sonatype.org/com.alibaba.fastjson2/fastjson2?label=latest%20snapshot)](https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/)\n[![GitHub Stars](https://img.shields.io/github/stars/alibaba/fastjson2)](https://github.com/alibaba/fastjson2/stargazers)\n[![GitHub Forks](https://img.shields.io/github/forks/alibaba/fastjson2)](https://github.com/alibaba/fastjson2/fork)\n[![user repos](https://badgen.net/github/dependents-repo/alibaba/fastjson2?label=user%20repos)](https://github.com/alibaba/fastjson2/network/dependents)\n[![GitHub Contributors](https://img.shields.io/github/contributors/alibaba/fastjson2)](https://github.com/alibaba/fastjson2/graphs/contributors)\n\n##### [📖 English Documentation](README_EN.md) | 📖 中文文档\n##### 本项目的Issues会被同步沉淀至[阿里云开发者社区](https://developer.aliyun.com/ask/)\n\n# FASTJSON v2\n\n`FASTJSON 2`是一个性能极致并且简单易用的Java JSON库。\n\n- `FASTJSON 2`是`FASTJSON`项目的重要升级，和FASTJSON 1相比，性能有非常大的提升，解决了autoType功能因为兼容和白名单的安全性问题。\n- 性能极致，性能远超过其他流行JSON库，包括jackson/gson/org.json，性能数据: [https://github.com/alibaba/fastjson2/wiki/fastjson_benchmark](https://github.com/alibaba/fastjson2/wiki/fastjson_benchmark)\n- 支持JDK新特性，包括`JDK 11`/`JDK 17`，针对`compact string`优化，支持Record，支持`GraalVM Native-Image`\n- 完善的[`JSONPath`](https://alibaba.github.io/fastjson2/jsonpath_cn)支持，支持[SQL:2016](https://en.wikipedia.org/wiki/SQL:2016)的JSONPath语法\n- 支持`Android 8+`，客户端和服务器一套API\n- 支持`Kotlin` [https://alibaba.github.io/fastjson2/kotlin_cn](https://alibaba.github.io/fastjson2/kotlin_cn)\n- 支持`JSON Schema` [https://alibaba.github.io/fastjson2/json_schema_cn](https://alibaba.github.io/fastjson2/json_schema_cn)\n- 新增加支持二进制格式JSONB [https://alibaba.github.io/fastjson2/jsonb_format_cn](https://alibaba.github.io/fastjson2/jsonb_format_cn)\n\n![fastjson logo](https://user-images.githubusercontent.com/1063891/233821110-0c912009-4de3-4664-a27e-25274f2fa9c1.jpg)\n\n# 1. 使用准备\n\n## 1.1 添加依赖\n\n在`fastjson v2`中，`groupId`和`1.x`不一样，是`com.alibaba.fastjson2`：\n\n`Maven`:\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.alibaba.fastjson2\u003c/groupId\u003e\n    \u003cartifactId\u003efastjson2\u003c/artifactId\u003e\n    \u003cversion\u003e2.0.57\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n`Gradle`:\n\n```groovy\ndependencies {\n    implementation 'com.alibaba.fastjson2:fastjson2:2.0.57'\n}\n```\n\n可以在 [maven.org](https://search.maven.org/artifact/com.alibaba.fastjson2/fastjson2) 查看最新可用的版本。\n\n## 1.2 其他模块\n\n### `Fastjson v1`兼容模块\n\n如果原来使用`fastjson 1.2.x`版本，可以使用兼容包，兼容包不能保证100%兼容，请仔细测试验证，发现问题请及时反馈。\n\n`Maven`:\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.alibaba\u003c/groupId\u003e\n    \u003cartifactId\u003efastjson\u003c/artifactId\u003e\n    \u003cversion\u003e2.0.57\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n`Gradle`:\n\n```groovy\ndependencies {\n    implementation 'com.alibaba:fastjson:2.0.57'\n}\n```\n\n### `Fastjson Kotlin`集成模块\n\n如果项目使用`Kotlin`，可以使用`fastjson-kotlin`模块，使用方式上采用`kotlin`的特性。\n\n* `Maven`:\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.alibaba.fastjson2\u003c/groupId\u003e\n    \u003cartifactId\u003efastjson2-kotlin\u003c/artifactId\u003e\n    \u003cversion\u003e2.0.57\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n酌情添加标准库(kotlin-stdlib)、反射库(kotlin-reflect)，\n其中若使用数据类(data class)、通过构造函数传入参数则添加反射库。\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.jetbrains.kotlin\u003c/groupId\u003e\n    \u003cartifactId\u003ekotlin-stdlib\u003c/artifactId\u003e\n    \u003cversion\u003e${kotlin-version}\u003c/version\u003e\n\u003c/dependency\u003e\n\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.jetbrains.kotlin\u003c/groupId\u003e\n    \u003cartifactId\u003ekotlin-reflect\u003c/artifactId\u003e\n    \u003cversion\u003e${kotlin-version}\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n* `Kotlin Gradle`:\n\n```kotlin\ndependencies {\n    implementation(\"com.alibaba.fastjson2:fastjson2-kotlin:2.0.57\")\n}\n```\n\n```kotlin\ndependencies {\n    implementation(\"org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version\")\n    implementation(\"org.jetbrains.kotlin:kotlin-reflect:$kotlin_version\")\n}\n```\n\n### `Fastjson Extension`扩展模块\n\n如果项目使用`SpringFramework`等框架，可以使用`fastjson-extension`模块，使用方式参考 [SpringFramework Support](docs/spring_support_cn.md)。\n\n`Maven`:\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.alibaba.fastjson2\u003c/groupId\u003e\n    \u003cartifactId\u003efastjson2-extension-spring5\u003c/artifactId\u003e\n    \u003cversion\u003e2.0.57\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.alibaba.fastjson2\u003c/groupId\u003e\n    \u003cartifactId\u003efastjson2-extension-spring6\u003c/artifactId\u003e\n    \u003cversion\u003e2.0.57\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n`Gradle`:\n\n```groovy\ndependencies {\n    implementation 'com.alibaba.fastjson2:fastjson2-extension-spring5:2.0.57'\n}\n```\n\n\n```groovy\ndependencies {\n    implementation 'com.alibaba.fastjson2:fastjson2-extension-spring6:2.0.57'\n}\n```\n\n# 2. 简单使用\n\n在`fastjson v2`中，`package`和`1.x`不一样，是`com.alibaba.fastjson2`。如果你之前用的是`fastjson1`，大多数情况直接更包名就即可。\n\n### 2.1 将`JSON`解析为`JSONObject`\n\n`Java`:\n\n```java\nString text = \"...\";\nJSONObject data = JSON.parseObject(text);\n\nbyte[] bytes = ...;\nJSONObject data = JSON.parseObject(bytes);\n```\n\n`Kotlin`:\n\n```kotlin\nimport com.alibaba.fastjson2.*\n\nval text = ... // String\nval data = text.parseObject()\n\nval bytes = ... // ByteArray\nval data = bytes.parseObject() // JSONObject\n```\n\n### 2.2 将`JSON`解析为`JSONArray`\n\n`Java`:\n\n```java\nString text = \"...\";\nJSONArray data = JSON.parseArray(text);\n```\n\n`Kotlin`:\n\n```kotlin\nimport com.alibaba.fastjson2.*\n\nval text = ... // String\nval data = text.parseArray() // JSONArray\n```\n\n### 2.3 将`JSON`解析为`Java`对象\n\n`Java`:\n\n```java\nString text = \"...\";\nUser data = JSON.parseObject(text, User.class);\n```\n\n`Kotlin`:\n\n```kotlin\nimport com.alibaba.fastjson2.*\n\nval text = ... // String\nval data = text.to\u003cUser\u003e() // User\nval data = text.parseObject\u003cUser\u003e() // User\n```\n\n### 2.4 将`Java`对象序列化为`JSON`\n\n`Java`:\n\n```java\nObject data = \"...\";\nString text = JSON.toJSONString(data);\nbyte[] text = JSON.toJSONBytes(data);\n```\n\n`Kotlin`:\n\n```kotlin\nimport com.alibaba.fastjson2.*\n\nval data = ... // Any\nval text = data.toJSONString() // String\nval bytes = data.toJSONByteArray() // ByteArray\n```\n\n### 2.5 使用`JSONObject`、`JSONArray`\n\n#### 2.5.1 获取简单属性\n\n```java\nString text = \"{\\\"id\\\": 2,\\\"name\\\": \\\"fastjson2\\\"}\";\nJSONObject obj = JSON.parseObject(text);\n\nint id = obj.getIntValue(\"id\");\nString name = obj.getString(\"name\");\n```\n\n```java\nString text = \"[2, \\\"fastjson2\\\"]\";\nJSONArray array = JSON.parseArray(text);\n\nint id = array.getIntValue(0);\nString name = array.getString(1);\n```\n\n#### 2.5.2 读取`JavaBean`\n\n`Java`:\n\n```java\nJSONArray array = ...\nJSONObject obj = ...\n\nUser user = array.getObject(0, User.class);\nUser user = obj.getObject(\"key\", User.class);\n```\n\n`Kotlin`:\n\n```kotlin\nval array = ... // JSONArray\nval obj = ... // JSONObject\n\nval user = array.to\u003cUser\u003e(0)\nval user = obj.to\u003cUser\u003e(\"key\")\n```\n\n#### 2.5.3 转为`JavaBean`\n\n`Java`:\n\n```java\nJSONArray array = ...\nJSONObject obj = ...\n\nUser user = obj.toJavaObject(User.class);\nList\u003cUser\u003e users = array.toJavaList(User.class);\n```\n\n`Kotlin`:\n\n```kotlin\nval array = ... // JSONArray\nval obj = ... // JSONObject\n\nval user = obj.to\u003cUser\u003e() // User\nval users = array.toList\u003cUser\u003e() // List\u003cUser\u003e\n```\n\n### 2.6 将`JavaBean`对象序列化为`JSON`\n\n`Java`:\n\n```java\nclass User {\n    public int id;\n    public String name;\n}\n\nUser user = new User();\nuser.id = 2;\nuser.name = \"FastJson2\";\n\nString text = JSON.toJSONString(user);\nbyte[] bytes = JSON.toJSONBytes(user);\n```\n\n`Kotlin`:\n\n```kotlin\nclass User(\n    var id: Int,\n    var name: String\n)\n\nval user = User()\nuser.id = 2\nuser.name = \"FastJson2\"\n\nval text = user.toJSONString() // String\nval bytes = user.toJSONByteArray() // ByteArray\n```\n\n序列化结果:\n\n```json\n{\n    \"id\"   : 2,\n    \"name\" : \"FastJson2\"\n}\n```\n\n# 3. 进阶使用\n\n### 3.1 使用`JSONB`\n\n#### 3.1.1 将`JavaBean`对象序列化`JSONB`\n\n```java\nUser user = ...;\nbyte[] bytes = JSONB.toBytes(user);\nbyte[] bytes = JSONB.toBytes(user, JSONWriter.Feature.BeanToArray);\n```\n\n#### 3.1.2 将`JSONB`数据解析为`JavaBean`\n\n```java\nbyte[] bytes = ...\nUser user = JSONB.parseObject(bytes, User.class);\nUser user = JSONB.parseObject(bytes, User.class, JSONReader.Feature.SupportBeanArrayMapping);\n```\n\n### 3.2 使用`JSONPath`\n\n#### 3.2.1 使用`JSONPath`读取部分数据\n\n```java\nString text = ...;\nJSONPath path = JSONPath.of(\"$.id\"); // 缓存起来重复使用能提升性能\n\nJSONReader parser = JSONReader.of(text);\nObject result = path.extract(parser);\n```\n\n#### 3.2.2 使用`JSONPath`读取部分`byte[]`的数据\n\n```java\nbyte[] bytes = ...;\nJSONPath path = JSONPath.of(\"$.id\"); // 缓存起来重复使用能提升性能\n\nJSONReader parser = JSONReader.of(bytes);\nObject result = path.extract(parser);\n```\n\n#### 3.2.3 使用`JSONPath`读取部分`byte[]`的数据\n\n```java\nbyte[] bytes = ...;\nJSONPath path = JSONPath.of(\"$.id\"); // 缓存起来重复使用能提升性能\n\nJSONReader parser = JSONReader.ofJSONB(bytes); // 注意这里使用ofJSONB方法\nObject result = path.extract(parser);\n```\n\n## Star History\n\n\n[![Star History Chart](https://api.star-history.com/svg?repos=alibaba/fastjson2\u0026type=Date)](https://star-history.com/#alibaba/fastjson2)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falibaba%2Ffastjson2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falibaba%2Ffastjson2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falibaba%2Ffastjson2/lists"}