]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/jsolait/lib/langlite.js
Release 6.2.1
[Github/sugarcrm.git] / jssource / src_files / include / jsolait / lib / langlite.js
1 /*
2
3 Modification information for LGPL compliance
4
5 r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
6
7 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
8
9 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
10
11 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3  tags and updated the build system 
12
13 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
14
15 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
16
17 r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
18
19 r10573 - 2005-12-13 11:12:35 -0800 (Tue, 13 Dec 2005) - roger - Bug 3663.
20
21 r10511 - 2005-12-12 15:06:37 -0800 (Mon, 12 Dec 2005) - wayne - removed debug code
22
23 r10487 - 2005-12-12 03:57:07 -0800 (Mon, 12 Dec 2005) - robert - fixed: 3478
24 also changed jsonrpc on the client side so that javascript classes
25 will no longer get the toJSON method added to it.
26
27
28 */
29
30 /*
31   Copyright (c) 2004 Jan-Klaas Kollhof
32   
33   This file is part of the JavaScript o lait library(jsolait).
34   
35   jsolait is free software; you can redistribute it and/or modify
36   it under the terms of the GNU Lesser General Public License as published by
37   the Free Software Foundation; either version 2.1 of the License, or
38   (at your option) any later version.
39  
40   This software is distributed in the hope that it will be useful,
41   but WITHOUT ANY WARRANTY; without even the implied warranty of
42   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
43   GNU Lesser General Public License for more details.
44  
45   You should have received a copy of the GNU Lesser General Public License
46   along with this software; if not, write to the Free Software
47   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
48 */
49
50 /**
51     FOR SUGARCRM
52     This is a stripped down version of lang.php, all it does is:
53     convert JavaScript objects to  JSON (see json.org).
54     but *does not* add toJSON() function to every class that is derived from Object
55 */
56 Module("langlite", "0.3.7", function(mod){
57    
58     mod.JSONParser=Class("JSONParser", function(publ, supr){
59         publ.init=function(){
60             this.libs = {};
61         }
62         
63         publ.addLib = function(obj, name, exports){
64             if(exports == null){
65                 this.libs[name] = obj;
66             }else{
67                 for(var i=0;i<exports.length;i++){
68                     this.libs[name + "." + exports[i]] = obj[exports[i]];
69                 }
70             }
71         }
72                 
73         publ.objToJson=function(obj){
74             if(obj == null){
75                 return "null";
76             }else{
77                 return mod.objToJson(obj);
78             }
79         }
80     })
81         
82     mod.parser = new mod.JSONParser();
83     
84     /**
85         Turns JSON code into JavaScript objects.
86         @param src  The source as a String.
87     */
88     mod.jsonToObj=function(src){
89         return mod.parser.jsonToObj(src);
90     }
91     
92     var json_types = new Object();
93                                                                                              
94     json_types['object'] = function(obj){
95         var v=[];
96         for(attr in obj){
97             if(typeof obj[attr] != "function"){
98                 v.push('"' + attr + '": ' + mod.objToJson(obj[attr]));
99             }
100         }
101         return "{" + v.join(", ") + "}";
102     }
103                                                                                              
104     json_types['string'] = function(obj){
105         var s = '"' + obj.replace(/(["\\])/g, '\\$1') + '"';
106         s = s.replace(/(\n)/g,"\\n");
107         return s;
108     }
109                                                                                              
110     json_types['number']  = function(obj){
111         return obj.toString();
112     }
113
114     json_types['boolean'] = function(obj){
115         return obj.toString();
116     }
117
118     json_types['date'] = function(obj){
119         var padd=function(s, p){
120             s=p+s
121             return s.substring(s.length - p.length)
122         }
123         var y = padd(obj.getUTCFullYear(), "0000");
124         var m = padd(obj.getUTCMonth() + 1, "00");
125         var d = padd(obj.getUTCDate(), "00");
126         var h = padd(obj.getUTCHours(), "00");
127         var min = padd(obj.getUTCMinutes(), "00");
128         var s = padd(obj.getUTCSeconds(), "00");
129                                                                                              
130         var isodate = y +  m  + d + "T" + h +  ":" + min + ":" + s
131         
132         return '{"jsonclass":["sys.ISODate", ["' + isodate + '"]]}';
133     }
134
135     json_types['array'] = function(obj){
136         var v = [];
137         for(var i=0;i<obj.length;i++){
138             v.push(mod.objToJson(obj[i])) ;
139         }
140         return "[" + v.join(", ") + "]";
141     }
142
143
144     mod.objToJson=function(obj){
145       if ( typeof(obj) == 'undefined')
146       {
147         return '';
148       }
149       if ( typeof(json_types[typeof(obj)]) == 'undefined')
150       {
151         alert('class not defined for toJSON():'+typeof(obj));
152       }
153       return json_types[typeof(obj)](obj);
154     }
155
156         
157     mod.test=function(){
158         try{
159             print(mod.objToJson(['sds', -12377,-1212.1212, 12, '-2312']));
160         }catch(e){
161             print(e.toTraceString());
162         }
163         
164     }
165     
166 })
167