Giter Site home page Giter Site logo

Comments (9)

cowtowncoder avatar cowtowncoder commented on May 24, 2024

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.

cowtowncoder avatar cowtowncoder commented on May 24, 2024

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.

christophercurrie avatar christophercurrie commented on May 24, 2024

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.

cowtowncoder avatar cowtowncoder commented on May 24, 2024

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.

cowtowncoder avatar cowtowncoder commented on May 24, 2024

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.

christophercurrie avatar christophercurrie commented on May 24, 2024

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 or Deserializer
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.

cowtowncoder avatar cowtowncoder commented on May 24, 2024

I will have to read & think about this. At high (enough) level I think it does make sense.

from jackson-module-scala.

cowtowncoder avatar cowtowncoder commented on May 24, 2024

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.

christophercurrie avatar christophercurrie commented on May 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.