001/* 002 * Units of Measurement Jackson Library for JSON support 003 * Copyright (c) 2012-2019, Werner Keil and others 004 * 005 * All rights reserved. 006 * 007 * Redistribution and use in source and binary forms, with or without modification, 008 * are permitted provided that the following conditions are met: 009 * 010 * 1. Redistributions of source code must retain the above copyright notice, 011 * this list of conditions and the following disclaimer. 012 * 013 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions 014 * and the following disclaimer in the documentation and/or other materials provided with the distribution. 015 * 016 * 3. Neither the name of JSR-385, Units of Measurement nor the names of their contributors may be used to endorse or promote products 017 * derived from this software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 021 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 023 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 026 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 028 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package tech.uom.lib.jackson; 031 032import java.io.IOException; 033import java.text.ParsePosition; 034import com.fasterxml.jackson.core.JsonGenerator; 035import com.fasterxml.jackson.core.JsonParser; 036import com.fasterxml.jackson.core.JsonToken; 037import com.fasterxml.jackson.core.Version; 038import com.fasterxml.jackson.databind.DeserializationContext; 039import com.fasterxml.jackson.databind.SerializerProvider; 040import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer; 041import com.fasterxml.jackson.databind.module.SimpleModule; 042import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer; 043import javax.measure.Dimension; 044import javax.measure.Unit; 045import systems.uom.ucum.format.UCUMFormat; 046import systems.uom.ucum.format.UCUMFormat.Variant; 047 048/** 049 * Configures Jackson to (de)serialize JSR 363 Unit objects using their UCUM representation, since the actual objects don't 050 * translate well into JSON. 051 * @version 2.0 052 */ 053public class UnitJacksonModule extends SimpleModule { 054 /** 055 * 056 */ 057 private static final long serialVersionUID = 7601584599518016604L; 058 059 public UnitJacksonModule() { 060 super("UnitJsonSerializationModule", new Version(1, 3, 3, null, 061 UnitJacksonModule.class.getPackage().getName(), "uom-lib-jackson")); 062 063 addSerializer(Unit.class, new UnitJsonSerializer()); 064 addSerializer(Dimension.class, new DimensionJsonSerializer()); 065 addDeserializer(Unit.class, new UnitJsonDeserializer()); 066 addDeserializer(Dimension.class, new DimensionJsonDeserializer()); 067 } 068 069 @SuppressWarnings("rawtypes") 070 private class UnitJsonSerializer extends StdScalarSerializer<Unit> { 071 /** 072 * 073 */ 074 private static final long serialVersionUID = 2500234678114311932L; 075 076 protected UnitJsonSerializer() { 077 super(Unit.class); 078 } 079 080 @Override 081 public void serialize(Unit unit, JsonGenerator jgen, SerializerProvider provider) throws IOException { 082 if (unit == null) { 083 jgen.writeNull(); 084 } 085 else { 086 // Format the unit using the UCUM representation. 087 // The string produced for a given unit is always the same; it is not affected by the locale. 088 // It can be used as a canonical string representation for exchanging units. 089 String ucumFormattedUnit = UCUMFormat.getInstance(Variant.CASE_SENSITIVE).format(unit, new StringBuilder()).toString(); 090 091 jgen.writeString(ucumFormattedUnit); 092 } 093 } 094 } 095 096 @SuppressWarnings("rawtypes") 097 private class UnitJsonDeserializer extends StdScalarDeserializer<Unit> { 098 /** 099 * 100 */ 101 private static final long serialVersionUID = -6327531740958676293L; 102 103 protected UnitJsonDeserializer() { 104 super(Unit.class); 105 } 106 107 @Override 108 public Unit deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { 109 JsonToken currentToken = jsonParser.getCurrentToken(); 110 111 if (currentToken == JsonToken.VALUE_STRING) { 112 return UCUMFormat.getInstance(Variant.CASE_SENSITIVE).parse(jsonParser.getText(), new ParsePosition(0)); 113 } 114 throw deserializationContext.wrongTokenException(jsonParser, String.class, 115 JsonToken.VALUE_STRING, 116 "Expected unit value in String format"); 117 } 118 } 119}