I want to serialize an object using java jackson.
I want to serialize it once in a short version
and second in a full version.
I have added a #JsonIgnore annotation over the non-mandatory fields.
But how can I tell jackson to serialize all fields including #JsonIgnore when desired?
It is possible to ignore #JsonIgnore. I can think of 2 approaches:
Extend JacksonAnnotationIntrospector, over hasIgnoreMarker(...) method (I think that's the name -- can check javadocs)
Add #JsonIgnore(false) annotation override using "mix-in annotations": false means "do not ignore", and is explicitly allowed to be used (default value is true) for such overrides.
However: a better way would be to use JSON Views, or JSON filters.
Related
I'm using Java jackson for dynamic serialization and deserialization to/from json.
I want to have a custom annotation #JsonDebug, which will basically turn on/off the serialization of
field etc. depending on a static variable debugMode. The idea is that if an enum key is tagged with the annotation,
it shouldn't be useable while serialization/deserialization process is on.
The problem is that the only way I find doing it is by either implementing custom serializer/deserializer for the enum I want, which is not a general solution or by overriding the existing Enum serialization/deserialization process which is kind of a overkill for a single annotation processing. I tried using AnnotationIntrospector and override behavior of hasIgnoreMarker which is the way it works for fields, but it doesn't ignore enum keys. I thought mixin annotations might be the way to go, but they only work for specified classes with specified field names. I found out that even with ignore annotations on keys, they still serialize/deserialize to/from json.
.
Is there a way to make #JsonIgnoreProperties target nested attributes?
Something like the code below:
public class ParentObject() {
#JsonIgnoreProperties({ "subAttributeA.subAttributeB.subAttributeC" })
private ChildObject attribute;
}
In this example, I want that subAttributeC is not included in the serialization of a ParentObject - but this same subAttributeC still need to be serializable in other scenarios.
If not possible with annotations, how to achieve this?
One possible way is to use #JsonSerialize with a custom serializer. This operates on a low abstraction level and you basically have to specify how to serialize every single attribute. There's an example here that uses a flat object, but given that JsonGenerator has methods like writeObject etc., I'm sure this can be used for a hierarchical structure, too.
If jackson is configured to not serialize null attributes:
objectMapper.setSerializationInclusion(Include.NON_NULL);
Then a simpler workaround is to set the unwanted attribute as null before returning it.
I found that jackson comes equipped with a UUID serizlizer/deserializer that can be used like this:
#Data
#NoArgsConstructor
public class MyClass {
#JsonSerialize(using=UUIDSerializer.class)
#JsonDeserialize(using=UUDIDeserializer.class)
private UUID myUUID;
}
And then using ObjectMapper on MyClass will correctly serialize/deserialize the myUUID field.
However, my class has a set of UUIDs that I want to serialize. I tried annotating the field the same way as above, but it complains that Set cannot be converted to UUID (as I half expected).
I know I can create my own serializer/deserializers by extending JsonSerializer/JsonDeserializer, but this feels hacky. Is there another solution I can use? I also don't have the option to configure the ObjectMapper with my classes, since I don't have access to the ObjectMapper. I am using Amazon SWF and it automatically uses Jackson.
Jackson should automatically use UUID serializers, deserializers, so your annotations should not be necessary.
But as to annotation usage, as suggested, (de)serializer for content (instead of value itself!) does need to use contentUsing property of the annotation -- otherwise Jackson will try to apply given (de)serializer directly for the value, with reported mismatch,
Jackson 2.0 has pretty good support for cyclic reference, however, it seems that all require to annotate the POJO classes. What if I cannot edit the java classes I want to serialize to json, but want to just ignore cyclic reference like the json lib jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT) which basically ignore cyclic reference when one occurs.
No. Jackson does not do full cycle detection; identity checks are only done when annotation indicates that they are needed.
We just upgraded some maven dependencies, that triggered an update of Jackson from 1.7.x to 1.9.x
We use to annotate #JsonIgnore on setter methods, on methods not supposed to be set from client side. for example: the owner of an object (with should be set from the authentication principal), etc.
It seems to us that the semanthincs of this annotation have changed, is that possible? now the field is been jsonignored in all case, and not only when being set.
is that the case? is there an alternative way in 1.9 to implement ignore SET only?
thanks!
r.
Ok, I think I found and explicit mention to this issue in the javadoc. (jackson documentation is not easy to go through).
In addition, starting with Jackson 1.9, if this is the only annotation associated with a property, it will also cause cause the whole property to be ignored: that is, if setter has this annotation and getter has no annotations, getter is also effectively ignored. It is still possible for different accessors to use different annotations; so if only "getter" is to be ignored, other accessors (setter or field) would need explicit annotation to prevent ignoral (usually JsonProperty).