当前位置: 首页 > 知识库问答 >
问题:

使用Jackson fasterxml反序列化属性类

苏德容
2023-03-14
Can not deserialize instance of java.lang.String out of START_OBJECT token Properties("property1")
    package com.sharecare.service.segment;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.jamconsultg.jamplatform.domain.segmentation.Segment;

import java.util.List;
import java.util.Properties;

/**
 * Created by wawada on 1/15/16.
 */

public class SubSkyScraper{

    @JsonProperty("_id")
    private String id;

    private String name;

    private Properties properties;

    @JsonProperty("_segments")
    private List<Segment> segments;

    public SubSkyScraper() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public List<Segment> getSegments() {
        return segments;
    }

    public void setSegments(List<Segment> segments) {
        this.segments = segments;
    }
}
    package com.sharecare.service.segment;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jamconsultg.jamplatform.domain.segmentation.Segment;
import com.sharecare.segment.SkyScraper;
import org.junit.Test;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

/**
 * Created by wawada on 1/15/16.
 */


public class ObjectMapperTest {

    @Test
    public void testGettingTheObject(){

        String json = "{\n" +
                "\"_id\": \"objectid\" ,\n" +
                "\"_segments\" : [ \"global\"] , \n" +
                "\"name\" : \"sc\" , \n" +
                "\"properties\" : { \n" +
                "    \"property1\": {\n" +
                "        \"property2\" : \"value1\"\n" +
                "    },\n" +
                "    \"property3\" : { \"property4\": \"value2\"} , \n" +
                "    \"property5\": {\n" +
                "        \"property6\" : { \n" +
                "                    \"property7\": \"value3\" , \n" +
                "                    \"property8\": \"value4\"\n" +
                "    }\n" +
                "}\n" +
                "}";


        ObjectMapper objectMapper = new ObjectMapper();
        try {
            objectMapper.readValue(json, SubSkyScraper.class);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }




}
    com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
 at [Source: {
"_id": "objectid" ,
"_segments" : [ "global"] , 
"name" : "sc" , 
"properties" : { 
    "property1": {
        "property2" : "value1"
    },
    "property3" : { "property4": "value2"} , 
    "property5": {
        "property6" : { 
                    "property7": "value3" , 
                    "property8": "value4"
    }
}
}; line: 6, column: 5] (through reference chain: com.sharecare.service.segment.SubSkyScraper["properties"]->java.util.Properties["property1"])
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:857)
    at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:62)
    at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:11)
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringMap(MapDeserializer.java:495)
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:341)
    at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:26)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:520)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:95)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:258)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:125)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3736)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2726)
    at com.sharecare.service.segment.ObjectMapperTest.testGettingTheObject(ObjectMapperTest.java:43)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
{
"_id": "objectid" ,
"_segments" : [ "global "] , 
"name" : "sc" , 
"properties" : { 
    "property1": {
        "property2" : "value1"
    },
    "property3" : { "property4": "value2"} , 
    "property5": {
        "property6" : { 
                    "property7": "value3" , 
                    "property8": "value4"
    }
}
}

共有1个答案

申屠弘图
2023-03-14

以下是我的方法:

我正在使用如下所示的自定义反序列化器:

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

public class PropertiesDeserializer extends StdDeserializer<Properties> {
    private static final long serialVersionUID = 2743004642083542567L;

    public PropertiesDeserializer() { 
        this(null); 
    } 

    public PropertiesDeserializer(Class<?> vc) { 
        super(vc); 
    }

    @Override
    public Properties deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        JsonDeserializer<Object> mapDeserializer = findDeserializer(ctxt, ctxt.getTypeFactory().constructMapType(HashMap.class, String.class, Object.class), null);
        @SuppressWarnings("unchecked")
        Map<String,Object> rawValue = (Map<String, Object>) mapDeserializer.deserialize(jp, ctxt);
        if (rawValue == null) {
            return null;
        }
        Properties mappedValue = new Properties();
        rawValue.forEach((key, value) -> mappedValue.put(key, value));
        return mappedValue;
    }

}

然后将其注册到ObjectMapper中:

ObjectMapper mapper = new ObjectMapper();
SimpleModule simpleModule = new SimpleModule();
simpleModule.addDeserializer(Properties.class, new PropertiesDeserializer());
mapper.registerModule(simpleModule);
 类似资料:
  • 问题内容: 我如何才能序列化一个特定的属性,但又防止其反序列化回POCO?是否可以使用属性装饰特定属性? 基本上,我正在寻找一个与ShouldSerialize *方法等效的反序列化方法。 我知道我可以编写一个自定义转换器,但是这样做似乎有点过头了。 编辑: 这里还有一些背景。这背后的原因是我的课看起来像: 我需要请求的属性,但是我不希望它反序列化并触发设置程序逻辑。 问题答案: 最简单的方法是将

  • 问题内容: 我有一个简单的接口与属性的getter和setter。 我还有另一个实现此接口的UserAccount类。 我的问题是我想序列化money属性,但在反序列化它时忽略它,即,不接受用户对此属性的任何值。我在setter上尝试过@JsonIgnore,在getter上尝试过@JsonIgnore(false),它确实会忽略它,但是在序列化它时也会这样做。 我在setter上尝试了@Json

  • 我有另一个类UserAccount实现了这个接口。 我的问题是,我想序列化money属性,但在反序列化时忽略它,即不接受用户对该属性的任何值。我在setter上尝试了@jsonIgnore,在getter上尝试了@jsonIgnore(false),它确实忽略了它,但是它在序列化它的同时也忽略了它。 我在setter上尝试了@jsonIgnore,在getter上尝试了@jsonProperty,

  • 关于这个问题: 如何在使用JSON.NET序列化时更改属性名称? 会像预期的那样反序列化。 作为一个解决方案,没有属性也可以工作,或者类上有一个属性,比如:

  • 问题内容: 使用Jackson 2,我正在寻找一种 通用的 方式将对象序列化为单个值(然后序列化它们,然后再填充该单个字段),而不必重复创建JsonSerializer / JsonDeserializer来处理每种情况。@JsonIdentityInfo批注非常接近,但由于我知道,它将始终对完整的子对象进行序列化,因此略微遗漏了该标记。 这是我想做的一个例子。给定的类: 我希望Order可以序列