Skip to content

Commit

Permalink
ver 1.0.4
Browse files Browse the repository at this point in the history
add more type check
add new native PtrGetIsNull
  • Loading branch information
ProjectSky committed Sep 23, 2024
1 parent 4a7d0b6 commit 467bc39
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pushbuild.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
0x0
0x1
0x2
0x2
0x4
31 changes: 31 additions & 0 deletions src/json_natives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,11 @@ static cell_t pawn_json_pointer_get_bool(IPluginContext *pContext, const cell_t
return BAD_HANDLE;
}

if (!yyjson_mut_is_bool(ptr)) {
pContext->ReportError("JSON value at path '%s' is not a boolean", path);
return 0;
}

return yyjson_mut_get_bool(ptr);
}

Expand Down Expand Up @@ -1358,12 +1363,37 @@ static cell_t pawn_json_pointer_get_string(IPluginContext *pContext, const cell_
pContext->ReportError("JSON pointer get error (%u): %s at position: %d", ptrGetError.code, ptrGetError.msg, ptrGetError.pos);
return BAD_HANDLE;
}

if (!yyjson_mut_is_str(ptr)) {
pContext->ReportError("JSON value at path '%s' is not a string", path);
return 0;
}

pContext->StringToLocalUTF8(params[3], params[4], yyjson_mut_get_str(ptr), NULL);

return 1;
}

static cell_t pawn_json_pointer_get_is_null(IPluginContext *pContext, const cell_t *params)
{
YYJsonWrapper *handle = g_JsonExtension.GetJSONPointer(pContext, params[1]);

if (!handle) return BAD_HANDLE;

char *path;
pContext->LocalToString(params[2], &path);

yyjson_ptr_err ptrGetError;
yyjson_mut_val *ptr = yyjson_mut_doc_ptr_getx(handle->m_pDocument_mut, path, strlen(path), NULL, &ptrGetError);

if (ptrGetError.code) {
pContext->ReportError("JSON pointer get error (%u): %s at position: %d", ptrGetError.code, ptrGetError.msg, ptrGetError.pos);
return BAD_HANDLE;
}

return yyjson_mut_is_null(ptr);
}

static cell_t pawn_json_pointer_set(IPluginContext *pContext, const cell_t *params)
{
YYJsonWrapper *handle1 = g_JsonExtension.GetJSONPointer(pContext, params[1]);
Expand Down Expand Up @@ -1769,6 +1799,7 @@ const sp_nativeinfo_t json_natives[] =
{"YYJSON.PtrGetInt", pawn_json_pointer_get_int},
{"YYJSON.PtrGetInt64", pawn_json_pointer_get_integer64},
{"YYJSON.PtrGetString", pawn_json_pointer_get_string},
{"YYJSON.PtrGetIsNull", pawn_json_pointer_get_is_null},
{"YYJSON.PtrSet", pawn_json_pointer_set},
{"YYJSON.PtrSetBool", pawn_json_pointer_set_bool},
{"YYJSON.PtrSetFloat", pawn_json_pointer_set_float},
Expand Down
2 changes: 1 addition & 1 deletion src/smsdk_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#define SMEXT_CONF_NAME "SourceMod YYJSON Extension"
#define SMEXT_CONF_DESCRIPTION "Provide JSON Native"
#define SMEXT_CONF_VERSION "1.0.3"
#define SMEXT_CONF_VERSION "1.0.4"
#define SMEXT_CONF_AUTHOR "ProjectSky"
#define SMEXT_CONF_URL "https://github.com/ProjectSky/sm-ext-yyjson"
#define SMEXT_CONF_LOGTAG "yyjson"
Expand Down

0 comments on commit 467bc39

Please sign in to comment.