summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/modules/json/json.hpp
blob: 497624070b7fa0a3539b9faab02b732ae7232acb (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/* $Rev$ $Date$ */

#ifndef tuscany_json_hpp
#define tuscany_json_hpp

/**
 * JSON data conversion functions.
 */

#include "string.hpp"
#include "list.hpp"
#include "value.hpp"
#include "element.hpp"
#include "monad.hpp"
#include "jansson.h"

namespace tuscany {
namespace json {

/**
 * Customize JSON memory allocation functions.
 */
inline void* jsonAlloc(size_t size) {
    return gc_pool_malloc(size);
}

inline void jsonFree(void *ptr) {
    return gc_pool_free(ptr);
}

class JSONAllocFuncs {
public:
    inline JSONAllocFuncs() {
        json_set_alloc_funcs(jsonAlloc, jsonFree);
    }
} jsonAllocFuncs;

/**
 * Returns true if a list represents a JS array.
 */
inline const bool isJSArray(const list<value>& l) {
    if(isNil(l))
        return true;
    const value v = car(l);
    if (isSymbol(v))
        return false;
    if(isList(v)) {
        if(!isNil((list<value>)v) && isSymbol(car<value>(v)))
            return false;
    }
    return true;
}

/**
 * Converts a JS array to values.
 */
inline const list<value> jsArrayToValues(const list<value>& listSoFar, json_t* const a, const int i) {
    const value jsValToValue(json_t* const jsv);

    if (i < 0)
        return listSoFar;
    json_t* const jsv = json_array_get(a, i);
    const value val = jsValToValue(jsv);
    return jsArrayToValues(cons(val, listSoFar), a, i - 1);
}

/**
 * Converts JS properties to values.
 */
inline const list<value> jsPropertiesToValues(const list<value>& propertiesSoFar, json_t* const o, void* i) {
    const value jsValToValue(json_t* const jsv);

    if (i == NULL)
        return reverse(propertiesSoFar);
    const char* const name = json_object_iter_key(i);
    json_t* const jsv = json_object_iter_value(i);
    const value val = jsValToValue(jsv);
    if (isNil(val) && !isList(val))
        return jsPropertiesToValues(cons<value> (mklist<value> (element, c_str(name), val), propertiesSoFar), o, json_object_iter_next(o, i));
    if (substr(name, 0, 1) == atsign)
        return jsPropertiesToValues(cons<value>(mklist<value>(attribute, c_str(substr(name, 1)), val), propertiesSoFar), o, json_object_iter_next(o, i));
    if (isList(val) && !isJSArray(val))
        return jsPropertiesToValues(cons<value>(cons<value>(element, cons<value>(c_str(name), list<value>(val))), propertiesSoFar), o, json_object_iter_next(o, i));
    return jsPropertiesToValues(cons<value> (mklist<value> (element, c_str(name), val), propertiesSoFar), o, json_object_iter_next(o, i));
}

/**
 * Converts a JS val to a value.
 */
const value jsValToValue(json_t* const jsv) {
    switch(json_typeof(jsv)) {
    case json_type::JSON_STRING: {
        const string svalue = string(json_string_value(jsv));
        return value(svalue);
    }
    case json_type::JSON_TRUE: {
        return value((bool)true);
    }
    case json_type::JSON_FALSE: {
        return value((bool)false);
    }
    case json_type::JSON_REAL: {
        return value((double)json_real_value(jsv));
    }
    case json_type::JSON_INTEGER: {
        return value((double)json_integer_value(jsv));
    }
    case json_type::JSON_NULL: {
        return nilValue;
    }
    case json_type::JSON_ARRAY: {
        const int i = (int)json_array_size(jsv) - 1;
        const value v = jsArrayToValues(list<value> (), jsv, i);
        return v;
    }
    case json_type::JSON_OBJECT: {
        void* const i = json_object_iter(jsv);
        const value v = jsPropertiesToValues(list<value> (), jsv, i);
        return v;
    }
    default: {
        return nilValue;
    }
    }
}

/**
 * Converts a list of values to JS array elements.
 */
inline json_t* const valuesToJSElements(json_t* const a, const list<value>& l) {
    json_t* const valueToJSVal(const value& val);
    if (isNil(l))
        return a;
    json_t* const v = valueToJSVal(car(l));
    json_array_append(a, v);
    return valuesToJSElements(a, cdr(l));
}

/**
 * Converts a value to a JS val.
 */
inline json_t* const valueToJSVal(const value& val) {
    json_t* const valuesToJSProperties(json_t* const o, const list<value>& l);

    switch(type(val)) {
    case value::String: {
        return json_string(c_str((string)val));
    }
    case value::Symbol: {
        return json_string(c_str((string)val));
    }
    case value::Bool: {
        return json_boolean((bool)val);
    }
    case value::Number: {
        const double d = (double)val;
        if (((double)((json_int_t)d)) == d)
            return json_integer((json_int_t)d);
        else
            return json_real(d);
    }
    case value::List: {
        if (isJSArray(val)) {
            json_t* const a = json_array();
            return valuesToJSElements(a, val);
        }
        json_t* const c = json_object();
        return valuesToJSProperties(c, val);
    }
    case value::Nil: {
        return json_null();
    }
    default: {
        return json_null();
    }
    }
}

/**
 * Converts a list of values to JS properties.
 */
inline json_t* const valuesToJSProperties(json_t* const o, const list<value>& l) {
    if (isNil(l))
        return o;

    // Write an attribute
    const value token(car(l));

    if (isTaggedList(token, attribute)) {
        json_t* const v = valueToJSVal(attributeValue(token));
        json_object_set(o, c_str(atsign + string(attributeName(token))), v);

    } else if (isTaggedList(token, element)) {

        // Write the value of an element
        if (elementHasValue(token)) {
            json_t* const v = valueToJSVal(elementValue(token));
            json_object_set(o, c_str(string(elementName(token))), v);

        } else {

            // Write a parent element
            json_t* const c = json_object();
            json_object_set(o, c_str(string(elementName(token))), c);

            // Write its children
            valuesToJSProperties(c, elementChildren(token));
        }
    }

    // Go on
    return valuesToJSProperties(o, cdr(l));
}

/**
 * Return true if a list of strings contains a JSON document.
 */
inline const bool isJSON(const list<string>& ls) {
    if (isNil(ls))
        return false;
    const string s = substr(car(ls), 0, 1);
    return s == "[" || s == "{";
}

 /**
  * Convert a list of strings representing a JSON document to a list of elements.
  */
inline const failable<list<value> > readElements(const list<string>& ilist) {
    ostringstream os;
    write(ilist, os);
    json_error_t e;
    json_t* const val = json_loads(c_str(str(os)), 0, &e);
   if(val == NULL)
        return mkfailure<list<value> >(string("json_loads failed: ") + string(e.text));
    return list<value>(jsValToValue(val));
}
 
/**
 * Convert a list of strings representing a JSON document to a value
 */
inline const failable<value> readValue(const list<string>& ilist) {
    ostringstream os;
    write(ilist, os);
    json_error_t e;
    json_t* const jv = json_loads(c_str(str(os)), JSON_DECODE_ANY, &e);
    if(jv == NULL)
        return mkfailure<value>(string("json_loads failed: ") + string(e.text));
    const value val = jsValToValue(jv);
    if (!isList(val))
        return val;
    return (value)elementsToValues(val);
}

/**
 * Context passed to the JSON write callback function.
 */
template<typename R> class WriteContext {
public:
    inline WriteContext(const lambda<const R(const string&, const R)>& reduce, const R& accum) : reduce(reduce), accum(accum) {
    }

    const lambda<const R(const string&, const R)> reduce;
    gc_mutable_ref<R> accum;
};

/**
 * Called by dump_callback to write JSON out.
 */
template<typename R> inline int writeCallback(const char *buf, size_t len, void *data) {
    WriteContext<R>& wcx = *(WriteContext<R>*)data;
    const string str(buf, len);
    wcx.accum = wcx.reduce(str, wcx.accum);
    return 0;
}

/**
 * Convert a value to a JSON document.
 */
template<typename R> inline const failable<R> writeValue(const lambda<const R(const string&, const R)>& reduce, const R& initial, const list<value>& l) {
    json_t* const val = valueToJSVal(l);
    WriteContext<R> wcx(reduce, initial);
    if (json_dump_callback(val, writeCallback<R>, &wcx, JSON_COMPACT | JSON_ENSURE_ASCII | JSON_PRESERVE_ORDER | JSON_ENCODE_ANY) == -1)
        return mkfailure<R>("json_dump_callback failed");
    return (R)wcx.accum;
}

/**
 * Convert a list of elements to a JSON document.
 */
template<typename R> inline const failable<R> writeElements(const lambda<const R(const string&, const R)>& reduce, const R& initial, const list<value>& l) {
    json_t* const val = isJSArray(l)? valuesToJSElements(::json_array(), l) : valuesToJSProperties(::json_object(), l);

    WriteContext<R> wcx(reduce, initial);
    if (json_dump_callback(val, writeCallback<R>, &wcx, JSON_COMPACT | JSON_ENSURE_ASCII | JSON_PRESERVE_ORDER | JSON_ENCODE_ANY) == -1)
        return mkfailure<R>("json_dump_callback failed");
    return (R)wcx.accum;
}

/**
 * Convert a list of elements to a list of strings representing a JSON document.
 */
inline const failable<list<string> > writeElements(const list<value>& l) {
    const failable<list<string> > ls = writeElements<list<string>>(rcons<string>, list<string>(), valuesToElements(elementsToValues(l)));
     if (!hasContent(ls))
         return ls;
     return reverse(list<string>(content(ls)));
}
 
/**
 * Convert a value to a list of strings representing a JSON document.
 */
inline const failable<list<string> > writeValue(const value& v) {
    if (!isList(v)) {
        const failable<list<string> > ls = writeValue<list<string>>(rcons<string>, list<string>(), v);
        if (!hasContent(ls))
            return ls;
        return reverse(list<string>(content(ls)));
    }

    const failable<list<string> > ls = writeElements<list<string>>(rcons<string>, list<string>(), valuesToElements(elementsToValues(valuesToElements(v))));
    if (!hasContent(ls))
        return ls;
    return reverse(list<string>(content(ls)));
}

/**
 * Convert a list of function + params to a JSON-RPC request.
 */
inline const failable<list<string> > jsonRequest(const value& id, const value& func, const value& params) {
    const list<value> r = mklist<value>(mklist<value>("id", id), mklist<value>("method", string(func)), mklist<value>("params", params));
    return writeElements(valuesToElements(r));
}

/**
 * Convert a value to a JSON-RPC result.
 */
inline const failable<list<string> > jsonResult(const value& id, const value& val) {
    return writeElements(valuesToElements(mklist<value>(mklist<value>("id", id), mklist<value>("result", val))));
}

/**
 * Convert a JSON-RPC result to a value.
 */
inline const failable<value> jsonResultValue(const list<string>& s) {
    const failable<value> jsres = json::readValue(s);
    if (!hasContent(jsres))
        return mkfailure<value>(jsres);
    const list<value> rval(cadr<value>(content(jsres)));
    const value val = cadr(rval);
    if (isList(val) && !isJSArray(val))
        return value(mklist<value>(val));
    return val;
}

/**
 * Return a portable function name from a JSON-RPC function name.
 * Strip the ".", "system." and "Service." prefixes added by some JSON-RPC clients.
 */
inline const string funcName(const string& f) {
    if (length(f) > 1 && find(f, ".", 0) == 0)
        return c_str(f) + 1;
    if (length(f) > 7 && find(f, "system.", 0) == 0)
        return c_str(f) + 7;
    if (length(f) > 8 && find(f, "Service.", 0) == 0)
        return c_str(f) + 8;
    return f;
}

/**
 * Returns a list of param values other than the id and method args from a list
 * of key value pairs.
 */
inline const list<value> queryParams(const list<value>& a) {
    if (isNil(a))
        return nilListValue;
    const list<value> p = car(a);
    if (car(p) == value("id") || car(p) == value("method"))
        return queryParams(cdr(a));
    return cons<value>(cadr(p), queryParams(cdr(a)));
}

}
}

#endif /* tuscany_json_hpp */