Coder Social home page Coder Social logo

Comments (11)

keilw avatar keilw commented on June 9, 2024 3

You're correct and I don't think you did something wrong.
This Jackson library was ported from the now archived https://github.com/unitsofmeasurement/jackson-module-unitsofmeasure created by Opower before Oracle took it over. There are serializers and deserializers for Unit and Dimension, but so far they did not need or add one for a Quantity. The functionality was pretty much as-is, but if you'd like to contribute to enhancements, you are more than welcome.

from uom-lib.

keilw avatar keilw commented on June 9, 2024 1

I didn't start working on it, so help or PRs are appreciated if you or someone else had time to help. The existing functionality is pretty identical to what was donated by the ex Opower developer, they did not seem to have a use for quantity serialization, thus it did not exist so far. I also started working on a Jakarta JSON-B variant using Jakarta JSON standards, but that is also work in progress: https://github.com/unitsofmeasurement/uom-lib/tree/master/yasson

from uom-lib.

bantu avatar bantu commented on June 9, 2024 1

I hacked this together. However, this is unlikely a proper solution.

import java.io.IOException;
import java.math.BigDecimal;

import javax.measure.Quantity;
import javax.measure.Quantity.Scale;
import javax.measure.Unit;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import tech.units.indriya.quantity.Quantities;

@SuppressWarnings("rawtypes")
public class QuantityJsonDeserializer extends StdDeserializer<Quantity> {
    public QuantityJsonDeserializer() {
        super(Quantity.class);
    }

    @Override
    public Quantity deserialize(JsonParser jp, DeserializationContext deserializationContext)
        throws IOException, JsonProcessingException {
        JsonToken currentToken = null;
        BigDecimal value = null;
        Unit<?> unit = null;
        Scale scale = null;
        while ((currentToken = jp.nextValue()) != null) {
            switch (currentToken) {
                case VALUE_NUMBER_FLOAT:
                case VALUE_NUMBER_INT:
                    switch (jp.getCurrentName()) {
                        case "value":
                            value = jp.getDecimalValue();
                            break;
                        default:
                            break;
                    }
                    break;
                case VALUE_STRING:
                    switch (jp.getCurrentName()) {
                        case "unit":
                            unit = jp.readValueAs(Unit.class);
                            break;
                        case "scale":
                            scale = Scale.valueOf(jp.getText());
                            break;
                        default:
                            break;
                    }
                default:
                    break;
            }
        }
        return Quantities.getQuantity(value, unit, scale);
    }
}

from uom-lib.

keilw avatar keilw commented on June 9, 2024 1

Regardless of the maturiy, would you be able to turn that into a PR @bantu? Unlike the core JSR modules this one is free to contribute (as long as you abide to BSD license) without JCP membership, so any PR is welcome.

from uom-lib.

bantu avatar bantu commented on June 9, 2024 1

Regardless of the maturiy, would you be able to turn that into a PR @bantu? Unlike the core JSR modules this one is free to contribute (as long as you abide to BSD license) without JCP membership, so any PR is welcome.

PR opened: #72

Only thing I might add is error checking, to ensure all three items are actually found and appropriately deserialized before handing them to Quantities.getQuantity()

Feel free to add that to the PR.

from uom-lib.

lucasvc avatar lucasvc commented on June 9, 2024

Thanks for your reply @keilw.
I've been on holidays, but I see you took it over :)
Thanksยฒ

from uom-lib.

lucasvc avatar lucasvc commented on June 9, 2024

Oh sorry, I could not review all the commits and I assumed ๐Ÿ˜…
Then I will try to do some work on it.

from uom-lib.

daniel-shuy avatar daniel-shuy commented on June 9, 2024

I've started looking into this. The main issue is with ProductUnits (eg. Units#CUBIC_METRE).

The value that is used for serialization and deserialization must be the same.

For example, if we take a naive approach to serialize Units#CUBIC_METRE using SimpleUnitFormat#format(Unit), it returns mยณ. But if we then try to deserialize it using SimpleUnitFormat#parse(String), it will fail, because it expects m^3.

To work around this, to serialize/deserialize Units, UnitJacksonModule uses UCUMFormat instead of SimpleUnitFormat, which serializes/deserializes Units#CUBIC_METRE as m3.

Unfortunately, for Quantity, SimpleQuantityFormat uses SimpleUnitFormat internally, and there is no UCUM equivalent for it. The most obvious solution now would be to create an AbstractQuantityFormat that uses UCUMFormat internally instead of SimpleUnitFormat (eg. UCUMQuantityFormat), or to manually parse the String to separate the numeric value from the unit.

from uom-lib.

GregJohnStewart avatar GregJohnStewart commented on June 9, 2024

Bump to this! A hard blocker on the project I am working on, need any help?

from uom-lib.

GregJohnStewart avatar GregJohnStewart commented on June 9, 2024

I was in the midst of making my own deserializer, and this is along the lines of what I was attempting to come up with. I actually have a fork going that has a few other organization changes as well. I don't mind as long as I can deserialize Quantity, but thought I would mention! I can also wait for the simpler fix to get in, I can't test this anyways due to the other PR I posted on using Java 11

from uom-lib.

GregJohnStewart avatar GregJohnStewart commented on June 9, 2024

Only thing I might add is error checking, to ensure all three items are actually found and appropriately deserialized before handing them to Quantities.getQuantity()

from uom-lib.

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.