unmarshalling

JAXB should ignore element

偶尔善良 提交于 2020-02-05 07:14:18
问题 The structure <html> <span><h1>test</h1></span> <table> </table> </html> How can i get the text "test" if <span> might be a <div> ? @XmlAccessorType(XmlAccessType.FIELD) public class HtmlTag { @XmlElement(name = "h1") String h1; } Unmarshalls to null. 回答1: @XmlAccessorType(XmlAccessType.FIELD) public class HtmlTag { @XmlAnyElement List<org.w3c.dom.Element> elements; } get test string HtmlTag htmlTag = //... Element firstElement = htmlTag.elements.get(0); // this is first element, // currently

unmarshal ignore empty fields

巧了我就是萌 提交于 2020-01-24 03:15:09
问题 I get a JSON from a client on the successful submit of user details. Some element in the JSON can be skipped since they were not updated. On the Go server side, I have an equivalent struct defined. The server successfully marshals the JSON bytes into the struct. type user struct { Id *int64 `json:",omitempty"` Name *string `json:",omitempty"` Age *int64 `json:",omitempty"` } But for fields which are not recieved from client, unmarshal by default hard-codes nil for string and empty array for

How to work with non required JSON parameters in Go?

一世执手 提交于 2020-01-21 10:01:25
问题 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? 回答1: When

Apache Camel split JSONArray remove the double quote

落花浮王杯 提交于 2020-01-17 05:48:26
问题 I've split the JSONArray using JsonPathExpression, but the result removed every double quote in each JSON, here is my RouteBuilder. from("timer:scheduler?repeatCount=1") .to("http:localhost:8901/rest/getData") .split(new JsonPathExpression("$.[*]")) .process(new Processor() { @java.lang.Override public void process(Exchange exchange) throws Exception { String input = exchange.getIn().getBody(String.class); exchange.getIn().setBody(input); } }) .unmarshal().json(JsonLibrary.Jackson, Map.class)

JAXB Marshal and Unmarshal Map to/from <key>value</key>

心已入冬 提交于 2020-01-16 05:06:09
问题 I'm trying to marshal and unmarshal Map to/from value pairs. I can marshal the object successfully, however, I cannot unmarshal it from the xml. The unmarshal result is the key exist in the Map, however, its value is null. Here's the model I want to marshal and unmarshal: import java.util.Map; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import

Unmarshal a YAML to a struct with unexpected fields in Go

岁酱吖の 提交于 2020-01-15 12:11:23
问题 I encountered an issue while trying to unmarshal a struct with an unexported field using github.com/go-yaml/yaml . The struct looks like: type Example struct { ExportedField string `yaml:"exported-field"` OneMoreExported string `yaml:"one-more-exported"` unexportedField map[string]*AnotherExample `yaml:"unexported-field"` } type AnotherExample struct { Name string `yaml:"name"` } And I'd like to unmarshal such YAML as exported-field: lorem ipsum one-more-exported: dolor set unexported-field:

Unmarshal a YAML to a struct with unexpected fields in Go

走远了吗. 提交于 2020-01-15 12:09:20
问题 I encountered an issue while trying to unmarshal a struct with an unexported field using github.com/go-yaml/yaml . The struct looks like: type Example struct { ExportedField string `yaml:"exported-field"` OneMoreExported string `yaml:"one-more-exported"` unexportedField map[string]*AnotherExample `yaml:"unexported-field"` } type AnotherExample struct { Name string `yaml:"name"` } And I'd like to unmarshal such YAML as exported-field: lorem ipsum one-more-exported: dolor set unexported-field:

panic: json: cannot unmarshal array into Go value of type main.Structure

浪尽此生 提交于 2020-01-14 14:12:33
问题 What are you trying to accomplish? I am trying to parse data from a json api. Paste the part of the code that shows the problem. package main import ( "encoding/json" "fmt" "io/ioutil" "net/http" ) type Structure struct { stuff []interface{} } func main() { url := "https://api.coinmarketcap.com/v1/ticker/?start=0&limit=100" response, err := http.Get(url) if err != nil { panic(err) } body, err := ioutil.ReadAll(response.Body) if err != nil { panic(err) } decoded := &Structure{} fmt.Println(url

JAXB unmarshalling with @XmlMixed Annotation

╄→尐↘猪︶ㄣ 提交于 2020-01-14 06:07:34
问题 I have a problem while unmarshalling an XML document. I found good hints here ("JAXB- @XmlMixed usage for reading @XmlValue and @XmlElement") but I wasn't able to adopt this on my code. First.. here is an example of the xml: <test> <content type="text">This is a content text</content> <content type="attributes"> <attributeGroup name="group1"> <attribute name="attr1">value1</attribute> </attributeGroup> <attributeGroup name="group2"> <attribute name="attr2">value2</attribute> <attribute name=

Golang interface{} type misunderstanding

江枫思渺然 提交于 2020-01-13 13:00:12
问题 I got a bug in Go when using an interface{} as function parameter type, when given a non-pointer type, and using json.Unmarshal with it. Because a piece of code is worth a thousand words, here is an example: package main import ( "encoding/json" "fmt" ) func test(i interface{}) { j := []byte(`{ "foo": "bar" }`) fmt.Printf("%T\n", i) fmt.Printf("%T\n", &i) json.Unmarshal(j, &i) fmt.Printf("%T\n", i) } type Test struct { Foo string } func main() { test(Test{}) } Which outputs: main.Test