package com.changhu.support.fastjson2.deserialze;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.reader.ObjectReader;
import com.changhu.common.utils.GeometryUtil;
import org.locationtech.jts.geom.Point;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * author: luozhun
 * desc: PointDeserializer
 * createTime: 2023/8/26 16:28
 */
public class PointDeserializer implements ObjectReader<Point> {

    public static final PointDeserializer instance = new PointDeserializer();

    private PointDeserializer() {
    }

    @Override
    public Point readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) {
        List<BigDecimal> list = new ArrayList<>();
        jsonReader.readArray(list, BigDecimal.class);
        if (CollUtil.isEmpty(list)) {
            return null;
        }
        return GeometryUtil.createPoint(list);
    }
}