Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package br.com.winmidia
- {
- import flash.utils.*;
- public class ClassTransform
- {
- /**
- * Transform a JSON data to this ValueObject representation
- * Important: The VO Class must be setted in the application
- *
- * @param json:String The JSON to be converted
- * @param type:String The name of the field that have the ClassName of the VO
- * @param data:String The name of the field that have the Data of the VO
- * @author Wemerson C. Guimarães <[email protected]>
- * @version 0.0.1
- *
- * IF not 'type' or 'data' present, all JSON will be used as the object data...
- * Only when 'type' is not informed, the valueObject must be present
- *
- * Data examples:
- *
- * -----------------------------------------------
- * // DEFAULT USAGE
- * -----------------------------------------------
- * ClassTransform.json2vo(json)
- * {
- * "type": "UserVO",
- * "data": {
- * "id": 999,
- * "name": "Wemerson Guimaraes",
- * "e-mail": "[email protected]
- * }
- * }
- *
- * -----------------------------------------------
- * // USE WHEN THE JSON IS JUST THE VO
- * -----------------------------------------------
- * ClassTransform.json2vo(json, UserVO)
- * {
- * "id": 999,
- * "name": "Wemerson Guimaraes",
- * "e-mail": "[email protected]
- * }
- *
- * -----------------------------------------------
- * // USED WITH TYPE/DATA CUSTOMIZED
- * -----------------------------------------------
- * ClassTransform.json2vo(json, null, 'tipo', 'dados' ) // without VO and type and data customized
- * {
- * "tipo": "UserVO",
- * "dados": {
- * "id": 999,
- * "name": "Wemerson Guimaraes",
- * "e-mail": "[email protected]
- * }
- * }
- *
- */
- public static function json2vo(json:String, valueObject:Class = null, type:String = 'type', data:String = 'data' ):*{
- // Object complete, with all fields, including 'type' and 'data'
- // (if they exists will be parsed later in this function)
- try{
- var oFullConverted:Object = JSON.parse( json );
- }catch(e:Error){
- throw new Error("Falha tentar fazer o parse do JSON");
- }
- // get the 'type' of object, must have more manipulation after dataRoot
- var iType:String = oFullConverted[type];
- // get tye 'data' from the object
- var dataRoot:Object;
- try{
- if(oFullConverted[data] == null || iType == null)
- dataRoot = oFullConverted;// type and data invalid, get all data
- else
- dataRoot = oFullConverted[data]; // get data
- }catch(e:Error){
- throw new Error("JSON inválido!!!");
- }
- if(iType == null && valueObject == null)
- throw new Error("Tipagem do VO não definida no JSON, necessário informar ValueObject corretamente");
- else if(valueObject != null)
- iType = getQualifiedClassName(valueObject);
- // Instantiate the VO class
- try{
- var clazz:Class = Class( getDefinitionByName(iType) );
- var vo:Object = new clazz();
- }catch(e:Error){
- throw new Error("Tentativa de mapear uma classe inválida!");
- }
- var classInfo:XML = describeType(vo);
- // List accessors as properties. To convert to VO's only the part of properties is needed
- // For now only VOs with simple fields is supported
- // Feel free to implement complex fields like arrays and collections of data as a VO field
- for each (var accessor:XML in classInfo..accessor) {
- if (accessor.@access != 'writeonly')
- vo[accessor.@name] = dataRoot[accessor.@name];
- }
- return vo;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment