Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 41 lines (28 sloc) 1.698 kb
347eb9a @darronschall Edited README via GitHub
authored
1 ObjectTranslator
2 =============
3
4 ObjectTranslator is a utility class I wrote when ActionScript 3 was brand new. It converts generic ActionScript 3 objects into class instances.
5
6 This code originally comes from a weblog entry in October of 2006, now archived at http://archive.darronschall.com/weblog/2006/10/convert-generic-objects-into-class-instances.html
7
8 Usage
9 -----
10
11 Usage is as follows:
12
049b504 @darronschall Edited README.md via GitHub
authored
13 import com.darronschall.examples.vo.Book;
14 import com.darronschall.serialization.ObjectTranslator;
347eb9a @darronschall Edited README via GitHub
authored
15
049b504 @darronschall Edited README.md via GitHub
authored
16 // Define an object with properties that mimic the variable names
17 // inside of the Book class
18 var bookObj:Object = { title: "My Book title", pageCount: 10, inLibrary: true };
347eb9a @darronschall Edited README via GitHub
authored
19
049b504 @darronschall Edited README.md via GitHub
authored
20 // Convert the generic object into an instance of the Book class
21 var book:Book = ObjectTranslator.objectToInstance( bookObj, Book ) as Book;
347eb9a @darronschall Edited README via GitHub
authored
22
23 Limitations
24 -----
25
049b504 @darronschall Edited README.md via GitHub
authored
26 The code is not yet recursive (feel free to fork, implement, and submit a pull request!). If you have nested value objects that you're converting to class instances, you'll have to manually go through the object graph:
347eb9a @darronschall Edited README via GitHub
authored
27
049b504 @darronschall Edited README.md via GitHub
authored
28 import com.darronschall.examples.vo.Book;
29 import com.darronschall.examples.vo.Student;
30 import com.darronschall.serialization.ObjectTranslator;
347eb9a @darronschall Edited README via GitHub
authored
31
049b504 @darronschall Edited README.md via GitHub
authored
32 var studentObj:Object = { firstName: "test first",
347eb9a @darronschall Edited README via GitHub
authored
33 lastName: "test last",
34 favoriteBook: { title: "Favorite Book!" }
35 };
36
049b504 @darronschall Edited README.md via GitHub
authored
37 // First we need to convert the nested objects to classes
38 studentObj.favoriteBook = ObjectTranslator.objectToInstance( studentObj.favoriteBook, Book );
347eb9a @darronschall Edited README via GitHub
authored
39
049b504 @darronschall Edited README.md via GitHub
authored
40 // Convert the student object to a Student class
41 var student:Student = ObjectTranslator.objectToInstance( studentObj, Student ) as Student;
Something went wrong with that request. Please try again.