unmarshalling

What's the difference between JAXB annotations put on getter versus setters versus members?

一曲冷凌霜 提交于 2019-12-04 10:12:44
问题 Title says it all. I would like to know what is the principial difference between putting JAXB annotation (like @XmlElement ) on field / getter / setter. It seems to me that (in simple cases) it does not matter. E.g. lets take this class A { private String a; public String getA() { return a; } public void setA(String a) { this.a = a; } } now it seems to me that it does not matter if I put @XmlElement on member field or on getter / setter. It just marshalls ok. Are there any usecases when I

How to Unmarshal simple xml with multiple items in Go?

ぃ、小莉子 提交于 2019-12-04 06:19:42
问题 I would like to get a slice of people ([]People) from the following xml: <file> <person> <name>John Doe</name> <age>18</age> </person> <person> <name>Jane Doe</name> <age>20</age> </person> </file> (All other similar questions were too specific and verbose) 回答1: You need to create two structs: one to represent the <file> </file> one for the repeating record <person> </person> Please see the comments inside the code: package main import ( "encoding/xml" "fmt" ) var sourceXML = []byte(`<file>

How do I serialize & deserialize CSV properly?

泄露秘密 提交于 2019-12-04 05:19:52
问题 I've been trying to serialize an object to a CSV String but the object contains a List and @JsonUnwrapped doesn't work on List objects. Expected sample output : color,part.name\n red,gearbox\n red,door\n red,bumper Actual output : com.fasterxml.jackson.core.JsonGenerationException: Unrecognized column 'name': Here is my code : (Most of it is the 2 POJO's) import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson

JSON Unmarshal struct case-sensitively

别说谁变了你拦得住时间么 提交于 2019-12-04 02:57:09
问题 Is there any way to make json.Unmarshal not accept a case-insensitive match? I receive a JSON with tags such as "e" and "E" and would like to unmarshal the object with tag "e" but ignore the one with "E". Right now the only solution I found was to define a struct containing both tags and then to simply ignore tag "E", but I'm looking for a cleaner solution. From the official doc: To unmarshal JSON into a struct, Unmarshal matches incoming object keys to the keys used by Marshal (either the

Unmarshalling an XML using Xpath expression and jaxb

痞子三分冷 提交于 2019-12-03 16:56:11
问题 I am new to JAXB and would like to know if there is a way by which I can unmarshall an XML to my response object but using xpath expressions. The issue is that I am calling a third party webservice and the response which I receive has a lot of details. I do not wish to map all the details in the XML to my response object. I just wish to map few details from the xml using which I can get using specific XPath expressions and map those to my response object. Is there an annotation which can help

JAXB (un)marshalling of xsd types: xsd:base64Binary and xsd:hexBinary

萝らか妹 提交于 2019-12-03 15:57:26
JAXB maps both xsd:base64Binary and xsd:hexBinary types to byte[] . Given that I have a schema/a DOM Element representing each of these types such as: <foo>ABCD</foo> for xsd:hexBinary and <foo>YTM0NZomIzI2OTsmIzM0NTueYQ==</foo> for xsd:base64Binary , it's not clear how JAXB 2.1 handles it. JAXB.unmarshal(new DOMSource(node), byte[].class) does not like the payload. Neither does the following: JAXBContext ctx = JAXBContext.newInstance(byte[].class); ctx.createUnmarshaller().unmarshal(node); What's the correct way of handling these types? Thanks in advance. The conversion between byte[] and the

Unmarshal CSV record into struct in Go

佐手、 提交于 2019-12-03 13:01:40
The problem how to automatically deserialize/unmarshal record from CSV file into Go struct. For example, I have type Test struct { Name string Surname string Age int } And CSV file contains records John;Smith;42 Piter;Abel;50 Is there an easy way to unmarshal those records into struct except by using "encoding/csv" package for reading record and then doing something like record, _ := reader.Read() test := Test{record[0],record[1],atoi(record[2])} Seems I've done with automatic marshaling of CSV records into structs (limited to string and int). Hope this would be useful. Here is a link to

JAXB: How can I unmarshal XML without namespaces

本秂侑毒 提交于 2019-12-03 11:33:36
I have an XML file: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <object> <str>the type</str> <bool type="boolean">true</bool> </object> And I want to unmarshal it to an object of the class below @XmlRootElement(name="object") public class Spec { public String str; public Object bool; } How can I do this? Unless I specify namespaces (see below), it doesn't work. <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <object> <str>the type</str> <bool xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:boolean">true</bool> <

Golang XML Unmarshal and time.Time fields

纵然是瞬间 提交于 2019-12-03 08:39:57
问题 I have XML data I am retrieving via a REST API that I am unmarshal-ing into a GO struct. One of the fields is a date field, however the date format returned by the API does not match the default time.Time parse format and thus the unmarshal fails. Is there any way to specify to the unmarshal function which date format to use in the time.Time parsing? I'd like to use properly defined types and using a string to hold a datetime field feels wrong. Sample struct: type Transaction struct { Id

Is using Parcelable the right way to send data between applications?

▼魔方 西西 提交于 2019-12-03 07:44:22
I am trying to understand how to communicate between applications in Android - not just between Activity instances. I set up a 'client' that sends a Messenger obj to a Service (in the Intent sent to the service); the service creates a Message obj and sends it back to the 'client' using messenger.send(message) . This works fine until I try to use the Message.obj to hold an object. I created my own Parcelable class MyParcelable in the service and put it into the message. All works until the message is unmarshalled in the 'client'. The unmarshall fails because the 'client' has no access to the