How to configure Jackson mapper - kotlin

How can I globally configure json serializer for http4k? For example, snake case field names or formatting DateTime as ISO8601.

Since the ObjectMapper instance is private within ConfigurableJackson you cannot get at it after construction to do any configuration.
So you either need to construct your own direct instance of ConfigurableJackson and pass in a customized ObjectMapper or you need to subclass ConfigurableJackson with your own class. And then during the constructor, create an ObjectMapper (see example below) or intercept one being passed into your constructor and change its settings.
Whatever you do, be sure you do not break the http4k framework or anything else that might be using the same instance. You can see the defaults used by http4k declared in their source code:
object Jackson : ConfigurableJackson(ObjectMapper()
.registerModule(defaultKotlinModuleWithHttp4kSerialisers)
.disableDefaultTyping()
.configure(FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(FAIL_ON_IGNORED_PROPERTIES, false)
.configure(USE_BIG_DECIMAL_FOR_FLOATS, true)
.configure(USE_BIG_INTEGER_FOR_INTS, true)
)
You can use code similar to above to create your own instance.
See this thread for some conversation about this topic: https://github.com/http4k/http4k/issues/183

You don't necessarily need to extend ConfigurableJackson - it's just that extending it is the most convenient way to do this (in our experience).
All configuration is done by tweaking the ObjectMapper instance which is injected into the ConfigurableJackson constructor - the ConfigurableJackson itself just provides the wrapper API around that mapper. The question is to do with standard configuration of Jackson, so you should seek answers to your specific questions (snake case etc) from the Jackson docs directly as http4k doesn't own that API.

Related

How to use InjectableValues (for properties annotated as #JacksonInject) when using Spring RestTemplate?

Jackson's #JacksonInject annotation is useful for declaring properties of your deserialized object that are to be "injected" by the code calling for deserialization (as opposed to only being parsed from the JSON). To use this feature, it seems you have to either:
Set the InjectableValues into the ObjectMapper (which would tie them to that ObjectMapper instance and be used for all calls to it).
Get an ObjectReader [via ObjectMapper.reader(InjectableValues)] and use that ObjectReader directly to parse the JSON.
Unfortunately, neither of these is doable (from what I can see) when using Spring's RestTemplate without jumping through a lot of hoops. I don't want every object being deserialized from the RestTemplate to use injected values; nor do I see a way to customize how RestTemplate uses the underlying ObjectMapper1.
Is there a way to incorporate InjectableValues into RestTemplate's JSON deserialization?
1I suppose I could write my own custom HttpMessageConverter and figure out how to inject that into RestTemplate. But even then I don't see a way to pass the InjectableValues into ObjectMapper's read... methods. It's a lot of work even if I could.

Apache Ignite: type substitution on serialization

I have a class A, a cache A_CACHE and a proxy object AProxy extends A. My goal is to serialize AProxy objects as if they are A objects (automatically substitute type) and put them into A_CACHE.
Is there any way in Apache Ignite to substitute type of an object that I am trying to put into cache (serialize using BinarySerializer)?
What I have tried so far.
I have implemented and registered the same BinarySerializer for both types. I have also tried to play with BinaryNameMapper class to return the same class name for both classes, but without success. The only option that comes to my mind now is to use BinaryObjectBuilder. Is it really the only option for me?
After a small research the solution was found.
AProxy should implement writeReplace method of Serializable interface. Return proxied instance from this method. If proxied class is Serializable or Externalizable and one wants to apply custom serialization, than Binarylizable interface should be implemented by proxied class (custom binary serializers are not applied when using the hack above, but instead OptimizedMarshaller is being used).

Default JsonNetSerializer does not camel case property names

I'm injecting a custom serializer using NEST.JsonNetSerializer like this
var settings = new ConnectionSettings(connPool, sourceSerializer: JsonNetSerializer.Default);
When I was using the built in serializer it camel cased the property names for me (from snake case) automatically. How can I make the custom serializer work the same way? I see that I can use ConnectionSettings.DefaultFieldNameInferrer() to specify how property names are inferred. But it seems unneccessary to copy the NEST code to make it work as before.
The only reason I'm not using the built-in serializer is because I have to work with dynamic models, and the internal JSON.NET objects in NEST are inaccessible.

Spring Data Rest Make an Entity Read Only by Default

I have an API exposed via Spring Data Rest which, for the most part, is read-only but which allows for updating of some properties via PATCH requests.
Is there any (I'm supposing Jackson) configuration at a global level that would essentially make an entity read only unless specific properties were annotated in some way.
I am familiar with the#JsonProperty(access = Access.READ_ONLY) Jackson annotation however would like to avoid having to annotate all read-only properties.
For example, given the class below only the field explicitly annotated would be writable. All other fields would be readable by default:
public class Thing{
private String fieldOne;
#JsonProperty(access = Access.READ_WRITE)
private String fieldTwo;
private String fieldThree;
// a lot of other properties
}
Failing any global configuration, is there anything that can be applied at the class level?
I am not aware of any way to globally set all attributes in a class to read only. Since version 2.6+ of FaserXML you can use the following annotation to at least defined the set of properties you would ignore and only allow for serialization. The following annotation would be used at the class level:
#JsonIgnoreProperties(value={ "fieldOne", "fieldThree"}, allowGetters=true)
It is not exactly what you are looking for, but arguably makes coding a little easier.

How can i influence instance creation for annotated serializers

I wrote a jackson module to enable a specific type of serialization. Now i want to enable global configuration of one of the new serializers. so i have to set a property on a serializer instance during creation.
Is there a way i can do that from within a jackson module?
Module interface is stateless, one-of-thing, so it does not have default wiring to affect things it adds.
But what you can do is to use a work-around; possibilities include:
use of ThreadLocal; set before serialization, read from serializer
use new (Jackson 2.3) feature of "attributes"; can set those for writing (ObjectWriter.setAttribute()) and reading (ObjectReader.setAttribute()), accessible by serializer/deserializer through context object (SerializerProvider / DeserializationContext)
So hopefully one of these works for your use case.