wemersonrv

ClassTransform

Apr 15th, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package br.com.winmidia
  2. {
  3.     import flash.utils.*;
  4.  
  5.     public class ClassTransform
  6.     {
  7.         /**
  8.          * Transform a JSON data to this ValueObject representation
  9.          * Important: The VO Class must be setted in the application
  10.          *
  11.          * @param json:String   The JSON to be converted
  12.          * @param type:String   The name of the field that have the ClassName of the VO
  13.          * @param data:String   The name of the field that have the Data of the VO
  14.          * @author Wemerson C. Guimarães <[email protected]>
  15.          * @version 0.0.1
  16.          *
  17.          * IF not 'type' or 'data' present, all JSON will be used as the object data...
  18.          * Only when 'type' is not informed, the valueObject must be present
  19.          *
  20.          * Data examples:
  21.          *
  22.          * -----------------------------------------------
  23.          *    // DEFAULT USAGE
  24.          * -----------------------------------------------  
  25.          * ClassTransform.json2vo(json)
  26.          * {
  27.          *      "type": "UserVO",
  28.          *      "data": {
  29.          *          "id": 999,
  30.          *          "name": "Wemerson Guimaraes",
  31.          *          "e-mail": "[email protected]
  32.          *      }
  33.          * }
  34.          *
  35.          * -----------------------------------------------
  36.          *    // USE WHEN THE JSON IS JUST THE VO
  37.          * -----------------------------------------------
  38.          * ClassTransform.json2vo(json, UserVO)
  39.          * {
  40.          *      "id": 999,
  41.          *      "name": "Wemerson Guimaraes",
  42.          *      "e-mail": "[email protected]
  43.          * }
  44.          *
  45.          * -----------------------------------------------
  46.          *    // USED WITH TYPE/DATA CUSTOMIZED
  47.          * -----------------------------------------------
  48.          * ClassTransform.json2vo(json, null, 'tipo', 'dados' ) // without VO and type and data customized
  49.          * {
  50.          *      "tipo": "UserVO",
  51.          *      "dados": {
  52.          *          "id": 999,
  53.          *          "name": "Wemerson Guimaraes",
  54.          *          "e-mail": "[email protected]
  55.          *      }
  56.          * }
  57.          *
  58.          */
  59.         public static function json2vo(json:String, valueObject:Class = null, type:String = 'type', data:String = 'data' ):*{
  60.            
  61.             // Object complete, with all fields, including 'type' and 'data'
  62.             // (if they exists will be parsed later in this function)          
  63.             try{
  64.                 var oFullConverted:Object = JSON.parse( json );
  65.             }catch(e:Error){
  66.                 throw new Error("Falha tentar fazer o parse do JSON");
  67.             }
  68.            
  69.             // get the 'type' of object, must have more manipulation after dataRoot
  70.             var iType:String = oFullConverted[type];
  71.            
  72.             // get tye 'data' from the object
  73.             var dataRoot:Object;
  74.             try{
  75.                 if(oFullConverted[data] == null || iType == null)
  76.                     dataRoot = oFullConverted;// type and data invalid, get all data
  77.                 else
  78.                     dataRoot = oFullConverted[data]; // get data
  79.             }catch(e:Error){
  80.                 throw new Error("JSON inválido!!!");
  81.             }
  82.            
  83.            
  84.             if(iType == null && valueObject == null)
  85.                 throw new Error("Tipagem do VO não definida no JSON, necessário informar ValueObject corretamente");
  86.             else if(valueObject != null)
  87.                 iType = getQualifiedClassName(valueObject);
  88.  
  89.             // Instantiate the VO class
  90.             try{
  91.                 var clazz:Class = Class( getDefinitionByName(iType) );
  92.                 var vo:Object = new clazz();
  93.             }catch(e:Error){
  94.                 throw new Error("Tentativa de mapear uma classe inválida!");
  95.             }
  96.            
  97.             var classInfo:XML = describeType(vo);
  98.            
  99.             // List accessors as properties. To convert to VO's only the part of properties is needed
  100.             // For now only VOs with simple fields is supported
  101.             // Feel free to implement complex fields like arrays and collections of data as a VO field
  102.             for each (var accessor:XML in classInfo..accessor) {
  103.                 if (accessor.@access != 'writeonly')
  104.                     vo[accessor.@name] = dataRoot[accessor.@name];
  105.             }
  106.            
  107.             return vo;
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment