Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@JSONField(jsonDirect = true) 和 filter 一起使用,导致该属性的数据内容为空 #2950

Open
sugar-tgg opened this issue Dec 23, 2019 · 0 comments

Comments

@sugar-tgg
Copy link

@sugar-tgg sugar-tgg commented Dec 23, 2019

实体部分:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Colour {
      private String name;
       @JSONField(jsonDirect = true)
       private String value;
}

测试代码:

public class Test {

    public static void main(String[] args) {
        JSONArray list= new JSONArray();

        JSONObject ob1 = new JSONObject();
        ob1.put("1",100);
        list.add(ob1);

        JSONObject ob2 = new JSONObject();
        ob2.put("2",200);
        list.add(ob2);

        Colour colour = new Colour("test1",list.toJSONString());
        System.out.println("未使用filter, 使用  @JSONField(jsonDirect = true)   操作↓");
        System.out.println(JSON.toJSONString(colour));
        System.out.println();

        System.out.println("使用filter,使用 Excludes 来排除属性  ↓");
        SimplePropertyPreFilter filter2 = new SimplePropertyPreFilter();
        filter2.getExcludes().add("name");
        System.out.println(JSON.toJSONString(colour,filter2));
        System.out.println();

        System.out.println("使用filter, Includes 选择需要的属性 ↓");
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
        filter.getIncludes().add("value");
        System.out.println(JSON.toJSONString(colour,filter));
        System.out.println();

    }
}

结果是:

未使用filter, 使用  @JSONField(jsonDirect = true)   操作↓
{"name":"test1","value":[{"1":100},{"2":200}]}

使用filter,使用 Excludes 来排除属性  ↓
{"value":[{"1":100},{"2":200}]}

使用filter, Includes 选择需要的属性 ↓
{"value":[{},{}]}

我的问题: 使用 @JSONField(jsonDirect = true) 的目的是因为我的需求是将一个 json 字符串放入 String 类型的字段里,然后再通过filter 去选择要返回的属性字段。现在 jsonDirect 和 filter 一起使用。结果如上。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.