~efargaspro/+junk/codeblocks-16.01-release

« back to all changes in this revision

Viewing changes to src/plugins/contrib/wxCrafterCB/wxcLib/json_node.cpp

  • Committer: damienlmoore at gmail
  • Date: 2016-02-02 02:43:22 UTC
  • Revision ID: damienlmoore@gmail.com-20160202024322-yql5qmtbwdyamdwd
Code::BlocksĀ 16.01

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "json_node.h"
 
2
#include <stdlib.h>
 
3
#include <wx/filename.h>
 
4
#include <wx/ffile.h>
 
5
 
 
6
JSONRoot::JSONRoot(const wxString& text)
 
7
    : _json(NULL)
 
8
{
 
9
    _json = cJSON_Parse(text.mb_str(wxConvUTF8).data());
 
10
}
 
11
 
 
12
JSONRoot::JSONRoot(int type)
 
13
    : _json(NULL)
 
14
{
 
15
    if(type == cJSON_Array)
 
16
        _json = cJSON_CreateArray();
 
17
    else
 
18
        _json = cJSON_CreateObject();
 
19
}
 
20
 
 
21
 
 
22
JSONRoot::JSONRoot(const wxFileName& filename)
 
23
    : _json(NULL)
 
24
{
 
25
    wxString content;
 
26
    wxFFile fp(filename.GetFullPath(), wxT("r+b"));
 
27
    if( fp.IsOpened() ) {
 
28
        if( fp.ReadAll( &content, wxConvUTF8 ) ) {
 
29
            _json = cJSON_Parse( content.mb_str(wxConvUTF8).data() );
 
30
        }
 
31
    }
 
32
    
 
33
    if ( !_json ) {
 
34
        _json = cJSON_CreateObject();
 
35
    }
 
36
}
 
37
 
 
38
JSONRoot::~JSONRoot()
 
39
{
 
40
    if(_json) {
 
41
        cJSON_Delete(_json);
 
42
        _json = NULL;
 
43
    }
 
44
}
 
45
 
 
46
void JSONRoot::save(const wxFileName& fn) const
 
47
{
 
48
    wxFFile fp( fn.GetFullPath(), wxT("w+b") );
 
49
    if( fp.IsOpened() ) {
 
50
        fp.Write( toElement().format(), wxConvUTF8 );
 
51
        fp.Close();
 
52
    }
 
53
}
 
54
 
 
55
JSONElement JSONRoot::toElement() const 
 
56
{
 
57
    if(!_json) {
 
58
        return JSONElement(NULL);
 
59
    }
 
60
    return JSONElement(_json);
 
61
}
 
62
 
 
63
wxString JSONRoot::errorString() const
 
64
{
 
65
    return _errorString;
 
66
}
 
67
 
 
68
JSONElement JSONElement::namedObject(const wxString& name) const 
 
69
{
 
70
    if(!_json) {
 
71
        return JSONElement(NULL);
 
72
    }
 
73
 
 
74
    cJSON *obj = cJSON_GetObjectItem(_json, name.mb_str(wxConvUTF8).data());
 
75
    if(!obj) {
 
76
        return JSONElement(NULL);
 
77
    }
 
78
    return JSONElement(obj);
 
79
}
 
80
 
 
81
void JSONRoot::clear()
 
82
{
 
83
    int type = cJSON_Object;
 
84
    if(_json) {
 
85
        type = _json->type;
 
86
        cJSON_Delete(_json);
 
87
        _json = NULL;
 
88
    }
 
89
    if(type == cJSON_Array)
 
90
        _json = cJSON_CreateArray();
 
91
    else
 
92
        _json = cJSON_CreateObject();
 
93
}
 
94
 
 
95
///////////////////////////////////////////////////////////
 
96
///////////////////////////////////////////////////////////
 
97
JSONElement::JSONElement(cJSON* json)
 
98
    : _json(json)
 
99
    , _type(-1)
 
100
{
 
101
    if(_json) {
 
102
        _name = wxString(_json->string, wxConvUTF8);
 
103
        _type = _json->type;
 
104
    }
 
105
}
 
106
 
 
107
JSONElement::JSONElement(const wxString &name, const wxVariant& val, int type)
 
108
    : _json(NULL)
 
109
    , _type(type)
 
110
{
 
111
    _value = val;
 
112
    _name = name;
 
113
}
 
114
 
 
115
JSONElement JSONElement::arrayItem(int pos) const 
 
116
{
 
117
    if(!_json) {
 
118
        return JSONElement(NULL);
 
119
    }
 
120
 
 
121
    if(_json->type != cJSON_Array)
 
122
        return JSONElement(NULL);
 
123
 
 
124
    int size = cJSON_GetArraySize(_json);
 
125
    if(pos >= size)
 
126
        return JSONElement(NULL);
 
127
 
 
128
    return JSONElement(cJSON_GetArrayItem(_json, pos));
 
129
}
 
