Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wd1105040417
retrofit
提交
fb3a40c8
R
retrofit
项目概览
wd1105040417
/
retrofit
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
retrofit
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
fb3a40c8
编写于
9月 17, 2016
作者:
J
Jake Wharton
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add method for returning a Moshi conveter that serializes nulls.
上级
a2765946
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
34 addition
and
7 deletion
+34
-7
retrofit-converters/moshi/src/main/java/retrofit2/converter/moshi/MoshiConverterFactory.java
...java/retrofit2/converter/moshi/MoshiConverterFactory.java
+12
-5
retrofit-converters/moshi/src/main/java/retrofit2/converter/moshi/MoshiRequestBodyConverter.java
.../retrofit2/converter/moshi/MoshiRequestBodyConverter.java
+7
-2
retrofit-converters/moshi/src/test/java/retrofit2/converter/moshi/MoshiConverterFactoryTest.java
.../retrofit2/converter/moshi/MoshiConverterFactoryTest.java
+15
-0
未找到文件。
retrofit-converters/moshi/src/main/java/retrofit2/converter/moshi/MoshiConverterFactory.java
浏览文件 @
fb3a40c8
...
...
@@ -50,21 +50,28 @@ public final class MoshiConverterFactory extends Converter.Factory {
/** Create an instance using {@code moshi} for conversion. */
public
static
MoshiConverterFactory
create
(
Moshi
moshi
)
{
return
new
MoshiConverterFactory
(
moshi
,
false
);
if
(
moshi
==
null
)
throw
new
NullPointerException
(
"moshi == null"
);
return
new
MoshiConverterFactory
(
moshi
,
false
,
false
);
}
private
final
Moshi
moshi
;
private
final
boolean
lenient
;
private
final
boolean
serializeNulls
;
private
MoshiConverterFactory
(
Moshi
moshi
,
boolean
lenient
)
{
if
(
moshi
==
null
)
throw
new
NullPointerException
(
"moshi == null"
);
private
MoshiConverterFactory
(
Moshi
moshi
,
boolean
lenient
,
boolean
serializeNulls
)
{
this
.
moshi
=
moshi
;
this
.
lenient
=
lenient
;
this
.
serializeNulls
=
serializeNulls
;
}
/** Return a new factory which uses {@linkplain JsonAdapter#lenient() lenient} adapters. */
public
MoshiConverterFactory
asLenient
()
{
return
new
MoshiConverterFactory
(
moshi
,
true
);
return
new
MoshiConverterFactory
(
moshi
,
true
,
serializeNulls
);
}
/** Return a new factory which includes null values into the serialized JSON. */
public
MoshiConverterFactory
withNullSerialization
()
{
return
new
MoshiConverterFactory
(
moshi
,
lenient
,
true
);
}
@Override
...
...
@@ -84,7 +91,7 @@ public final class MoshiConverterFactory extends Converter.Factory {
if
(
lenient
)
{
adapter
=
adapter
.
lenient
();
}
return
new
MoshiRequestBodyConverter
<>(
adapter
);
return
new
MoshiRequestBodyConverter
<>(
adapter
,
serializeNulls
);
}
private
static
Set
<?
extends
Annotation
>
jsonAnnotations
(
Annotation
[]
annotations
)
{
...
...
retrofit-converters/moshi/src/main/java/retrofit2/converter/moshi/MoshiRequestBodyConverter.java
浏览文件 @
fb3a40c8
...
...
@@ -16,6 +16,7 @@
package
retrofit2.converter.moshi
;
import
com.squareup.moshi.JsonAdapter
;
import
com.squareup.moshi.JsonWriter
;
import
java.io.IOException
;
import
okhttp3.MediaType
;
import
okhttp3.RequestBody
;
...
...
@@ -26,14 +27,18 @@ final class MoshiRequestBodyConverter<T> implements Converter<T, RequestBody> {
private
static
final
MediaType
MEDIA_TYPE
=
MediaType
.
parse
(
"application/json; charset=UTF-8"
);
private
final
JsonAdapter
<
T
>
adapter
;
private
final
boolean
serializeNulls
;
MoshiRequestBodyConverter
(
JsonAdapter
<
T
>
adapter
)
{
MoshiRequestBodyConverter
(
JsonAdapter
<
T
>
adapter
,
boolean
serializeNulls
)
{
this
.
adapter
=
adapter
;
this
.
serializeNulls
=
serializeNulls
;
}
@Override
public
RequestBody
convert
(
T
value
)
throws
IOException
{
Buffer
buffer
=
new
Buffer
();
adapter
.
toJson
(
buffer
,
value
);
JsonWriter
writer
=
JsonWriter
.
of
(
buffer
);
writer
.
setSerializeNulls
(
serializeNulls
);
adapter
.
toJson
(
writer
,
value
);
return
RequestBody
.
create
(
MEDIA_TYPE
,
buffer
.
readByteString
());
}
}
retrofit-converters/moshi/src/test/java/retrofit2/converter/moshi/MoshiConverterFactoryTest.java
浏览文件 @
fb3a40c8
...
...
@@ -119,6 +119,7 @@ public final class MoshiConverterFactoryTest {
private
Service
service
;
private
Service
serviceLenient
;
private
Service
serviceNulls
;
@Before
public
void
setUp
()
{
Moshi
moshi
=
new
Moshi
.
Builder
()
...
...
@@ -137,6 +138,7 @@ public final class MoshiConverterFactoryTest {
.
build
();
MoshiConverterFactory
factory
=
MoshiConverterFactory
.
create
(
moshi
);
MoshiConverterFactory
factoryLenient
=
factory
.
asLenient
();
MoshiConverterFactory
factoryNulls
=
factory
.
withNullSerialization
();
Retrofit
retrofit
=
new
Retrofit
.
Builder
()
.
baseUrl
(
server
.
url
(
"/"
))
.
addConverterFactory
(
factory
)
...
...
@@ -145,8 +147,13 @@ public final class MoshiConverterFactoryTest {
.
baseUrl
(
server
.
url
(
"/"
))
.
addConverterFactory
(
factoryLenient
)
.
build
();
Retrofit
retrofitNulls
=
new
Retrofit
.
Builder
()
.
baseUrl
(
server
.
url
(
"/"
))
.
addConverterFactory
(
factoryNulls
)
.
build
();
service
=
retrofit
.
create
(
Service
.
class
);
serviceLenient
=
retrofitLenient
.
create
(
Service
.
class
);
serviceNulls
=
retrofitNulls
.
create
(
Service
.
class
);
}
@Test
public
void
anInterface
()
throws
IOException
,
InterruptedException
{
...
...
@@ -207,6 +214,14 @@ public final class MoshiConverterFactoryTest {
assertThat
(
body
.
theName
).
isEqualTo
(
"value"
);
}
@Test
public
void
withNulls
()
throws
IOException
,
InterruptedException
{
server
.
enqueue
(
new
MockResponse
().
setBody
(
"{}"
));
Call
<
AnImplementation
>
call
=
serviceNulls
.
anImplementation
(
new
AnImplementation
(
null
));
call
.
execute
();
assertEquals
(
"{\"theName\":null}"
,
server
.
takeRequest
().
getBody
().
readUtf8
());
}
@Test
public
void
utf8BomSkipped
()
throws
IOException
{
Buffer
responseBody
=
new
Buffer
()
.
write
(
ByteString
.
decodeHex
(
"EFBBBF"
))
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录