java - 使从 JSON 模式生成的 POJO 类可序列化

标签 java json serialization pojo jsonschema2pojo

我正在使用 jsonschema2pojo-maven-plugin v0.4.7 从 JSON 模式生成 POJO 类。 示例架构如下:

 "features": {
            "title": "Feature",
            "description": "Name and type of every feature in the model",
            "type": "array",
            "items": {
                "properties": {
                    "columnName": {
                        "description": "Name of the table column",
                        "type": "string"
                    },
                    "featureName": {
                        "description": "Name of that column's feature for the pipeline",
                        "type": "string"
                    },
                    "type": {
                        "description": "Type of the feature",
                        "type": "string"
                    }
                },
                "required": ["columnName", "type"]
            }

生成的 POJO 类大致如下:

public class Feature {

    /**
     * Name of the table column
     * 
     */
    @JsonProperty("columnName")
    private String columnName;
    /**
     * Name of that column's feature for the pipeline
     * 
     */
    @JsonProperty("featureName")
    private String featureName;
    /**
     * Type of the feature
     * 
     */
    @JsonProperty("type")
    private String type;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    /**
     * Name of the table column
     * 
     * @return
     *     The columnName
     */
    @JsonProperty("columnName")
    public String getColumnName() {
        return columnName;
    }

    /**
     * Name of the table column
     * 
     * @param columnName
     *     The columnName
     */
    @JsonProperty("columnName")
    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    /**
     * Name of that column's feature for the pipeline
     * 
     * @return
     *     The featureName
     */
    @JsonProperty("featureName")
    public String getFeatureName() {
        return featureName;
    }

    /**
     * Name of that column's feature for the pipeline
     * 
     * @param featureName
     *     The featureName
     */
    @JsonProperty("featureName")
    public void setFeatureName(String featureName) {
        this.featureName = featureName;
    }

    /**
     * Type of the feature
     * 
     * @return
     *     The type
     */
    @JsonProperty("type")
    public String getType() {
        return type;
    }

    /**
     * Type of the feature
     * 
     * @param type
     *     The type
     */
    @JsonProperty("type")
    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(columnName).append(featureName).append(type).append(additionalProperties).toHashCode();
    }

    @Override
    public boolean equals(Object other) {
        if (other == this) {
            return true;
        }
        if ((other instanceof Feature) == false) {
            return false;
        }
        Feature rhs = ((Feature) other);
        return new EqualsBuilder().append(columnName, rhs.columnName).append(featureName, rhs.featureName).append(type, rhs.type).append(additionalProperties, rhs.additionalProperties).isEquals();
    }

}

我正在使用从 Spark 应用程序中的架构生成的 POJO 类之一,这要求此类实现 Serializable 以便能够将其用于分布式设置。

我需要生成的类来实现序列化,如下所示:

public class Feature implements Serializable {

}

有谁知道使 POJO 类实现可序列化的方法吗? 他们的 JSON 模式设置是否使其可序列化?

我已经在 google 上找遍了,但没有运气。 提前致谢。

最佳答案

jsonschema2pojo 团队表示已经实现:

jsonschema2pojo/issues

“已为 0.3.7 修复。现在有一个新的扩展属性“javaInterfaces”,它接受一个字符串数组,其中每个字符串都是 Java 接口(interface)的完全限定名称。生成的类型将实现这些接口(interface)。”

关于java - 使从 JSON 模式生成的 POJO 类可序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28753405/

相关文章:

javascript - D3.js 未在 HTML 中附加更新的树

c++ - 可变长度数据和零长度数组的二进制序列化,安全吗?

Java XML 序列化,文件中缺少字段

java - 如何序列化 Scala lambda 以实现与 Java 方法的互操作?

java - Flutter+Android Studio : Can't resolve Java symbols from external libraries

javascript - JSON 自动解析为 Javascript 中的对象

java - 如何在android中的 ListView 中为每一行添加不同的 Activity

java - 将 HashMap 序列化为 JSON 字符串,同时避免 Java 中的某些字段

java - ArrayList 相等 JUnit 测试

java - JDK 中的哪个类创建 JRMI ProtocolAck?