5
5
* to define 'glue' and 'syntax sugar' functions.
6
6
*/
7
7
8
- /*
9
- * Perform the JSON serialization in javascript and send the serialized string over
10
- */
11
- Butterfly . prototype . sendJSON = function ( request , response , object , wrap ) {
12
- var json = butterfly . toJSONString ( object ) ;
13
- if ( wrap ) json = "<textarea>" + json + "</textarea>" ;
14
- this . sendString ( request , response , json , "UTF-8" , "text/plain" ) ;
15
- }
16
-
17
- /*
18
- * Perform the JSON serialization in javascript and send the serialized string over
19
- * wrapped in a callback function call
20
- */
21
- Butterfly . prototype . sendJSONP = function ( request , response , object , callback ) {
22
- var json = butterfly . toJSONString ( object ) ;
23
- this . sendString ( request , response , callback + "(" + json + ");" , "UTF-8" , "text/plain" ) ;
24
- }
25
-
26
- /*
27
- * Obtain the request payload as a JSON object using the given optional filtering
28
- * function to perform more specific data-type conversion
29
- */
30
- Butterfly . prototype . getJSON = function ( request ) {
31
- return butterfly . parseJSON ( this . getString ( request ) ) ;
32
- }
33
-
34
8
/*
35
9
* Return the module path wirings as a JSON object.
36
10
*/
@@ -46,131 +20,7 @@ Butterfly.prototype.getWirings = function(request) {
46
20
return result ;
47
21
}
48
22
49
- // ---------------------------------------------------------------------------------------
50
-
51
- Butterfly . prototype . toJSONString = function ( o ) {
52
- if ( o instanceof Object ) {
53
- var str = s . object ( o ) ;
54
- } else if ( o instanceof Array ) {
55
- var str = s . array ( o ) ;
56
- } else {
57
- var str = o . toString ( ) ;
58
- }
59
- return str ;
60
- } ;
61
-
62
- var JSON_cleaning_RE = / " ( \\ .| [ ^ " \\ ] ) * " / g;
63
- var JSON_problematic_RE = / [ ^ , : { } \[ \] 0 - 9 . \- + E a e f l n r - u \n \r \t ] / ;
64
-
65
- Butterfly . prototype . parseJSON = function ( str ) {
66
- try {
67
- var s = new String ( str ) ; // NOTE(SM) this is to avoid nasty ambiguous java/javascript
68
- // mappings on the 'replace' call below
69
- var cleaned_str = s . replace ( JSON_cleaning_RE , "" ) ;
70
- if ( ! ( JSON_problematic_RE . test ( cleaned_str ) ) ) {
71
- return eval ( '(' + str + ')' ) ;
72
- }
73
- } catch ( e ) {
74
- butterfly . log ( e ) ;
75
- }
76
- } ;
77
23
78
- /*
79
- * Adapted from http://www.json.org/json.js.
80
- */
81
-
82
- var m = {
83
- '\b' : '\\b' ,
84
- '\t' : '\\t' ,
85
- '\n' : '\\n' ,
86
- '\f' : '\\f' ,
87
- '\r' : '\\r' ,
88
- '"' : '\\"' ,
89
- '\\' : '\\\\'
90
- } ;
91
-
92
- var s = {
93
-
94
- object : function ( x ) {
95
- if ( x ) {
96
- var h = x . hashCode ;
97
- if ( h && ( typeof h == "function" ) ) { // here we identify Java objects that are wrapped and made available to javascript
98
- var str = "" + x . toString ( ) ; // IMPORTANT: this converts a Java String object into a JavaScript string object!
99
- return s . string ( str ) ;
100
- } else if ( x instanceof Array || ( "0" in x && "length" in x ) ) { // HACK: this tries to detect arrays
101
- return s . array ( x ) ;
102
- } else {
103
- var a = [ '{' ] , b , f , i , v ;
104
- for ( i in x ) {
105
- if ( typeof i === 'string' && Object . prototype . hasOwnProperty . apply ( x , [ i ] ) ) {
106
- v = x [ i ] ;
107
- f = s [ typeof v ] ;
108
- if ( f ) {
109
- v = f ( v ) ;
110
- if ( typeof v == 'string' ) {
111
- if ( b ) {
112
- a [ a . length ] = ',' ;
113
- }
114
- a . push ( s . string ( i ) , ':' , v ) ;
115
- b = true ;
116
- }
117
- }
118
- }
119
- }
120
- a [ a . length ] = '}' ;
121
- return a . join ( '' ) ;
122
- }
123
- }
124
- return 'null' ;
125
- } ,
126
-
127
- array : function ( x ) {
128
- var a = [ '[' ] , b , f , i , l = x . length , v ;
129
- for ( i = 0 ; i < l ; i += 1 ) {
130
- v = x [ i ] ;
131
- f = s [ typeof v ] ;
132
- if ( f ) {
133
- v = f ( v ) ;
134
- if ( typeof v == 'string' ) {
135
- if ( b ) {
136
- a [ a . length ] = ',' ;
137
- }
138
- a [ a . length ] = v ;
139
- b = true ;
140
- }
141
- }
142
- }
143
- a [ a . length ] = ']' ;
144
- return a . join ( '' ) ;
145
- } ,
146
-
147
- 'null' : function ( x ) {
148
- return "null" ;
149
- } ,
150
-
151
- 'boolean' : function ( x ) {
152
- return String ( x ) ;
153
- } ,
154
-
155
- number : function ( x ) {
156
- return isFinite ( x ) ? String ( x ) : 'null' ;
157
- } ,
158
-
159
- string : function ( x ) {
160
- if ( / [ " \\ \x00 - \x1f ] / . test ( x ) ) {
161
- x = x . replace ( / ( [ \x00 - \x1f \\ " ] ) / g, function ( a , b ) {
162
- var c = m [ b ] ;
163
- if ( c ) {
164
- return c ;
165
- }
166
- c = b . charCodeAt ( ) ;
167
- return '\\u00' + Math . floor ( c / 16 ) . toString ( 16 ) + ( c % 16 ) . toString ( 16 ) ;
168
- } ) ;
169
- }
170
- return '"' + x + '"' ;
171
- }
172
- } ;
173
-
174
24
// ---------------------------------------------------------------------------------------
175
25
176
26
String . prototype . trim = function ( ) {
0 commit comments