Actually, is your schema allowing for complex types with a mixed content ? That woudl probably explain then why thos whitspaces are captured .... I guess if you could also send me the schema or just generated classes (send it to me directly please) then it would help

cheers, Sergey
----- Original Message ----- From: "Sergey Beryozkin" <[email protected]>
To: <[email protected]>
Sent: Wednesday, July 22, 2009 12:56 PM
Subject: RE: bad json response



Hi

I can not reproduce it at the moment. I've created a test which reads
exactly the same XML you posted like this :

@Test
public void test() {
       InputStream is = getClass().getResourceAsStream("book.xml");
       JAXBContext context = JAXBContext.newInstance(new
Class[]{Component.class});
       Unmarshaller um = context.createUnmarshaller();
       JAXBElement jaxbEl = um.unmarshal(new StreamSource(is),
Component.class);
       Map<String, String> namespaceMap = new HashMap<String, String>();
       namespaceMap.put("http://xmp.cisco.com/wap/schemas/ui/Component";,
"");
       namespaceMap.put("http://xmp.cisco.com/wap/schemas/ui/Tree";, "");

       JSONProvider p = new JSONProvider();
       p.setSerializeAsArray(true);
       p.setNamespaceMap(namespaceMap);
       ByteArrayOutputStream os = new ByteArrayOutputStream();

       p.writeTo(jaxbEl, (Class)JAXBElement.class, JAXBElement.class,
JAXBElement.class.getAnnotations(),
                 MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String,
Object>(), os);
       String s = os.toString();
       System.out.println(s);
}

it outputs :

{"component":{"@id":"Tree","properties":{"treeProperties":{"multiSelect":true,"doubleClickOpen":false,"singleClickOpen":false,"lazyLoad":false},"node":{"iconclass":"myIconClass"}}}}

I created Component, Properties, TreeProperties and Node classes using the
posted XML as the source so I might've missed some specifics but I think the
problem is really to do with the fact that for some reasons
ignorable whitespaces have been captured somehow during the initial read and
they trick Jettison into outputting these BadgerFish notation like '$'
symbols (thanks to dejan for explaining it to me).

Can you please send me the actual XML file ? I created mine on Windows, so
may be in your case it has some special characters or something. Also, is it
how your read XML into JAXBElement ? I tried reading it into DOM first and
using Unmarshaller.unmarshall(Node node, Class) too, no difference

cheers, Sergey



Sergey Beryozkin-2 wrote:

Hi Vinh,

Ok, it's a JAXBElement which is being returned...I'll try to reproduce
the problem,
cheers, Sergey

-----Original Message-----
From: Vinh Nguyen (vinguye2) [mailto:[email protected]]
Sent: 21 July 2009 19:51
To: [email protected]
Subject: RE: bad json response

Hi Sergey,

Sorry for the late reply.  I'm using XSDs to define the object models,
generate the Java classes using standard JAXB, load data from XML files,
and unmarshal to Java classes previously generated.

The resource implementation looks like this:

@Path("/metaregistry")
public class MetaRegistryService {
    @GET
    @Path("/{objectID}")
    @Produces({"application/json", "application/xml"})
    public JAXBElement getMetaObject(@PathParam("objectID") String
objectID)
    throws WebApplicationException {
        // Simply load the XML file with the given objectID, and
ummarshal to the JAXB class.
        JAXBElement xmlObj = ...;
        return xmlObj;
    }
}

My beans.xml looks like this:

    <jaxrs:server id="metaRegistryService" address="/">
        <jaxrs:serviceBeans>
            <ref bean="metaRegistryServiceBean" />
        </jaxrs:serviceBeans>
        <jaxrs:providers>
            <ref bean="jsonProviderBean" />
        </jaxrs:providers>
    </jaxrs:server>
    <bean id="metaRegistryServiceBean"
class="com.cisco.xmp.wap.core.service.metaregistry.MetaRegistryService"
/>
    <util:map id="jsonNamespaceMap" map-class="java.util.Hashtable">
        <entry key="http://xmp.cisco.com/wap/schemas/ui/Component";
