summaryrefslogtreecommitdiff
path: root/jsonrpc/json.esp
blob: ad0e13a6c339ab3a7c82ec7dc1b7a763d5cf86a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<%

/*
 * Copyright:
 *   (C) 2006 by Derrell Lipman
 *       All rights reserved
 *
 * License:
 *   LGPL 2.1: http://creativecommons.org/licenses/LGPL/2.1/
 */

/*
 * This module provides a JSON encoder.
 */


/* escape a string as required by json */
function _escape(s)
{
    var i;
    var arr = new Array();

    for (i = 0; i < strlen(s); i++)
    {
        var c = substr(s, i, 1);
        if (c == '\x00')
        {
            arr[i] = '\\u0000';
        }
        if (Json._internal.convert[c] != undefined)
        {
            arr[i] = Json._internal.convert[c];
        }
        else
        {
            arr[i] = c;
        }
    }

    return join("", arr);
}

/* encode an arbitrary object.  called recursively, for object and array */
function _encode(o)
{
    var type = nativeTypeOf(o);

    if (type == "undefined")
    {
        return "null";          /* you really shouldn't count on this! */
    }
    else if (type == "null")
    {
        return "null";
    }
    else if (type == "boolean")
    {
        if (o)
        {
            return "true";
        }
        else
        {
            return "false";
        }
    }
    else if (type == "c_function" ||
             type == "js_function" ||
             type == "string_c_function")
    {
        /* no output */
    }
    else if (type == "float" ||
             type == "integer" ||
             type == "integer64" ||
             type == "pointer")
    {
        return (o + 0);
    }
    else if (type == "object")
    {
        var buf;

        /* Is this an array or an ordinary object? */
        if (o["length"] != undefined)
        {
            var i;

            /* Assume it's an array if there's a length field */
            buf = "[";
            for (i = 0; i < o.length; i++)
            {
                /*
                 * NOTE: We don't support sparse arrays nor associative
                 * arrays.  Should we later want to do either, we're supposed
                 * to send it as an object rather than as an array.
                 */
                if (i > 0)
                {
                    buf = buf + ",";
                }
                buf = buf + this.encode(o[i]);
            }
            buf = buf + "]";
        }
        else
        {
            /* No length field, so it must be an ordinary object */
            var key;
            var first = true;

            buf = "{";
            for (key in o)
            {
                if (! first)
                {
                    buf = buf + ",";
                }
                buf = buf + '"' + key + '":' + this.encode(o[key]);
                first = false;
            }
            buf = buf + "}";
        }

        return buf;
    }
    else if (type == "string")
    {
        return '"' + this._internal.escape(o) + '"';
    }
}

/* Allocate the public Json access object */
Json = new Object();

/* Json.encode(): encode an arbitrary object */
Json.encode = _encode;
_encode = null;

/* Json.decode(): decode a string into its object form */
Json.decode = literal_to_var;

/* Internal stuff, not for external access */
Json._internal = new Object();

Json._internal.escape = _escape;
_escape = null;

Json._internal.convert = new Object();
Json._internal.convert['\b'] = '\\b';
Json._internal.convert['\t'] = '\\t';
Json._internal.convert['\n'] = '\\n';
Json._internal.convert['\f'] = '\\f';
Json._internal.convert['\r'] = '\\r';
Json._internal.convert['"'] = '\\"';
Json._internal.convert['\\'] = '\\\\';
Json._internal.convert['\x01'] = '\\u0001';
Json._internal.convert['\x02'] = '\\u0002';
Json._internal.convert['\x03'] = '\\u0003';
Json._internal.convert['\x04'] = '\\u0004';
Json._internal.convert['\x05'] = '\\u0005';
Json._internal.convert['\x06'] = '\\u0006';
Json._internal.convert['\x07'] = '\\u0007';
Json._internal.convert['\x08'] = '\\u0008';
Json._internal.convert['\x09'] = '\\u0009';
Json._internal.convert['\x0a'] = '\\u000a';
Json._internal.convert['\x0b'] = '\\u000b';
Json._internal.convert['\x0c'] = '\\u000c';
Json._internal.convert['\x0d'] = '\\u000d';
Json._internal.convert['\x0e'] = '\\u000e';
Json._internal.convert['\x0f'] = '\\u000f';
Json._internal.convert['\x10'] = '\\u0010';
Json._internal.convert['\x11'] = '\\u0011';
Json._internal.convert['\x12'] = '\\u0012';
Json._internal.convert['\x13'] = '\\u0013';
Json._internal.convert['\x14'] = '\\u0014';
Json._internal.convert['\x15'] = '\\u0015';
Json._internal.convert['\x16'] = '\\u0016';
Json._internal.convert['\x17'] = '\\u0017';
Json._internal.convert['\x18'] = '\\u0018';
Json._internal.convert['\x19'] = '\\u0019';
Json._internal.convert['\x1a'] = '\\u001a';
Json._internal.convert['\x1b'] = '\\u001b';
Json._internal.convert['\x1c'] = '\\u001c';
Json._internal.convert['\x1d'] = '\\u001d';
Json._internal.convert['\x1e'] = '\\u001e';
Json._internal.convert['\x1f'] = '\\u001f';
/*
 * At some point, we probably want to add \x80-\xff as well, and it's then
 * probably more efficient to generate these strings dynamically.  (Even now
 * it may be, but this was the the way I started, and so it remains.)
 */


/* Test it */
libinclude("base.js");
function testFormat()
{
    var test = new Object();
    test.int = 23;
    test.str = "hello world";
    test.float = 223.1;
    test.bool = true;
    test.array = new Array();
    test.array[0] = "hello";
    test.array[1] = "world";
    test.obj = new Object();
    test.obj.int = 1000;
    test.obj.array = new Array();
    test.obj.array[0] = 42;
    test.obj.array[1] = 223;
    printf("%s\n", Json.encode(test));
}

function testParse()
{
    var s;

    s = '{ "x" : 23 }';
    obj = Json.decode(s);
    printf("Decode/encode of\n\t%s\nyielded\n\t%s\n\n", s, Json.encode(obj));

    s = '{ "x" : [ 23, 42] }';
    obj = Json.decode(s);
    printf("Decode/encode of\n\t%s\nyielded\n\t%s\n\n", s, Json.encode(obj));

    s = '[ 13, 19, { "x" : [ 23, 42] }, 223 ]';
    obj = Json.decode(s);
    printf("Decode/encode of\n\t%s\nyielded\n\t%s\n\n", s, Json.encode(obj));

    s = '{ "x" : [ "hi" ] }';
    obj = Json.decode(s);
    printf("Decode/encode of\n\t%s\nyielded\n\t%s\n\n", s, Json.encode(obj));

    s = '[ 13, 19, { "x" : [ 23, 42, { "y":{"a":"hello", "b":"world", "c":[1,2,3]}}] }, 223 ]';
    obj = Json.decode(s);
    printf("Decode/encode of\n\t%s\nyielded\n\t%s\n\n", s, Json.encode(obj));
}

//testFormat();
testParse();

%>