deserialization

XML deserialization to POJO using Jackson XmlMapper

醉酒当歌 提交于 2019-12-17 22:50:35
问题 Using Jackson XmlMapper annotations, how would I deserialize this XML into a pojo? <?xml version="1.0" encoding="UTF-8"?> <open> <creds priv="write" type="internal"> <user>Username1</user> <client_token>abcplaudzrbcy37c</client_token> <client_secret>0cxDE3LE0000=</client_secret> </creds> <creds priv="read" type="internal"> <user>Username1</user> <client_token>123plaudzrbcy37c</client_token> <client_secret>0cxDE3LE1234=</client_secret> </creds> <creds priv="none" type="internal"> <user

Dictionary is empty on deserialization

两盒软妹~` 提交于 2019-12-17 20:50:57
问题 I'm currently writing a bidirectional map class, and I'm having some troubles with the serialization/deserialization of the class (question at bottom). Here's the parts of the class that's relevant. /// <summary> /// Represents a dictionary where both keys and values are unique, and the mapping between them is bidirectional. /// </summary> /// <typeparam name="TKey"> The type of the keys in the dictionary. </typeparam> /// <typeparam name="TValue"> The type of the values in the dictionary. <

Deserializing public property with non-public setter in json.net

耗尽温柔 提交于 2019-12-17 20:15:16
问题 Suppose I have the following class - public class A { public int P1 { get; internal set; } } Using json.net, I am able to serialize the type with P1 property. However, during deserialization, P1 is not set. Without modifying class A, is there an in build way to handle this? In my case, I am using a class from a different assembly and cannot modify it. 回答1: Yes, you can use a custom ContractResolver to make the internal property writable to Json.Net. Here is the code you would need: class

JsonProperty - Use different name for deserialization, but use original name for serialization?

拟墨画扇 提交于 2019-12-17 19:47:12
问题 I am retrieving JSON from an API. I am using newtonsoft (this is json.net right?) to deserialize this into a list of objects. It works. Unfortunately I also need to pass this along to someone else as JSON (they can't call the API directly only I have access to it). I say unfortunately because I need to OUTPUT my JSON that is different from what is being received (the property names need to be different). For example, I have a class called Person, with a property called Name. I want to get

Deserialize multiple Java Objects

放肆的年华 提交于 2019-12-17 19:39:52
问题 hello dear colleagues, I have a Garden class in which I serialize and deserialize multiple Plant class objects. The serializing is working but the deserializing is not working if a want to assign it to calling variable in the mein static method. public void searilizePlant(ArrayList<Plant> _plants) { try { FileOutputStream fileOut = new FileOutputStream(fileName); ObjectOutputStream out = new ObjectOutputStream(fileOut); for (int i = 0; i < _plants.size(); i++) { out.writeObject(_plants.get(i)

Force GSON to use specific constructor

眉间皱痕 提交于 2019-12-17 16:33:44
问题 public class UserAction { private final UUID uuid; private String userId; /* more fields, setters and getters here */ public UserAction(){ this.uuid = UUID.fromString(new com.eaio.uuid.UUID().toString()); } public UserAction(UUID uuid){ this.uuid = uuid; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final UserAction other = (UserAction) obj; if (this.uuid != other.uuid && (this.uuid == null || !this.uuid

Gson - deserialization to specific object type based on field value

萝らか妹 提交于 2019-12-17 10:46:19
问题 I want to deserialize json objects to specific types of objects (using Gson library) based on type field value, eg.: [ { "type": "type1", "id": "131481204101", "url": "http://something.com", "name": "BLAH BLAH", "icon": "SOME_STRING", "price": "FREE", "backgroundUrl": "SOME_STRING" }, { .... } ] So type field will have different (but known) values. Based on that value I need to deserialize that json object to appropriate model object, eg.: Type1Model, Type2Model etc. I know I can easily do

Make Jackson use a custom deserializer everywhere (for a type which isn't mine)

泄露秘密 提交于 2019-12-17 10:34:42
问题 I am trying to setup Jackson JSON custom deserializer to convert JSON values into a Long object. I followed the instructions on this site : http://wiki.fasterxml.com/JacksonHowToCustomDeserializers to setup the custom deserializer. However, for the custom deserializer to kick in, I have to always annotate each time e.g. public class TestBean { Long value; @JsonDeserialize(using=LongJsonDeserializer.class) public void setValue(Long value) { this.value = value; } } Is there a way to tell

Jackson JSON library: how to instantiate a class that contains abstract fields

人盡茶涼 提交于 2019-12-17 09:35:44
问题 I want to convert a JSON string into java object, but the class of this object contains abstract fields, which Jackson can't instantiate, and doesn't produce the object. What is the easiest way to tell it about some default implementation of an abstract class, like setDefault(AbstractAnimal.class, Cat.class); or to decide about the implementation class based on JSON attribute name, eg. for JSON object: { ... cat: {...} ... } i would just wite: setImpl("cat", Cat.class); I know it's possible

Ignore parsing errors during JSON.NET data parsing

*爱你&永不变心* 提交于 2019-12-17 09:14:09
问题 I have an object with predefined data structure: public class A { public string Id {get;set;} public bool? Enabled {get;set;} public int? Age {get;set;} } and JSON is supposed to be { "Id": "123", "Enabled": true, "Age": 23 } I want to handle JSON error in positive way, and whenever server returns unexpected values for defined data-types I want it to be ignore and default value is set (null). Right now when JSON is partially invalid I'm getting JSON reader exception: { "Id": "123", "Enabled":