Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kokorin/AStream

XML to Object (and vice versa) mapping library written in AS3. Compatible with XStream.
https://github.com/kokorin/AStream

Last synced: 3 months ago
JSON representation

XML to Object (and vice versa) mapping library written in AS3. Compatible with XStream.

Awesome Lists containing this project

README

        

AStream
=======

AStream can handle enums. Enum's name passed to super() is used to represent enum's value in XML.
```as3
package com.example.domain {
import as3.lang.Enum;

public class UserRole extends Enum {
public static const ADMINISTRATOR:UserRole = new UserRole("ROLE_ADMINISTRATOR");
public static const OPERATOR:UserRole = new UserRole("ROLE_OPERATOR");

public function UserRole(name:String) {
super(name);
}
}
}
```

**There is no guarantee in flash that you will get class' properies in order they were declared!**

You can add AStreamOrder metadata to enforce tag order in xml.
```as3
package com.example.domain {
[AStreamAlias("User")]
public class User {
private var _name:String;

[AStreamOrder(10)]
public var id:Number;
[AStreamOrder(30)]
public var role:UserRole;
[AStreamOrder(40)]
[AStreamConverter("ru.kokorin.astream.converter.DateConverter", params="yyyy-MM-dd")]
public var birth:Date;

public function User() {
}

[AStreamOrder(20)]
public function get name():String {
return _name;
}

public function set name(value:String):void {
_name = value;
}

public function toString():String {
return "User{name=" + String(name) + ",id=" + String(id) + ",role=" + String(role) + ",birth=" + String(birth) + "}";
}
}
}
```

**Use AStream's metadata autodetection with caution!**

If metadata autodetection is on, every time AStream converts an object it will first process the object's type and all the types related. Therefore it is no problem to serialize an object graph into XML, since AStream will know of all types in advance.

This is no longer true at deserialization time. AStream has to know the alias to turn it into the proper type, but it can find the annotation for the alias only if it has processed the type in advance. Therefore deserialization will fail if the type has not already been processed either by having called AStream's processMetadata method or by already having serialized this type. However, AStreamAlias is the only metadata that may fail in this case.
```as3
const aStream:AStream = new AStream();
aStream.processMetadata(User);
//or aStream.autodetectMetadata(true);

const user:User = new User();
user.id = 1;
user.name = "Ivanov Ivan";
user.role = UserRole.ADMINISTRATOR;
user.birth = new Date(1960, 4, 19, 0, 0, 0, 0);

const xml:XML = aStream.toXML(user);
/* xml.toXMLString()

1
Ivanov Ivan
ROLE_ADMINISTRATOR
1960-05-19
*/

const restoredUser:User = aStream.fromXML(xml) as User;
/* restoredUser.toString()
User{name=Ivanov Ivan,id=1,role=ROLE_ADMINISTRATOR,birth=Thu May 19 00:00:00 GMT+0300 1960} */
```

**Сorresponding types (by default)**

Java | XML | AS3
---------------------|---------------------|------------------------------------
byte[] | byte-array | ByteArray
Type[] | Type-array | Vector.<Type>
java.util.ArrayList | list | org.spicefactory.lib.collection.List
java.util.HashMap | map | org.spicefactory.lib.collection.Map
Float, float | float | Number
Integer, int | int | int
java.util.Date | date | Date
java.lang.String | string | String