读取但不在XStream中写入字段

时间:2013-02-25 23:54:52

标签: java xstream

我正在更新一些使用XStream读取和编写其配置的代码。有一个布尔标志现在被另一种机制取代。我需要读取标志,如果是,则更新到新机制,然后在序列化时将其写出来。

到目前为止,我已经能够使用readResolve检查字段并更新反序列化并进行更新,但我无法弄清楚如何避免编写它。我已经尝试过omitField,它只是说它会阻止文档中的序列化,但这也会阻止反序列化。

所以,在阅读时我需要区分

<GeoServerTileLayer>
  ...
  <autoCacheStyles>true</autoCacheStyles>
</GeoServerTileLayer>

<GeoServerTileLayer>
  ...
  <autoCacheStyles>false</autoCacheStyles>
</GeoServerTileLayer>

然后我需要写出来

<GeoServerTileLayer>
  ...
</GeoServerTileLayer>

而不是

<GeoServerTileLayer>
  ...
  <autoCacheStyles>false</autoCacheStyles>
</GeoServerTileLayer>

我正在使用XStream 1.4.3

这似乎是代码的相关部分。整个序列化/反序列化过程通过多个子系统进行,并通过插件/扩展系统在两个不同的项目之间来回跳转。

public class GeoServerTileLayerInfoImpl implements Serializable, GeoServerTileLayerInfo {

    ...

    // The actual storage
    transient private Map<String, ParameterFilter> parameterFiltersMap;

    // Just used for serialize/deserialize to make xstream keep the same format it used to.
    private Set<ParameterFilter> parameterFilters;

    ...

    private boolean autoCacheStyles;

    public GeoServerTileLayerInfoImpl() {
        readResolve();
    }

    ...

    private final GeoServerTileLayerInfo readResolve() {
        if (null == metaWidthHeight) {
            metaWidthHeight = new int[2];
        }
        gridSubsets = nonNull(gridSubsets);
        mimeFormats = nonNull(mimeFormats);

        // Convert the deserialized set into a map.
        parameterFilters = nonNull(parameterFilters);
        setParameterFilters(parameterFilters);

        // Switch from old style autoCacheStyles to new StyleParameterFilter
        if(autoCacheStyles){
            autoCacheStyles = false;
            addParameterFilter(new StyleParameterFilter());
        }
        return this;
    }

    private final Object writeReplace() {
        parameterFilters = getParameterFilters();
        return this;
    }

    ...

    @Override
    public Set<ParameterFilter> getParameterFilters() {
        return new HashSet<ParameterFilter>(parameterFiltersMap.values());
    }

    @Override
    public void setParameterFilters(Set<ParameterFilter> parameterFilters) {
        parameterFiltersMap = new HashMap<String, ParameterFilter>();
        for (ParameterFilter pf: parameterFilters){
            addParameterFilter(pf);
        }
    }
}



public class GWCGeoServerConfigurationProvider implements ContextualConfigurationProvider {

    @Override
    public XStream getConfiguredXStream(XStream xs) {
        xs.alias("GeoServerTileLayer", GeoServerTileLayerInfo.class);
        xs.processAnnotations(GeoServerTileLayerInfoImpl.class);
        xs.processAnnotations(StyleParameterFilter.class);
        xs.addDefaultImplementation(GeoServerTileLayerInfoImpl.class, GeoServerTileLayerInfo.class);
        return xs;
    }

    @Override
    public boolean appliesTo(Context ctxt) {
        return Context.PERSIST==ctxt;
    }
}

0 个答案:

没有答案