mirror of
https://github.com/pocketpy/pocketpy
synced 2025-11-10 21:50:19 +00:00
Implemenet loads function
This commit is contained in:
parent
6159165a2e
commit
d052b112ef
@ -4,34 +4,84 @@
|
|||||||
|
|
||||||
namespace pkpy{
|
namespace pkpy{
|
||||||
|
|
||||||
|
static Dict convert_cjson_to_dict(const cJSON * const item, VM* vm)
|
||||||
|
{
|
||||||
|
|
||||||
|
Dict output(vm);
|
||||||
|
List list;
|
||||||
|
|
||||||
|
if ((item == NULL))
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (((item->type) & 0xFF) == cJSON_Object){
|
||||||
|
cJSON *child = item->child;
|
||||||
|
while(child != NULL){
|
||||||
|
const cJSON *child_value = cJSON_GetObjectItemCaseSensitive(item, child->string);
|
||||||
|
|
||||||
|
if (cJSON_IsString(child_value))
|
||||||
|
{
|
||||||
|
output.set(VAR(Str(child->string)), VAR(Str(child_value->valuestring)));
|
||||||
|
}
|
||||||
|
else if (cJSON_IsNumber(child_value)){
|
||||||
|
output.set(VAR(Str(child->string)), VAR(child_value->valueint));
|
||||||
|
}
|
||||||
|
else if (cJSON_IsBool(child_value)){
|
||||||
|
output.set(VAR(Str(child->string)), VAR(child_value->valueint)); //Todo: Covert to python boolean
|
||||||
|
}
|
||||||
|
else if (cJSON_IsNull(child_value)){
|
||||||
|
output.set(VAR(Str(child->string)), VAR(vm->None)); //Todo: Covert to python None
|
||||||
|
}
|
||||||
|
else if (cJSON_IsArray(child_value)){
|
||||||
|
printf("array\n");
|
||||||
|
cJSON *array_child = child_value->child;
|
||||||
|
while(array_child != NULL){
|
||||||
|
printf("array_child: %s\n", cJSON_Print(array_child));
|
||||||
|
if (cJSON_IsString(array_child)) /*&& (child_value->valuestring != NULL*/
|
||||||
|
{
|
||||||
|
list.push_back(VAR(Str(array_child->valuestring)));
|
||||||
|
}
|
||||||
|
else if (cJSON_IsNumber(array_child)){
|
||||||
|
list.push_back(VAR(array_child->valueint));
|
||||||
|
}
|
||||||
|
else if (cJSON_IsBool(array_child)){
|
||||||
|
list.push_back(VAR(array_child->valueint));
|
||||||
|
}
|
||||||
|
else if (cJSON_IsNull(array_child)){
|
||||||
|
list.push_back(VAR(vm->None));
|
||||||
|
}
|
||||||
|
array_child = array_child->next;
|
||||||
|
}
|
||||||
|
output.set(VAR(Str(child->string)), VAR(list));
|
||||||
|
}
|
||||||
|
else if (cJSON_IsObject(child_value)){
|
||||||
|
Dict child_object = convert_cjson_to_dict(child_value, vm);
|
||||||
|
output.set(VAR(Str(child->string)), VAR(child_object));
|
||||||
|
}
|
||||||
|
child = child->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
inline void add_module_cjson(VM* vm){
|
inline void add_module_cjson(VM* vm){
|
||||||
PyObject* mod = vm->new_module("cjson");
|
PyObject* mod = vm->new_module("cjson");
|
||||||
vm->bind_func<1>(mod, "loads", [](VM* vm, ArgsView args) {
|
vm->bind_func<1>(mod, "loads", [](VM* vm, ArgsView args) {
|
||||||
|
|
||||||
const Str& expr = CAST(Str&, args[0]);
|
const Str& expr = CAST(Str&, args[0]);
|
||||||
|
|
||||||
cJSON *json = cJSON_Parse(expr.c_str());
|
cJSON *json = cJSON_Parse(expr.c_str());
|
||||||
/*
|
|
||||||
Plan:
|
|
||||||
Create dictionary from cJSON object
|
|
||||||
Return dictionary
|
|
||||||
*/
|
|
||||||
|
|
||||||
//Following is an example dictionary that we will return
|
Dict output = convert_cjson_to_dict(json, vm);
|
||||||
Dict d(vm);
|
return VAR(output);
|
||||||
d.set(py_var(vm, "x"), py_var(vm, 1));
|
|
||||||
d.set(py_var(vm, "y"), py_var(vm, "123"));
|
|
||||||
return VAR(d);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
vm->bind_func<1>(mod, "dumps", [](VM* vm, ArgsView args) {
|
vm->bind_func<1>(mod, "dumps", [](VM* vm, ArgsView args) {
|
||||||
/*
|
|
||||||
Plan:
|
|
||||||
Create cJSON object from dictionary
|
|
||||||
Return string
|
|
||||||
*/
|
|
||||||
const Dict& dict = CAST(Dict&, args[0]);
|
const Dict& dict = CAST(Dict&, args[0]);
|
||||||
|
|
||||||
|
//TODO: Convert dict to cJSON object
|
||||||
char* out = "This will be created from the dictionary";
|
char* out = "This will be created from the dictionary";
|
||||||
return VAR(Str(out));
|
return VAR(Str(out));
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user