parse(), print() for cJSON is ok

This commit is contained in:
pikastech 2022-06-02 16:50:31 +08:00
parent b016924634
commit 604711f708
6 changed files with 69 additions and 14 deletions

View File

@ -4,6 +4,6 @@ import json
class cJSON(TinyObj):
def __init__(self): ...
def print(self) -> str: ...
def parse(self, val: str) -> any: ...
def parse(self, value: str): ...
def __del__(self): ...

View File

@ -1,9 +1,22 @@
#include "cJSON_cJSON.h"
#include "cJSON.h"
void cJSON_cJSON___init__(PikaObj* self) {}
Arg* cJSON_cJSON_parse(PikaObj* self, char* val) {
return NULL;
void cJSON_cJSON_parse(PikaObj* self, char* value) {
cJSON* item = cJSON_Parse(value);
obj_setPtr(self, "item", item);
}
char* cJSON_cJSON_print(PikaObj* self) {
return NULL;
cJSON* item = obj_getPtr(self, "item");
char* res = cJSON_Print(item);
obj_setStr(self, "_buf", res);
cJSON_free(res);
return obj_getStr(self, "_buf");
}
void cJSON_cJSON___del__(PikaObj* self) {
cJSON* item = obj_getPtr(self, "item");
if (NULL != item) {
cJSON_Delete(item);
}
}

View File

@ -11,7 +11,7 @@
"program": "${workspaceFolder}/build/test/pikascript_test",
// "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
"args": [
"--gtest_filter=pikaMain.module_import_from_module"
// "--gtest_filter=pikaMain.module_import_from_module"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",

View File

@ -4,6 +4,6 @@ import json
class cJSON(TinyObj):
def __init__(self): ...
def print(self) -> str: ...
def parse(self, value: str) -> any: ...
def parse(self, value: str): ...
def __del__(self): ...

View File

@ -1,10 +1,22 @@
#include "cJSON_cJSON.h"
#include "cJSON.h"
void cJSON_cJSON___init__(PikaObj* self) {}
Arg* cJSON_cJSON_parse(PikaObj* self, char* value) {
cJSON* cJSON_struct = cJSON_Parse(value);
void cJSON_cJSON_parse(PikaObj* self, char* value) {
cJSON* item = cJSON_Parse(value);
obj_setPtr(self, "item", item);
}
char* cJSON_cJSON_print(PikaObj* self) {
return NULL;
cJSON* item = obj_getPtr(self, "item");
char* res = cJSON_Print(item);
obj_setStr(self, "_buf", res);
cJSON_free(res);
return obj_getStr(self, "_buf");
}
void cJSON_cJSON___del__(PikaObj* self) {
cJSON* item = obj_getPtr(self, "item");
if (NULL != item) {
cJSON_Delete(item);
}
}

View File

@ -13,3 +13,33 @@ extern "C" {
}
extern PikaMemInfo pikaMemInfo;
TEST(cJSON, parse_print) {
/* init */
pikaMemInfo.heapUsedMax = 0;
PikaObj* pikaMain = newRootObj("pikaMain", New_PikaMain);
char testjson[] =
"{\n"
"\"name\": \"mculover666\",\n"
"\"age\": 22,\n"
"\"weight\": 55.5,\n"
"\"address\":\n"
"{\n"
" \"country\": \"China\",\n"
" \"zip-code\": 111111\n"
"},\n"
"\"skill\": [\"c\", \"Java\", \"Python\"],\n"
"\"student\": false\n"
"}\n";
/* run */
obj_setStr(pikaMain, "testjson", testjson);
obj_run(pikaMain,
"a = cJSON.cJSON()\n"
"a.parse(testjson)\n"
"a.print()\n");
/* collect */
/* assert */
/* deinit */
obj_deinit(pikaMain);
EXPECT_EQ(pikaMemNow(), 0);
}