75014

IExternalizable (as3)

Question:

has anyone used the IExternalizable interface? ...

I have a fundamental question. When I haved serialized my object ... and have saved as a file .... and now I wants to make some changes in the class ... e.g. add an attribute ... I can not re-convert the file into an object of this class ... because in the readExternal method is now one more attribute, which the stored object (file) does not own ....

is there a way to make this more flexible?

-- // german

Hallo hat schon mal jemand das im Titel erwähnte Interface benutzt? ...

Habe mal ne grundlegende Frage dazu. Wenn ich mein Objekt serialisiert habe... und als Datei abgespeichert habe.... und nun in der Klasse einige Änderungen vornehmen möchte... z.B. ein Attribut hinzufügen... dann kann ich die Datei nicht wieder in ein Objekt dieser Klasse umwandeln... weil in der readExternal methode nun ein Attribute mehr abgefragt wird, welches das gespeicherte Objekt (Datei) nicht besitzt....

gibts da Möglichkeiten das flexibler zu machen?

Answer1:

You have to register alias of that class by using flash.net.registerClassAlias function. This thing adds some metadata (if I'm correct) to writen Amf object that alows Flash to determine original Class.

Then load you file from file system or network as ByteArray and read object by using ByteArray#readObject() method.

More info on this: <a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/package.html" rel="nofollow">http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/package.html</a>

Answer2:

If you're adding, removing or changing an attribute to your class, this is an incompatible API change. Consider if your class is a part of an software library already used by other developers (e.g. your co-workers, or if your library is open-source, anybody who downloaded it and started making their project using it), if you make an incompatible change like that, immediately other people's code might stop working or work incorrectly. This kind of API change should only be allowed to happen between major new releases (e.g. your library version was v1.X.Y and you release a v2.0.0).

When you serialized the instance of your SomeClass to a file, the class name is also stored in that file, so when reading that instance back from the file, Flash will know an instance of which class to create. However consider that if you change your API, it's no longer the same SomeClass, the name is the same but the class definition is different. There's no easy way around it.

If you really need to keep access to the data in that file. A simple solution would be to store version info in your data file as the first item. Every time you change the file format, you should increment the version. When writing the file, write the current version first. When reading the file, read the current version and store it as a public static property in a class that can be accessed from anywhere in your project (e.g. create a separate class DataLoaderInfo and a public static var formatVersion:String or uint). Then keep reading your file as you did before. In your class SomeClass, which you want to modify, would look like this:

function readExternal(input:IDataInput):void { // Read format version 0 properties. oldIntProperty1 = input.readInt(); oldIntProperty2 = input.readInt(); ... // Read format version 1 properties. if (DataLoaderInfo.formatVersion >= 1) { newIntProperty1 = input.readInt(); ... } // Read format version 2 properties. if (DataLoaderInfo.formatVersion >= 2) { ... } ... }

You can imagine, if you have many file format versions, the code might become quite complicated. It all depends what kind of application you're developing and how many versions might be deployed in the field at the same time. For example, if your app is a mobile app, storing it's settings or state in a binary file, each user might update at different intervals, therefore a user might upgrade to your latest version from different older versions, so your app should be able to gracefully load configs of all these versions. If all your data is stored on your own server, and the only access to this data is through the latest version of your app deployed on your server as well, you only need to support formats of the last deployed version and the current (new) version.

You might also consider making a separate utility app to convert your data file to the updated format.<br /> 1) Create a subclass ExtSomeClass of your original class SomeClass. Add your new API (e.g. properties) to the subclass. Override the IExternalizable methods of the class - call super's methods first, then read/write the added new properties.<br /> 2) Make a utility conversion app that reads your original file, containing instances of SomeClass. For every SomeClass that you read, you should create a new instance of ExtSomeClass, copying all SomeClass's properties and initializing ExtSomeClass properties with some reasonable defaults. So every instance of SomeClass gets replaced by ExtSomeClass. Write the converted data back to your file. If you don't want to make a subclass, and want to indeed modify the original class SomeClass, you can also do that, but the conversion code will be more tricky, since you'll need access to both old and new versions of your class, obviously you can't have two classes with same name compiled in your project, you would have to compile your new (updated) class into a separate SWF, the conversion app should be compiled using the old version of SomeClass, then you can read your old-version file, every instance of SomeClass will be read correctly using the old version of your class. Load the SWF containing your new (updated) class using Loader, you can use loader.loaderInfo.applicationDomain.getDefinitionByName to get a reference to your new Class, you can create instances of the new class this way, and then copy all the properties from the instances you read from the file.

Recommend

  • How does Flex 3 datagrid paging work?
  • NSString MD5 with hash
  • Advice needed to properly indexing a table with many fields to be searched on
  • dynamically add buttons to dialog
  • Reading binary file from sdcard using Stream classes in android
  • Get SSID's in range iOS 7
  • Copying timestamp columns within a Postgres table
  • Specify source IP address for TCP socket when using Linux network device aliases
  • How to alias an executable using Powershell permanently?
  • Extend Name Box
  • record audio in HTML / js without Flash?
  • Disabling sound of embedded flash object with html
  • jersey/tomcat Description The origin server did not find a current representation for the target res
  • Magento get URL before current
  • How do I include a SWC in an AS2 Flash project?
  • Installing iPhone App to iPhone
  • Very simple C++ DLL that can be called from .net
  • Get object from AWS S3 as a stream
  • Cross-Platform Protobuf Serialization
  • Validaiting emails with Net.Mail MailAddress
  • Fill an image in a square container while keeping aspect ratio
  • Where to put my custom functions in Wordpress?
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • How to limit post in wp_query
  • Websockets service method fails during R startup
  • Do I've to free mysql result after storing it?
  • WOWZA + RTMP + HTML5 Playback?
  • bootstrap to use multiple ng-app
  • Comma separated Values
  • How to delete a row from a dynamic generate table using jquery?
  • How to set the response of a form post action to a iframe source?
  • python draw pie shapes with colour filled
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Running Map reduces the dimensions of the matrices
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?