Skip to content

Commit

Permalink
新增 FastJson 和 moshi 单元测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
getActivity committed Jan 22, 2024
1 parent 936d9ee commit bc5a08e
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 4 deletions.
7 changes: 6 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {

defaultConfig {
applicationId "com.hjq.gson.factory.demo"
minSdkVersion 16
minSdkVersion 26
targetSdkVersion 31
versionCode 950
versionName "9.5"
Expand Down Expand Up @@ -77,4 +77,9 @@ dependencies {
// Bugly 异常捕捉:https://bugly.qq.com/docs/user-guide/instruction-manual-android/?v=20190418140644
implementation 'com.tencent.bugly:crashreport:4.1.9'
implementation 'com.tencent.bugly:nativecrashreport:3.9.2'

implementation 'com.squareup.moshi:moshi:1.12.0'
implementation "com.squareup.moshi:moshi-kotlin:1.12.0"

implementation 'com.alibaba:fastjson:2.0.28'
}
2 changes: 1 addition & 1 deletion app/src/androidTest/assets/NormalJson.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"key" : "value"
},
"listTest1" : [1, 2],
"listTest2" : ["a", "b", "c"],
"listTest2" : [1.1, 1.2, 1.3],
"listTest3" : [1, 2, 3],
"listTest4" : [true, false],
"listTest5" : ["1.0", "1.1", "1.2"],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.hjq.gson.factory.test;

import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonDataException;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter;
import java.io.IOException;
import java.math.BigDecimal;

public class DecimalAdapter extends JsonAdapter<BigDecimal> {

@Override
public BigDecimal fromJson(JsonReader reader) throws IOException {
String value;

value = reader.nextString();
if (value == null || value.trim().isEmpty()) {
return null;
}

try {
return new BigDecimal(value);
} catch (NumberFormatException ex) {
throw new JsonDataException(ex.getMessage(), ex);
}
}

@Override
public void toJson(JsonWriter writer, BigDecimal value) throws IOException {
if (value == null) {
writer.nullValue();
} else {
writer.value(value.toPlainString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public final class JsonBean {
private TestBean bean1;
private TestBean bean2;

private Map<String, String> map1;
private Map<String, String> map2;
private Map<String, Boolean> map1;
private Map<String, Integer> map2;
private Map<String, Integer> map3;
private Map<String, Integer> map4;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
import android.content.Context;
import android.util.Log;
import androidx.test.platform.app.InstrumentationRegistry;
import com.alibaba.fastjson2.JSON;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonToken;
import com.hjq.gson.factory.GsonFactory;
import com.hjq.gson.factory.ParseExceptionCallback;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -102,6 +107,77 @@ public void kotlinDataClassDefaultValueTest() {
Log.i(TAG, mGson.toJson(dataClassBean));
}

@Test
public void moshiTest() {
Context context = InstrumentationRegistry.getInstrumentation().getContext();
Moshi moshi = new Moshi.Builder()
.addLast(new KotlinJsonAdapterFactory())
.add(BigDecimal.class, new DecimalAdapter())
.build();

try {
String json = getAssetsString(context, "NormalJson.json");
JsonAdapter<JsonBean> jsonAdapter = moshi.adapter(JsonBean.class);
JsonBean jsonBean = jsonAdapter.fromJson(json);
Log.i(TAG, "解析通过:" + jsonBean);
} catch (Exception e) {
Log.i(TAG, "解析失败");
e.printStackTrace();
}

try {
String json = getAssetsString(context, "AbnormalJson.json");
JsonAdapter<JsonBean> jsonAdapter = moshi.adapter(JsonBean.class);
JsonBean jsonBean = jsonAdapter.fromJson(json);
Log.i(TAG, "解析通过:" + jsonBean);
} catch (Exception e) {
Log.i(TAG, "解析失败");
e.printStackTrace();
}

try {
String json = getAssetsString(context, "NullJson.json");
JsonAdapter<DataClassBean> jsonAdapter = moshi.adapter(DataClassBean.class);
DataClassBean dataClassBean = jsonAdapter.fromJson(json);
Log.i(TAG, "解析通过:" + dataClassBean);
} catch (Exception e) {
Log.i(TAG, "解析失败");
e.printStackTrace();
}
}

@Test
public void fastJsonTest() {
Context context = InstrumentationRegistry.getInstrumentation().getContext();

try {
String json = getAssetsString(context, "NormalJson.json");
JsonBean jsonBean = JSON.parseObject(json, JsonBean.class);
Log.i(TAG, "解析通过:" + jsonBean);
} catch (Exception e) {
Log.i(TAG, "解析失败");
e.printStackTrace();
}

try {
String json = getAssetsString(context, "AbnormalJson.json");
JsonBean jsonBean = JSON.parseObject(json, JsonBean.class);
Log.i(TAG, "解析通过:" + jsonBean);
} catch (Exception e) {
Log.i(TAG, "解析失败");
e.printStackTrace();
}

try {
String json = getAssetsString(context, "NullJson.json");
DataClassBean dataClassBean = JSON.parseObject(json, DataClassBean.class);
Log.i(TAG, "解析通过:" + dataClassBean);
} catch (Exception e) {
Log.i(TAG, "解析失败");
e.printStackTrace();
}
}

/**
* 测试完成
*/
Expand Down

0 comments on commit bc5a08e

Please sign in to comment.