【问题标题】:Make POJO class generated from JSON schema serializable使从 JSON 模式生成的 POJO 类可序列化
【发布时间】:2015-04-29 11:26:13
【问题描述】:

我正在使用 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 应用程序中使用从 Schema 生成的 POJO 类之一,它要求此类实现 Serializable 以便能够在分布式设置中使用它。

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

public class Feature implements Serializable {

}

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

我浏览了整个谷歌,但没有运气。 提前致谢。

【问题讨论】:

  • 您可能只是在寻找 JSON.stringify 吗? developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • 我认为你没有遇到我的问题。我现在已经编辑了这个问题。谢谢。
  • 哈哈,我确实没有。我非常抱歉。我回答时认为我仍在查看 JavaScript 问题,而不是 Java 问题。我很抱歉。 :)

标签: java json serialization pojo jsonschema2pojo


【解决方案1】:

jsonschema2pojo 团队表示已经实现:

jsonschema2pojo/issues

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

【讨论】:

  • 成功了。太感谢了。不幸的是,我没有足够的声誉来支持这个答案。
猜你喜欢
  • 2019-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-19
  • 1970-01-01
  • 1970-01-01
  • 2015-03-07
相关资源
最近更新 更多