ObjectMapper readValue

后端 未结 3 695
猫巷女王i
猫巷女王i 2021-01-22 12:37

I load a ressource file json with the text format

{
    \"sources\": [{
            \"prop1\": \"1\",
            \"prop2\": \"2\"

        },
        {
                 


        
相关标签:
3条回答
  • 2021-01-22 12:38

    First thing: Your JSON is invalid. There is a comma after the second object in the sources array. This has to be deleted.

    Second: I think you didn't choose the right type for your result. What your JSON represents is a map which maps from string to an array of objects. So the type should be something like Map<String, Props[]> (Since you didn't provide the name of your class, I called it Props.

    With these considerations you can construct a MapType by using ObjectMappers getTypeFactory() method and deserialize the value using the constructed type like shown below.

    ObjectMapper mapper = new ObjectMapper();
    TypeFactory typeFactory = mapper.getTypeFactory();
    MapType mapType = typeFactory.constructMapType(HashMap.class, String.class, Props[].class);
    Map<String, Props[]> map = mapper.readValue(s, mapType);
    
    0 讨论(0)
  • 2021-01-22 12:40

    I actually voted for the other answer, but this is my idea, to create the classes and let jackson do the work :

    public class ResourceTest {
    
        @Test
        public void test1() throws IOException {
            assertTrue(true);
    
            Resource resource = new Resource();
    
            resource.getRedirectrions().add(makeRedirectrion("rprop11", "rprop12"));
            resource.getRedirectrions().add(makeRedirectrion("rprop21", "rprop22"));
    
            resource.getSources().add(makeSource("sprop11","sprop12"));
            resource.getSources().add(makeSource("sprop21","sprop22"));
    
            String json = new ObjectMapper().writeValueAsString(resource);
            System.out.println(json);
    
            Resource resource1 = new ObjectMapper().readValue(json, Resource.class);
            System.out.println(resource1);
        }
    
        private Source makeSource(String prop1, String prop2) {
            Source source = new Source();
            source.setProp1(prop1);
            source.setProp2(prop2);
            return source;
        }
    
        private Redirectrion makeRedirectrion(String prop1, String prop2) {
            Redirectrion redirectrion = new Redirectrion();
            redirectrion.setProp1(prop1);
            redirectrion.setProp2(prop2);
            return redirectrion;
        }
    
    }
    

    Output is:

    {"sources":[{"prop1":"sprop11","prop2":"sprop12"},{"prop1":"sprop21","prop2":"sprop22"}],"redirectrions":[{"prop1":"rprop11","prop2":"rprop12"},{"prop1":"rprop21","prop2":"rprop22"}]}
    Resource{sources=[Source{prop1='sprop11', prop2='sprop12'}, Source{prop1='sprop21', prop2='sprop22'}], redirectrions=[Source{prop1='rprop11', prop2='rprop12'}, Source{prop1='rprop21', prop2='rprop22'}]}
    
    0 讨论(0)
  • 2021-01-22 12:44

    In your case, I would write a custom JsonDeserializer. Haven't really tested the code, but I think the idea is clear:

        final MyClassDeserializer myClassDeserializer = new MyClassDeserializer();
        final SimpleModule deserializerModule = new SimpleModule();
        deserializerModule.addDeserializer(MyClass.class, myClassDeserializer);
    
        final ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(deserializerModule);
    

    And the code for JsonDeserializer:

        public class MyClassDeserializer extends JsonDeserializer<MyClass> {
    
        @Override
        public MyClass deserialize(final JsonParser jsonParser, final DeserializationContext context)
                throws IOException {
            final JsonNode node = jsonParser.getCodec().readTree(jsonParser);
            final JsonNode sourcesNode = node.get("sources");
            if(node.isArray()) {
                final ArrayNode arrayNode = (ArrayNode) node;
                final Iterable<JsonNode> nodes = arrayNode::elements;
                final Set<Source> set = StreamSupport.stream(nodes.spliterator(), false)
                        .map(mapper)
                        .collect(Collectors.toSet());
                ...
            }
    
            ...
        }
    
    0 讨论(0)
提交回复
热议问题