hoodwink.d enhanced
 

juretta.com

Ruby WDDX Plugin | December 22, 2006-->

December 22, 2006
WDDX is an XML-based technology that enables the exchange of complex data between Web programming languages, creating what some refer to as 'Web syndicate networks'. WDDX consists of a language-independent representation of data based on an XML 1.0 DTD, and a set of modules for a wide variety of languages that use WDDX.

WDDX is a very simple way of serializing data to XML. It describes an easy way to exchange data structures between different kind of programming languages. Although you might be better of using one of the more extensive standards (e.g. SOAP) WDDX can be very effective compared to more bloated (again SOAP) ways of serializing data.

WDDX implementations are available for many programming languages. Ruby now has a WDDX library that you can very easily use.

To install WDDX just type gem install wddx.

macbook:~ stefan$ sudo gem install wddx
Successfully installed wddx-0.2.1
Installing ri documentation for wddx-0.2.1...
Installing RDoc documentation for wddx-0.2.1...

The Ruby gem contains a simple example that shows how to deserialize a WDDX packet. Go to the wddx gem directory to open the example file (wddx_deserialize.rb).

macbook:~ stefan$ gem environment gempath
/usr/local/lib/ruby/gems/1.8

macbook:~ stefan$ cd `gem environment gempath`/gems/wddx-0.2.1/examples
macbook:~ stefan$ ruby wddx_deserialize.rb 
WDDX::WddxPacket
Das ist ein Kommentar
{"aBoolean"=>true,
 "anObject"=>{"n"=>-12.456, "s"=>"a string"},
 "aNumber"=>-12.456,
 "aRecordset"=>
  #,
 "anArray"=>[10, "second element"],
 "aBinary"=>
  #,
 "aString"=>"a string",
 "aNull"=>nil,
 "aDateTime"=>Fri Jun 12 04:32:12 CEST 1998}
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
-12.456
-12.456

To serialize data you can call to_wddx using one of the core classes: String, Array, Hash, Time, Numeric (e.g. Integer, Bignum and so on), TrueClass, FalseClass or NilClass

123.667.to_wddx # => "<wddxPacket version='1.0'><header/><data>\
                # <number>123.667</number></data></wddxPacket>"

More complex data structures can be build by mixing the WDDX module into your own classes.

class MyObject
  include WDDX
  attr_accessor :name, :value, :price
  
  def initialize(name, price)
    @name, @price = name, price  
  end

  # Modelled after YAML you can define the instance variables and methods
  # that should be serialized by adding a method to_wddx_properties
  # to your class that returns an array of fields.
  def to_wddx_properties
    ["@name", :custom_price]
  end               

  def custom_price
    @price * 1.05
  end
end

obj = MyObject.new("Macbook Pro", 120)
print obj.to_wddx

The WDDX gem enables easy serialization of Rails ActiveRecord objects. Just add a require 'wddx' to your config/environment.rb file and you can call to_wddx on AR objects (and even on collections of AR objects).

# Method wddx in app/controllers/users_controller.rb
  def wddx
    headers['Content-Type'] ||= 'text/xml'
    render :text => User.find(:first).to_wddx
  end

A call to /users/wddx/1 shows the User object with id 1 as a WDDX XML packet.

@19:28 | Comments: 0 | Tags: WDDX (2), XML (7)

Diggman

About

juretta.com is the personal workspace of Stefan Saasen. You can send him an email or read more about this site in the „About“ section.

« Previous entry

Rspec Rails plugin
posted about 1 year ago

» Next entry

Ruby SequencedHash
posted about 1 year ago

Recent comment

On: “Attachr.com: OpenID support added

You need to kill this spam stuff!

posted about 1 year ago by entropie

Look!

Latest links  RSS  

More...