Comments (9)
Quick note -- related issue for Jackson core is [http://jira.codehaus.org/browse/JACKSON-695].
I'll have to look, I only now realized that it is actually related to the empty value comparison thing.
from jackson-module-scala.
One possibly related thing is that JsonDeserializer has getEmptyValue() accessor, used mostly to work around problem with primitive/wrapper value handling. But perhaps similar method should be added in JsonSerializer (with default impl of just retuning null) -- the idea here being that if a non-null value is returned, it can be used for equality comparison to determine if a given value of that type is empty; PropertyBuilder could easily construct a standard checker object that simply calls emptyValue.equals(comparedValue).
This seems like a relatively simple way to achieve extensibility, since registering of deserializers is already fully supported.
from jackson-module-scala.
I like this plan, though obviously emptyValue.equals(comparedValue)
won't work so well if the default emptyValue
is null
. But in general I like the idea that the serializer can own this logic.
from jackson-module-scala.
Yes, although since one can easily return object that is not of expected type (but rather artificial object with equals() redefined), it is possible to also accept null. This is actually how NON_EMPTY comparators for arrays and Collections work -- it is bit of abuse of equals(), but seems acceptable in this case.
This may be a good example where we would have been better off, if original idea (for getEmptyValue() used with JsonDeserializer) had been thought of more broadly. Mea culpa. :)
from jackson-module-scala.
Chris -- can you formulate improvement idea in a statement to include in that "plans for 2.0" page? It sounds like the thing to do for 2.0, where we can easily add new methods to JsonSerializer/-Deserializer
from jackson-module-scala.
Sorry this is late in coming, I haven't had time to think about how to state the idea in my head. The gist of it goes:
Expose the representation of the attributes and qualities of a type separately from the
Serializer
orDeserializer
that encodes/decodes them.
The basic theory is that the set of representations in JSON is small, so we can define the set of operations on a type that might stand for those representations a priori. This is kind of like a pimped out Bean Descriptor, that has an API that custom types can define. Things like:
- What kind of values does this type encode? Scalar (boolean, numeric, string), Vector, or Map?
- Does this type have a 'null' value, and if so what is it?
- For Scalars:
- Given an instance of this type, produce the correct scalar value
- For a given scalar value, produce an instance of this type, if possible.
- For Vectors:
- Is the length of the vector fixed or variable?
- What is the 'n-th' value of this vector?
- What is the type descriptor of the 'n-th' value of this vector?
- Create a builder for this type that can accept values in order and can construct a valid instance of this type when complete.
- For Maps:
- is the set of keys fixed or variable?
- What is the type descriptor for keys?
- What is the value of key "foo"?
- What is the type descriptor for key "foo"?
- Create a builder for this type that can accept keys/value pairs and can construct a valid instance of this type when complete.
With this kind of framework in place, in many cases you might not need a new serializer or deserializer for a type, just a TypeDescriptor
, which a standard ScalarSerializer
, VectorSerializer
, or MapDeserializer
could pull values from, and the corresponding Deserializers
would use to create builders to feed values into.
Some things get complicated, like Dates. We'd need a way for a TypeDescriptor
to express the idea of multiple valid representations, such as numeric or string, some expression of which representation is the default, and an appropriate annotation to override the default. Descriptors that can serialize as a string could optionally support a format string, to allow for type-specific formatting.
I could dive more deeply into detail, but I think I've gone on long enough. Let me know if any of this makes sense.
from jackson-module-scala.
I will have to read & think about this. At high (enough) level I think it does make sense.
from jackson-module-scala.
Quick note: [JACKSON-695] now implemented for 2.0; JsonSerializer has 'boolean isEmpty(T value)' method, so Scala collection/map/option types can now add support for explicit pruning of values considered empty.
from jackson-module-scala.
As of 361d4b1 I have specifically added this for Map, Option, and Iterable. I considered adding it for Tuple but rejected it as there is no Tuple0 that would reasonably be empty.
from jackson-module-scala.
Related Issues (20)
- Release 2.16.0-rc1 HOT 1
- error when deserializing yaml HOT 2
- Release 2.16.0 HOT 2
- release jackson-module-scala v2.12 for Scala 2.10 HOT 2
- Release 2.16.1 HOT 1
- Update `2.17` branch to `2.17.0-rc1-SNAPSHOT` HOT 2
- Release 2.15.4 HOT 1
- Release 2.17.0-rc1 HOT 1
- Release 2.16.2 HOT 2
- Release 2.17.0 HOT 2
- Performance regression with 2.17.x HOT 32
- Update 2.17 release note wiki with Scala module 2.17 fixes HOT 4
- Release 2.17.1 HOT 1
- See if there is benefit from integrating with jackson-databind better wrt detecting "canonical" Constructor for Scala (case) classes HOT 4
- Jackson Scala module build from`master` (v3) fails HOT 16
- investigate OptionSerializerTest default typing test and CaseClassSerializerTest serialize java getters test
- Release 2.17.2 HOT 2
- potentially implement annotation introspector findPrimaryCreator HOT 1
- investigate deserialization case that works in Jackson 2.18 but fails in Jackson 3
- Change to `DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES` default in 3.0 databind leads to Scala test fails
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jackson-module-scala.