130
 
 
131
bool JSONElement::isNull() const 
 
132
{
 
133
    if(!_json) {
 
134
       return false;
 
135
    }
 
136
    return _json->type == cJSON_NULL;
 
137
}
 
138
 
 
139
bool JSONElement::toBool(bool defaultValue) const 
 
140
{
 
141
    if(!_json) {
 
142
        return defaultValue;
 
143
    }
 
144
 
 
145
    if(!isBool()) {
 
146
        return defaultValue;
 
147
    }
 
148
 
 
149
    return _json->type == cJSON_True;
 
150
}
 
151
 
 
152
wxString JSONElement::toString(const wxString& defaultValue) const 
 
153
{
 
154
    if(!_json) {
 
155
        return defaultValue;
 
156
    }
 
157
 
 
158
    if(_json->type != cJSON_String) {
 
159
        return defaultValue;
 
160
    }
 
161
 
 
162
    return wxString(_json->valuestring, wxConvUTF8);
 
163
}
 
164
 
 
165
bool JSONElement::isBool() const 
 
166
{
 
167
    if(!_json) {
 
168
        return false;
 
169
    }
 
170
 
 
171
    return _json->type == cJSON_True || _json->type == cJSON_False;
 
172
}
 
173
 
 
174
bool JSONElement::isString() const 
 
175
{
 
176
    if(!_json) {
 
177
        return false;
 
178
    }
 
179
 
 
180
    return _json->type == cJSON_String;
 
181
}
 
182
 
 
183
void JSONElement::append(const JSONElement& element) 
 
184
{
 
185
    if(!_json) {
 
186
        return;
 
187
    }
 
188
 
 
189
    switch(element.getType()) {
 
190
    case cJSON_False:
 
191
        cJSON_AddFalseToObject(_json, element.getName().mb_str(wxConvUTF8).data());
 
192
        break;
 
193
 
 
194
    case cJSON_True:
 
195
        cJSON_AddTrueToObject(_json, element.getName().mb_str(wxConvUTF8).data());
 
196
        break;
 
197
 
 
198
    case cJSON_NULL:
 
199
        cJSON_AddNullToObject(_json, element.getName().mb_str(wxConvUTF8).data());
 
200
        break;
 
201
 
 
202
    case cJSON_Number:
 
203
        cJSON_AddNumberToObject(_json, element.getName().mb_str(wxConvUTF8).data(), element.getValue().GetLong());
 
204
        break;
 
205
 
 
206
    case cJSON_String:
 
207
        cJSON_AddStringToObject(_json, element.getName().mb_str(wxConvUTF8).data(), element.getValue().GetString().mb_str(wxConvUTF8).data());
 
208
        break;
 
209
 
 
210
    case cJSON_Array:
 
211
    case cJSON_Object:
 
212
        cJSON_AddItemToObject(_json, element.getName().mb_str(wxConvUTF8).data(), element._json);
 
213
        break;
 
214
    }
 
215
}
 
216
 
 
217
void JSONElement::arrayAppend(const JSONElement& element) 
 
218
{
 
219
    if(!_json) {
 
220
       return;
 
221
    }
 
222
 
 
223
    cJSON* p = NULL;
 
224
    switch(element.getType()) {
 
225
    case cJSON_False:
 
226
        p = cJSON_CreateFalse();
 
227
        break;
 
228
 
 
229
    case cJSON_True:
 
230
        p = cJSON_CreateTrue();
 
231
        break;
 
232
 
 
233
    case cJSON_NULL:
 
234
        p = cJSON_CreateNull();
 
235
        break;
 
236
 
 
237
    case cJSON_Number:
 
238
        p = cJSON_CreateNumber(element.getValue().GetDouble());
 
239
        break;
 
240
 
 
241
    case cJSON_String:
 
242
        p = cJSON_CreateString(element.getValue().GetString().mb_str(wxConvUTF8).data());
 
243
        break;
 
244
    case cJSON_Array:
 
245
    case cJSON_Object:
 
246
        p = element._json;
 
247
        break;
 
248
    }
 
249
    if(p) {
 
250
        cJSON_AddItemToArray(_json, p);
 
251
 
 
252
    }
 
253
}
 
254
 
 
255
JSONElement JSONElement::createArray(const wxString &name) 
 
256
{
 
257
    JSONElement arr(cJSON_CreateArray());
 
258
    arr.setName(name);
 
259
    arr.setType(cJSON_Array);
 
260
    return arr;
 
261
}
 
