From 122d824a8a6dab8025a2fe02045c8d4dd843fc75 Mon Sep 17 00:00:00 2001
From: Mattes D
Date: Wed, 23 Dec 2015 11:08:39 +0100
Subject: Added a Json parser and serializer to Lua API.
---
Server/Plugins/APIDump/APIDesc.lua | 152 +++++++++++++++++++++++----------
Server/Plugins/Debuggers/Debuggers.lua | 28 ++++++
Server/Plugins/Debuggers/Info.lua | 8 +-
3 files changed, 140 insertions(+), 48 deletions(-)
(limited to 'Server/Plugins')
diff --git a/Server/Plugins/APIDump/APIDesc.lua b/Server/Plugins/APIDump/APIDesc.lua
index be9f60662..6be3795a8 100644
--- a/Server/Plugins/APIDump/APIDesc.lua
+++ b/Server/Plugins/APIDump/APIDesc.lua
@@ -1372,53 +1372,6 @@ local Item5 = cItem(E_ITEM_DIAMOND_CHESTPLATE, 1, 0, "thorns=1;unbreaking=3");
},
}, -- cItem
- cObjective =
- {
- Desc = [[
- This class represents a single scoreboard objective.
- ]],
- Functions =
- {
- AddScore = { Params = "string, number", Return = "Score", Notes = "Adds a value to the score of the specified player and returns the new value." },
- GetDisplayName = { Params = "", Return = "string", Notes = "Returns the display name of the objective. This name will be shown to the connected players." },
- GetName = { Params = "", Return = "string", Notes = "Returns the internal name of the objective." },
- GetScore = { Params = "string", Return = "Score", Notes = "Returns the score of the specified player." },
- GetType = { Params = "", Return = "eType", Notes = "Returns the type of the objective. (i.e what is being tracked)" },
- Reset = { Params = "", Return = "", Notes = "Resets the scores of the tracked players." },
- ResetScore = { Params = "string", Return = "", Notes = "Reset the score of the specified player." },
- SetDisplayName = { Params = "string", Return = "", Notes = "Sets the display name of the objective." },
- SetScore = { Params = "string, Score", Return = "", Notes = "Sets the score of the specified player." },
- SubScore = { Params = "string, number", Return = "Score", Notes = "Subtracts a value from the score of the specified player and returns the new value." },
- },
- Constants =
- {
- otAchievement = { Notes = "" },
- otDeathCount = { Notes = "" },
- otDummy = { Notes = "" },
- otHealth = { Notes = "" },
- otPlayerKillCount = { Notes = "" },
- otStat = { Notes = "" },
- otStatBlockMine = { Notes = "" },
- otStatEntityKill = { Notes = "" },
- otStatEntityKilledBy = { Notes = "" },
- otStatItemBreak = { Notes = "" },
- otStatItemCraft = { Notes = "" },
- otStatItemUse = { Notes = "" },
- otTotalKillCount = { Notes = "" },
- },
- }, -- cObjective
-
- cPainting =
- {
- Desc = "This class represents a painting in the world. These paintings are special and different from Vanilla in that they can be critical-hit.",
- Functions =
- {
- GetDirection = { Params = "", Return = "number", Notes = "Returns the direction the painting faces. Directions: ZP - 0, ZM - 2, XM - 1, XP - 3. Note that these are not the BLOCK_FACE constants." },
- GetName = { Params = "", Return = "string", Notes = "Returns the name of the painting" },
- },
-
- }, -- cPainting
-
cItemGrid =
{
Desc = [[This class represents a 2D array of items. It is used as the underlying storage and API for all cases that use a grid of items:
@@ -1556,6 +1509,65 @@ end
},
}, -- cItems
+ cJson =
+ {
+ Desc = [[
+ Exposes the Json parser and serializer available in the server. Plugins can parse Json strings into
+ Lua tables, and serialize Lua tables into Json strings easily.
+ ]],
+ Functions =
+ {
+ Parse = { Params = "string", Return = "table", Notes = "Parses the Json in the input string into a Lua table. Returns nil and detailed error message if parsing fails." },
+ Serialize = { Params = "table, [options]", Return = "string", Notes = "Serializes the input table into a Json string. The options table, if present, is used to adjust the formatting of the serialized string, see below for details." },
+ },
+ AdditionalInfo =
+ {
+ {
+ Header = "Serializer options",
+ Contents = [[
+ The "options" parameter given to the cJson:Serialize() function is a dictionary-table of "option
+ name" -> "option value". The serializer warns if any unknown options are used; the following
+ options are recognized:
+
+ - commentStyle - either "All" or "None", specifies whether comments are written to the
+ output. Currently unused since comments cannot be represented in a Lua table
+ - indentation - the string that is repeated for each level of indentation of the output.
+ If empty, the Json is compressed (without linebreaks).
+ - enableYAMLCompatibility - bool manipulating the whitespace around the colons.
+ - dropNullPlaceholders - bool specifying whether null placeholders should be dropped
+ from the output
+
+ ]],
+ },
+ {
+ Header = "Code example: Parsing a Json string",
+ Contents = [==[
+ The following code, adapted from the Debuggers plugin, parses a simple Json string and verifies
+ the results:
+
+local t1 = cJson:Parse([[{"a": 1, "b": "2", "c": [3, "4", 5]}]])
+assert(t1.a == 1)
+assert(t1.b == "2")
+assert(t1.c[1] == 3)
+assert(t1.c[2] == "4")
+assert(t1.c[3] == 5)
+
+ ]==],
+ },
+ {
+ Header = "Code example: Serializing into a Json string",
+ Contents = [==[
+ The following code, adapted from the Debuggers plugin, serializes a simple Lua table into a
+ string, using custom indentation:
+
+local s1 = cJson:Serialize({a = 1, b = "2", c = {3, "4", 5}}, {indentation = " "})
+LOG("Serialization result: " .. (s1 or ""))
+
+ ]==],
+ },
+ },
+ }, -- cJson
+
cLuaWindow =
{
Desc = [[This class is used by plugins wishing to display a custom window to the player, unrelated to block entities or entities near the player. The window can be of any type and have any contents that the plugin defines. Callbacks for when the player modifies the window contents and when the player closes the window can be set.
@@ -1813,6 +1825,52 @@ a_Player:OpenWindow(Window);
Inherits = "cPawn",
}, -- cMonster
+ cObjective =
+ {
+ Desc = [[
+ This class represents a single scoreboard objective.
+ ]],
+ Functions =
+ {
+ AddScore = { Params = "string, number", Return = "Score", Notes = "Adds a value to the score of the specified player and returns the new value." },
+ GetDisplayName = { Params = "", Return = "string", Notes = "Returns the display name of the objective. This name will be shown to the connected players." },
+ GetName = { Params = "", Return = "string", Notes = "Returns the internal name of the objective." },
+ GetScore = { Params = "string", Return = "Score", Notes = "Returns the score of the specified player." },
+ GetType = { Params = "", Return = "eType", Notes = "Returns the type of the objective. (i.e what is being tracked)" },
+ Reset = { Params = "", Return = "", Notes = "Resets the scores of the tracked players." },
+ ResetScore = { Params = "string", Return = "", Notes = "Reset the score of the specified player." },
+ SetDisplayName = { Params = "string", Return = "", Notes = "Sets the display name of the objective." },
+ SetScore = { Params = "string, Score", Return = "", Notes = "Sets the score of the specified player." },
+ SubScore = { Params = "string, number", Return = "Score", Notes = "Subtracts a value from the score of the specified player and returns the new value." },
+ },
+ Constants =
+ {
+ otAchievement = { Notes = "" },
+ otDeathCount = { Notes = "" },
+ otDummy = { Notes = "" },
+ otHealth = { Notes = "" },
+ otPlayerKillCount = { Notes = "" },
+ otStat = { Notes = "" },
+ otStatBlockMine = { Notes = "" },
+ otStatEntityKill = { Notes = "" },
+ otStatEntityKilledBy = { Notes = "" },
+ otStatItemBreak = { Notes = "" },
+ otStatItemCraft = { Notes = "" },
+ otStatItemUse = { Notes = "" },
+ otTotalKillCount = { Notes = "" },
+ },
+ }, -- cObjective
+
+ cPainting =
+ {
+ Desc = "This class represents a painting in the world. These paintings are special and different from Vanilla in that they can be critical-hit.",
+ Functions =
+ {
+ GetDirection = { Params = "", Return = "number", Notes = "Returns the direction the painting faces. Directions: ZP - 0, ZM - 2, XM - 1, XP - 3. Note that these are not the BLOCK_FACE constants." },
+ GetName = { Params = "", Return = "string", Notes = "Returns the name of the painting" },
+ },
+ }, -- cPainting
+
cPawn =
{
Desc = [[cPawn is a controllable pawn object, controlled by either AI or a player. cPawn inherits all functions and members of {{cEntity}}
diff --git a/Server/Plugins/Debuggers/Debuggers.lua b/Server/Plugins/Debuggers/Debuggers.lua
index 2b80e15c8..0559a4ef8 100644
--- a/Server/Plugins/Debuggers/Debuggers.lua
+++ b/Server/Plugins/Debuggers/Debuggers.lua
@@ -1921,6 +1921,34 @@ end
+function HandleConsoleTestJson(a_Split, a_EntireCmd)
+ LOG("Testing Json parsing...")
+ local t1 = cJson:Parse([[{"a": 1, "b": "2", "c": [3, "4", 5] }]])
+ assert(t1.a == 1)
+ assert(t1.b == "2")
+ assert(t1.c[1] == 3)
+ assert(t1.c[2] == "4")
+ assert(t1.c[3] == 5)
+
+ local t2, msg = cJson:Parse([[{"some": invalid, json}]])
+ assert(t2 == nil)
+ assert(type(msg) == "string")
+ LOG("Error message returned: " .. msg)
+
+ LOG("Json parsing test succeeded")
+
+ LOG("Testing Json serializing...")
+ local s1 = cJson:Serialize({a = 1, b = "2", c = {3, "4", 5}}, {indentation = " "})
+ LOG("Serialization result: " .. (s1 or ""))
+ LOG("Json serializing test succeeded")
+
+ return true
+end
+
+
+
+
+
function HandleConsoleTestTracer(a_Split, a_EntireCmd)
-- Check required params:
if not(a_Split[7]) then
diff --git a/Server/Plugins/Debuggers/Info.lua b/Server/Plugins/Debuggers/Info.lua
index 8f5ef27df..f71ee5509 100644
--- a/Server/Plugins/Debuggers/Info.lua
+++ b/Server/Plugins/Debuggers/Info.lua
@@ -242,11 +242,17 @@ g_PluginInfo =
HelpString = "Tests the world scheduling",
},
+ ["testjson"] =
+ {
+ Handler = HandleConsoleTestJson,
+ HelpString = "Tests the cJson parser and serializer",
+ },
+
["testtracer"] =
{
Handler = HandleConsoleTestTracer,
HelpString = "Tests the cLineBlockTracer",
- }
+ },
}, -- ConsoleCommands
} -- g_PluginInfo
--
cgit v1.2.3