AnsweredAssumed Answered

A Patch for DefaultTypeConverter

Question asked by manuelgentile on Mar 23, 2006
I have add a portion of code to allow conversion between String and any Serializable Object, i think is useful for d:any property in custom model.

INSTANCE.addConverter(Serializable.class, String.class, new TypeConverter.Converter<Serializable, String>()
              {
               public String convert(Serializable source) {
                  
                  try {
                     ByteArrayOutputStream b=new ByteArrayOutputStream();
                     ObjectOutputStream bs=new ObjectOutputStream(b);
                     bs.writeObject(source);
                     return b.toString();
                  } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                  }
                  return null;
               }}
        );
       
        INSTANCE.addConverter(String.class,Serializable.class, new TypeConverter.Converter<String,Serializable>()
              {
               public Serializable convert(String source) {
                  
                  try {
                     ByteArrayInputStream b=new ByteArrayInputStream(source.getBytes());
                     ObjectInputStream bs=new ObjectInputStream(b);
                     return (Serializable) bs.readObject();
                     
                  } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                  } catch (ClassNotFoundException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                  }
                  return null;
               }}
        );

[size=75]Ing. Manuel Gentile
Italian National Research Council
Institute for Educational Technologies
Via Ugo La Malfa 153 - 90146 Palermo - ITALY
Phone: +39 091 6809214
Fax: +39 091 6809239
e-mail: manuel.gentile@itd.cnr.it[/size]

Outcomes