Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
* some edits to the memory allocation logic
* calling exceptions is completely excluded
  • Loading branch information
rejchev committed Jul 19, 2023
1 parent 18078ae commit 93c30f5
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 495 deletions.
4 changes: 1 addition & 3 deletions extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ bool CJanssonExtension::SDK_OnLoad(char *error, size_t maxlength, bool late)
return false;
}

smutils->LogMessage(myself, "Add type: done");

return sharesys->AddInterface(myself, pJansson);
}

Expand All @@ -59,5 +57,5 @@ void CJanssonExtension::SDK_OnAllLoaded() {
}

void CJsonHandler::OnHandleDestroy(SourceMod::HandleType_t type, void *object) {
delete (nJansson::Json*) object;
pJansson->close(static_cast<nJansson::IJson *>(object));
}
2 changes: 1 addition & 1 deletion smsdk_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
/* Basic information exposed publicly */
#define SMEXT_CONF_NAME "Jansson-re"
#define SMEXT_CONF_DESCRIPTION "Provides JSON natives for plugins"
#define SMEXT_CONF_VERSION "1.6.0"
#define SMEXT_CONF_VERSION "1.6.1"
#define SMEXT_CONF_AUTHOR "rej.chev"
#define SMEXT_CONF_URL "https://discord.gg/cFZ97Mzrjy"
#define SMEXT_CONF_LOGTAG "Json"
Expand Down
7 changes: 7 additions & 0 deletions src/jansson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ using namespace nJansson;

IJson *Jansson::create(const char *str, const size_t& flags)
{
// because [] & {} len = 2
if(str == nullptr || strlen(str) < 2)
return nullptr;

return new Json(str, flags);
}

IJson *Jansson::create(FILE *input, const size_t &flags)
{
if(input == nullptr)
return nullptr;

return new Json(input, flags);
}

Expand Down
28 changes: 24 additions & 4 deletions src/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,21 @@ const char* Json::get()
}

IJson *Json::get(const char *key) const {
return new Json(get_t(key), JSON_ERROR_NULL, true);
json_t* pointer;

if((pointer = get_t(key)) == nullptr)
return nullptr;

return new Json(pointer, JSON_ERROR_NULL, true);
}

IJson *Json::get(const size_t &index) const {
return new Json(get_t(index), JSON_ERROR_NULL, true);
json_t* pointer;

if((pointer = get_t(index)) == nullptr)
return nullptr;

return new Json(pointer, JSON_ERROR_NULL, true);
}

bool Json::set(const char *key, const IJson *value) {
Expand Down Expand Up @@ -241,7 +251,12 @@ bool Json::extend(const IJsonArray *another) {
}

JsonType Json::type(const char *key) const {
return Json(get_t(key), JSON_ERROR_NULL).type();
json_t* pointer;

if((pointer = get_t(key)) == nullptr)
return jtInvalid;

return Json(pointer, JSON_ERROR_NULL).type();
}

json_t *Json::get_t(const char *key) const {
Expand All @@ -253,7 +268,12 @@ json_t *Json::get_t(const size_t &index) const {
}

JsonType Json::type(const size_t &index) const {
return Json(get_t(index), JSON_ERROR_NULL).type();
json_t* pointer;

if((pointer = get_t(index)) == nullptr)
return jtInvalid;

return Json(pointer, JSON_ERROR_NULL).type();
}

bool Json::exist(const char *key) const {
Expand Down
Loading

0 comments on commit 93c30f5

Please sign in to comment.