unmarshalling

how to use some indirection when unmarshalling json to java bean using Jersey using jaxb annotations

大兔子大兔子 提交于 2019-12-01 17:42:34
I'm trying to unmarshall some received json (from Jira restful web service). Problem is: an "issue" has a "summary" property and a list of fields. Summary is not present as an attribute in the received json, but as a value of the "fields" attribute. I insist on unmarshalling to this structure: @XmlRootElement class Issue { String summary; List<Field> fields; // getters/setters and lots of other fields } Received JSON: { "expand":"html", "self":"https://example.com/jira/rest/api/latest/issue/XYZ-1234", "key":"XYZ-1234", "fields": { "summary": { "name":"summary", "type":"java.lang.String",

how to use some indirection when unmarshalling json to java bean using Jersey using jaxb annotations

老子叫甜甜 提交于 2019-12-01 17:19:45
问题 I'm trying to unmarshall some received json (from Jira restful web service). Problem is: an "issue" has a "summary" property and a list of fields. Summary is not present as an attribute in the received json, but as a value of the "fields" attribute. I insist on unmarshalling to this structure: @XmlRootElement class Issue { String summary; List<Field> fields; // getters/setters and lots of other fields } Received JSON: { "expand":"html", "self":"https://example.com/jira/rest/api/latest/issue

json.Unmarshal nested object into string or []byte

余生颓废 提交于 2019-12-01 15:03:56
I'm trying to Unmarshal some json so that a nested object does not get parsed but just treated as a string or []byte . So I want to get the following: { "id" : 15, "foo" : { "foo": 123, "bar": "baz" } } Unmarshaled into: type Bar struct { Id int64 `json:"id"` Foo []byte `json:"foo"` } I get the following error: json: cannot unmarshal object into Go value of type []uint8 playground demo I think what you are looking for is the RawMessage type in the encoding/json package. The documentation states: type RawMessage []byte RawMessage is a raw encoded JSON object. It implements Marshaler and

Marshalling/Unmarshalling Java superclass and subclasses using JAXB

女生的网名这么多〃 提交于 2019-12-01 14:14:46
I've been experimenting with JAXB tutorials and have managed to get code working that generates an XML file from a Java object and then is able to use the XML to generate a Java object. At the moment it reads multiple instances of the same class to create an XML file similar to the one below <Car> <regplate>TR54</regplate> <colour>red</colour> <energyrating>5</energyrating> </Car> <Car> <regplate>BN04 THY</regplate> <colour>yellow</colour> <energyrating>3</energyrating> </Car> <Car> <regplate>BN05 THY</regplate> <colour>yellow</colour> <energyrating>5</energyrating> </Car> I would like to be

json.Unmarshal nested object into string or []byte

白昼怎懂夜的黑 提交于 2019-12-01 13:51:07
问题 I'm trying to Unmarshal some json so that a nested object does not get parsed but just treated as a string or []byte . So I want to get the following: { "id" : 15, "foo" : { "foo": 123, "bar": "baz" } } Unmarshaled into: type Bar struct { Id int64 `json:"id"` Foo []byte `json:"foo"` } I get the following error: json: cannot unmarshal object into Go value of type []uint8 playground demo 回答1: I think what you are looking for is the RawMessage type in the encoding/json package. The documentation

How to control JAXB in-memory schema generation ordering/sequence?

萝らか妹 提交于 2019-12-01 12:16:26
I've got 3 xsd files that depend on each other to build my element definitions. Each xsd file has its own namespace. When I generate my classes using JAXB xjc, I get 3 corresponding packages. So far so good. My problem comes when I want to do schema validation with the unmarshaller. In order to avoid having to read in the xsd files, I generate the schemas on the fly from the class in question being unmarshalled. However, since the class depends on objects from 2 other packages, it is unable to generate the schemas unless I specify all 3 packages. Already, that isn't a very practical solution,

How to work with non required JSON parameters in Go?

浪子不回头ぞ 提交于 2019-12-01 11:44:43
Hi I am working on a rest API in Go and I want the user to pass JSON parameters: Offset int64 `json:"offset"` Limit int64 `json:"limit"` SortBy string `json:"sortby"` Asc bool `json:"asc"` Username string `json:"username"` First_Name string `json:"first_name"` Last_Name string `json:"last_name"` Status string `json:"status"` But they are not always required so for example a user can pass only Offset and ignore the others. He can even send 0 parameters. How can I do this? When unmarshalling a value from JSON text, the json package does not require that all fields to be present in JSON, nor that

Unmarshalling XML with JAXB without unescaping characters

雨燕双飞 提交于 2019-12-01 09:41:22
imagine following situation: we receive a xml file from some external tool. Lately within this xml, there can be some escaped charakters in nodenames or within their richcontent tag, like in the following example (simplyfied): <map> <node TEXT="Project"> <node TEXT="ää"> <richcontent TYPE="NOTE"><html> <head> </head> <body> <p> I am a Note for Node ää! </p> </body> </html> </richcontent> </node> </node> </map> After unmarshalling the file with JAXB those escaped charakters get unescaped. Unfortunatly I need them to stay the way they are, meaning escaped. Is there any way to avoid unescaping

golang unmarshal complex json

ε祈祈猫儿з 提交于 2019-12-01 08:41:00
I have the following JSON blob, and I'm trying to decode it into Go. ["contig", "32", {"a":[33,41,35], "b":[44,34,42]}] I believe that I have to model the data structure of the JSON. I tried using a struct called Line : package main import ( "encoding/json" "fmt" ) type Line struct { Contig string Base string PopMap map[string][]int } func main() { j := []byte(`["contig", "32", {"a":[33,41,35], "b":[44,34,42]}]`) var dat Line err := json.Unmarshal(j, &dat) fmt.Println(dat) fmt.Println(err) } I got the following error: { map[]} json: cannot unmarshal array into Go value of type main.Line What

golang unmarshal complex json

六月ゝ 毕业季﹏ 提交于 2019-12-01 07:08:06
问题 I have the following JSON blob, and I'm trying to decode it into Go. ["contig", "32", {"a":[33,41,35], "b":[44,34,42]}] I believe that I have to model the data structure of the JSON. I tried using a struct called Line : package main import ( "encoding/json" "fmt" ) type Line struct { Contig string Base string PopMap map[string][]int } func main() { j := []byte(`["contig", "32", {"a":[33,41,35], "b":[44,34,42]}]`) var dat Line err := json.Unmarshal(j, &dat) fmt.Println(dat) fmt.Println(err) }