Hi
On 04/05/12 10:52, Govindaram PS wrote:
When the json data includes namespace, marshalling to json string and
unmarshling the same string is failing.
The test case to verify is below. This test case can be directly executed
in 
cxf/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/JSONProviderTest.java
let me know if anything wrong in this test case.


Base1 class has no XMLRootElement annotation, so the following applies:
http://cxf.apache.org/docs/jax-rs-data-bindings.html#JAX-RSDataBindings-HandlingJAXBbeanswithoutXmlRootElementannotations,

I used marshallAsJaxbElement and unmarshallAsJaxbElement properties instead, but the class map property can do too

Cheers, Sergey





     @Test
     public void testWriteReadNamespace() throws Exception {
         JSONProvider<Base1>  provider = new JSONProvider<Base1>();
         Map<String, String>  namespaceMap = new HashMap<String, String>();
         namespaceMap.put("http://derivedtest";, "ns1");
         provider.setNamespaceMap(namespaceMap);
         List<String>  classnames = new ArrayList<String>();
         classnames.add(Base1.class.getName());
         provider.setJaxbElementClassNames(classnames);
         Base1 b = new Base1("base");

         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         provider.writeTo(b, Base1.class, Base1.class,
                        new Annotation[0], MediaType.APPLICATION_JSON_TYPE,
new MetadataMap<String, Object>(), bos);
         readBase(bos.toString());
     }

     private void readBase(String data) throws Exception {
         JSONProvider<Base1>  provider = new JSONProvider<Base1>();
         Map<String, String>  namespaceMap = new HashMap<String, String>();
         namespaceMap.put("http://derivedtest";, "ns1");
         provider.setNamespaceMap(namespaceMap);
         List<String>  classnames = new ArrayList<String>();
         classnames.add(Base1.class.getName());
         provider.setJaxbElementClassNames(classnames);
         ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes());
         Base1 base = provider.readFrom(
                        Base1.class, Base1.class,
                        new Annotation[0], MediaType.APPLICATION_JSON_TYPE,
new MetadataMap<String, String>(), is);
         assertEquals("base", base.getBase1Field());
     }

     @XmlAccessorType(XmlAccessType.FIELD)
     @XmlType(name = "Base1", namespace = "http://derivedtest";)
     public static class Base1 {

         protected String base1Field;

         Base1() { }

         Base1(String base) {
             base1Field = base;
         }

         public String getBase1Field() {
             return base1Field;
         }

         public void setBase1Field(String value) {
             this.base1Field = value;
         }
     }



--
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

Reply via email to