Code example for StringWriter

Methods: getBuffertoString

0
   public static class MyElementC extends MyElement{ 
       
   } 
   public void testSerialization() throws Exception{     
      Serializer s = new Persister();
      StringWriter sw = new StringWriter();
      s.write(new Test4(new MyElement(), new MyElementA(), new MyElementB()), sw);     
      String serializedForm = sw.toString();
      System.out.println(serializedForm);
      System.out.println();
      Test4 o = s.read(Test4.class, serializedForm);
      sw.getBuffer().setLength(0);
      s.write(o, sw);
      System.out.println(sw.toString());
      System.out.println();
      sw.getBuffer().setLength(0);
             
      serializedForm =  "<test4>\n" + 
                     "   <single-element class=\"org.simpleframework.xml.core.Test4Test$MyElementC\"/>\n" +  
                     "   <elements>\n" +  
                     "      <elementA/>\n" +  
                     "      <elementB/>\n" +  
                     "   </elements>\n" +  
                     "</test4>"; 
      //FIXME read ignores the class statement 
      System.out.println(serializedForm);
      System.out.println();
      o = s.read(Test4.class, serializedForm);
      sw.getBuffer().setLength(0);
      s.write(o, sw);
      System.out.println(sw.toString());
      System.out.println();
      sw.getBuffer().setLength(0);
       
      serializedForm =  "<test4>\n" + 
            "   <single-element/>\n" +  
            "   <elements>\n" +  
            "      <element class=\"org.simpleframework.xml.core.Test4Test$MyElementC\"/>\n" +  
            "      <elementB/>\n" +  
            "   </elements>\n" +  
            "</test4>"; 
      //FIXME read uses the class statement and deserializes as expected 
      System.out.println(serializedForm);
      System.out.println();
      o = s.read(Test4.class, serializedForm);
      sw.getBuffer().setLength(0);
      s.write(o, sw);
      System.out.println(sw.toString());
      System.out.println();
      sw.getBuffer().setLength(0);
   } 
} 
Experience pair programming with AI  Get Codota for Java