001/*
002 * Units of Measurement Jakarta JSON-B 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.yasson;
031
032import static tech.uom.lib.yasson.SerializationMode.SIMPLE;
033
034import java.lang.reflect.Type;
035import java.text.ParsePosition;
036import javax.json.bind.serializer.DeserializationContext;
037import javax.json.bind.serializer.JsonbDeserializer;
038import javax.json.stream.JsonParser;
039import javax.json.stream.JsonParser.Event;
040import javax.measure.Unit;
041import javax.measure.format.UnitFormat;
042
043import systems.uom.ucum.format.UCUMFormat;
044import systems.uom.ucum.format.UCUMFormat.Variant;
045import tech.units.indriya.AbstractUnit;
046import tech.units.indriya.format.EBNFUnitFormat;
047import tech.units.indriya.format.SimpleUnitFormat;
048
049/**
050 * @author Werner Keil
051 * @version 0.5
052 */
053public class UnitJsonDeserializer implements JsonbDeserializer<Unit> {
054
055    /**
056     * @since 2.0.2
057     */
058    private final SerializationMode mode;
059    
060    private UnitJsonDeserializer(SerializationMode mode) {
061        this.mode = mode;
062    }
063    
064    public UnitJsonDeserializer() {
065        this(SIMPLE);
066    }
067    
068    /**
069     * Returns {@code UnitJsonDeserializer} using the given {@code SerializationMode}.
070     *
071     * @param mode the {@code SerializationMode} to use
072     * @return a {@code UnitJsonDeserializer} using the specified serialization-mode
073     */
074    public static UnitJsonDeserializer ofMode(SerializationMode mode) {
075        return new UnitJsonDeserializer(mode);
076    }
077        
078    /**
079     * Deserializes a unit.
080     *
081     * @param parser            the JSON parser
082     * @param ctx                       the DeserializationContext as provided by {@link JsonbDeserializer}
083     * @param runtimeType       the type of the returned object
084     */
085        @Override
086        public Unit deserialize(JsonParser parser, DeserializationContext ctx, Type runtimeType) {                         
087        Unit retValue = AbstractUnit.ONE; // TODO or null?
088        final UnitFormat format = getFormat(mode); // TODO could cache this on an instance level
089        
090                while (parser.hasNext()) {              
091                
092                        Event evt = parser.next();
093                        switch (evt) {
094                        case VALUE_STRING:
095                                String str = parser.getString();
096                                retValue = format.parse(str, new ParsePosition(0));                              
097                                break;
098                        default:
099                                break;
100                        }               
101                }
102        return retValue;
103        }
104        
105        private static final UnitFormat getFormat(final SerializationMode mode) { 
106                switch(mode) {  
107                case UCUM:
108                        return UCUMFormat.getInstance(Variant.CASE_SENSITIVE);
109                case EBNF:
110                        return EBNFUnitFormat.getInstance();
111                default:
112                        return SimpleUnitFormat.getInstance();
113                }
114        }
115}