简体   繁体   English

如何控制库类的 Jackson 序列化

[英]How to control Jackson serialization of a library class

I have a class (let us call it a Piece ) containing a member of type com.jme3.math.ColorRGBA .我有一个类(让我们称之为Piece )包含com.jme3.math.ColorRGBA类型的成员。 With a default Jackson serialization the member is serialized not only as its members r , g , b and a , but then also using getters like getAlpha .使用默认的 Jackson 序列化,成员不仅被序列化为其成员rgba ,而且还使用getAlpha类的getAlpha

As this is obviously redundant, I would like to control the serialization and to serialize only those primary members.由于这显然是多余的,我想控制序列化并仅序列化那些主要成员。 Are there some annotations that I can write to my class to control serialization of members with types not under my control, or some custom serializers for them?是否有一些注释可以写入我的类来控制类型不受我控制的成员的序列化,或者它们的一些自定义序列化器?

I can probably write a custom serializer for the Piece class, though other than ColorRGBA serializer being too verbose, default serialization works fine for me for all other properties of Piece , therefore I would like to customize as little of it as possible.我可能可以为Piece类编写一个自定义序列化程序,尽管除了ColorRGBA序列化程序过于冗长之外,默认序列化对Piece所有其他属性都适用,因此我想尽可能少地自定义它。

I do not want to modify jme3 library source, the solution should be implemented outside of the ColorRGBA class.我不想修改jme3库源,解决方案应该在ColorRGBA类之外实现。

You can use a mixin to make sure that the class is serialized as per your needs.您可以使用mixin来确保根据您的需要对类进行序列化。 Consider the following:考虑以下:

// The source class (where you do not own the source code)
public class ColorRGBA {
    public float a; // <-- Want to skip this one
    public float b;
    public float g;
    public float r;
}

Then, create the mixin where you ignore the a property.然后,创建忽略a属性的 mixin。

// Create a mixin where you ignore the "a" property
@JsonIgnoreProperties("a")
public abstract class RGBMixin {
    // Other settings if required such as @JsonProperty on abstract methods.
}

Lastly, configure your mapper with the mixin:最后,使用 mixin 配置映射器:

ObjectMapper mapper = new ObjectMapper();
mapper.addMixInAnnotations(ColorRGBA.class, RGBMixin.class);
System.out.println(mapper.writeValueAsString(new ColorRGBA()));

The output will be:输出将是:

{"b":0.0,"g":0.0,"r":0.0} {"b":0.0,"g":0.0,"r":0.0}

Note that the method ObjectMapper.addMixInAnnotations is deprecated from Jackson 2.5 and should be replaced with the more fluent version:请注意,方法ObjectMapper.addMixInAnnotations已从 Jackson 2.5 弃用,应替换为更流畅的版本:

mapper.addMixIn(ColorRGBA.class, RGBMixin.class);

The JavaDocs can be found here JavaDocs 可以在这里找到

Option A选项 A

If you control the source of the class, can put this above class:如果你控制了类的来源,可以把这个放在类上面:

@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
public class ColorRGBA {

Option B选项 B

Otherwise you can set up an object mapper to ignore getters:否则,您可以设置一个对象映射器来忽略 getter:

ObjectMapper mapper = new ObjectMapper();
mapper.setVisibilityChecker(mapper.getSerializationConfig().getDefaultVisibilityChecker()
                .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));

mapper.writeValue(stream, yourObject);

Option C选项 C

For more complicated requirements, you can write your own VisibilityChecker implementation.对于更复杂的要求,您可以编写自己的VisibilityChecker实现。

It is possible to write a custom serializer for the member only, using annotation for the member like this:可以只为成员编写自定义序列化程序,使用成员的注释,如下所示:

@JsonSerialize(using = CustomColorRGBASerializer.class)
ColorRGBA color;

See also this answer about how to custom-serialize a Date field .另请参阅有关如何自定义序列化日期字段的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM