XStream
  1. XStream
  2. XSTR-455

JPA Entity + JSON serialization

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Incomplete
    • Affects Version/s: 1.2.2
    • Fix Version/s: None
    • Component/s: Annotations, Converters
    • Labels:
      None
    • JDK version and platform:
      JDK 1.6 Ubuntu 7.10 Tomcat 6.0 Toplink with JPA

      Description

      Hello,

      Actually im using a JPA specification and is required to serialize these objects to JSON notation (View).
      When i try to serialize a object to JSON in the middle of the transaction a ClassCastException is throw.
      The context :

      First my Entity class

      @Entity
      public class CatalogoDeProdutos implements java.io.Serializable {

      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      @XStreamAlias("idCatalogo")
      private Long idCatalogo;

      @Column
      @XStreamAlias("descricao")
      private String descricao;

      @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
      private List<Produto> produtos;

      @XStreamAlias("tipo")
      @Enumerated(EnumType.STRING)
      private TipoDoCatalogo tipo;

      gets sets ......

      ...
      CatalogoDeProdutos cp = new CatalogoDeProdutos();

      aa = JSONSerializationHelper.getJSONFromObject("a", CatalogoDeProdutos.class, cp.clone());

      System.out.print("Entity class with annotation"+aa.toString());
      ....

      public static JSONObject getJSONFromObject(String alias,Class c,Object o) throws JSONException, ClassNotFoundException

      { xstream= new XStream(new JettisonMappedXmlDriver()); xstream.alias(alias, c.getClass()); String jr = xstream.toXML(c.cast(o)); JSONObject js = new JSONObject(jr); xstream = null; jr = null; return js; }

      Work's fine out of the transaction and results

      Entity class with annotation {"net.cjb.malacma.nemesis2web.model.CatalogoDeProdutos":{"idCatalogo":"-1","produtos":"","tipo":"CATALOGO_VENDAS"}}

      then....

      in the middle of the transaction i've cloned the object to the serialization and the folllowing exception is throw.....

      java.lang.ClassCastException: org.codehaus.jettison.json.JSONArray
      cannot be cast to org.codehaus.jettison.json.JSONObject
      org.codehaus.jettison.mapped.MappedXMLStreamWriter.writeAttribute(MappedXMLStreamWriter.java:93)
      org.codehaus.jettison.mapped.MappedXMLStreamWriter.writeAttribute
      (MappedXMLStreamWriter.java:130)
      org.codehaus.jettison.mapped.MappedXMLStreamWriter.writeAttribute(MappedXMLStreamWriter.java:134)
      com.thoughtworks.xstream.io.xml.StaxWriter.addAttribute(StaxWriter.java:86)
      com.thoughtworks.xstream.io.WriterWrapper.addAttribute
      (WriterWrapper.java:30)
      com.thoughtworks.xstream.converters.reflection.SerializableConverter$1.defaultWriteObject(SerializableConverter.java:146)
      com.thoughtworks.xstream.converters.reflection.SerializableConverter.doMarshal
      (SerializableConverter.java:201)
      com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:43)
      com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert
      (AbstractReferenceMarshaller.java:55)
      com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:50)
      com.thoughtworks.xstream.annotations.AnnotationReflectionConverter.marshallField(AnnotationReflectionConverter.java
      :46)
      com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:112)
      com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.visit
      (AbstractReflectionConverter.java:88)
      com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider.visitSerializableFields(PureJavaReflectionProvider.java:117)
      com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal
      (AbstractReflectionConverter.java:73)
      com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:43)
      com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert
      (AbstractReferenceMarshaller.java:55)
      com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:50)
      com.thoughtworks.xstream.core.TreeMarshaller.start(TreeMarshaller.java:73)
      com.thoughtworks.xstream.core.ReferenceByXPathMarshallingStrategy.marshal
      (ReferenceByXPathMarshallingStrategy.java:34)
      com.thoughtworks.xstream.XStream.marshal(XStream.java:765)
      com.thoughtworks.xstream.XStream.marshal(XStream.java:754)
      com.thoughtworks.xstream.XStream.toXML(XStream.java
      :735)
      com.thoughtworks.xstream.XStream.toXML(XStream.java:725)
      net.cjb.malacma.nemesis2web.helper.JSONSerializationHelper.getJSONFromObject(JSONSerializationHelper.java:37)
      net.cjb.malacma.nemesis2web.bo.ControleDeProduto.findCatalogoFull
      (ControleDeProduto.java:65)
      net.cjb.malacma.nemesis2web.facade.MDIFacadeSistema.findCatalogoFull(MDIFacadeSistema.java:878)
      net.cjb.malacma.nemesis2web.mvc.control.ProdutoController.handleRequest(ProdutoController.java
      :41)
      org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
      org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:819)
      org.springframework.web.servlet.DispatcherServlet.doService
      (DispatcherServlet.java:754)
      org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:399)
      org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:354)
      javax.servlet.http.HttpServlet.service
      (HttpServlet.java:690)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)

      Any ideia? Thanks for reading this!

      1. 1.png
        144 kB
      2. Captura_da_tela.png
        147 kB

        Issue Links

          People

          • Assignee:
            Jörg Schaible
            Reporter:
            Moretto
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: