Allow to provide a fallback serializer to avoid throwing an exception for non serializable objects #161
Replies: 4 comments 4 replies
-
I don't think a SerializationFeature is good for this - a feature is an on/off flag. You probably need to be able to provide a Serialization Provider instance - different people would want to have different fallback behaviours. Little point hardcoding one behaviour - the fallback could be Java serialization, Java toString or any host of alternatives. Honestly, I don't see any of this providing much better support than users having try/catch blocks around the Jackson serialization call and then having the fallback serialization in the catch. If this is because Clojure users hate exceptions, Jackson is a Java lib and exceptions are ok in Java. Chesire can add its own support for providing a fallback function and having the internal code catch the Jackson exception and call the fallback function. |
Beta Was this translation helpful? Give feedback.
-
Agreed
I don't think this covers my use case since I'm trying to serialize an heterogeneous map of objects and some of them might be non-serializable, but I don't want the whole serialization to fail, just skip (or provide a default serialization) the ones that couldn't be serialized. If I use a try catch then I will have to deal with serializing the whole map.
This was the thing I was looking for, I'll try to see if a SerilizationProblemHandler could be introduced. Thanks. |
Beta Was this translation helpful? Give feedback.
-
Right, we'd probably need a way to register fallback In a way, existing interface |
Beta Was this translation helpful? Give feedback.
-
I was thinking in catching InvalidDefinitionException, which is the exception being thrown for example when no properties are found (j/write-value-as-string {:a (Object.)})
; Execution error (InvalidDefinitionException) at com.fasterxml.jackson.databind.exc.InvalidDefinitionException/from (InvalidDefinitionException.java:77).
; No serializer found for class java.lang.Object and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: clojure.lang.PersistentArrayMap[":a"]) Are you saying that wouldn't be a reasonable decision to trigger the fallback serialization?
Yes, I'm fine with that, since the main goal is to avoid the serialization to fail. |
Beta Was this translation helpful? Give feedback.
-
Intro
We are writing a logback layout to write structured logs in JSON using Jackson (through cheshire currently but also testing alternatives like jsonista) but we have to fallback to the unstructured logging if the event contains non-serializable objects.
The goal is to make the serialization succeed even in the presence of non-serializable objects, serializing those with a special placeholder or an empty string.
Alternatives
Avoid adding non-serializable objects to the log event
One alternative is to make the user responsible of passing only serializable arguments to the log event, however this is quite cumbersome since in a concrete case is a big map containing different configuration properties, and this is injected from user code, while the logging is happening in a framework, so the framework would have to be explicit on which properties to log, which is (a) verbose and (b) discards information that could be serialized but, since the framework only logs known properties, it is discarded.
This alternative looks like this in our codebase
Custom serializers
The jsonista library allows to configure the ObjectMapper, but we don't want to configure a Serializer for each one of the objects that are non-serializable, even if we do that for the ones we know today, new objects could be included in the config and the logs will fallback to the unstructured format.
Tweak ObjectMapper features
Configuring an ObjectMapper like this
will do te trick of not failing when, for example, an attempt to serialize an
clojure.lang.Atom
is made. The visibility prevents Jackson from using the getters and theFAIL_ON_EMPTY_BEANS
is what makes the serialization succeed writing an empty json object instead of failing.However, this has the disadvantage of affecting the expected behavior for POJOs which are serialized out of the box by using their getters.
Feature request
Add a
SerializationFeature
that will allow to fallback to a general serializer when an object can't be serialized, instead of throwing the exception.Related issues
Beta Was this translation helpful? Give feedback.
All reactions