unmarshalling

golang fails to parse json for reflection created object

跟風遠走 提交于 2019-12-11 03:43:27
问题 I try to write simple message protocol in go and i've encountered a problem. I have a lot of message types and i want to have a dictionary like this to manipulate with messages: var dict map[reflect.Type]int = map[reflect.Type]int{ reflect.TypeOf(DataMessage{}): 1000, reflect.TypeOf(TextMessage{}): 1001, //.... } func GetMessageTypeId(value interface{}) int { if id, ok := dict[reflect.TypeOf(value)]; ok { return id } else { return -1 } } func GetValueByTypeId(typeId int) interface{} { for

Should a web service response include empty values?

风流意气都作罢 提交于 2019-12-11 02:47:31
问题 When invoking a web service I get a dynamic response in XML format. So response could be : <response> <test1>test1</test1> <test2>test1</test2> <test3>test1</test3> <test4>test1</test4> </response> or : <response> <test1>test1</test1> <test2>test1</test2> </response> But I think the response should be static in order for the Java class to be unmarshalled correctly from the XML. So instead of <response> <test1>test1</test1> <test2>test1</test2> </response> This should be : <response> <test1

How to remove not required Elements from generated XML via jaxb

会有一股神秘感。 提交于 2019-12-11 02:25:16
问题 I want to know if there is anyway for removing not required elements from generated xml using jaxb.I have my xsd element definition as follows. <xsd:element name="Title" maxOccurs="1" minOccurs="0"> <xsd:annotation> <xsd:documentation> A name given to the digital record. </xsd:documentation> </xsd:annotation> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:minLength value="1"></xsd:minLength> </xsd:restriction> </xsd:simpleType> </xsd:element> As you can see it is not a mandatory

JSON decode into struct as interface{} yields map[string]interface{}, not struct

耗尽温柔 提交于 2019-12-11 01:59:24
问题 Here is a go playground replicating the issue: https://play.golang.org/p/GgHsLffp1G Basically, I'm trying to write a function that takes a struct and returns a function that can decode http requests as that type. Unfortunately some type information is being lost and the type being returned is a map[string]interface{} and not the correct struct type. How can I communicate the correct type to the JSON decoder? Would JSON unmarshal work better? 回答1: This seems to work: Playground func

JAXB: partial unmarshalling returns empty domain object

穿精又带淫゛_ 提交于 2019-12-11 01:29:07
问题 I'm trying to unmarshal a (well-formed) part of a xml string using the "partial jaxb unmarshaling" technique, described in the answer of this question. For this I'm using a subset of a JAXB domain models (which have been created by xjc) of the corresponding XML schema. This subset corresponds exactly to the part xml string which the XMLStreamReader processes. After ensuring that the XMLStreamReader position is correct (which is in fact the first element), I'm invoking Unmarshaller

Simple XML ElementListUnion - two generic lists not allowed?

时间秒杀一切 提交于 2019-12-10 19:18:05
问题 I am trying to desrialize an xml through simple framework. I have two lists whose types will be known only at runtime. So I used @ElementListUnion. Customer.java @ElementListUnion({@ElementList(inline = true,type=Thing.class),@ElementList(inline = true,type=AnotherThing.class)}) List<Object> things; @ElementListUnion({@ElementList(inline = true,type=Thing.class),@ElementList(inline = true,type=AnotherThing.class)}) List<Object> anotherthings ; But Im getting the following exception 03-20 19

SimpleXML parsing elementlist exception

三世轮回 提交于 2019-12-10 18:55:27
问题 Hi i have problem with parsing this xml : <?xml version="1.0" encoding="UTF-8"?> <Response> <Items> <Item> <TdlID>202</TdlID> <TdlDesc>1234567890</TdlDesc> <Due>2013-07-19</Due> <Done>0</Done> <Creadate>2013-07-08 23:43:51</Creadate> <UsrLogin>demouser</UsrLogin> <Prj>3211</Prj> <Chngdate>2013-07-17</Chngdate> </Item> <Item> <TdlID>218</TdlID> <TdlDesc>fghjkljh</TdlDesc> <Due>2013-07-31</Due> <Done>1</Done> <Creadate>2013-07-10 11:06:10</Creadate> <UsrLogin>demouser</UsrLogin> <Prj>abcd</Prj>

Generate Java class based on XSD without block substitution?

社会主义新天地 提交于 2019-12-10 18:14:06
问题 I am getting below error while maven build using jaxb(maven-jaxb2-plugin) compiler A class/interface with the same name "org.somePackage.customer" is already in use. Use a class customization to resolve this conflict. at com.sun.tools.xjc.util.CodeModelClassFactory.createClass(CodeModelClassFactory.java:121) at com.sun.tools.xjc.util.CodeModelClassFactory.createClass(CodeModelClassFactory.java:82) at com.sun.tools.xjc.generator.bean.ImplStructureStrategy$1.createClasses(ImplStructureStrategy

Why does json.Unmarshal return a map instead of the expected struct?

我们两清 提交于 2019-12-10 16:13:26
问题 See this playground: http://play.golang.org/p/dWku6SPqj5 Basically, the library I'm working on receives an interface{} as a parameter and then needs to json.Unmarshal that from a byte array. Under the covers, the interface{} parameter is a struct that matches the json structure of the byte array but the library doesn't have a reference to that struct (but it does have a reference to the corresponding reflect.Type through). Why can't the json package detect the underlying type? For some reason

json.Unmarshal fails when embedded type has UnmarshalJSON

落爺英雄遲暮 提交于 2019-12-10 15:31:58
问题 I'm trying to unmarshal a struct that has an embedded type. When the embedded type has an UnmarshalJSON method, the unmarshaling of the outer type fails: https://play.golang.org/p/Y_Tt5O8A1Q package main import ( "fmt" "encoding/json" ) type Foo struct { EmbeddedStruct Field string } func (d *Foo) UnmarshalJSON(from []byte) error { fmt.Printf("Foo.UnmarshalJSON\n") type Alias Foo alias := &Alias{} if err := json.Unmarshal(from, alias); err != nil { return fmt.Errorf("Error in Foo