提交 f15de033 编写于 作者: B Blankj

see 04/06 log

上级 7666744d
......@@ -40,6 +40,7 @@ ext {
support_version = '27.0.2'
leakcanary_version = '1.5.4'
gson_version = '2.8.2'
junit_version = '4.12'
robolectric_version = '3.1.2'
......
......@@ -35,7 +35,9 @@ android {
dependencies {
compileOnly "com.android.support:appcompat-v7:$support_version"
compileOnly "com.android.support:design:$support_version"
compileOnly "com.google.code.gson:gson:$gson_version"
testImplementation "com.google.code.gson:gson:$gson_version"
testImplementation "junit:junit:$junit_version"
testImplementation "org.robolectric:robolectric:$robolectric_version"
}
\ No newline at end of file
package com.blankj.subutil.util;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.Reader;
import java.lang.reflect.Type;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2018/04/05
* desc :
* </pre>
*/
public final class GsonUtils {
private static final Gson GSON = createGson(true);
private static final Gson GSON_NO_NULLS = createGson(false);
private GsonUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Gets pre-configured {@link Gson} instance.
*
* @return {@link Gson} instance.
*/
public static Gson getGson() {
return getGson(true);
}
/**
* Gets pre-configured {@link Gson} instance.
*
* @param serializeNulls determines if nulls will be serialized.
* @return {@link Gson} instance.
*/
public static Gson getGson(final boolean serializeNulls) {
return serializeNulls ? GSON_NO_NULLS : GSON;
}
/**
* Serializes an object into json.
*
* @param object the object to serialize.
* @return object serialized into json.
*/
public static String toJson(final Object object) {
return toJson(object, true);
}
/**
* Serializes an object into json.
*
* @param object the object to serialize.
* @param includeNulls determines if nulls will be included.
* @return object serialized into json.
*/
public static String toJson(final Object object, final boolean includeNulls) {
return includeNulls ? GSON.toJson(object) : GSON_NO_NULLS.toJson(object);
}
/**
* Converts {@link String} to given type.
*
* @param json the json to convert.
* @param type type type json will be converted to.
* @return instance of type
*/
public static <T> T fromJson(final String json, final Class<T> type) {
return GSON.fromJson(json, type);
}
/**
* Converts {@link String} to given type.
*
* @param json the json to convert.
* @param type type type json will be converted to.
* @return instance of type
*/
public static <T> T fromJson(final String json, final Type type) {
return GSON.fromJson(json, type);
}
/**
* Converts {@link Reader} to given type.
*
* @param reader the reader to convert.
* @param type type type json will be converted to.
* @return instance of type
*/
public static <T> T fromJson(final Reader reader, final Class<T> type) {
return GSON.fromJson(reader, type);
}
/**
* Converts {@link Reader} to given type.
*
* @param reader the reader to convert.
* @param type type type json will be converted to.
* @return instance of type
*/
public static <T> T fromJson(final Reader reader, final Type type) {
return GSON.fromJson(reader, type);
}
/**
* Create a pre-configured {@link Gson} instance.
*
* @param serializeNulls determines if nulls will be serialized.
* @return {@link Gson} instance.
*/
private static Gson createGson(final boolean serializeNulls) {
final GsonBuilder builder = new GsonBuilder();
if (serializeNulls) builder.serializeNulls();
return builder.create();
}
}
......@@ -16,7 +16,7 @@ import static org.junit.Assert.assertEquals;
* author: Blankj
* blog : http://blankj.com
* time : 2016/09/26
* desc : ClipboardUtils单元测试
* desc : ClipboardUtils 单元测试
* </pre>
*/
@RunWith(RobolectricTestRunner.class)
......
......@@ -10,7 +10,7 @@ import static java.lang.Math.PI;
* author: Blankj
* blog : http://blankj.com
* time : 2018/03/22
* desc :
* desc : CoordinateConvertUtils 单元测试
* </pre>
*/
public class CoordinateConvertUtilsTest {
......
package com.blankj.subutil.util;
import org.junit.Assert;
import org.junit.Test;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2016/09/26
* desc : GsonUtils 单元测试
* </pre>
*/
public class GsonUtilsTest {
@Test
public void getGson() {
Assert.assertNotNull(GsonUtils.getGson());
Assert.assertNotNull(GsonUtils.getGson(false));
}
@Test
public void toJson() {
Person person = new Person("Blankj");
System.out.println(GsonUtils.toJson(person));
System.out.println(GsonUtils.toJson(person, false));
}
@Test
public void fromJson() {
Person person = new Person("Blankj");
Assert.assertEquals(
person,
GsonUtils.fromJson("{\"name\":\"Blankj\",\"gender\":0,\"address\":null}", Person.class)
);
Assert.assertEquals(
person,
GsonUtils.fromJson("{\"name\":\"Blankj\",\"gender\":0}", Person.class)
);
}
class Person {
String name;
int gender;
String address;
public Person(String name) {
this.name = name;
this.gender = gender;
this.address = address;
}
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || !(obj instanceof Person)) return false;
Person p = (Person) obj;
return equals(name, p.name) && p.gender == gender && equals(address, p.address);
}
private boolean equals(final Object o1, final Object o2) {
return o1 == o2 || (o1 != null && o1.equals(o2));
}
}
}
\ No newline at end of file
......@@ -35,6 +35,7 @@ android {
dependencies {
compileOnly "com.android.support:appcompat-v7:$support_version"
compileOnly "com.android.support:design:$support_version"
testImplementation "junit:junit:$junit_version"
testImplementation "org.robolectric:robolectric:$robolectric_version"
testImplementation "com.android.support:support-v4:$support_version"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册