262
 
 
263
JSONElement JSONElement::createObject(const wxString &name) 
 
264
{
 
265
    JSONElement obj(cJSON_CreateObject());
 
266
    obj.setName(name);
 
267
    obj.setType(cJSON_Object);
 
268
    return obj;
 
269
}
 
270
 
 
271
wxString JSONElement::format() const 
 
272
{
 
273
    if(!_json) {
 
274
        return wxT("");
 
275
    }
 
276
 
 
277
    char *p = cJSON_Print(_json);
 
278
    wxString output(p, wxConvUTF8);
 
279
    free(p);
 
280
    return output;
 
281
}
 
282
 
 
283
int JSONElement::toInt(int defaultVal) const
 
284
{
 
285
    if(!_json) {
 
286
        return defaultVal;
 
287
    }
 
288
 
 
289
    if(_json->type != cJSON_Number) {
 
290
        return defaultVal;
 
291
    }
 
292
 
 
293
    return _json->valueint;
 
294
}
 
295
 
 
296
size_t JSONElement::toSize_t(size_t defaultVal) const
 
297
{
 
298
    if(!_json) {
 
299
        return defaultVal;
 
300
    }
 
301
 
 
302
    if(_json->type != cJSON_Number) {
 
303
        return defaultVal;
 
304
    }
 
305
 
 
306
    return (size_t)_json->valueint;
 
307
}
 
308
 
 
309
double JSONElement::toDouble(double defaultVal) const
 
310
{
 
311
    if(!_json) {
 
312
        return defaultVal;
 
313
    }
 
314
 
 
315
    if(_json->type != cJSON_Number) {
 
316
        return defaultVal;
 
317
    }
 
318
 
 
319
    return _json->valuedouble;
 
320
}
 
321
 
 
322
int JSONElement::arraySize() const
 
323
{
 
324
    if(!_json) {
 
325
        return 0;
 
326
    }
 
327
 
 
328
    if(_json->type != cJSON_Array)
 
329
        return 0;
 
330
 
 
331
    return cJSON_GetArraySize(_json);
 
332
}
 
333
 
 
334
JSONElement& JSONElement::addProperty(const wxString& name, bool value)
 
335
{
 
336
    if(value) {
 
337
        append(JSONElement(name, value, cJSON_True));
 
338
        
 
339
    } else {
 
340
        append(JSONElement(name, value, cJSON_False));
 
341
    }
 
342
    return *this;
 
343
}
 
344
 
 
345
JSONElement& JSONElement::addProperty(const wxString& name, const wxString& value)
 
346
{
 
347
    append(JSONElement(name, value, cJSON_String));
 
348
    return *this;
 
349
}
 
350
 
 
351
JSONElement& JSONElement::addProperty(const wxString& name, const wxChar* value)
 
352
{
 
353
    append(JSONElement(name, wxString(value), cJSON_String));
 
354
    return *this;
 
355
}
 
356
 
 
357
JSONElement& JSONElement::addProperty(const wxString& name, int value)
 
358
{
 
359
    append(JSONElement(name, value, cJSON_Number));
 
360
    return *this;
 
361
}
 
362
 
 
363
JSONElement& JSONElement::addProperty(const wxString& name, const wxArrayString& arr)
 
364
{
 
365
    JSONElement arrEle = JSONElement::createArray(name);
 
366
    for(size_t i=0; i<arr.GetCount(); i++) {
 
367
        arrEle.arrayAppend(arr.Item(i));
 
368
    }
 
369
    append(arrEle);
 
370
    return *this;
 
371
}
 
372
 
 
373
void JSONElement::arrayAppend(const wxString& value)
 
374
{
 
375
    arrayAppend(JSONElement(wxT(""), value, cJSON_String));
 
376
}
 
377
 
 
378
wxArrayString JSONElement::toArrayString() const
 
379
{
 
380
    wxArrayString arr;
 
381
    if(!_json) {
 
382
        return arr;
 
383
    }
 
384
 
 
385
    if(_json->type != cJSON_Array) {
 
386
        return arr;
 
387
    }
 
388
    
 
389
    for(int i=0; i<arraySize(); i++) {
 
390
        arr.Add(arrayItem(i).toString());
 
391
    }
 
392
    return arr;
 
393
}
 
394
 
 
395
bool JSONElement::hasNamedObject(const wxString& name) const
 
396
{
 
397
    if(!_json) {
 
398
        return false;
 
399
    }
 
400
 
 
401
    cJSON *obj = cJSON_GetObjectItem(_json, name.mb_str(wxConvUTF8).data());
 
402
    return obj != NULL;
 
403
}
 
