ParseException on doing Unit.valueOf(NonSI.BYTE.times(1024).toString());

39 views
Skip to first unread message

Rohit Kumar

unread,
Mar 14, 2012, 11:34:57 AM3/14/12
to units...@googlegroups.com

I am trying to write code which can serialize javax.measure.unit.Unit instances into JSON. For this I am using the Jackson object mapper. The object mapper can't directly serialize Unit instances due to a self-reference, so instead I store a String representation of the Unit.

While serializing I do something like

 String jsonString = serialize(unit.toString());

While deserializing I do:

 String unitString = deserialize(jsonString);
 
Unit<?> unit = Unit.valueOf(unitString);

This works for most of the basic units, but not for something like say "NonSI.Byte.times(1024)". Essentially the following code throws a ParseException:

Unit.valueOf(NonSI.BYTE.times(1024).toString());

Does anyone know how to solve this?

Werner Keil

unread,
Mar 22, 2012, 8:18:54 PM3/22/12
to units...@googlegroups.com
Rohit,

Thanks a lot for your message and interest. Could you attach a running example, e.g. a few classes demonstrating this behavior?

A word of caveat, given the JSR (275) as it was way before its time and Java's capabilities (you may have heard, Oracle announced a more solid type system, which goes in a direction where new technologies and standards like it could make more sense, for Java 10, due not before 2017, that's 5 years from now!) back then, cannot actively be maintained.

You could get support via the JScience mailing lists like unitsof...@jscience.java.net since the current stable JScience version uses this stage of the JSR. I suppose you're using the JScience implementation or distribution for JSR 275 or do you use it by itself? There's also an equivalent to this groupus...@jscience.java.net

Beside possible patches or fixes for JScience 4, have you considered using Unit-API, the framework those groups and mailing lists were mainly created for?

Eclipse UOMo is in incubator at Eclipse.org, but has been around for 1.5 years now in a stable condition. JScience 5 also building on Unit-API is available in source code, but I cannot say, when a binary will be released.

Kind Regards,
Werner Keil
UOMo Lead
Reply all
Reply to author
Forward
0 new messages