This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCCJson.cpp
86 lines (77 loc) · 2.21 KB
/
CCJson.cpp
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
//
// Created by gin0606 on 2013/12/12.
//
#include "CCJson.h"
CCDictionary *CCJson::JSONObjectWithString(std::string jsonString) {
return CCJson::JSONObjectWithString(jsonString.c_str());
}
CCDictionary *CCJson::JSONObjectWithString(const char *jsonString) {
return CCJson::dictionaryWithJson(Json_create(jsonString));
}
CCDictionary *CCJson::dictionaryWithJson(Json *pJson) {
CCObject *o = parseValue(pJson);
if (!pJson->name) {
return (CCDictionary *) o;
} else {
CCDictionary *pRet = CCDictionary::create();
if (o) {
pRet->setObject(o, pJson->name);
}
return pRet;
}
}
CCObject *CCJson::parseObject(Json *pJson) {
CCDictionary *root = CCDictionary::create();
for (Json *item = pJson; item; item = item->next) {
CCObject *value = parseValue(item);
if (value) {
root->setObject(value, item->name);
}
}
return root;
}
CCArray *CCJson::parseArray(Json *pJson) {
int arrayLength = Json_getSize(pJson);
CCArray *array = CCArray::create();
for (int i = 0; i < arrayLength; i++) {
Json *item = Json_getItemAt(pJson, i);
CCObject *object = parseValue(item);
if (object) {
array->addObject(object);
}
}
return array;
}
CCObject *CCJson::parseValue(Json *pJsonValue) {
if (!pJsonValue) {return NULL;}
CCObject *object = NULL;
switch (pJsonValue->type) {
case Json_False:
object = CCBool::create(false);
break;
case Json_True:
object = CCBool::create(true);
break;
case Json_NULL:
object = NULL;
break;
case Json_Number:
object = CCFloat::create(pJsonValue->valuefloat);
break;
case Json_String:
if (pJsonValue->valuestring) {
object = CCString::create(pJsonValue->valuestring);
}
break;
case Json_Array:
object = parseArray(pJsonValue);
break;
case Json_Object:
object = parseObject(pJsonValue->child);
break;
default:
object = NULL;
break;
}
return object;
}