好吧,尽管我个人从来没有那样使用过杰克逊,但确实可以这样做。您可以将其反序列化为
JsonNode对象,然后将其转换为正确的类型。
final ObjectMapper objectMapper = new ObjectMapper();final MyClass myClass = new MyClass();myClass.foo = "bar";// Serializefinal String json = objectMapper.writevalueAsString(myClass);// Deserializefinal JsonNode jsonNode = objectMapper.readTree(json);// Get the @typefinal String type = jsonNode.get("@type").asText();// Create a Class-objectfinal Class<?> cls = Class.forName(type);// And convert itfinal Object o = objectMapper.convertValue(jsonNode, cls);System.out.println(o.getClass());
输出为:
我的课
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)