Serialization of Java object to AMF


/ Published in: Java
Save to your folder(s)

A working example that uses BlaszDS in order to serialize Java object into AMF (this code should be cleaned up a bit), in order for it to compile and work it is required to add flex-messaging-core.jar and flex-messaging-common.jar to the classpath.


Copy this code and paste it in your HTML
  1. package amfdemo;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.InputStream;
  6. import java.math.BigDecimal;
  7. import java.util.ArrayList;
  8. import java.util.Calendar;
  9. import java.util.HashMap;
  10.  
  11. import flex.messaging.io.SerializationContext;
  12. import flex.messaging.io.amf.Amf3Input;
  13. import flex.messaging.io.amf.Amf3Output;
  14. import javax.xml.parsers.DocumentBuilderFactory;
  15.  
  16. public class AmfTest {
  17.  
  18. public static void main(String[] args) {
  19. try {
  20.  
  21. SerializationContext context = getSerializationContext();
  22.  
  23. TestBean testBean = new TestBean();
  24. testBean.setByte(new Byte((byte) 9));
  25. testBean.setBigDecimal(new BigDecimal("9.9"));
  26. testBean.setBoolean(new Boolean("true"));
  27. testBean.setCharacter(new Character('c'));
  28. testBean.setCalendar(Calendar.getInstance());
  29. testBean.setDate(Calendar.getInstance().getTime());
  30. testBean.setDouble(new Double(999.9));
  31. testBean.setFloat(new Float(99.9f));
  32. testBean.setInteger(new Integer("999"));
  33. testBean.setList(new ArrayList());
  34. testBean.setLong(new Long(99999));
  35. testBean.setMap(new HashMap());
  36. testBean.setDocument(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
  37. testBean.setShort(new Short("99"));
  38. testBean.setString("test");
  39.  
  40. Amf3Output amf3Output = new Amf3Output(context);
  41. amf3Output.setOutputStream(bout);
  42. amf3Output.writeObject(testBean);
  43. amf3Output.flush();
  44. amf3Output.close();
  45.  
  46. InputStream bIn = new ByteArrayInputStream(bout.toByteArray());
  47. Amf3Input amf3Input = new Amf3Input(context);
  48. amf3Input.setInputStream(bIn);
  49. TestBean o = (TestBean) amf3Input.readObject();
  50.  
  51. System.out.println(o.getByte().equals(testBean.getByte()));
  52. System.out.println(o.getBigDecimal().equals(testBean.getBigDecimal()));
  53. System.out.println(o.getBoolean().equals(testBean.getBoolean()));
  54. System.out.println(o.getCharacter().equals(testBean.getCharacter()));
  55. System.out.println(o.getCalendar().equals(testBean.getCalendar()));
  56. System.out.println(o.getDate().equals(testBean.getDate()));
  57. System.out.println(o.getDouble().equals(testBean.getDouble()));
  58. System.out.println(o.getFloat().equals(testBean.getFloat()));
  59. System.out.println(o.getInteger().equals(testBean.getInteger()));
  60. System.out.println(o.getLong().equals(testBean.getLong()));
  61. System.out.println(o.getString().equals(testBean.getString()));
  62.  
  63. } catch (Exception e) {
  64. e.printStackTrace();
  65. }
  66. }
  67.  
  68. public static SerializationContext getSerializationContext() {
  69.  
  70. //Threadlocal SerializationContent
  71. SerializationContext serializationContext = SerializationContext.getSerializationContext();
  72. serializationContext.enableSmallMessages = true;
  73. serializationContext.instantiateTypes = true;
  74. //use _remoteClass field
  75. serializationContext.supportRemoteClass = true;
  76. //false Legacy Flex 1.5 behavior was to return a java.util.Collection for Array
  77. //ture New Flex 2+ behavior is to return Object[] for AS3 Array
  78. serializationContext.legacyCollection = false;
  79.  
  80. serializationContext.legacyMap = false;
  81. //false Legacy flash.xml.XMLDocument Type
  82. //true New E4X XML Type
  83. serializationContext.legacyXMLDocument = false;
  84.  
  85. //determines whether the constructed Document is name-space aware
  86. serializationContext.legacyXMLNamespaces = false;
  87. serializationContext.legacyThrowable = false;
  88. serializationContext.legacyBigNumbers = false;
  89.  
  90. serializationContext.restoreReferences = false;
  91. serializationContext.logPropertyErrors = false;
  92. serializationContext.ignorePropertyErrors = true;
  93. return serializationContext;
  94.  
  95. /*
  96.   serializationContext.enableSmallMessages = serialization.getPropertyAsBoolean(ENABLE_SMALL_MESSAGES, true);
  97.   serializationContext.instantiateTypes = serialization.getPropertyAsBoolean(INSTANTIATE_TYPES, true);
  98.   serializationContext.supportRemoteClass = serialization.getPropertyAsBoolean(SUPPORT_REMOTE_CLASS, false);
  99.   serializationContext.legacyCollection = serialization.getPropertyAsBoolean(LEGACY_COLLECTION, false);
  100.   serializationContext.legacyMap = serialization.getPropertyAsBoolean(LEGACY_MAP, false);
  101.   serializationContext.legacyXMLDocument = serialization.getPropertyAsBoolean(LEGACY_XML, false);
  102.   serializationContext.legacyXMLNamespaces = serialization.getPropertyAsBoolean(LEGACY_XML_NAMESPACES, false);
  103.   serializationContext.legacyThrowable = serialization.getPropertyAsBoolean(LEGACY_THROWABLE, false);
  104.   serializationContext.legacyBigNumbers = serialization.getPropertyAsBoolean(LEGACY_BIG_NUMBERS, false);
  105.   boolean showStacktraces = serialization.getPropertyAsBoolean(SHOW_STACKTRACES, false);
  106.   if (showStacktraces && Log.isWarn())
  107.   log.warn("The " + SHOW_STACKTRACES + " configuration option is deprecated and non-functional. Please remove this from your configuration file.");
  108.   serializationContext.restoreReferences = serialization.getPropertyAsBoolean(RESTORE_REFERENCES, false);
  109.   serializationContext.logPropertyErrors = serialization.getPropertyAsBoolean(LOG_PROPERTY_ERRORS, false);
  110.   serializationContext.ignorePropertyErrors = serialization.getPropertyAsBoolean(IGNORE_PROPERTY_ERRORS, true);
  111.   */
  112. }
  113. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.