JSON挑剔的Java

发布于 2025-02-10 08:16:25 字数 1218 浏览 0 评论 0原文

我有一个简单的问题,假设我有这样的json

{
   "outer-field":{
      "outer-a":"something",
      "outer-b":12345678,
      "inner-field":{
         "inner-a":false,
         "inner-b":0.0,
         "inner-c":29.99
      }
   }
}

映射:


public class OuterObject {

    @JsonProperty("outer-a")
    public String outerA;
   
    @JsonProperty("outer-b")
    public Integer outerB;
   
    @JsonProperty("inner-field")
    public InnerObject innerField;

}

public class InnerObject{

    @JsonProperty("inner-a")
    public Boolean innerA;

    @JsonProperty("inner-b")
    public Double innerB;

    @JsonProperty("inner-c")
    public Double innerC;

我想知道是否可以通过使用一些自定义setter/entotation/shots或其他内容来保存内部对象中的外部字段:


public class InnerObject{

    @JsonProperty("inner-a")
    public Boolean innerA;

    @JsonProperty("inner-b")
    public Double innerB;

    @JsonProperty("inner-c")
    public Double innerC;

    //how to map this?
    @JsonProperty("outer-a")
    public String outerA;

ps:使用自定义由于JSON的复杂性,次要化是我的最后一项手段

i have a simple question, let's say I have this json

{
   "outer-field":{
      "outer-a":"something",
      "outer-b":12345678,
      "inner-field":{
         "inner-a":false,
         "inner-b":0.0,
         "inner-c":29.99
      }
   }
}

Mapped this way:


public class OuterObject {

    @JsonProperty("outer-a")
    public String outerA;
   
    @JsonProperty("outer-b")
    public Integer outerB;
   
    @JsonProperty("inner-field")
    public InnerObject innerField;

}

public class InnerObject{

    @JsonProperty("inner-a")
    public Boolean innerA;

    @JsonProperty("inner-b")
    public Double innerB;

    @JsonProperty("inner-c")
    public Double innerC;

I was wondering if it was possible to just save an outer field in the inner object like this by using some custom setter/annotation or something:


public class InnerObject{

    @JsonProperty("inner-a")
    public Boolean innerA;

    @JsonProperty("inner-b")
    public Double innerB;

    @JsonProperty("inner-c")
    public Double innerC;

    //how to map this?
    @JsonProperty("outer-a")
    public String outerA;

PS: using a custom deserialization is my last resort due to the complexity of the json

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伊面 2025-02-17 08:16:25

如果有人问我如何解决问题,我决定通过使用内部的xxx方法使用自定义构建器将外部属性传递给创建期间的内部对象

@Jacksonized
@JsonDeserialize(builder = OuterObject.Builder.class)
@AllArgsConstructor
@Data
public class OuterObject {

    public String outerA;
    public Integer outerB;
    public InnerObject innerObject;

    public static class Builder {

        public String outerA;
        public Integer outerB;
        public InnerObject innerObject;

        Builder withInnerObject(InnerObject innerObject) {
            // set attributes here
            innerObject.outerA = this.outerA
            this.innerObject = innerObject
            return this;
        }

        public OuterObject build() {
            return new(outerA, outerB, innerObject)
        }

Hope someone might find this helpful

If anyone was asking how I solve my problem, I decided to use a custom Builder by using the withXXX methods inside to pass the outer attributes to the inner object during creation

@Jacksonized
@JsonDeserialize(builder = OuterObject.Builder.class)
@AllArgsConstructor
@Data
public class OuterObject {

    public String outerA;
    public Integer outerB;
    public InnerObject innerObject;

    public static class Builder {

        public String outerA;
        public Integer outerB;
        public InnerObject innerObject;

        Builder withInnerObject(InnerObject innerObject) {
            // set attributes here
            innerObject.outerA = this.outerA
            this.innerObject = innerObject
            return this;
        }

        public OuterObject build() {
            return new(outerA, outerB, innerObject)
        }

Hope someone might find this helpful
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文