JSON serialization between Python and Java

266 views
Skip to first unread message

Ryan Lubke

unread,
Oct 14, 2017, 11:37:28 PM10/14/17
to jackson-user
Hey Folks,

Fairly new to Jackson, so if this is covered in the docs somewhere, my apologies.

I'm working on a POC to serialize objects between Python and Java using a Python library called jsonpickle [1]

Everything has been working fine until I moved to a more complex scenario involving Python classes.  In order to support proper deserialization into proper Python objects, I had to enable a flag that inserts type data into the stream (similar to Jackson).  However, this flag is global.  There currently is no way to turn this on and off per entity, i.e., include the metadata for Python objects, but not collection types such as Set.

Give the above, consider the following sent from Python to Java:

{
 
"py/object": "test.GetAll",
 
"@class": "test.GetAll",
 
"cacheName": "test-cache",
 
"keys":
 
{
   
"py/set": [ 1, 2 ]
 
}
}

The deserialization process chokes when dealing with the type py/set (stack trace [2] if anyone thinks it would help).  

My question is this:  Is there a configuration for Jackson such that if it comes across a field name that would typically be used for a type
and it doesn't understand said type, to fall back to reflection of the container object's bean type for that field?


Tatu Saloranta

unread,
Oct 14, 2017, 11:53:46 PM10/14/17
to jackson-user
Jackson always bases its decision on when to accept (or, rather, require) type information (and when to include it in serialization) on metadata about property being processed (based on type and property declaration, annotations, and possibly rules of default typing).
Conversely, input json is not considered for this purpose: by the time json is being read, decision will have been made. This is not something that is easy to change given that handling of polymorphic types is quite different from regular types, type information being handled using separate serializers/deserializers that wrap content serializers/deserializers.

So: with out-of-the-box functionality you can tailor rules based on annotations on types and properties, as well as purely based on types (if enabling default typing). But not on whether something is encountered.

You can probably register custom deserializers that do something like this, however: deserializer could, for example, read content as `JsonNode` and then determine what to do. It could use and remove type information, and then depending on if such was found or not delegate to appropriate Jackson deserializer. You might want to limit usage of this to Python types if it is possible to recognize those solely based on type; if not, you would want to use this wrapping deserializer for all types.

-+ Tatu +-

 
Reply all
Reply to author
Forward
0 new messages