Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mmm-rain
AndroidUtilCode
提交
f15de033
A
AndroidUtilCode
项目概览
mmm-rain
/
AndroidUtilCode
与 Fork 源项目一致
从无法访问的项目Fork
通知
4
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
AndroidUtilCode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
f15de033
编写于
4月 06, 2018
作者:
B
Blankj
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
see 04/06 log
上级
7666744d
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
197 addition
and
2 deletion
+197
-2
build.gradle
build.gradle
+1
-0
subutil/build.gradle
subutil/build.gradle
+2
-0
subutil/src/main/java/com/blankj/subutil/util/GsonUtils.java
subutil/src/main/java/com/blankj/subutil/util/GsonUtils.java
+124
-0
subutil/src/test/java/com/blankj/subutil/util/ClipboardUtilsTest.java
...test/java/com/blankj/subutil/util/ClipboardUtilsTest.java
+1
-1
subutil/src/test/java/com/blankj/subutil/util/CoordinateConvertUtilsTest.java
...a/com/blankj/subutil/util/CoordinateConvertUtilsTest.java
+1
-1
subutil/src/test/java/com/blankj/subutil/util/GsonUtilsTest.java
.../src/test/java/com/blankj/subutil/util/GsonUtilsTest.java
+67
-0
utilcode/build.gradle
utilcode/build.gradle
+1
-0
未找到文件。
build.gradle
浏览文件 @
f15de033
...
...
@@ -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'
...
...
subutil/build.gradle
浏览文件 @
f15de033
...
...
@@ -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
subutil/src/main/java/com/blankj/subutil/util/GsonUtils.java
0 → 100644
浏览文件 @
f15de033
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
();
}
}
subutil/src/test/java/com/blankj/subutil/util/ClipboardUtilsTest.java
浏览文件 @
f15de033
...
...
@@ -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
)
...
...
subutil/src/test/java/com/blankj/subutil/util/CoordinateConvertUtilsTest.java
浏览文件 @
f15de033
...
...
@@ -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
{
...
...
subutil/src/test/java/com/blankj/subutil/util/GsonUtilsTest.java
0 → 100644
浏览文件 @
f15de033
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
utilcode/build.gradle
浏览文件 @
f15de033
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录