I normally use objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL) because I never want the null values of my classes serialized. Except now I have a specific field should be written out, even if it is null. Is there a quick annotation I can put on this one field that overrides the Inclusion.NON_NULL property for that one field? What's a good way to achieve this?
With Jackson 1.x you can use #JsonSerialize(include = Inclusion.ALWAYS) and with Jackson 2.x you can use #JsonInclude(Include.ALWAYS). These annotations will override the default config from your ObjectMapper.
#user1433372, JsonInclude is an annotation only for Jackson 2.x.
in Jackson 1.9
#JsonSerialize(include=JsonSerialize.Inclusion.NON_EMPTY)
is the same in Jackson 2.x as
#JsonInclude(JsonInclude.Include.NON_EMPTY)
With Jackson 1.9 is used #JsonSerialize(include= JsonSerialize.Inclusion.ALWAYS)
#JsonSerialize(include=Include.ALWAYS) did not compile.
Related
When a Kotlin class has a property that is one of the new #JvmInline value classes, Jackson converts it to JSON with a weird suffix on the field name.
e.g.
#JvmInline
value class ModelName(val value: String)
data class MyDto(val modelName: ModelName)
Jackson will produce JSON that looks like this:
{
"modelName-11MJ8YI": "Some Model Name"
}
I've tried adding a #JsonProperty("modelName") annotation but it doesn't make a difference.
Probably, upgrading jackson-module-kotlin will solve this problem.
https://github.com/FasterXML/jackson-module-kotlin/issues/356
If you can't upgrade, naming the getter as #get:JvmName("getModelName") should also solve the problem.
The reason for this problem is that the name of the method (getter) for the value class in Kotlin has a random suffix.
I have JAXB annotated class which i am trying to convert to JSON using jackson objectMapper. The issue with generated json is jackson is introduction a new property value:"xxx" for XMLValue annotation which is dont value. for example it is generated as below
{
"employeeName":
{
"value":"ABC"
}
}
but i want it as {"employeeName":"ABC"} instead
One more thing is while it wraps the arrays i am getting as below in the generated json
{
"Employers":{
"Employer":[
{
"name":"ddd"
}
]
}
instead i want it as only
{"Employer":[{"name":"ddd"}]}
Can someone suggest if above two are possible with jackson.
Yes, this is possible. It might be helpful when you provide your class so you could get a more specific answer which might use annotations. But at least you could write a custom serializer. If you do so you have full control over the produced json.
I just started developing in Java and chose Jersey (2.4.1) as the framework I will use to build restful services.
When serializing a model, is there any annotation (moxy or jackson) to not serialize a property only if the value is default?
For example, in the case of reference types this would be null, but in the case of an int or long, the default value is 0.
Is this possible?
Just figured this out. Using Jackson, you would use the following annotation:
#JsonSerialize(include=JsonSerialize.Inclusion.NON_DEFAULT)
public class User
{
...
}
Haven't figured out MOXy yet, however.
I'm using Jackson 2.1.4 and want to serialize collections of objects that extend an Event class, and where the JSON has to include the class name for polymorphism purposes. I have put a #JsonTypeInfo annotation on Event, but unfortunately I can't use CLASS as strategy, because my object instantiation pattern is "new FooEvent(){{bar=someValue;}}", which will create anonymous inner classes. I want to put in "FooEvent" as the type in JSON, rather than the anonymous inner class.
It seems like what I need is to do this:
#JsonTypeInfo(use= JsonTypeInfo.Id.CUSTOM, include= JsonTypeInfo.As.PROPERTY, property="type")
But how do I register my own custom id serializer that will output the name of the class?
Figured out how to solve this: basically add a #JsonTypeIdResolver annotation on the same class as has #JsonTypeInfo, then implement a resolver which it can refer to. Here's what mine looks like. It essentially just removes anonymous inner class if present.
Is it possible to use Morphia in Scala?
Are there any other lightweight ORMs for MongoDB that support scala?
Check out Salat:
https://github.com/novus/salat
Salat uses pickled Scala signatures to serialize and deserialize case classes.
Morphia is just a persistence layer based on mongo-java-driver that uses annotation in a JPA-style for object mapping. It should perfectly work with Scala.
Among the "native" Scala drivers (worth to mention that all of them are also based on mongo-java-driver), Rogue (developed by Foursquare) is the closest ideologically to Morphia (though it doesn't use annotations, which aren't considered to be Scala-idiomatic).
I prefer "Mongo Scala Driver":
https://github.com/osinka/mongo-scala-driver
Morphia is probably much more approachable and has a (much) smoother learning curve, but it's crucial to realize that the static type-safety and auto-completion support Rogue gives you when querying is really one level above Morphia—Morphia is only runtime safe, which they also admit right the beginning of the README.
Compare:
val checkin: Option[Checkin] =
Checkin where (_.venueid eqs id)
and (_.userid eqs mayor.id)
and (_.cheat eqs false)
and (_._id after sixtyDaysAgo)
limit(1).get()
vs
Employee scottsBoss =
ds.find(Employee.class).filter("underlings", scottsKey).get();
If you change any of the field names or query values to be incorrect, you'll get an immediate typing error, whereas Morphia will only throw an exception at runtime.
See http://engineering.foursquare.com/2011/01/21/rogue-a-type-safe-scala-dsl-for-querying-mongodb/