404
 
 
405
JSONElement& JSONElement::addProperty(const wxString& name, const wxPoint& pt)
 
406
{
 
407
    wxString szStr;
 
408
    szStr << pt.x << wxT(",") << pt.y;
 
409
    return addProperty(name, szStr);
 
410
}
 
411
 
 
412
JSONElement& JSONElement::addProperty(const wxString& name, const wxSize& sz)
 
413
{
 
414
    wxString szStr;
 
415
    szStr << sz.x << wxT(",") << sz.y;
 
416
    return addProperty(name, szStr);
 
417
 
 
418
}
 
419
 
 
420
JSONElement& JSONElement::addProperty(const wxString& name, const JSONElement& element) 
 
421
{
 
422
    if(!_json) {
 
423
        return *this;
 
424
    }
 
425
    cJSON_AddItemToObject(_json, name.mb_str(wxConvUTF8).data(), element._json);
 
426
    return *this;
 
427
}
 
428
 
 
429
wxPoint JSONElement::toPoint() const
 
430
{
 
431
    if(!_json) {
 
432
        return wxDefaultPosition;
 
433
    }
 
434
 
 
435
    if(_json->type != cJSON_String) {
 
436
        return wxDefaultPosition;
 
437
    }
 
438
 
 
439
    wxString str = wxString(_json->valuestring, wxConvUTF8);
 
440
    wxString x = str.BeforeFirst(',');
 
441
    wxString y = str.AfterFirst(',');
 
442
    
 
443
    long nX(-1), nY(-1);
 
444
    if ( !x.ToLong(&nX) || !y.ToLong(&nY) )
 
445
        return wxDefaultPosition;
 
446
    
 
447
    return wxPoint(nX, nY);
 
448
}
 
449
 
 
450
wxColour JSONElement::toColour(const wxColour& defaultColour) const
 
451
{
 
452
    if(!_json) {
 
453
        return defaultColour;
 
454
    }
 
455
 
 
456
    if(_json->type != cJSON_String) {
 
457
        return defaultColour;
 
458
    }
 
459
    
 
460
    return wxColour( wxString(_json->valuestring, wxConvUTF8) );
 
461
}
 
462
 
 
463
wxSize JSONElement::toSize() const
 
464
{
 
465
    wxPoint pt = toPoint();
 
466
    return wxSize(pt.x, pt.y);
 
467
}
 
468
 
 
469
void JSONElement::removeProperty(const wxString& name)
 
470
{
 
471
    // delete child property
 
472
    if(!_json) {
 
473
        return;
 
474
    }
 
475
    cJSON_DeleteItemFromObject(_json, name.mb_str(wxConvUTF8).data());
 
476
}
 
477
 
 
478
JSONElement& JSONElement::addProperty(const wxString& name, const JSONElement::wxStringMap_t& stringMap)
 
479
{
 
480
    if ( !_json )
 
481
        return *this;
 
482
        
 
483
    JSONElement arr = JSONElement::createArray(name);
 
484
    JSONElement::wxStringMap_t::const_iterator iter = stringMap.begin();
 
485
    for( ; iter != stringMap.end(); ++iter ) {
 
486
        JSONElement obj = JSONElement::createObject();
 
487
        obj.addProperty(wxT("key"), iter->first);
 
488
        obj.addProperty(wxT("value"), iter->second);
 
489
        arr.arrayAppend(obj);
 
490
    }
 
491
    append(arr);
 
492
    return *this;
 
493
}
 
494
 
 
495
JSONElement::wxStringMap_t JSONElement::toStringMap() const
 
496
{
 
497
    JSONElement::wxStringMap_t res;
 
498
    if(!_json) {
 
499
        return res;
 
500
    }
 
501
 
 
502
    if(_json->type != cJSON_Array) {
 
503
        return res;
 
504
    }
 
505
    
 
506
    for(int i=0; i<arraySize(); ++i) {
 
507
        wxString key = arrayItem(i).namedObject(wxT("key")).toString();
 
508
        wxString val = arrayItem(i).namedObject(wxT("value")).toString();
 
509
        res.insert(std::make_pair(key, val));
 
510
    }
 
511
    return res;
 
512
}
 
513
 
 
514
JSONElement& JSONElement::addProperty(const wxString& name, size_t value)
 
515
{
 
516
    return addProperty(name, (int)value);
 
517
}
 
518
 
 
519
JSONElement& JSONElement::addProperty(const wxString& name, const wxColour& colour)
 
520
{
 
521
    wxString colourValue;
 
522
    if ( colour.IsOk() ) {
 
523
        colourValue = colour.GetAsString(wxC2S_HTML_SYNTAX);
 
524
    }
 
525
    return addProperty(name, colourValue);
 
526
}