pocketpy/3rd/cjson/src/cJSONw.cpp
2024-06-07 22:56:09 +08:00

121 lines
3.9 KiB
C++

#include "cJSONw.hpp"
namespace pkpy{
static cJSON* convert_python_object_to_cjson(PyVar obj, VM* vm);
static PyVar convert_cjson_to_python_object(const cJSON * const item, VM* vm);
template<typename T>
static cJSON* convert_list_to_cjson(const T& list, VM* vm){
cJSON *cjson_list = cJSON_CreateArray();
for(auto& element : list){
cJSON_AddItemToArray(cjson_list, convert_python_object_to_cjson(element, vm));
}
return cjson_list;
}
static cJSON* covert_dict_to_cjson(const Dict& dict, VM* vm){
cJSON *cjson_object = cJSON_CreateObject();
dict.apply([&](PyVar key, PyVar val){
cJSON_AddItemToObject(cjson_object, CAST(Str&, key).c_str(), convert_python_object_to_cjson(val, vm));
});
return cjson_object;
}
static cJSON* convert_python_object_to_cjson(PyVar obj, VM* vm){
if(is_none(obj)) return cJSON_CreateNull();
Type obj_t = vm->_tp(obj);
switch(obj_t){
case VM::tp_int.index: cJSON_CreateNumber(_CAST(i64, obj));
case VM::tp_float.index: cJSON_CreateNumber(_CAST(f64, obj));
case VM::tp_bool.index: cJSON_CreateBool(obj == vm->True);
case VM::tp_str.index: cJSON_CreateString(_CAST(Str&, obj).c_str());
case VM::tp_dict.index: return covert_dict_to_cjson(_CAST(Dict&, obj), vm);
case VM::tp_list.index: return convert_list_to_cjson<List>(_CAST(List&, obj), vm);
case VM::tp_tuple.index: return convert_list_to_cjson<Tuple>(_CAST(Tuple&, obj), vm);
default: break;
}
vm->TypeError(_S("unrecognized type ", _type_name(vm, obj_t).escape()));
}
static PyVar convert_cjson_to_list(const cJSON * const item, VM* vm){
List output;
cJSON *element = item->child;
while(element != NULL){
output.push_back(convert_cjson_to_python_object(element, vm));
element = element->next;
}
return VAR(std::move(output));
}
static PyVar convert_cjson_to_dict(const cJSON* const item, VM* vm){
Dict output;
cJSON *child = item->child;
while(child != NULL){
const char* key = child->string;
const cJSON *child_value = cJSON_GetObjectItemCaseSensitive(item, key);
output.set(vm, VAR(key), convert_cjson_to_python_object(child_value, vm));
child = child->next;
}
return VAR(std::move(output));
}
static PyVar convert_cjson_to_python_object(const cJSON * const item, VM* vm)
{
if (cJSON_IsString(item))
{
return VAR(Str(item->valuestring));
}
else if (cJSON_IsNumber(item)){
if(item->valuedouble != item->valueint){
return VAR(item->valuedouble);
}
return VAR(item->valueint);
}
else if (cJSON_IsBool(item)){
return item->valueint!=0 ? vm->True : vm->False;
}
else if (cJSON_IsNull(item)){
return vm->None;
}
else if (cJSON_IsArray(item)){
return convert_cjson_to_list(item, vm);
}
else if (cJSON_IsObject(item)){
return convert_cjson_to_dict(item, vm);
}
return vm->None;
}
void add_module_cjson(VM* vm){
PyObject* mod = vm->new_module("cjson");
vm->bind_func(mod, "loads", 1, [](VM* vm, ArgsView args){
std::string_view sv;
if(is_type(args[0], vm->tp_bytes)){
const Bytes& b = PK_OBJ_GET(Bytes, args[0]);
sv = std::string_view((char*)b.data(), b.size());
}else{
sv = CAST(Str&, args[0]).sv();
}
cJSON *json = cJSON_ParseWithLength(sv.data(), sv.size());
if(json == NULL){
const char* start = cJSON_GetErrorPtr();
const char* end = start;
while(*end != '\0' && *end != '\n') end++;
vm->IOError(_S("cjson: ", std::string_view(start, end-start)));
}
PyVar output = convert_cjson_to_python_object(json, vm);
cJSON_Delete(json);
return output;
});
vm->bind_func(mod, "dumps", 1, [](VM* vm, ArgsView args) {
return VAR(vm->py_json(args[0]));
});
}
} // namespace pkpy