Skip to content

Commit f5a2254

Browse files
committed
feat(scripting/cpp/utils): JSON API
1 parent 3744724 commit f5a2254

File tree

8 files changed

+139
-0
lines changed

8 files changed

+139
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "JSON",
3+
"position": 1,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "A list of API functions for Swiftly JSON Utils."
7+
}
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Create
2+
3+
This function create a JSON Object.
4+
5+
### Syntax
6+
7+
```cpp
8+
@returns JSONObject*
9+
json->Create()
10+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
sidebar_position: 1
3+
---
4+
5+
# Getting started
6+
7+
To use the Swiftly JSON system you need the following code in your plugin.
8+
9+
```cpp
10+
JSON *json = nullptr;
11+
12+
void OnProgramLoad(const char *pluginName, const char *mainFilePath)
13+
{
14+
...
15+
json = new JSON();
16+
...
17+
}
18+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "JSON Object",
3+
"position": 1,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "A list of API functions for Swiftly JSON Object."
7+
}
8+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# API
2+
3+
To access the JSON Object you need to use the following syntax:
4+
5+
```cpp
6+
@returns rapidjson::Document
7+
jsonobject->document
8+
```
9+
10+
To use the JSON API you need to use the RapidJSON's API.
11+
12+
This can be found at the following URL: [https://rapidjson.org](https://rapidjson.org)
13+
14+
### Useful links
15+
16+
- [RapidJSON Features](https://rapidjson.org/md_doc_features.html)
17+
- [RapidJSON Tutorial](https://rapidjson.org/md_doc_tutorial.html)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Parse
2+
3+
This function parses a JSON string and creates an object from it. If it fails it prints a message in console.
4+
5+
### Syntax
6+
7+
```cpp
8+
@returns JSONObject*
9+
json->Parse(std::string jsonstr)
10+
```

plugin_files/scripting/includes/swiftly/swiftly_scripting_utils.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,27 @@ int GetPlayerId(const char *str, bool matchbots)
2626
void CreateThread(std::function<void()> fn)
2727
{
2828
std::thread(fn).detach();
29+
}
30+
31+
bool JSONObject::Parse(std::string str)
32+
{
33+
document.Parse(str.c_str());
34+
return (document.HasParseError());
35+
}
36+
37+
const char *JSONObject::GetParseError()
38+
{
39+
if (document.HasParseError())
40+
return GetParseError_En(document.GetParseError());
41+
else
42+
return "";
43+
}
44+
45+
const char *JSONObject::Encode()
46+
{
47+
rapidjson::StringBuffer buffer;
48+
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
49+
document.Accept(writer);
50+
const char *encoded = buffer.GetString();
51+
return encoded;
2952
}

plugin_files/scripting/includes/swiftly/swiftly_scripting_utils.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,53 @@
66

77
#include <functional>
88
#include <thread>
9+
#include <string>
10+
11+
#include <rapidjson/document.h>
12+
#include <rapidjson/writer.h>
13+
#include <rapidjson/stringbuffer.h>
14+
#include <rapidjson/error/en.h>
915

1016
int GetPlayerId(const char *str, bool matchbots = false);
1117
void CreateThread(std::function<void()> fn);
1218

19+
class JSONObject
20+
{
21+
public:
22+
rapidjson::Document document;
23+
24+
JSONObject() {}
25+
~JSONObject()
26+
{
27+
document.Clear();
28+
}
29+
30+
bool Parse(std::string str);
31+
const char *GetParseError();
32+
const char *Encode();
33+
};
34+
35+
class JSON
36+
{
37+
private:
38+
public:
39+
JSON() {}
40+
41+
JSONObject *Create()
42+
{
43+
return (new JSONObject());
44+
}
45+
46+
JSONObject *Parse(std::string jsonstr)
47+
{
48+
JSONObject *obj = new JSONObject();
49+
if (obj->Parse(jsonstr))
50+
print("[Swiftly] An error has occured while trying to parse JSON.\n[Swiftly] Error: %s\n", obj->GetParseError());
51+
52+
return obj;
53+
}
54+
};
55+
56+
extern JSON *json;
57+
1358
#endif

0 commit comments

Comments
 (0)