如何使用Fastjson反序列化json以自定义不可修改的列表类?

时间:2019-12-19 15:26:44

标签: java

我已经定义了一个自定义的不可修改列表类 NodePath ,如何使用 Fastjson << >>将JSON “ [1,2,3]” 反序列化为NodePath实例/ strong>?我尝试使用ObjectDeserializer注释,但没有效果。

public final class NodePath<T extends Serializable & Comparable<? super T>> 
    extends ArrayList<T> implements Serializable, Comparable<PathNodeId<T>>, Cloneable {

    private final boolean initialized;

    public NodePath() {
        // Non-args constructor: For json deserialize
        initialized = true;
    }

    public NodePath(@NotEmpty List<T> path) {
        this.addAll(path);
        initialized = true;
    }

    @Override
    public boolean add(T e) {
        if (this.initialized) {
            throw new UnsupportedOperationException();
        } else {
            return super.add(e);
        }
    }

    // -------------------------------------------------------------unsupported operation
    @Override @Deprecated
    public void add(int index, T element) {
        throw new UnsupportedOperationException();
    }
    // ...................... others unsupportedoperation ...................... //
}

0 个答案:

没有答案