使用Jackson将JSON字符串数组反序列化为JAVA枚举列表失败

时间:2014-11-14 16:44:13

标签: java json jackson

我使用Jackson映射器使用其convertValue()方法反序列化JSON字符串。

它适用于我尝试做的几乎所有事情。

我的问题是我有一个包含字符串列表的JSON字符串:

{ 'game_models' : ["S", "T"] }

在我的Java代码中,mapper.convertValue尝试将数组转换为Java枚举,如下所示:

@Converter(autoApply=true)
public class GameModelConverter implements AttributeConverter<GameModelType, Character> {

public enum GameModelType {
    SINGLE('S'),
    TOURNAMENT('T');

    private final char model;

    private final String collectionName;

    GameModelType(char model) {
        this.model = model;
    }


    @JsonValue
    public char value() {
        return model;
    }
}

这是我的转换方法:

public void convert(GameModelType... game_models) {
    mapper.convertValue(game_models, GameModelType.class);
}

例外:

GameModelConverter$GameModelType out of START_OBJECT token at [Source: N/A; line: -1, column: -1]

修改 堆栈跟踪:

2014-11-14 16:41:02,707 ERROR [JsonUniChannel] - Unhandled exception in     
Message=RxMessage[{"action":"games_inventory","session_id":"096768b6-c2ad-4111-8b9f-    
862fbe15684d","game_models":["T","S"]}]
java.lang.IllegalArgumentException: Can not deserialize instance of    
com.sbg.helper.GameModelConverter$GameModelType out of START_OBJECT   
token at [Source: N/A; line: -1, column: -1] 
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:2615) ~[jackson- 
databind-2.2.2.jar:2.2.2] 
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:2542) ~ 
[jackson-databind-2.2.2.jar:2.2.2]
at com.luckyrhino.core.common.commands.JsonUniChannel.buildParams(JsonUniChannel.java:135)  
[core-common-1.0.jar:na] 
at com.luckyrhino.core.common.commands.JsonUniChannel.call(JsonUniChannel.java:76) [core-common-1.0.jar:na] 
at com.luckyrhino.core.common.commands.JsonUniChannel.call(JsonUniChannel.java:24) [core-common-1.0.jar:na] at rx.Observable$25.onNext(Observable.java:6656) [rxjava-core-0.20.0- RC2.jar:na]
at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:105) [rxjava-core-0.20.0-RC2.jar:na] 
at io.vertx.rxcore.java.impl.SingleSubscriptionHandler.fireNext(SingleSubscriptionHandler.java:107) [mod-rxvertx-1.0.0-beta4.jar:na]
at io.vertx.rxcore.java.eventbus.RxEventBus$ReceiveHandler.handle(RxEventBus.java:77) [mod-rxvertx-1.0.0-beta4.jar:na]
at io.vertx.rxcore.java.eventbus.RxEventBus$ReceiveHandler.handle(RxEventBus.java:75) [mod-rxvertx-1.0.0-beta4.jar:na]
at org.vertx.java.core.eventbus.impl.DefaultEventBus$11.run(DefaultEventBus.java:951) [vertx-core-2.1.2.jar:na]
at org.vertx.java.core.impl.DefaultContext$3.run(DefaultContext.java:175) [vertx-core-2.1.2.jar:na]
at   io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:370) [netty-all-4.0.20.Final.jar:4.0.20.Final]
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:353) [netty-all-4.0.20.Final.jar:4.0.20.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116) [netty-all-4.0.20.Final.jar:4.0.20.Final]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_25] Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.luckyrhino.sbg.shareddata.helper.GameModelConverter$GameModelType out of START_OBJECT token
at [Source: N/A; line: -1, column: -1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164) ~[jackson-databind-2.2.2.jar:2.2.2]
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:575) ~[jackson-databind-2.2.2.jar:2.2.2]
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:569) ~[jackson-databind-2.2.2.jar:2.2.2]
at com.fasterxml.jackson.databind.deser.std.EnumDeserializer.deserialize(EnumDeserializer.java:114) ~[jackson-databind-2.2.2.jar:2.2.2]
at com.fasterxml.jackson.databind.deser.std.EnumDeserializer.deserialize(EnumDeserializer.java:20) ~[jackson-databind-2.2.2.jar:2.2.2]
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.handleNonArray(ObjectArrayDeserializer.java:232) ~[jackson-databind-2.2.2.jar:2.2.2]
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:135) ~[jackson-databind-2.2.2.jar:2.2.2]
at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:18) ~[jackson-databind-2.2.2.jar:2.2.2]
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:2610) ~[jackson-databind-2.2.2.jar:2.2.2]
... 15 common frames omitted

0 个答案:

没有答案