Tag: 反序列化

jackson将额外的字段反序列化为地图

我希望将JSON对象中的任何未知字段反序列化为地图中的条目,该地图是pojo的成员。 例如json { “knownField” : 5, “unknownField1” : “926f7c2f-1ae2-426b-9f36-4ba042334b68”, “unknownField2” : “ed51e59d-a551-4cdc-be69-7d337162b691” } 和pojo class myObject{ int knownField; Map unknownFields; // getters/setters whatever } 有没有办法用jackson配置? 如果没有,是否有一种有效的方法来编写StdDeserializer来执行此操作(假设unknownFields的值可能是更复杂但众所周知的一致类型)?

jackson多态反序列化 – 您是否可以要求存在字段而不是特定值?

在动物园示例中使用旋转: public class ZooPen { public String type; public List animals; } public class Animal { public String name; public int age; } public class Bird extends Animal { public double wingspan; } 如果没有指定翼展,我想使用多态反序列化来构造Animal实例,如果是,则使用Bird实例。 在Jackson中,无类型反序列化通常看起来像这样: @JsonTypeInfo( use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = “wingspan”, visible = true, defaultImpl = Animal.class ) @JsonSubTypes({ @Type(value = […]

Gson使用递归依赖关系反序列化复杂对象

我有几个类包含彼此的递归依赖,我使用Gson GraphAdapterBuilder将它们序列化为JSON格式,并且它完美地工作。 现在我想将它们反序列化为相同的结构,但无法找到方法。 我举了一个例子: class ClassA{ public int field; public ClassB parent; public ClassA(int f, ClassB p){ field = f; parent = p; } } class ClassB{ public Vector vector = new Vector(); } … ClassB b = new ClassB(); ClassA a1 = new ClassA(1,b); ClassA a2 = new ClassA(2,b); ClassA a3 = new ClassA(3,b); […]

Java模型的JSON字段映射

发送的JSON: { “Banner”: “ABC” } Java模型: … @JsonIgnoreProperties(ignoreUnknown = true) @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) public class BannerData implements java.io.Serializable { private static final long serialVersionUID = 5664846645733319592L; @JsonProperty(value = “Banner”) private String banner; public String getBanner() { return banner; } public void setBanner(String banner) { this.banner = banner; } } 控制器: @RequestMapping(value = {“/GetBanner”}, method = […]

从单个文件反序列化多个对象

我有许多(同一类)的对象序列化到一个文件中。 但是在反序列化时,只反序列化了第一个序列化对象。 序列化代码: public void save() { File f = new File(“vehicule.txt”); try { if(!f.exists()) f.createNewFile(); } catch(IOException e) { } try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f,true)); oos.writeObject(this); } catch(IOException e) { } } 我认为问题在于: Vehicule v; while( (v = (Vehicule)ois.readObject()) != null ) 有没有更好的方法来检查文件的结尾?

从xstream反序列化xml文件

我正在使用Xstream来序列化Job对象。 它看起来很好。 但反序列化,我有一个问题: Exception in thread “main” com.thoughtworks.xstream.io.StreamException: : only whitespace content allowed before start tag and not . (position: START_DOCUMENT seen …. @1:1) at com.thoughtworks.xstream.io.xml.XppReader.pullNextEvent(XppReader.java:78) at com.thoughtworks.xstream.io.xml.AbstractPullReader.readRealEvent(AbstractPullReader.java:137) at com.thoughtworks.xstream.io.xml.AbstractPullReader.readEvent(AbstractPullReader.java:130) at com.thoughtworks.xstream.io.xml.AbstractPullReader.move(AbstractPullReader.java:109) at com.thoughtworks.xstream.io.xml.AbstractPullReader.moveDown(AbstractPullReader.java:94) at com.thoughtworks.xstream.io.xml.XppReader.(XppReader.java:48) at com.thoughtworks.xstream.io.xml.XppDriver.createReader(XppDriver.java:44) at com.thoughtworks.xstream.XStream.fromXML(XStream.java:853) at com.thoughtworks.xstream.XStream.fromXML(XStream.java:845) 你们其中一个人之前遇到过这个问题吗? 这是我为序列化做的方式: XStream xstream = new XStream(); Writer writer = new FileWriter(new File(“model.xml”)); […]

使用Gson对generics类型进行Java反序列化

我正在尝试反序列化一些generics类型。 它工作得很好,但我想把这段代码换成一个方法。 Type listType = new TypeToken<TestResult>(){}.getType(); TestResult result = new Gson().fromJson(json, listType); 如何做到这一点,certificate有关类型的信息?

Jackson De /在通用地图中序列化日期到字符串的日期

jackson有很多来自java.util.Date代码的例子,但他们似乎都在利用POJO注释。 我有通用的标量映射,我希望将其序列化为JSON。 这是当前的解串器设置; 非常简单: public class JSONUtils { static { DateFormat df = new SimpleDateFormat(“yyyy-MM-dd’T’HH:mm:ss.SSS”); mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true); mapper.setDateFormat(df); // this works for outbounds but has no effect on inbounds mapper.getDeserializationConfig().with(df); // Gave this a shot but still does not sniff strings for a format that we declare should be treated as […]

如何使用Flexjson JSONDeserializer?

我有一个字符串: [{“product_id”:”2″,”name”:’stack”‘},{“product_id”:”2″,”name”:”overflow”}]” 如何使用Flexjson的JSONDeserializer从上面的字符串中获取所有product_id ? 我有一个名为productinformation的类,其中包含product_id和name等字段。

在GSON中反序列化递归多态类

class Complex implements Recursive { Map map; … } class Simple implements Recursive { … } 如何反序列化这个json: { “type” : “complex”, “map” : { “a” : { “type” : “simple” }, “b” : { “type” : “complex”, “map” : { “ba” : { “type” : “simple” } } } } 使用谷歌GSON?