I was waiting to post this until the results were announced which they were today. O’Reilly just of the and I’m pleased to say I won the grand prize!
My recipe was . Thanks to anyone who voted for it.
I have a follow-up recipe I haven’t gotten to write up yet, but I’ll try to get it up in the next week. It will provide a solution to add the marker which is required for this serialization to work to the front of your structures if you already have files being serialized in an existing AIR application and you want to start taking advantage of this migration technique.
We are currently working on a project that involves Flex and active resource + will_paginate and we needed to be able to paginate the xml transactions easily. Unfortunately, will_paginate and to_xml don’t play nicely when it comes to adding the current_page, total_pages, and page attributes to the xml. After many failed attempts I went looking around and found in a few of that some people had solved this problem but, I didn’t want to install another version of the gem to risk breaking other apps on the server so I did it the rails way!
I started by creating a module that opens up the will_paginate class and includes ActiveResource and alias method chain the to_xml method to include these values. Example code below.
#enviroment.rb
...
require'to_xml_extensions'
#lib/to_xml_extensions.rbmodule WillPaginateHelpers
includeActiveSupport::CoreExtensions::Array::Conversions
def to_xml_with_collection_type(options = {})
serializeable_collection.to_xml_without_collection_type(options)do|xml|
xml.tag!(:current_page, {:type =>ActiveSupport::CoreExtensions::Hash::Conversions::XML_TYPE_NAMES[current_page.class.name]}, current_page)
xml.tag!(:per_page, {:type =>ActiveSupport::CoreExtensions::Hash::Conversions::XML_TYPE_NAMES[per_page.class.name]}, per_page)
xml.tag!(:total_entries, {:type =>ActiveSupport::CoreExtensions::Hash::Conversions::XML_TYPE_NAMES[total_entries.class.name]}, total_entries)end.sub(%{type="array"}, %{type="collection"})end
alias_method_chain :to_xml, :collection_typedef serializeable_collection #:nodoc:# Ugly hack because to_xml will not yield the XML Builder object when empty?
empty? ? returning(self.clone){|c| c.instance_eval{|i|def empty?; false; end}} : selfendendWillPaginate::Collection.send(:include, WillPaginateHelpers)
This now gives me the proper xml when I call to_xml
It solves the problem of having your AIR application store an IExternalizable class on disk but then later adding more fields to that class. How would you read the old version of the class without encountering a runtime error if you tried to read the new field that wasn’t present in the older serialization?
Go the cookbook entry to find out how to do this and please rate the article!
I’ve encountered some odd behavior that I would have expected to work when calling readObject() to de-serialize an array of anonymous objects. If anyone knows what’s going on here, please enlighten me. I’ve also on Adobe’s bug tracking system if anyone wants to follow the progress at Adobe’s end.
I’m trying to read all of the bytes in the readExternal() function of a class implementing IExternalizable, so that I may use the position property to move back in stream in case I need to do so. The problem only seems to occur if I am trying to de-serialize an Array of anonymous objects. If I put plain old Strings in the Array it will work fine. I find this odd since I would expect
publicfunction readExternal(input:IDataInput):void{var arr:Array = input.readObject() as Array;
}
to have the exact same behavior as:
publicfunction readExternal(input:IDataInput):void{var ba:ByteArray = new ByteArray();
var inputBytes:uint = input.bytesAvailable;
input.readBytes(ba);
var baBytes:uint = ba.bytesAvailable;
var arr:Array = ba.readObject() as Array;
trace("inputBytes == baBytes ?= " + (inputBytes == baBytes)); // traces "inputBytes == baBytes ?= true}
One of our developers recently encountered some strange behavior when he was dragging a piece of data (which was a custom class) from a TileList and dropping it into a List, but losing some pieces of the dragged data when it was dropped into the List. I discovered the root of the issue by delving into the source for the dragDropHandler function within ListBase…