Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

android 1.1.57版本 使用ParserConfig.getGlobalInstance().putDeserializer注入自定义反序列化类的时候,(T) JSON.toJavaObject这种方式会导致嵌套反序列化失败 #1233

Closed
sandywen123 opened this issue May 26, 2017 · 1 comment
Labels

Comments

@sandywen123
Copy link

ParserConfig.getGlobalInstance().putDeserializer(Area.class, new ObjectDeserializer() {
            @Override
            public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
                JSONObject jsonObject = (JSONObject) parser.parse();
                String areaType;

                if (jsonObject.get("type") instanceof String) {
                    areaType = (String) jsonObject.get("type");
                } else {
                    return null;
                }
                if (Area.TYPE_SECTION.equals(areaType)) {
                    return (T) JSON.toJavaObject(jsonObject, Section.class);
                } else if (Area.TYPE_FLOORV1.equals(areaType)) {
                    return (T) JSON.toJavaObject(jsonObject, FloorV1.class);
                } else if (Area.TYPE_FLOORV2.equals(areaType)) {
                    return (T) JSON.toJavaObject(jsonObject, FloorV2.class);
                }
                return null;
            }

        });

Area接口 :

public interface Area {
    public static final String TYPE_SECTION = "section";
    public static final String TYPE_FLOORV1 = "floorV1";
    public static final String TYPE_FLOORV2 = "floorV2";

    String getName();
}

Section类:

public class Section implements Area {
    public List<Area> children;

    public String type;

    public String templateId;

    @Override
    public String getName() {
        return templateId;
    }
}

FloorV1类:

public class FloorV1 implements Area {
    public String type;
    public String templateId;

    @Override
    public String getName() {
        return templateId;
    }
}

FloorV2类:

public class Section implements Area {
    public List<Area> children;

    public String type;

    public String templateId;

    @Override
    public String getName() {
        return templateId;
    }
}

JSON 结构

    static String jsonData = "{\n" +
            "    \"areas\": [\n" +
            "      {\n" +
            "        \"type\": \"section\",\n" +
            "        \"templateId\": \"grid\",\n" +
            "        \"children\": [\n" +
            "        {\n" +
            "        \"type\": \"section\",\n" +
            "        \"templateId\": \"grid\",\n" +
            "        \"children\": [\n" +
            "          {\n" +
            "            \"type\": \"floorV2\",\n" +
            "            \"templateId\": \"base\",\n" +
            "          },\n" +
            "          {\n" +
            "            \"type\": \"floorV2\",\n" +
            "            \"templateId\": \"base\",\n" +
            "          }\n" +
            "        ]\n" +
            "      },\n" +
            "          {\n" +
            "            \"type\": \"floorV2\",\n" +
            "            \"templateId\": \"base\",\n" +
            "          },\n" +
            "          {\n" +
            "            \"type\": \"floorV2\",\n" +
            "            \"templateId\": \"base\",\n" +
            "          }\n" +
            "        ]\n" +
            "      },\n" +
            "          {\n" +
            "            \"type\": \"floorV2\",\n" +
            "            \"templateId\": \"base\",\n" +
            "          }\n" +
            "    ]\n" +
            "  }";

最后的结果就是最里层的floor被解析成了$Proxy 没有解析成floor

@sandywen123 sandywen123 changed the title 使用ParserConfig.getGlobalInstance().putDeserializer注入自定义反序列化类的时候,(T) JSON.toJavaObject这种方式会导致嵌套反序列化失败 android 1.1.57版本 使用ParserConfig.getGlobalInstance().putDeserializer注入自定义反序列化类的时候,(T) JSON.toJavaObject这种方式会导致嵌套反序列化失败 May 26, 2017
@wenshao wenshao added this to the 1.1.58.android milestone May 30, 2017
@wenshao wenshao added the bug label May 30, 2017
@wenshao
Copy link
Member

wenshao commented May 30, 2017

已经支持,请用新版本 https://github.com/alibaba/fastjson/releases/tag/1.1.58.android

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants