001/* 002 * Units of Measurement Jackson Library 003 * Copyright (c) 2005-2021, 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, Indriya 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 static tech.uom.lib.jackson.UnitJacksonModule.Mode.UCUM; 033 034import java.io.IOException; 035import java.text.ParsePosition; 036import com.fasterxml.jackson.core.JsonGenerator; 037import com.fasterxml.jackson.core.JsonParser; 038import com.fasterxml.jackson.core.JsonToken; 039import com.fasterxml.jackson.core.Version; 040import com.fasterxml.jackson.databind.DeserializationContext; 041import com.fasterxml.jackson.databind.SerializerProvider; 042import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer; 043import com.fasterxml.jackson.databind.module.SimpleModule; 044import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer; 045import javax.measure.Dimension; 046import javax.measure.Unit; 047import tech.units.indriya.format.EBNFUnitFormat; 048import tech.units.indriya.format.SimpleUnitFormat; 049import systems.uom.ucum.format.UCUMFormat; 050import systems.uom.ucum.format.UCUMFormat.Variant; 051 052/** 053 * Configures Jackson to (de)serialize JSR 385 Unit objects using their UCUM representation, since the actual objects don't 054 * translate well into JSON. 055 * @version 2.1 056 */ 057public class UnitJacksonModule extends SimpleModule { 058 /** 059 * 060 */ 061 private static final long serialVersionUID = 7601584599518016604L; 062 063 /** 064 * @since 2.0.2 065 */ 066 public enum Mode { 067 /** 068 * Serialization-mode using {@link SimpleUnitFormat}. 069 */ 070 SIMPLE, 071 /** 072 * Serialization-mode using {@link EBNFUnitFormat}. 073 */ 074 EBNF, 075 /** 076 * Serialization-mode using {@link UCUMFormat}. This is the <strong>default</strong> mode if none is explicitly selected. 077 */ 078 UCUM 079 }; 080 081 /** 082 * @since 2.0.2 083 */ 084 final Mode mode; 085 086 /** 087 * 088 * @param mode the serialization-mode 089 * @since 2.0.2 090 */ 091 public UnitJacksonModule(Mode mode) { 092 super("UnitJsonSerializationModule", new Version(2, 1, 0, null, 093 UnitJacksonModule.class.getPackage().getName(), "uom-lib-jackson")); 094 this.mode = mode; 095 addSerializer(Unit.class, new UnitJsonSerializer()); 096 addSerializer(Dimension.class, new DimensionJsonSerializer()); 097 addDeserializer(Unit.class, new UnitJsonDeserializer()); 098 addDeserializer(Dimension.class, new DimensionJsonDeserializer()); 099 } 100 101 public UnitJacksonModule() { 102 this(UCUM); 103 } 104 105 @SuppressWarnings("rawtypes") 106 private class UnitJsonSerializer extends StdScalarSerializer<Unit> { 107 /** 108 * 109 */ 110 private static final long serialVersionUID = 2500234678114311932L; 111 112 protected UnitJsonSerializer() { 113 super(Unit.class); 114 } 115 116 @Override 117 public void serialize(Unit unit, JsonGenerator jgen, SerializerProvider provider) throws IOException { 118 if (unit == null) { 119 jgen.writeNull(); 120 } 121 else { 122 switch (mode) { 123 // currently we use only UCUM 124 default: 125 // Format the unit using the UCUM representation. 126 // The string produced for a given unit is always the same; it is not affected by the locale. 127 // It can be used as a canonical string representation for exchanging units. 128 String ucumFormattedUnit = UCUMFormat.getInstance(Variant.CASE_SENSITIVE).format(unit, new StringBuilder()).toString(); 129 jgen.writeString(ucumFormattedUnit); 130 break; 131 } 132 } 133 } 134 } 135 136 @SuppressWarnings("rawtypes") 137 private class UnitJsonDeserializer extends StdScalarDeserializer<Unit> { 138 /** 139 * 140 */ 141 private static final long serialVersionUID = -6327531740958676293L; 142 143 protected UnitJsonDeserializer() { 144 super(Unit.class); 145 } 146 147 @Override 148 public Unit deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { 149 JsonToken currentToken = jsonParser.getCurrentToken(); 150 151 if (currentToken == JsonToken.VALUE_STRING) { 152 switch(mode) { 153 // currently we use only UCUM 154 default: 155 return UCUMFormat.getInstance(Variant.CASE_SENSITIVE).parse(jsonParser.getText(), new ParsePosition(0)); 156 } 157 } 158 throw deserializationContext.wrongTokenException(jsonParser, String.class, 159 JsonToken.VALUE_STRING, 160 "Expected unit value in String format"); 161 } 162 } 163}