1 /* 2 * Naos Framework 3 * 4 * LICENSE 5 * http://www.naos-framework.com/website.naos.framework/en/license 6 * 7 * Copyright (c) 2008 Jupiter GmbH 8 */ 9 10 /** 11 * The NaosTools namespace provides useful tool methods 12 * for object handling and similar stuff. 13 * 14 * @namespace 15 */ 16 NaosTools = {} 17 18 /** 19 * Copies all attributes of objects and all entries of 20 * arrays to a vanilla object. This is done recursively.<br/><br/> 21 * 22 * Because Prototype pollutes most of the JavaScript classes 23 * with several methods it is necesarry to get rid of 24 * all these methods if you want to handle plain data in an 25 * vanilla object. Additionally arrays are converted to 26 * a vanilla object too. 27 * 28 * @param object Data to be converted to a vanilla object 29 * 30 * @type Object 31 */ 32 NaosTools.toVanillaObject = function(/**Object|Array*/ object) { 33 34 if(Object.isArray(object)) 35 { 36 var vaob = {}; 37 38 for(var i=0; i<object.length; ++i) 39 { 40 vaob[i] = NaosTools.toVanillaObject(object[i]); 41 } 42 43 return vaob; 44 } 45 else if(object === null) 46 { 47 return object; 48 } 49 else if(typeof object == "object") 50 { 51 var vaob = {}; 52 53 for(var i in object) 54 { 55 vaob[i] = NaosTools.toVanillaObject(object[i]); 56 } 57 58 return vaob; 59 } 60 else 61 { 62 return object; 63 } 64 } 65 66 /** 67 * Creates a copy of an object which does not contain the attribute given 68 * as parameter. 69 * 70 * @param object Object in charge. 71 * @param attribute Name of atttribute to be removed from the object. 72 * 73 * @type Object 74 */ 75 NaosTools.removeObjectAttribute = function(/**Object*/ object, /**String*/ attribute) 76 { 77 var no = {}; 78 79 for(i in object) 80 { 81 if(i != attribute) 82 { 83 no[i] = object[i]; 84 } 85 } 86 87 return no; 88 } 89 90 /** 91 * Modifies the object by adding the value to an free attribute name. This 92 * is a convenient method to simulate Array behaviour on vanilla objects. 93 * It returns a pointer to the modified object for convenience. 94 * 95 * @param object Object in charge. 96 * @param value Value to be added to the object. 97 * 98 * @type Object 99 */ 100 NaosTools.insertFreeObjectAttribute = function(/**Object*/ object, value) 101 { 102 var i = 0; 103 104 while(object[i] != undefined) 105 ++i; 106 107 object[i] = value; 108 109 return object; 110 } 111 112 /** 113 * Converts a JSON string to a vanilla object. Arrays are converted 114 * to vanilla objects automatically. 115 * 116 * @param json JSON string 117 * 118 * @type Object 119 */ 120 NaosTools.fromJSON = function(/**String*/ json) { 121 if(json == '') 122 return {}; 123 124 // decode in a standard way 125 var object = json.evalJSON(true); 126 127 // transform to hash tree 128 return NaosTools.toVanillaObject(object); 129 } 130 131 /** 132 * Converts a data structure to a JSON string. 133 * 134 * @param object Data structure 135 * 136 * @type String 137 */ 138 NaosTools.toJSON = function(object) { 139 return Object.toJSON(object); 140 } 141