alibaba / fastjson

FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
https://github.com/alibaba/fastjson2/wiki/fastjson_1_upgrade_cn
Apache License 2.0
25.74k stars 6.5k forks source link

反序列化问题,大佬来看看 #3014

Open oooopl opened 4 years ago

oooopl commented 4 years ago

版本:1.2.62

jts maven resp

<dependency>
     <groupId>com.vividsolutions</groupId>
     <artifactId>jts</artifactId>
     <version>1.13</version>
</dependency>

deserializer类

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import com.vividsolutions.jts.geom.*;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Type;
import java.util.List;

public class PolygonDeserializer  implements ObjectDeserializer {
    @Override
    public Polygon deserialze(DefaultJSONParser parser, Type type, Object fieldName) {

        String key;
        if (fieldName==null|| StringUtils.isEmpty((key = (String)fieldName))) {
            return null;
        }
        Object value = JSONObject.parseObject(parser.getInput()).get(key);

        if (value instanceof JSONArray){
            JSONArray array = (JSONArray)value;
            List<Double[]> points = array.toJavaList(Double[].class);
            /*
             * 最后一个点和初始点一致 形成闭环
             * */
            if (
                !points.get(0)[0] .equals(points.get(points.size()-1)[0]) ||
                    !points.get(0)[1] .equals(points.get(points.size()-1)[1])
            ){
                points.add(points.get(0));
            }

            Coordinate[] coordinates = points.stream().map(
                point -> new Coordinate(point[0], point[1])
            ).toArray(Coordinate[]::new);

            GeometryFactory factory = new GeometryFactory(new PrecisionModel(PrecisionModel.FLOATING), 4326);

            return  factory.createPolygon(coordinates);
        }
       return null;
    }
    @Override
    public int getFastMatchToken() {
        return 0;
    }
}

CustomerDoorVO 类

import com.vividsolutions.jts.geom.Polygon;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class CustomerDoorVO {

    /**
     * 门点名称
     */
    private String name;

    /**
     * 联系方式
     */
    private String telephone;

    /**
     * 门点地址
     */
    private String address;

    /**
     * 多边形定位
     */
    private Polygon location;

}

测试代码

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.util.TypeUtils;
import com.howhan.cloud.e.customer.service.vo.CustomerDoorVO;
import com.howhan.cloud.e.spring.autoconfigure.deserializer.PolygonDeserializer;
import com.vividsolutions.jts.geom.Polygon;
import org.junit.Test;

public class CTest {
    @Test
    public void deserializer(){
        String value = "{\"id\":4,\"name\":\"浦东新区\",\"telephone\":\"123456\",\"address\":\"上海浦东\",\"location\":[[121.62456900000001,31.278098],[121.616569,31.278098],[121.616569,31.271098000000002],[121.62456900000001,31.271098000000002]]}";
        JSONObject jsonObject = JSONObject.parseObject(value);

        ParserConfig config = ParserConfig.getGlobalInstance();
        config.putDeserializer(Polygon.class,new PolygonDeserializer());

        CustomerDoorVO customerDoorVO = TypeUtils.castToJavaBean(jsonObject, CustomerDoorVO.class, config);
    }
}

异常信息

com.alibaba.fastjson.JSONException: not close json text, token : [

    at com.alibaba.fastjson.util.TypeUtils.castToJavaBean(TypeUtils.java:1378)
    at com.alibaba.fastjson.JSONObject.toJavaObject(JSONObject.java:611)
    at CTest.deserializer(CTest.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: com.alibaba.fastjson.JSONException: not close json text, token : [
    at com.alibaba.fastjson.parser.DefaultJSONParser.close(DefaultJSONParser.java:1526)
    at com.alibaba.fastjson.JSON.parseObject(JSON.java:387)
    at com.alibaba.fastjson.JSON.parseObject(JSON.java:287)
    at com.alibaba.fastjson.JSON.parseObject(JSON.java:560)
    at com.alibaba.fastjson.util.TypeUtils.cast(TypeUtils.java:1069)
    at com.alibaba.fastjson.util.TypeUtils.cast(TypeUtils.java:1134)
    at com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer.createInstance(JavaBeanDeserializer.java:1421)
    at com.alibaba.fastjson.util.TypeUtils.castToJavaBean(TypeUtils.java:1376)
    ... 24 more
oooopl commented 4 years ago

数据格式是正确的, 目前使用List<Double[]>格式 接收数据,然后在用工具类转一下,但是不如反序列化器好用