我正在使用Jackson 2.1.0.鉴于:
public static final class GetCompanies
{
private final List<URI> companies;
/**
* Creates a new GetCompanies.
* <p/>
* @param companies the list of available companies
* @throws NullPointerException if companies is null
*/
@JsonCreator
public GetCompanies(@JsonUnwrapped @NotNull List<URI> companies)
{
Preconditions.checkNotNull(companies, "companies");
this.companies = ImmutableList.copyOf(companies);
}
/**
* @return the list of available companies
*/
@JsonUnwrapped
@SuppressWarnings("ReturnOfCollectionOrArrayField")
public List<URI> getCompanies()
{
return companies;
}
}
Run Code Online (Sandbox Code Playgroud)
当输入列表包含时http://test.com/,杰克逊生成:
{"companies":["http://test.com/"]}
Run Code Online (Sandbox Code Playgroud)
代替:
["http://test.com/"]
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
更新:有关相关讨论,请参阅https://github.com/FasterXML/jackson-core/issues/41.
Sta*_*Man 24
在这种情况下,如果这样做,你最终会尝试产生以下内容:
{ "http://test.com" }
Run Code Online (Sandbox Code Playgroud)
这不是合法的JSON.@JsonUnwrapped真的只是删除一层包装.虽然它理论上可以用于"数组阵列"的情况,但事实并非如此.事实上,我想知道添加此功能是否是一个错误:主要是因为它鼓励使用经常违反数据绑定的最佳实践(简单,一对一映射).
但是,相反的是@JsonValue:
@JsonValue
private final List<URI> companies;
Run Code Online (Sandbox Code Playgroud)
这意味着"使用此属性的值而不是序列化包含它的对象".
并且创建者方法实际上可以按原样工作,不需要任何一个@JsonUnwrapped或者@JsonProperty.
这是更正后的代码:
public static final class GetCompanies
{
private final List<URI> companies;
/**
* Creates a new GetCompanies.
* <p/>
* @param companies the list of available companies
* @throws NullPointerException if companies is null
*/
@JsonCreator
public GetCompanies(@NotNull List<URI> companies)
{
Preconditions.checkNotNull(companies, "companies");
this.companies = ImmutableList.copyOf(companies);
}
/**
* @return the list of available companies
*/
@JsonValue
@SuppressWarnings("ReturnOfCollectionOrArrayField")
public List<URI> getCompanies()
{
return companies;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10045 次 |
| 最近记录: |