Skip to content

Commit 201b42a

Browse files
author
Jason
committed
change object to pointer
1 parent 5de51af commit 201b42a

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

rapidjson.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,19 +109,13 @@ PHP_METHOD(rapidjson, __construct) /* {{{ */ {
109109
return;
110110
}
111111

112-
//printf("Original JSON:\n %s\n", json);
113-
114-
Document document; // Default template parameter uses UTF8 and MemoryPoolAllocator.
115-
116-
// In-situ parsing, decode strings directly in the source string. Source must be string.
117-
//char buffer[sizeof(json)];
118-
//memcpy(buffer, json, sizeof(json));
119-
if (document.ParseInsitu(json).HasParseError()) {
112+
Document *document = new Document();
113+
if (document->Parse(json).HasParseError()) {
120114
printf("\nParsing error\n");
121115
return;
122116
}
123117
self = getThis();
124-
zdoc.value.ptr = &document;
118+
zdoc.value.ptr = document;
125119
zend_update_property(rapidjson_ce, self, ZEND_STRL("obj"), &zdoc);
126120

127121
}
@@ -159,14 +153,25 @@ PHP_METHOD(rapidjson, offsetGet) /* {{{ */ {
159153
obj = zend_read_property(rapidjson_ce, self, ZEND_STRL("obj"), 1, NULL);
160154
Document *document;
161155
document = (Document *)obj->value.ptr;
162-
163156
if (!document->HasMember(offset->value.str->val)) {
164157
RETURN_NULL();
165158
return;
166159
}
167-
const char *val = (*document)[offset->value.str->val].GetString();
168-
zend_string *ret = zend_string_init(val, strlen(val), 0);
169-
RETURN_STR(zend_string_copy(ret));
160+
const Value& val = (*document)[offset->value.str->val];
161+
if (val.IsString()) {
162+
163+
zend_string *ret = zend_string_init(val.GetString(), strlen(val.GetString()), 0);
164+
RETURN_STR(zend_string_copy(ret));
165+
}
166+
if (val.IsInt() || val.IsUint() || val.IsInt64() || val.IsUint64()) {
167+
RETURN_LONG(val.GetUint64());
168+
}
169+
if (val.IsDouble()) {
170+
RETURN_DOUBLE(val.GetDouble());
171+
}
172+
if (val.IsBool()) {
173+
RETURN_BOOL(val.GetBool());
174+
}
170175
}
171176
/* }}} */
172177

rapidjson.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,22 @@
1818
$json = file_get_contents("status.json");
1919
var_dump(strlen($json));
2020

21+
$times = 1000;
22+
23+
////////////////////////////////////////
24+
$r = new Rapidjson($json);
25+
foreach(json_decode($json, true) as $k => $v) {
26+
var_dump($k);
27+
var_dump($r[$k]);
28+
}
29+
exit;
30+
31+
2132
$start_time = microtime(true);
2233
$start_mem = memory_get_usage(); // 36640
2334

24-
for($i = 0; $i < 1; $i++) {
25-
//new Rapidjson($json);
26-
// $json = file_get_contents("status.json");
35+
for($i = 0; $i < $times; $i++) {
2736
$r = new Rapidjson($json);
28-
//echo $r["has_unread"]."\n";
2937
}
3038

3139
$end_time = microtime(true);
@@ -34,13 +42,13 @@
3442
echo "Rapidjson: " . ($end_time - $start_time)."\n";
3543
echo "Rapidjson: " . ($end_mem - $start_mem)."\n";
3644

45+
3746
/////////////////////////////////////
3847

3948
$start_time = microtime(true);
4049
$start_mem = memory_get_usage();
4150

42-
for($i = 0; $i < 1; $i++) {
43-
// $json = file_get_contents("status.json");
51+
for($i = 0; $i < $times; $i++) {
4452
$r = json_decode($json);
4553
}
4654
$end_time = microtime(true);
@@ -49,6 +57,5 @@
4957
echo "json_decode: " . ($end_time - $start_time)."\n";
5058
echo "json_decode: " . ($end_mem - $start_mem)."\n";
5159

52-
//var_dump($r['a']);
5360

5461
?>

0 commit comments

Comments
 (0)