我正在尝试使用Jackson将一些JSON转换为包含一些简单字符串和另一个类的类的实例,我正在使用@JsonCreator。 杰克逊似乎无法创建其他类的实例。
问题是,当我将此代码作为测试的一部分运行时:
ObjectMapper mapper = new ObjectMapper(); Player player = mapper.readValue(json.toString(), Player.class);我得到以下异常:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "characterClass" (class xxx.xxx.Player), not marked as ignorable (2 known properties: "name", "character"])我试图在我的简单测试中解析的JSON看起来像这样:
{ "name": "joe", "characterClass": "warrior", "difficulty": "easy", "timesDied": 2 }我有一个看起来有点像这样的“玩家”
public class Player { @JsonProperty("name") private String playerName; @JsonProperty // <-- This is probably wrong private Character character; // Some getters and setters for those two fields and more }还有另一个看起来像这样的“角色”
public class Character{ private PlayerClass playerClass; private Difficulty difficulty; private int timesDied; @JsonCreator public Character(@JsonProperty("characterClass") String playerClass, @JsonProperty("difficulty") String diff, @JsonProperty("timesDied") int died) { // Validation and conversion to enums this.playerClass = PlayerClass.WARRIOR; this.difficulty = Difficulty.EASY; this.timesDied = died; } // Again, lots of getters, setters, and other stuff }对于像这样的小数据集,可以有更好的方法来构建整个事物,但我认为这可以用于示例的目的。 我的真实代码更复杂,但我想做一个简单的例子。
我想我已经搞砸了杰克逊的注释,但我不确定我做错了什么。
I'm trying to use Jackson to convert some JSON into an instance of a class that contains some simple Strings and another class, which I'm using @JsonCreator for. It seems that Jackson isn't able to create the instance of the other class.
The problem is that when I run this code as part of a test:
ObjectMapper mapper = new ObjectMapper(); Player player = mapper.readValue(json.toString(), Player.class);I get the following exception:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "characterClass" (class xxx.xxx.Player), not marked as ignorable (2 known properties: "name", "character"])The JSON I'm trying to parse in my simple test looks like this:
{ "name": "joe", "characterClass": "warrior", "difficulty": "easy", "timesDied": 2 }I have a class 'Player' that looks a bit like this
public class Player { @JsonProperty("name") private String playerName; @JsonProperty // <-- This is probably wrong private Character character; // Some getters and setters for those two fields and more }And another class 'Character' that looks like this
public class Character{ private PlayerClass playerClass; private Difficulty difficulty; private int timesDied; @JsonCreator public Character(@JsonProperty("characterClass") String playerClass, @JsonProperty("difficulty") String diff, @JsonProperty("timesDied") int died) { // Validation and conversion to enums this.playerClass = PlayerClass.WARRIOR; this.difficulty = Difficulty.EASY; this.timesDied = died; } // Again, lots of getters, setters, and other stuff }For small sets of data like this there would be better ways to structure the whole thing, but I think this works for the purposes of an example. The real code I have is more complex but I wanted to make simple example.
I think I've messed up the Jackson annotations, but I'm not sure what I've done wrong.
最满意答案
您需要在Player上指定与JSON输入匹配的创建者。 例如:
@JsonCreator public static Player fromStringValues(@JsonProperty("name") String name, @JsonProperty("characterClass") String characterClass, @JsonProperty("difficulty") String difficulty, @JsonProperty("timesDied") Integer timesDied) { Player player = new Player(); player.setPlayerName(name); player.setCharacter(new Character(characterClass, difficulty, timesDied)); return player; }请注意,您可以像这样构建您的枚举,杰克逊将为您执行从字符串到枚举的转换。
You need to specify a creator on Player that matches your JSON input. For example:
@JsonCreator public static Player fromStringValues(@JsonProperty("name") String name, @JsonProperty("characterClass") String characterClass, @JsonProperty("difficulty") String difficulty, @JsonProperty("timesDied") Integer timesDied) { Player player = new Player(); player.setPlayerName(name); player.setCharacter(new Character(characterClass, difficulty, timesDied)); return player; }A side note, you can structure your enums like this and Jackson will do the conversion from string to enum for you.
更多推荐
发布评论