Skip to content

Commit f15de03

Browse files
committed
see 04/06 log
1 parent 7666744 commit f15de03

File tree

7 files changed

+197
-2
lines changed

7 files changed

+197
-2
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ ext {
4040
support_version = '27.0.2'
4141

4242
leakcanary_version = '1.5.4'
43+
gson_version = '2.8.2'
4344

4445
junit_version = '4.12'
4546
robolectric_version = '3.1.2'

subutil/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ android {
3535
dependencies {
3636
compileOnly "com.android.support:appcompat-v7:$support_version"
3737
compileOnly "com.android.support:design:$support_version"
38+
compileOnly "com.google.code.gson:gson:$gson_version"
3839

40+
testImplementation "com.google.code.gson:gson:$gson_version"
3941
testImplementation "junit:junit:$junit_version"
4042
testImplementation "org.robolectric:robolectric:$robolectric_version"
4143
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package com.blankj.subutil.util;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
6+
import java.io.Reader;
7+
import java.lang.reflect.Type;
8+
9+
10+
/**
11+
* <pre>
12+
* author: Blankj
13+
* blog : http://blankj.com
14+
* time : 2018/04/05
15+
* desc :
16+
* </pre>
17+
*/
18+
public final class GsonUtils {
19+
20+
private static final Gson GSON = createGson(true);
21+
22+
private static final Gson GSON_NO_NULLS = createGson(false);
23+
24+
private GsonUtils() {
25+
throw new UnsupportedOperationException("u can't instantiate me...");
26+
}
27+
28+
/**
29+
* Gets pre-configured {@link Gson} instance.
30+
*
31+
* @return {@link Gson} instance.
32+
*/
33+
public static Gson getGson() {
34+
return getGson(true);
35+
}
36+
37+
/**
38+
* Gets pre-configured {@link Gson} instance.
39+
*
40+
* @param serializeNulls determines if nulls will be serialized.
41+
* @return {@link Gson} instance.
42+
*/
43+
public static Gson getGson(final boolean serializeNulls) {
44+
return serializeNulls ? GSON_NO_NULLS : GSON;
45+
}
46+
47+
/**
48+
* Serializes an object into json.
49+
*
50+
* @param object the object to serialize.
51+
* @return object serialized into json.
52+
*/
53+
public static String toJson(final Object object) {
54+
return toJson(object, true);
55+
}
56+
57+
/**
58+
* Serializes an object into json.
59+
*
60+
* @param object the object to serialize.
61+
* @param includeNulls determines if nulls will be included.
62+
* @return object serialized into json.
63+
*/
64+
public static String toJson(final Object object, final boolean includeNulls) {
65+
return includeNulls ? GSON.toJson(object) : GSON_NO_NULLS.toJson(object);
66+
}
67+
68+
69+
/**
70+
* Converts {@link String} to given type.
71+
*
72+
* @param json the json to convert.
73+
* @param type type type json will be converted to.
74+
* @return instance of type
75+
*/
76+
public static <T> T fromJson(final String json, final Class<T> type) {
77+
return GSON.fromJson(json, type);
78+
}
79+
80+
/**
81+
* Converts {@link String} to given type.
82+
*
83+
* @param json the json to convert.
84+
* @param type type type json will be converted to.
85+
* @return instance of type
86+
*/
87+
public static <T> T fromJson(final String json, final Type type) {
88+
return GSON.fromJson(json, type);
89+
}
90+
91+
/**
92+
* Converts {@link Reader} to given type.
93+
*
94+
* @param reader the reader to convert.
95+
* @param type type type json will be converted to.
96+
* @return instance of type
97+
*/
98+
public static <T> T fromJson(final Reader reader, final Class<T> type) {
99+
return GSON.fromJson(reader, type);
100+
}
101+
102+
/**
103+
* Converts {@link Reader} to given type.
104+
*
105+
* @param reader the reader to convert.
106+
* @param type type type json will be converted to.
107+
* @return instance of type
108+
*/
109+
public static <T> T fromJson(final Reader reader, final Type type) {
110+
return GSON.fromJson(reader, type);
111+
}
112+
113+
/**
114+
* Create a pre-configured {@link Gson} instance.
115+
*
116+
* @param serializeNulls determines if nulls will be serialized.
117+
* @return {@link Gson} instance.
118+
*/
119+
private static Gson createGson(final boolean serializeNulls) {
120+
final GsonBuilder builder = new GsonBuilder();
121+
if (serializeNulls) builder.serializeNulls();
122+
return builder.create();
123+
}
124+
}

subutil/src/test/java/com/blankj/subutil/util/ClipboardUtilsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* author: Blankj
1717
* blog : http://blankj.com
1818
* time : 2016/09/26
19-
* desc : ClipboardUtils单元测试
19+
* desc : ClipboardUtils 单元测试
2020
* </pre>
2121
*/
2222
@RunWith(RobolectricTestRunner.class)

subutil/src/test/java/com/blankj/subutil/util/CoordinateConvertUtilsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* author: Blankj
1111
* blog : http://blankj.com
1212
* time : 2018/03/22
13-
* desc :
13+
* desc : CoordinateConvertUtils 单元测试
1414
* </pre>
1515
*/
1616
public class CoordinateConvertUtilsTest {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.blankj.subutil.util;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
7+
/**
8+
* <pre>
9+
* author: Blankj
10+
* blog : http://blankj.com
11+
* time : 2016/09/26
12+
* desc : GsonUtils 单元测试
13+
* </pre>
14+
*/
15+
public class GsonUtilsTest {
16+
17+
@Test
18+
public void getGson() {
19+
Assert.assertNotNull(GsonUtils.getGson());
20+
Assert.assertNotNull(GsonUtils.getGson(false));
21+
}
22+
23+
@Test
24+
public void toJson() {
25+
Person person = new Person("Blankj");
26+
System.out.println(GsonUtils.toJson(person));
27+
System.out.println(GsonUtils.toJson(person, false));
28+
}
29+
30+
@Test
31+
public void fromJson() {
32+
Person person = new Person("Blankj");
33+
Assert.assertEquals(
34+
person,
35+
GsonUtils.fromJson("{\"name\":\"Blankj\",\"gender\":0,\"address\":null}", Person.class)
36+
);
37+
Assert.assertEquals(
38+
person,
39+
GsonUtils.fromJson("{\"name\":\"Blankj\",\"gender\":0}", Person.class)
40+
);
41+
}
42+
43+
class Person {
44+
45+
String name;
46+
int gender;
47+
String address;
48+
49+
public Person(String name) {
50+
this.name = name;
51+
this.gender = gender;
52+
this.address = address;
53+
}
54+
55+
@Override
56+
public boolean equals(Object obj) {
57+
if (obj == this) return true;
58+
if (obj == null || !(obj instanceof Person)) return false;
59+
Person p = (Person) obj;
60+
return equals(name, p.name) && p.gender == gender && equals(address, p.address);
61+
}
62+
63+
private boolean equals(final Object o1, final Object o2) {
64+
return o1 == o2 || (o1 != null && o1.equals(o2));
65+
}
66+
}
67+
}

utilcode/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ android {
3535
dependencies {
3636
compileOnly "com.android.support:appcompat-v7:$support_version"
3737
compileOnly "com.android.support:design:$support_version"
38+
3839
testImplementation "junit:junit:$junit_version"
3940
testImplementation "org.robolectric:robolectric:$robolectric_version"
4041
testImplementation "com.android.support:support-v4:$support_version"

0 commit comments

Comments
 (0)