简体   繁体   English

@JsonAnySetter,@ JsonAnyGetter与DSL Json(De)/序列化不取值(总是为null)

[英]@JsonAnySetter, @JsonAnyGetter with DSL Json (De)/Serialization not taking values(always null)

Im using @JsonAnySetter and @JsonAnyGetter in my POJO class using my Custom serialization with DSL JSON class, the Map is initialized but the other properties are always null. 我使用带有DSL JSON类的自定义序列化在我的POJO类中使用@JsonAnySetter和@JsonAnyGetter,Map已初始化但其他属性始终为null。 My POJO class: 我的POJO课程:

  @CompiledJson
  public class Name {

String name;
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

Map<String,String> properties = new HashMap<String,String>();


public Name() {
    // TODO Auto-generated constructor stub
}

@JsonAnyGetter
public Map<String, String> get() {
    return this.properties;
}

@JsonAnySetter
public void set(String key, String value) {
    this.properties.put(key, value);
}

De/Serializing using DSLJson serialize() and deserialize() methods. 使用DSLJson serialize()和deserialize()方法进行序列化。 I do not see any error also, but properties remains null in JSON. 我也没有看到任何错误,但JSON中的属性仍然为null。 I doubt if Jackson annotations are supported by DSL Json. 我怀疑DSL Json是否支持Jackson注释。 :/ :/

Spring Boot App with DSL Json and Jackson Annotations 使用DSL Json和Jackson Annotations的Spring Boot应用程序

UPDATE I want to parse MyClass, which is a part of RootClass: 更新我想解析MyClass,它是RootClass的一部分:

 @Compiledjson
 public class RootClass {

private String id;
private List<MyClass> myclass;
private AnotherCLass class2;

//getters and setter here
}

 @CompiledJson
 public class MyClass implements JsonObject {

 private String name;

 private Map<String, String> properties; //want this to behave like Jackson's       @JsonAnySetter/Getter annotation.
 //The implementation of MapConverter serializer you mentioned below.
}

The entire code parses through custom Message reader and writer. 整个代码通过自定义Message reader和writer进行解析。

While sending my JSON Body, It'll be like this : 在发送我的JSON Body时,它会是这样的:

{
"id" : "1234",
"myclass" :
[
{
"name" : "abcd",
//any dynamic properties I want to add will go here
 "test" : "test1",
 "anything" : "anything"
}
],
"class2" : "test5"
}

Thank you :) 谢谢 :)

DSL-JSON doesn't support such get()/set(string, string) method pairs. DSL-JSON不支持这样的get()/ set(字符串,字符串)方法对。 It does understand Map<String, String> so if you expose properties it will work on that. 它确实理解Map <String,String>所以如果你公开properties ,它将适用于它。 But not in this kind of setup. 但不是在这种设置中。

As of v1.1 you have two options for solving such problems, both of them are covered in example project 从v1.1开始,您有两种解决此类问题的方法,它们都在示例项目中介绍

If you wish to reuse existing converters, your solution can look like this: 如果您希望重用现有转换器,您的解决方案可能如下所示:

public static class MyClass {

    private String name;
    private Map<String, String> properties;

    @JsonConverter(target = MyClass.class)
    public static class MyClassConverter {
        public static final JsonReader.ReadObject<MyClass> JSON_READER = new JsonReader.ReadObject<MyClass>() {
            public MyClass read(JsonReader reader) throws IOException {
                Map<String, String> properties = MapConverter.deserialize(reader);
                MyClass result = new MyClass();
                result.name = properties.get("name");
                result.properties = properties;
                return result;
            }
        };
        public static final JsonWriter.WriteObject<MyClass> JSON_WRITER = new JsonWriter.WriteObject<MyClass>() {
            public void write(JsonWriter writer, MyClass value) {
                MapConverter.serialize(value.properties, writer);
            }
        };
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM