Re: JSON with XML Stream API/moshi
Stefan Traud <[email protected]> Fri, 21 Feb 2025 10:52:29 +0000 (UTC)
| Newsgroups | gmane.text.xml.axis.user |
|---|---|
| Message-ID | <[email protected]> |
------=_Part_2859222_226956580.1740135149825
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
In between I have also looked into the source of org.apache.axis2.json.mos=
hi.MoshiXMLStreamReader and I find
public int getAttributeCount() {
if (isStartElement()) {
return 0; // don't support attributes on tags in JSON convention
} else {
throw new IllegalStateException("Only valid on START_ELEMENT state");
}
}
public QName getAttributeName(int index) {
throw new UnsupportedOperationException("Method is not implemented");
}
Attributes get ignored (attribute count will be returned as 0). It appears =
that I cannot use the current XML Stream API based approach togehter with A=
DB in my case, at least not without substantial changes in the used xml-sch=
emas.
Am Mittwoch, 19. Februar 2025 um 09:26:07 MEZ hat Stefan Traud <stefan_=
[email protected]> Folgendes geschrieben: =20
=20
I'm attaching 2 debug logs: =20
- soap.log: processing of the getPrice request using the ADBClient from =
samples.quickstartadb (modified only to set the required currency attribute=
using StockQuoteService.setCurrency). This returns the expected result.
- json.log: processing of the getPrice request using a post of the json =
message string=C2=A0{"getPrice":{"@currency":"USD","symbol":"IBM"}} with co=
ntent-type application/json and url=C2=A0http://localhost:8080/axis2/servic=
es/StockQuoteService/getPrice.=C2=A0This results in internal server error d=
ue to missing required attribute.
For your reference, I'm also attaching the modified StockQuoteService.wsdl =
from the samples.quickstartadb (adding required attribute 'currency' to the=
getPrice data type).
I also played with various variations of the json message (using "currency"=
instead of "@currency", changing order, or using "_attributes" : {"currenc=
y":"USD"}. This trial-and-error approach does not work.
I also tried what happens if I change the wsdl to require an attribute in t=
he getPriceResponse (instead of in the request), as this will use the=C2=A0=
moshi.JsonFormatter to write the output JSON (I hoped to see what conventio=
n would be used by moshi when producing the json string). However the attri=
bute is simply ignored and not included in the response json.
Am Dienstag, 18. Februar 2025 um 16:38:57 MEZ hat robertlazarski <rober=
[email protected]> Folgendes geschrieben: =20
=20
Could you please paste or attach your logs? That may help me understand th=
e problem better. I don't have use cases combining XML and JSON in my own p=
rojects but I know the code pretty well and I can probably help.=20
On Mon, Feb 17, 2025 at 5:23=E2=80=AFAM Stefan Traud <[email protected]=
m> wrote:
Hi RobertUsing contract first with ADB, I continue to check feasibility to =
amend our existing service with JSON support (based on XML Stream API with =
moshi).
I'm currently struggling with xml types that include attributes. To reprodu=
ce the issue I have amended the wsdl in the samples.quickstartadb to includ=
e an attribute in the getPrice element:
<xs:element name=3D"getPrice">
=C2=A0=C2=A0=C2=A0=C2=A0<xs:complexType>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0<xs:sequence>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0<xs=
:element name=3D"symbol"=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0nillable=3D"true"=C2=A0t=
ype=3D"xs:string" />
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0</xs:sequence>
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0<xs:attribute name=3D"curre=
ncy"=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0type=3D"xs:string" use=3D"required"/>
=C2=A0=C2=A0=C2=A0=C2=A0</xs:complexType>
</xs:element>
My corresponding JSON message should then be something like:
{ "getPrice" : {"@currency": "USD", "symbol": "IBM" }}
However the parse method in the GetPrice ADBBean keeps complaining "Require=
d attribute currency is missing"; specifically this call returns null:
reader.getAttributeValue("http://quickstart.samples/xsd", "currency");
(However it works when using SOAP).
Do I need to define attributes differently in the JSON message?
=20
------=_Part_2859222_226956580.1740135149825
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<html><head></head><body><div class=3D"ydp3338840byahoo-style-wrap" style=
=3D"font-family:Helvetica Neue, Helvetica, Arial, sans-serif;font-size:13px=
;"><div></div>
<div dir=3D"ltr" data-setdir=3D"false">In between I have also looke=
d into the source of org.apache.axis2.json.moshi.MoshiXMLStreamReader and I=
find</div><div dir=3D"ltr" data-setdir=3D"false"><br></div><div dir=3D"ltr=
" data-setdir=3D"false"><div><div style=3D"padding: 0px 0px 0px 2px;"><div =
style=3D"font-family: Consolas; font-size: 10pt;"><p style=3D"color: rgb(0,=
0, 0); margin: 0px;"><span style=3D"color:#7f0055;font-weight:bold;">publi=
c</span> <span style=3D"color:#7f0055;font-weight:bold;">int</span> getAttr=
ibuteCount() {</p><p style=3D"color: rgb(0, 0, 0); margin: 0px;"> <s=
pan style=3D"color:#7f0055;font-weight:bold;">if</span> (isStartElement()) =
{</p><p style=3D"margin: 0px;"> <span style=3D"color: rgb(127, 0=
, 85); font-weight: bold;">return</span> 0; <font color=3D"#cd232c"><b>//</=
b></font><span style=3D"color: rgb(63, 127, 95);"> </span><font color=3D"#c=
d232c"><b>don't support attributes on tags in JSON convention</b></font></=
p><p style=3D"color: rgb(0, 0, 0); margin: 0px;"> } <span style=3D"c=
olor:#7f0055;font-weight:bold;">else</span> {</p><p style=3D"color: rgb(0, =
0, 0); margin: 0px;"> <span style=3D"color:#7f0055;font-weight:b=
old;">throw</span> <span style=3D"color:#7f0055;font-weight:bold;">new</spa=
n> IllegalStateException(<span style=3D"color:#2a00ff;">"Only valid on STAR=
T_ELEMENT state"</span>);</p><p style=3D"color: rgb(0, 0, 0); margin: 0px;"=
> }</p><p style=3D"color: rgb(0, 0, 0); margin: 0px;"> }</p><p st=
yle=3D"color: rgb(0, 0, 0); margin: 0px;"><br></p><p style=3D"color: rgb(0,=
0, 0); margin: 0px;"> <span style=3D"color:#7f0055;font-weight:bold;">p=
ublic</span> QName getAttributeName(<span style=3D"color:#7f0055;font-weigh=
t:bold;">int</span> <span style=3D"color:#6a3e3e;">index</span>) {</p><p st=
yle=3D"color: rgb(0, 0, 0); margin: 0px;"> <span style=3D"color:#7f0=
055;font-weight:bold;">throw</span> <span style=3D"color:#7f0055;font-weigh=
t:bold;">new</span> UnsupportedOperationException(<span style=3D"color:#2a0=
0ff;">"Method is not implemented"</span>);</p><div style=3D"color: rgb(0, 0=
, 0); margin: 0px;"> }</div><div style=3D"color: rgb(0, 0, 0); margin: 0=
px;"><br></div><div style=3D"color: rgb(0, 0, 0); margin: 0px;" dir=3D"ltr"=
data-setdir=3D"false">Attributes get ignored (attribute count will be retu=
rned as 0). It appears that I cannot use the current XML Stream API based a=
pproach togehter with ADB in my case, at least not without substantial chan=
ges in the used xml-schemas.</div></div></div></div><br></div><div><br></di=
v>
=20
</div><div id=3D"yahoo_quoted_0984148450" class=3D"yahoo_quoted">
<div style=3D"font-family:'Helvetica Neue', Helvetica, Arial, s=
ans-serif;font-size:13px;color:#26282a;">
=20
<div>
Am Mittwoch, 19. Februar 2025 um 09:26:07 MEZ hat S=
tefan Traud <[email protected]> Folgendes geschrieben:
</div>
<div><br></div>
<div><br></div>
=20
=20
<div><div id=3D"yiv9955715974"><div><div style=3D"font-fami=
ly:Helvetica Neue, Helvetica, Arial, sans-serif;font-size:13px;" class=3D"y=
iv9955715974ydpcdc29160yahoo-style-wrap"><div></div>
<div dir=3D"ltr">I'm attaching 2 debug logs:</div><div dir=3D"ltr">=
<ul dir=3D"ltr"><li>soap.log: processing of the getPrice request using the =
ADBClient from samples.quickstartadb (modified only to set the required cur=
rency attribute using StockQuoteService.setCurrency). This returns the expe=
cted result.</li><li>json.log: processing of the getPrice request using a p=
ost of the json message string <span>{"getPrice":{"@currency":"USD","s=
ymbol":"IBM"}} with content-type application/json and url </span><a re=
l=3D"nofollow noopener noreferrer" shape=3D"rect" target=3D"_blank" onclick=
=3D"return window.theMainWindow.showLinkWarning(this)" href=3D"http://local=
host:8080/axis2/services/StockQuoteService/getPrice.">http://localhost:8080=
/axis2/services/StockQuoteService/getPrice.</a> This results in intern=
al server error due to missing required attribute.</li></ul><div><br clear=
=3D"none"></div><div dir=3D"ltr">For your reference, I'm also attaching the=
modified StockQuoteService.wsdl from the samples.quickstartadb (adding req=
uired attribute 'currency' to the getPrice data type).</div><div dir=3D"ltr=
"><br clear=3D"none"></div><div dir=3D"ltr">I also played with various vari=
ations of the json message (using "currency" instead of "@currency", changi=
ng order, or using "_attributes" : {"currency":"USD"}. This trial-and-error=
approach does not work.</div><div dir=3D"ltr"><br clear=3D"none"></div><di=
v dir=3D"ltr">I also tried what happens if I change the wsdl to require an =
attribute in the getPriceResponse (instead of in the request), as this will=
use the <span>moshi.JsonFormatter to write the output JSON (I hoped t=
o see what convention would be used by moshi when producing the json string=
). However the attribute is simply ignored and not included in the response=
json.</span></div></div><div><br clear=3D"none"></div>
=20
</div><div id=3D"yiv9955715974yahoo_quoted_0201809127" class=3D"yiv=
9955715974yahoo_quoted">
<div style=3D"font-family:'Helvetica Neue', Helvetica, Arial, s=
ans-serif;font-size:13px;color:#26282a;">
=20
<div>
Am Dienstag, 18. Februar 2025 um 16:38:57 MEZ hat r=
obertlazarski <[email protected]> Folgendes geschrieben:
</div>
<div><br clear=3D"none"></div>
<div><br clear=3D"none"></div>
=20
=20
<div id=3D"yiv9955715974yqt48543" class=3D"yiv9955715974yqt=
2758732879"><div><div id=3D"yiv9955715974"><div><div dir=3D"ltr">Could you =
please paste or attach your logs? That may help me understand the problem b=
etter. I don't have use cases combining XML and JSON in my own projects but=
I know the code pretty well and I can probably help. <br clear=3D"none"></=
div><br clear=3D"none"><div id=3D"yiv9955715974yqt01052" class=3D"yiv995571=
5974yqt4468875365"><div class=3D"yiv9955715974gmail_quote yiv9955715974gmai=
l_quote_container"><div dir=3D"ltr" class=3D"yiv9955715974gmail_attr">On Mo=
n, Feb 17, 2025 at 5:23=E2=80=AFAM Stefan Traud <<a rel=3D"nofollow noop=
ener noreferrer" shape=3D"rect" ymailto=3D"mailto:[email protected]" t=
arget=3D"_blank" href=3D"mailto:[email protected]">stefan_traud@yahoo.=
com</a>> wrote:<br clear=3D"none"></div><blockquote style=3D"margin:0px =
0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex;" cla=
ss=3D"yiv9955715974gmail_quote"><div><div style=3D"font-family:Helvetica Ne=
ue, Helvetica, Arial, sans-serif;font-size:13px;"><div dir=3D"ltr">Hi Rober=
t</div><div dir=3D"ltr">Using contract first with ADB, I continue to check =
feasibility to amend our existing service with JSON support (based on XML S=
tream API with moshi).</div><div dir=3D"ltr"><br clear=3D"none"></div><div =
dir=3D"ltr">I'm currently struggling with xml types that include attributes=
. To reproduce the issue I have amended the wsdl in the samples.quickstarta=
db to include an attribute in the getPrice element:</div><div dir=3D"ltr"><=
br clear=3D"none"></div><div dir=3D"ltr"><div><div style=3D"padding:0px 0px=
0px 2px;"><div style=3D"color:rgb(0,0,0);font-family:Consolas;font-size:10=
pt;"><p style=3D"margin:0px;">=09=09=09<span style=3D"color:rgb(0,128,128);=
"><</span><span style=3D"color:rgb(63,127,127);">xs:element</span> <span=
style=3D"color:rgb(127,0,127);">name</span>=3D<span style=3D"color:rgb(42,=
0,255);font-style:italic;">"getPrice"</span><span style=3D"color:rgb(0,128,=
128);">></span></p><p style=3D"margin:0px;">=09=09=09=09<span style=3D"c=
olor:rgb(0,128,128);"> <</span><span style=3D"col=
or:rgb(63,127,127);">xs:complexType</span><span style=3D"color:rgb(0,128,12=
8);">></span></p><p style=3D"margin:0px;">=09=09=09=09=09<span style=3D"=
color:rgb(0,128,128);"> <=
</span><span style=3D"color:rgb(63,127,127);">xs:sequence</span><span style=
=3D"color:rgb(0,128,128);">></span></p><div style=3D"margin:0px;"><span =
style=3D"color:rgb(0,128,128);"> &=
nbsp; <</span><span style=3D"color:rgb(63,127,127=
);">xs:element</span> <span style=3D"color:rgb(127,0,127);">name</span>=3D<=
span style=3D"color:rgb(42,0,255);font-style:italic;">"symbol"</span> =
</div><div style=3D"margin:0px;"><span style=3D"color:rgb(127,0,127);">&nbs=
p; &=
nbsp; nillable</span>=3D<span style=3D"color:rgb(42,0,255);font-=
style:italic;">"true" </span><span style=3D"font-size:10pt;color:rgb(1=
27,0,127);">type</span><span style=3D"font-size:10pt;">=3D</span><span styl=
e=3D"font-size:10pt;color:rgb(42,0,255);font-style:italic;">"xs:string"</sp=
an><span style=3D"font-size:10pt;"> </span><span style=3D"font-size:10pt;co=
lor:rgb(0,128,128);">/></span></div><p style=3D"margin:0px;">=09=09=09=
=09=09<span style=3D"color:rgb(0,128,128);"> &=
nbsp; </</span><span style=3D"color:rgb(63,127,127);">xs:sequ=
ence</span><span style=3D"color:rgb(0,128,128);">></span></p><div style=
=3D"margin:0px;"><span style=3D"color:rgb(0,128,128);"> &n=
bsp; <</span><span style=3D"color:rgb(63,127,127)=
;">xs:attribute</span> <span style=3D"color:rgb(127,0,127);">name</span>=3D=
<span style=3D"color:rgb(42,0,255);font-style:italic;">"currency"</span>&nb=
sp;</div><div style=3D"margin:0px;"><span style=3D"color:rgb(127,0,127);">&=
nbsp; type=
</span>=3D<span style=3D"color:rgb(42,0,255);font-style:italic;">"xs:string=
"</span> <span style=3D"color:rgb(127,0,127);">use</span>=3D<span style=3D"=
color:rgb(42,0,255);font-style:italic;">"required"</span><span style=3D"col=
or:rgb(0,128,128);">/></span></div><p style=3D"margin:0px;">=09=09=09=09=
<span style=3D"color:rgb(0,128,128);"> </</span><=
span style=3D"color:rgb(63,127,127);">xs:complexType</span><span style=3D"c=
olor:rgb(0,128,128);">></span></p><p style=3D"margin:0px;">=09=09=09<spa=
n style=3D"color:rgb(0,128,128);"></</span><span style=3D"color:rgb(63,1=
27,127);">xs:element</span><span style=3D"color:rgb(0,128,128);">></span=
></p></div></div></div><br clear=3D"none"></div><div dir=3D"ltr">My corresp=
onding JSON message should then be something like:</div><div dir=3D"ltr"><b=
r clear=3D"none"></div><div dir=3D"ltr">{ "getPrice" : {"@currency": "USD",=
"symbol": "IBM" }}</div><div dir=3D"ltr"><br clear=3D"none"></div><div dir=
=3D"ltr">However the parse method in the GetPrice ADBBean keeps complaining=
"<span><span style=3D"padding:0px 0px 0px 2px;"><span style=3D"color:rgb(0=
,0,0);font-family:Consolas;font-size:10pt;">Required attribute currency is =
missing</span></span></span>"; specifically this call returns null:</div><d=
iv dir=3D"ltr"><br clear=3D"none"></div><div dir=3D"ltr"><div><div style=3D=
"padding:0px 0px 0px 2px;"><div style=3D"color:rgb(0,0,0);font-family:Conso=
las;font-size:10pt;"><div style=3D"margin:0px;"><span style=3D"color:rgb(10=
6,62,62);">reader</span>.getAttributeValue(<span style=3D"color:rgb(42,0,25=
5);">"<a rel=3D"nofollow noopener noreferrer" shape=3D"rect" target=3D"_bla=
nk" href=3D"http://quickstart.samples/xsd">http://quickstart.samples/xsd</a=
>"</span>, <span style=3D"color:rgb(42,0,255);">"currency"</span>);</div><d=
iv style=3D"margin:0px;"><br clear=3D"none"></div></div></div></div>(Howeve=
r it works when using SOAP).</div><div dir=3D"ltr"><br clear=3D"none"></div=
><div dir=3D"ltr">Do I need to define attributes differently in the JSON me=
ssage?</div><div dir=3D"ltr"><br clear=3D"none"></div><div dir=3D"ltr"><br =
clear=3D"none"></div></div></div></blockquote></div></div>
</div></div></div></div>
</div>
</div></div></div></div>
</div>
</div></body></html>
------=_Part_2859222_226956580.1740135149825--