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 javax.json.bind.serializer.JsonbSerializer;
035import javax.json.bind.serializer.SerializationContext;
036import javax.json.stream.JsonGenerator;
037import javax.measure.Unit;
038import javax.measure.format.UnitFormat;
039
040import systems.uom.ucum.format.UCUMFormat;
041import systems.uom.ucum.format.UCUMFormat.Variant;
042import tech.units.indriya.format.EBNFUnitFormat;
043import tech.units.indriya.format.SimpleUnitFormat;
044
045/**
046 * @author Werner Keil
047 * @version 0.5
048 */
049public class UnitJsonSerializer implements JsonbSerializer<Unit> {
050        
051    /**
052     * @since 2.0.2
053     */
054    private final SerializationMode mode;
055    
056    private UnitJsonSerializer(SerializationMode mode) {
057        this.mode = mode;
058    }
059    
060    public UnitJsonSerializer() {
061        this(SIMPLE);
062    }    
063    
064    /**
065     * Returns {@code UnitJsonSerializer} using the given {@code SerializationMode}.
066     *
067     * @param mode the {@code SerializationMode} to use
068     * @return a {@code UnitJsonSerializer} using the specified serialization-mode
069     */
070    public static UnitJsonSerializer ofMode(SerializationMode mode) {
071        return new UnitJsonSerializer(mode);
072    }
073    
074    /**
075     * Serializes a unit.
076     *
077     *
078     * @param value     the unit to serialize
079     * @param generator the generator as provided by {@link JsonbSerializer}
080     * @param ctx               the SerializationContext as provided by {@link JsonbSerializer}
081     */
082        @Override
083        public void serialize(Unit value, JsonGenerator generator, SerializationContext ctx) {
084           final UnitFormat format = getFormat(mode); // TODO could cache this on an instance level       
085           
086           if (value == null) {
087                   generator.writeNull();
088           } else {
089                   String formattedUnit = format.format(value);
090                   generator.write(formattedUnit);         
091       }
092        }
093        
094        private static final UnitFormat getFormat(final SerializationMode mode) { 
095                switch(mode) {  
096                case UCUM:
097                        return UCUMFormat.getInstance(Variant.CASE_SENSITIVE);
098                case EBNF:
099                        return EBNFUnitFormat.getInstance();
100                default:
101                        return SimpleUnitFormat.getInstance();
102                }
103        }
104}