From 349422afe3cb8f286647a399b0fca6bc6cbf3683 Mon Sep 17 00:00:00 2001 From: pikastech Date: Mon, 6 Jun 2022 14:51:23 +0800 Subject: [PATCH] move Parse as the constructor --- port/linux/package/pikascript/pika_cjson.pyi | 5 ++++- .../pikascript-lib/pika_cjson/pika_cjson_cJSON.c | 11 ----------- .../pikascript-lib/pika_cjson/pika_cjson_item.c | 14 +++++++++++++- port/linux/test/cJSON-test.cpp | 12 ++++-------- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/port/linux/package/pikascript/pika_cjson.pyi b/port/linux/package/pikascript/pika_cjson.pyi index d47bd5710..77cb13abd 100644 --- a/port/linux/package/pikascript/pika_cjson.pyi +++ b/port/linux/package/pikascript/pika_cjson.pyi @@ -12,7 +12,6 @@ class cJSON(TinyObj): cJSON_Object: int cJSON_Raw: int def print(self) -> str: ... - def parse(self, value: str): ... def __del__(self): ... def __init__(self): ... def getObjectItem(self, string: str) -> cJSON: ... @@ -41,6 +40,10 @@ class cJSON(TinyObj): def addItemToObject(self, string: str, item: cJSON): ... +class Parse(cJSON): + def __init__(self, value: str): ... + + class Null(cJSON): def __init__(self): ... diff --git a/port/linux/package/pikascript/pikascript-lib/pika_cjson/pika_cjson_cJSON.c b/port/linux/package/pikascript/pikascript-lib/pika_cjson/pika_cjson_cJSON.c index d40ce14e3..4eef4fe49 100644 --- a/port/linux/package/pikascript/pikascript-lib/pika_cjson/pika_cjson_cJSON.c +++ b/port/linux/package/pikascript/pikascript-lib/pika_cjson/pika_cjson_cJSON.c @@ -1,17 +1,6 @@ #include "pika_cjson_cJSON.h" #include "cJSON.h" -void pika_cjson_cJSON_parse(PikaObj* self, char* value) { - cJSON* item = cJSON_Parse(value); - if (NULL == item) { - obj_setErrorCode(self, 3); - __platform_printf("Error: cJSON parse faild.\r\n"); - return; - } - obj_setPtr(self, "item", item); - obj_setInt(self, "needfree", 1); -} - char* pika_cjson_cJSON_print(PikaObj* self) { cJSON* item = obj_getPtr(self, "item"); char* res = cJSON_Print(item); diff --git a/port/linux/package/pikascript/pikascript-lib/pika_cjson/pika_cjson_item.c b/port/linux/package/pikascript/pikascript-lib/pika_cjson/pika_cjson_item.c index 35a48ae65..9cd232a29 100644 --- a/port/linux/package/pikascript/pikascript-lib/pika_cjson/pika_cjson_item.c +++ b/port/linux/package/pikascript/pikascript-lib/pika_cjson/pika_cjson_item.c @@ -7,6 +7,7 @@ #include "pika_cjson_Number.h" #include "pika_cjson_Object.h" #include "pika_cjson_ObjectReference.h" +#include "pika_cjson_Parse.h" #include "pika_cjson_Raw.h" #include "pika_cjson_String.h" #include "pika_cjson_StringReference.h" @@ -97,4 +98,15 @@ void pika_cjson_ArrayReference___init__(PikaObj* self, PikaObj* child) { cJSON* item = cJSON_CreateArrayReference(child_item); obj_setPtr(self, "item", item); obj_setInt(self, "needfree", 1); -} \ No newline at end of file +} + +void pika_cjson_Parse___init__(PikaObj* self, char* value) { + cJSON* item = cJSON_Parse(value); + if (NULL == item) { + obj_setErrorCode(self, 3); + __platform_printf("Error: cJSON parse faild.\r\n"); + return; + } + obj_setPtr(self, "item", item); + obj_setInt(self, "needfree", 1); +} diff --git a/port/linux/test/cJSON-test.cpp b/port/linux/test/cJSON-test.cpp index e999c58b3..3a4208f2e 100644 --- a/port/linux/test/cJSON-test.cpp +++ b/port/linux/test/cJSON-test.cpp @@ -36,8 +36,7 @@ TEST(cJSON, parse_print) { /* run */ obj_setStr(pikaMain, "testjson", testjson); obj_run(pikaMain, - "a = pika_cjson.cJSON()\n" - "a.parse(testjson)\n" + "a = pika_cjson.Parse(testjson)\n" "a.print()\n"); /* collect */ /* assert */ @@ -67,8 +66,7 @@ TEST(cJSON, getItem) { obj_setStr(pikaMain, "testjson", testjson); __platform_printf("BEGIN\r\n"); obj_run(pikaMain, - "a = pika_cjson.cJSON()\n" - "a.parse(testjson)\n" + "a = pika_cjson.Parse(testjson)\n" "age = a.getObjectItem('age')\n" "age.print()\n"); /* collect */ @@ -101,8 +99,7 @@ TEST(cJSON, next) { obj_setStr(pikaMain, "testjson", testjson); __platform_printf("BEGIN\r\n"); obj_run(pikaMain, - "a = pika_cjson.cJSON()\n" - "a.parse(testjson)\n" + "a = pika_cjson.Parse(testjson)\n" "node = a.getChild()\n" "for i in range(0, 3):\n" " node.print()\n" @@ -140,8 +137,7 @@ TEST(cJSON, next_get_value) { obj_setStr(pikaMain, "testjson", testjson); __platform_printf("BEGIN\r\n"); obj_run(pikaMain, - "a = pika_cjson.cJSON()\n" - "a.parse(testjson)\n" + "a = pika_cjson.Parse(testjson)\n" "node = a.getChild()\n" "val = PikaStdData.List()\n" "for i in range(0, 3):\n"