/ Published in: Java
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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
package amfdemo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; import flex.messaging.io.SerializationContext; import flex.messaging.io.amf.Amf3Input; import flex.messaging.io.amf.Amf3Output; import javax.xml.parsers.DocumentBuilderFactory; public class AmfTest { try { SerializationContext context = getSerializationContext(); TestBean testBean = new TestBean(); testBean.setDocument(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()); testBean.setString("test"); Amf3Output amf3Output = new Amf3Output(context); amf3Output.setOutputStream(bout); amf3Output.writeObject(testBean); amf3Output.flush(); amf3Output.close(); Amf3Input amf3Input = new Amf3Input(context); amf3Input.setInputStream(bIn); TestBean o = (TestBean) amf3Input.readObject(); e.printStackTrace(); } } public static SerializationContext getSerializationContext() { //Threadlocal SerializationContent SerializationContext serializationContext = SerializationContext.getSerializationContext(); serializationContext.enableSmallMessages = true; serializationContext.instantiateTypes = true; //use _remoteClass field serializationContext.supportRemoteClass = true; //false Legacy Flex 1.5 behavior was to return a java.util.Collection for Array //ture New Flex 2+ behavior is to return Object[] for AS3 Array serializationContext.legacyCollection = false; serializationContext.legacyMap = false; //false Legacy flash.xml.XMLDocument Type //true New E4X XML Type serializationContext.legacyXMLDocument = false; //determines whether the constructed Document is name-space aware serializationContext.legacyXMLNamespaces = false; serializationContext.legacyThrowable = false; serializationContext.legacyBigNumbers = false; serializationContext.restoreReferences = false; serializationContext.logPropertyErrors = false; serializationContext.ignorePropertyErrors = true; return serializationContext; /* serializationContext.enableSmallMessages = serialization.getPropertyAsBoolean(ENABLE_SMALL_MESSAGES, true); serializationContext.instantiateTypes = serialization.getPropertyAsBoolean(INSTANTIATE_TYPES, true); serializationContext.supportRemoteClass = serialization.getPropertyAsBoolean(SUPPORT_REMOTE_CLASS, false); serializationContext.legacyCollection = serialization.getPropertyAsBoolean(LEGACY_COLLECTION, false); serializationContext.legacyMap = serialization.getPropertyAsBoolean(LEGACY_MAP, false); serializationContext.legacyXMLDocument = serialization.getPropertyAsBoolean(LEGACY_XML, false); serializationContext.legacyXMLNamespaces = serialization.getPropertyAsBoolean(LEGACY_XML_NAMESPACES, false); serializationContext.legacyThrowable = serialization.getPropertyAsBoolean(LEGACY_THROWABLE, false); serializationContext.legacyBigNumbers = serialization.getPropertyAsBoolean(LEGACY_BIG_NUMBERS, false); boolean showStacktraces = serialization.getPropertyAsBoolean(SHOW_STACKTRACES, false); if (showStacktraces && Log.isWarn()) log.warn("The " + SHOW_STACKTRACES + " configuration option is deprecated and non-functional. Please remove this from your configuration file."); serializationContext.restoreReferences = serialization.getPropertyAsBoolean(RESTORE_REFERENCES, false); serializationContext.logPropertyErrors = serialization.getPropertyAsBoolean(LOG_PROPERTY_ERRORS, false); serializationContext.ignorePropertyErrors = serialization.getPropertyAsBoolean(IGNORE_PROPERTY_ERRORS, true); */ } }