All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.raynigon.unit_api.jackson.serializer.QuantitySerializer Maven / Gradle / Ivy

There is a newer version: 1.1.7
Show newest version
package com.raynigon.unit_api.jackson.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.raynigon.unit_api.core.service.UnitResolverService;
import com.raynigon.unit_api.jackson.annotation.JsonUnit;
import com.raynigon.unit_api.core.annotation.QuantityShape;
import com.raynigon.unit_api.jackson.annotation.JsonUnitHelper;
import com.raynigon.unit_api.jackson.exception.UnknownUnitException;

import javax.measure.Quantity;
import javax.measure.Unit;
import java.io.IOException;

@SuppressWarnings("rawtypes")
public class QuantitySerializer extends JsonSerializer implements ContextualSerializer {

    private Unit unit;
    private QuantityShape shape = QuantityShape.NUMBER;

    public QuantitySerializer() {
    }

    public QuantitySerializer(Unit unit, QuantityShape shape) {
        this.unit = unit;
        this.shape = shape;
    }

    @Override
    @SuppressWarnings({"unchecked", "rawtypes"})
    public JsonSerializer createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
        Class quantityType = (Class) property.getType().getBindings().getBoundType(0).getRawClass();
        unit = UnitResolverService.getInstance().getUnit(quantityType);

        JsonUnit unitWrapper = property.getAnnotation(JsonUnit.class);
        if (unitWrapper == null) return new QuantitySerializer(unit, shape);
        shape = JsonUnitHelper.getShape(unitWrapper);
        String unitName = JsonUnitHelper.getUnitName(unitWrapper);

        if (unitName != null) {
            unit = UnitResolverService.getInstance().getUnit(unitName);
        }
        if (unit == null) {
            throw new UnknownUnitException(prov.getGenerator(), unitName);
        }

        return new QuantitySerializer(unit, shape);
    }

    @Override
    @SuppressWarnings({"unchecked", "rawtypes"})
    public void serialize(Quantity quantity, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        Quantity convertedQuantity = quantity;
        if (this.unit != null) {
            convertedQuantity = quantity.to(unit);
        }
        switch (shape) {
            case NUMBER:
                gen.writeNumber(convertedQuantity.getValue().doubleValue());
                break;
            case STRING:
                gen.writeString(convertedQuantity.toString());
                break;
            default:
                // TODO raise exception
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy