【问题标题】:@JsonAnySetter, @JsonAnyGetter with DSL Json (De)/Serialization not taking values(always null)@JsonAnySetter,@JsonAnyGetter 与 DSL Json (De)/序列化不取值(始终为空)
【发布时间】:2016-05-20 06:42:45
【问题描述】:

我在我的 POJO 类中使用 @JsonAnySetter 和 @JsonAnyGetter,使用我的自定义序列化与 DSL JSON 类,映射已初始化,但其他属性始终为空。 我的 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);
}

使用 DSLJson serialize() 和 deserialize() 方法进行反序列化。我也没有看到任何错误,但 JSON 中的属性仍然为空。我怀疑 DSL Json 是否支持 Jackson 注释。 :/

带有 DSL Json 和 Jackson 注释的 Spring Boot 应用

更新 我要解析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.
}

整个代码通过自定义消息读取器和写入器进行解析。

在发送我的 JSON 正文时,它会是这样的:

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

谢谢你:)

【问题讨论】:

    标签: json annotations jackson dsl


    【解决方案1】:

    DSL-JSON 不支持这样的 get()/set(string, string) 方法对。 它确实理解 Map 所以如果你公开properties 它会起作用。但不是这种设置。

    从 v1.1 开始,您有两个解决此类问题的选项,它们都包含在 example project

    如果您希望重用现有转换器,您的解决方案可能如下所示:

    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);
                }
            };
        }
    }
    

    【讨论】:

    • 这是我的 JSON 对象:"name" : [ { "name" : "test", "test" : "testProps" } ] 这给了我这个错误 - 期待 '{' 在位置 92。找到“。而且我的变量不是最终的。我应该如何更改我的 JSON 对象?还有,我的名字对象是另一个对象的一部分,像这样 - { "Obj1" : "1221111", "SomeObj" :[ { "someID" : "123456", "name" : [ { "name" : "123334", "test" : "test" } ] } ] }
    • 我只是写了一个实现的例子,请随意更改。如果您需要将其用作数组“[”,则必须使用转换器。 JsonObject 必须以“{”开头。此实现需要“名称”:{。用更多信息更新您的问题,我可以更改实现来解析它。
    • 更新了我的问题!
    • 非常感谢 :) 你能解释一下 JSONConverter 是如何工作的吗?以及为什么我们不需要为这个类使用@CompiledJson 注解?
    • CompiledJson 用于指示编译器构建基于类结构的转换器。 JsonConverter 用于指示编译器您有自己的特定类转换器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多