在JSON序列化中如何在Class1是Class2的字段的情况下如何忽略一些Class1字段? [英] In JSON serialization howto ignore some of Class1 field(s) in case Class1 is a field of Class2?
问题描述
我如何忽略对象中的对象字段?
/ p>
我认为通过一个例子可以更容易理解:
(编者按:
Class1
= Engine
Class2
= <$ c $ class Car {
)
整数id;车辆
整数numberOfWheels;
引擎引擎;
}
类引擎{
整数ID;
字符串名称;
字符串生成器;
整数horsePower;
整数重量;
$ / code>
Car JSON应包含所有字段,但Car中的Engine对象对象应限制为 id,name,producer
。
{
id:1,
numberOfWheels:4,
engine:{
id:1,
name:some engine
producer:某个引擎生产者
}
}
<然而,引擎JSON应该包含所有字段 id,name,producer,horsePower,weight
{
id:1,
name:some engine
producer:一些引擎制作者
horsePower :250
重量:500
}
澄清。只有在Car生成的JSON中才会忽略字段 horsePower
和 weight
。
看看
gson
s JsonSerializer
和 ExclusionStrategy
。也许不是最简单的方法 - 特别是与你自己的解决方案相比 - 这是一个很好的选择。 启用 Car
有一些特殊待遇创建
JsonSerializer
like
公共类CarSerializer实现了JsonSerializer< Car> {
private final Gson gson = new GsonBuilder()
.addSerializationExclusionStrategy(new FieldExclusionStrategy())。create();
@Override
public JsonElement serialize(Car arg0,Type arg1,JsonSerializationContext arg2){
return new JsonParser()。parse(gson.toJson(arg0));
$ / code $ / pre
$ b $ p 上面有它自己的 gson
只能处理 Car
,而不能混淆其他序列化。前面提到的 ExclusionStrategy
指向它自己的私人用途,它检查如果 Car
s字段是 Engine
然后跳过 Engine
中的任何不需要的字段。
public class FieldExclusionStrategy实现ExclusionStrategy {
private Set< String>忽略=
新的HashSet< String>(Arrays.asList(new String [] {horsePower,weight}));
@Override
public boolean shouldSkipField(FieldAttributes arg0){
if(arg0.getDeclaringClass()。isAssignableFrom(Engine.class))
if(ignored.contains(arg0.getName ()))返回true;
返回false;
}
@Override
public boolean shouldSkipClass(Class<?> arg0){return false; }
}
它可以与 gson 注册 JsonSerializer
作为它的类型适配器,用于 Car.class
:
Gson gson = new GsonBuilder()。setPrettyPrinting()
.registerTypeAdapter(Car.class,new CarSerializer())。create();
I searched for this question on stackoverflow and google and I couldn't find any proper answer.
How do I ignore fields from an object within an object?
I think it will be much easier to understand with an example:
(Editors note, in title: Class1
=Engine
a field for Class2
=Car
)
class Car {
Integer id;
Integer numberOfWheels;
Engine engine;
}
class Engine {
Integer id;
String name;
String producer;
Integer horsePower;
Integer weight;
}
The Car JSON should consist of all fields, but the Engine object within the Car object should be limited to id, name, producer
.
{
"id":1,
"numberOfWheels":4,
"engine": {
"id":1,
"name":"some engine"
"producer":"some engine producer"
}
}
The Engine JSON should however consist of all fields id, name, producer, horsePower, weight
{
"id":1,
"name":"some engine"
"producer":"some engine producer"
"horsePower":250
"weight":500
}
Just to clarify. The fields horsePower
and weight
should only be ignored in the JSON generated from Car.
解决方案 Have a look on gson
s JsonSerializer
and ExclusionStrategy
. Maybe not the most terse way to do it - especially compared to your own solution - but a good option in general.
To enable Car
to have some special treatment create JsonSerializer
like
public class CarSerializer implements JsonSerializer<Car> {
private final Gson gson = new GsonBuilder()
.addSerializationExclusionStrategy(new FieldExclusionStrategy()).create();
@Override
public JsonElement serialize(Car arg0, Type arg1, JsonSerializationContext arg2) {
return new JsonParser().parse(gson.toJson(arg0));
}
}
Above has its own gson
to handle only Car
and not to mess any other serialization. Beforementioned registers ExclusionStrategy
to its own private use that checks that if Car
s field is Engine
then any unwanted field in Engine
is skipped.
public class FieldExclusionStrategy implements ExclusionStrategy {
private Set<String> ignored =
new HashSet<String>(Arrays.asList( new String[]{"horsePower","weight"}));
@Override
public boolean shouldSkipField(FieldAttributes arg0) {
if(arg0.getDeclaringClass().isAssignableFrom(Engine.class))
if(ignored.contains(arg0.getName())) return true;
return false;
}
@Override
public boolean shouldSkipClass(Class<?> arg0) { return false; }
}
And it can be used with gson
that registers JsonSerializer
as its type adapter for Car.class
:
Gson gson = new GsonBuilder().setPrettyPrinting()
.registerTypeAdapter(Car.class, new CarSerializer()).create();
这篇关于在JSON序列化中如何在Class1是Class2的字段的情况下如何忽略一些Class1字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!