value="" />
        <entry key="http://xmp.cisco.com/wap/schemas/ui/Tree"; value=""
/>
    </util:map>
    <util:list id="jsonTypes">
        <value>application/json</value>
        <value>application/jettison</value>
    </util:list>
    <bean id="jsonProviderBean"
class="org.apache.cxf.jaxrs.provider.JSONProvider">
        <property name="namespaceMap" ref="jsonNamespaceMap" />
        <property name="serializeAsArray" value="true" />
        <property name="consumeMediaTypes" ref="jsonTypes" />
        <property name="produceMediaTypes" ref="jsonTypes" />
    </bean>

-Vinh


-----Original Message-----
From: Sergey Beryozkin [mailto:[email protected]]
Sent: Tuesday, July 21, 2009 9:12 AM
To: [email protected]
Subject: RE: bad json response


Can you please give me more info about this issue ?

cheers, Sergey

Sergey Beryozkin-2 wrote:

Actually, you did say your data are in the XML file.
So can you please post a sample resource class with a method showing
how the data is being returned ?

Just would like to see a bit better how JSONProvider does (eventually)

start working.

Thanks, Sergey

-----Original Message-----
From: Sergey Beryozkin
Sent: 19 July 2009 16:37
To: '[email protected]'
Subject: RE: bad json response

Hi

How does the Java class representing <component/> look like ?
JSONProvider uses Jettison which is a STAX parser, so JAXB just
streams the events and jettison reacts by converting them into JSON
sequences.
Perhaps some JAXB annotation confuses Jettison...

Thanks, Sergey

-----Original Message-----
From: Vinh Nguyen (vinguye2) [mailto:[email protected]]
Sent: 18 July 2009 20:36
To: [email protected]
Subject: bad json response

Hi,
I've implemented a REST service.  I have data in an XML file, use
standard JAXB, and configured
org.apache.cxf.jaxrs.provider.JSONProvider
in my beans.xml to convert the JAXB objects to JSON.

But when I visually inspect the JSON response, there seems to be
name-value entries appearing like "$":"\n        ".  Should these be
there?  I assume the XML parser used by CXF should have already
stripped out these unnecessary white spaces (i.e. carriage returns)
before passing to the JSON converter.

My XML looks like:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <component
xmlns="http://xmp.cisco.com/wap/schemas/ui/Component";
xmlns:ns2="http://xmp.cisco.com/wap/schemas/ui/Tree"; id="Tree">
    <properties>
        <ns2:treeProperties>
            <ns2:multiSelect>true</ns2:multiSelect>
            <ns2:doubleClickOpen>false</ns2:doubleClickOpen>
            <ns2:singleClickOpen>false</ns2:singleClickOpen>
            <ns2:lazyLoad>false</ns2:lazyLoad>
        </ns2:treeProperties>
        <ns2:node>
             <ns2:iconclass>myIconClass</ns2:iconclass>
        </ns2:node>
    </properties>
</component>

The JSON response is coming as:


{"component":{"@id":"Tree","properties":{"treeProperties":{"multiSelect"

:true,"doubleClickOpen":false,"singleClickOpen":false,"lazyLoad":false,"
$":"\n        "},"node":{"iconclass":"myIconClass","$":"\t\t\t
\n        "}}}}

The $ property seems to appear after each corresponding element
definition in the XML.  Any idea how to get rid of these "$"
properties?
On the client side, I'm using Dojo JSONPATH utils, and it's failing to

process the JSON string.

Thanks,
-Vinh




--
View this message in context:
http://www.nabble.com/bad-json-response-tp24551156p24590780.html
Sent from the cxf-user mailing list archive at Nabble.com.




--
View this message in context: 
http://www.nabble.com/bad-json-response-tp24551156p24604907.html
Sent from the cxf-user mailing list archive at Nabble.com.


Reply via email to