简体   繁体   English

用于非标准JSON的Spring自定义对象映射器

[英]Spring Custom Object Mapper for non-standard JSON

I'm using Spring's RestTemplate, to hit a restful webservice and getting non-standard JSON back. 我正在使用Spring的RestTemplate来启动一个宁静的Web服务并获取非标准JSON。

Here is what I mean: 这是我的意思:

{
    ...
    rules : {
        matched : "rule one",
        matched : "rule B",
        matched : "another rule"
    }
    ...
}

So basically I need this Hash mapped to a list. 所以基本上我需要将此哈希映射到列表。 In my pojo I would like the field to look like this: 在我的pojo中,我希望该字段看起来像这样:

private List<String> rules; // once parsed, should contain "rule one",
                    // "rule B", "another rule", etc

Here is what I have attempted thus far. 到目前为止,这是我尝试过的。 Here's my serializer: 这是我的序列化器:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

public class MapValuesToListSerializer extends JsonSerializer<Map<?, ?>> {

    @Override
    public void serialize(Map<?, ?> map, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        serializerProvider.defaultSerializeValue(map.values(), jsonGenerator);
    }
}

And within my POJO, I've annotated my field like this: 在我的POJO中,我这样注释了我的领域:

@JsonSerialize(using = MapValuesToListSerializer.class)
private List<String> rules;

This doesn't work. 这行不通。 The standard fields all serialize correctly, but not the non-standard fields that conform to this field. 标准字段全部正确序列化,但不符合该字段的非标准字段。 I'm missing an important piece to this, but I do not know what. 我缺少重要的内容,但是我不知道是什么。

While I completely agree with JB Nizet when he states, "If it's one of your own web services, fix it." 尽管我完全同意JB Nizet所说的话,“如果它是您自己的Web服务之一,请对其进行修复。” This is just not something within my grasp, due to the powers that be. 由于存在的力量,这不是我能掌握的。 I would if I could, otherwise, but that isn't up to me. 如果可以的话我会的,否则,但这不取决于我。 (Nor do I have the personal bandwidth fix this and any other nSize downstream issues with would cause.) (我也没有个人带宽来解决此问题,否则将导致任何其他nSize下游问题。)

Fortunately, Jackson does come with the tools to do this. 幸运的是,杰克逊确实提供了执行此操作的工具。

For the field: 对于领域:

@JsonProperty("rules")
@JsonDeserialize(as = List.class, using = BadmapToListDeserializer.class)
private List<String> rules;

The actual deserializer: 实际的反序列化器:

public class BadmapToListDeserializer extends JsonDeserializer<List<String>> {
    @Override
    public List<String> deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException, JsonProcessingException {
        List<String> valuesList = new ArrayList<>();
        for (JsonToken jsonToken = jsonParser.nextToken(); jsonToken != JsonToken.END_OBJECT; jsonToken = jsonParser.nextToken()) {
            if (jsonToken == JsonToken.VALUE_STRING) {
                String value = jsonParser.getText();
                valuesList.add(value);
            }
        }
        return valuesList;
    }
}

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

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