Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GsonUtils #29

Closed
Jasperben opened this issue Apr 26, 2023 · 1 comment
Closed

GsonUtils #29

Jasperben opened this issue Apr 26, 2023 · 1 comment

Comments

@Jasperben
Copy link

可以把GsonUtils也加进去

@afkT
Copy link
Owner

afkT commented Jun 21, 2023

这个不方便添加到 DevApp 库中,因为会依赖 Gson 且导致别人项目版本不一致,但是我有专门的 kotlin 库里面实现了。

// DevEngine - 第三方框架解耦、一键替换第三方库、同类库多 Engine 组件化混合使用
implementation 'io.github.afkt:DevEngine:1.0.8'

如何使用 Gson 可查看 json.kt 以下为使用示例:

// 初始化只需调用
DevEngine.completeInitialize(context)

// 假设存在 User 实体类
val user = User()
// 转换为 JSON 字符串
val json = user.toJson()
// 映射为实体类
val user1 = json.fromJson(
    classOfT = User::class.java
)

// 上面是最简单常见的使用,下面还有其他方法

// 转换为 JSON 字符串并进行格式化缩进
val json = user.toJsonIndent()
// 判断字符串是否 JSON 格式
json.isJSON()
// 判断字符串是否 JSON Object 格式
json.isJSONObject()
// 判断字符串是否 JSON Array 格式
json.isJSONArray()

只要调用初始化 DevEngine.completeInitialize(context) 方法,无需考虑内部实现,默认使用 Gson 作为 JSON Engine 进行处理了。

@afkT afkT closed this as completed Jun 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants