unitsofmeasurement / uom-lib

Units of Measurement Libraries
Other
14 stars 13 forks source link

Support JavaBeans XML serialization for Quantities #69

Open arpieb opened 7 years ago

arpieb commented 7 years ago

Hello all! I'm using the UOM-SE library in a simulation project, and am running into a bit of trouble when trying to serialize objects containing ComparableQuantity objects via XMLEncoder. A minimal sample codebase that exhibits the problem (with most of the original classes involved intact) can be found in this gist. The SimulationParameter objects export, but due to exceptions thrown trying to export the UOM objects, no values show up in the XML output. Errors I am seeing are:

java.lang.InstantiationException: tec.uom.se.quantity.NumberQuantity
Continuing ...
java.lang.RuntimeException: failed to evaluate: <unbound>=Class.new();
Continuing ...
java.lang.InstantiationException: tec.uom.se.quantity.DoubleQuantity
Continuing ...
java.lang.RuntimeException: failed to evaluate: <unbound>=Class.new();
Continuing ...

(repeated several times)

I tried to register a DefaultPersistenceDelegate on the XMLEncoder to handle ComparableQuantity by converting the quantity to a string but it is never getting called - and neither does one registered for NumberQuantity. Apparently the DoubleQuantity class is package private so I cannot register a DefaultPersistenceDelegate for it to test. Any suggestions as to how I can get this to work?

Thanks! -R

andi-huber commented 4 years ago

My intent is to provide a utility class for RI (indriya), where one could just write the following:

ComparableQuantity<Time> q = Quantities.getQuantity(1.0, Units.DAY); // any Quantity

XMLEncoder encoder = new XMLEncoder(...);
EncodingUtil.setupXMLEncoder(encoder); // <-- proposed new utility with indriya

encoder.writeObject(q);
andi-huber commented 4 years ago

@keilw where would you like me to put such a utility class EncodingUtil? e.g. the quantities package tech.units.indriya.quantity?

andi-huber commented 4 years ago

@arpieb feel free to use (copy&paste) the newly proposed class EncodingUtil from associated PR

keilw commented 4 years ago

I gave it a slightly different name and put it under https://github.com/unitsofmeasurement/uom-clients, have a look, I think "Desktop Client" is good as there might be another approach via JAX-B under https://github.com/unitsofmeasurement/uom-lib similar to the Jackson libraries. Therefore "desktop" involving the "java.desktop" module (not yet applied) sounds OK to me.

Btw, @andi-huber if you're in Vienna over Christmas, I also visit for a couple of days, if you have time, I'd be happy to meet you in person.