cJson


Index:
Articles
Classes
Hooks

Quick navigation:
BannerPattern
BossBarColor
BossBarDivisionType
cArrowEntity
cBeaconEntity
cBedEntity
cBlockArea
cBlockEntity
cBlockEntityWithItems
cBlockInfo
cBoat
cBoundingBox
cBrewingstandEntity
cChatColor
cChestEntity
cChunkDesc
cClientHandle
cColor
cCommandBlockEntity
cCompositeChat
cCraftingGrid
cCraftingRecipe
cCryptoHash
cCuboid
cDispenserEntity
cDropperEntity
cDropSpenserEntity
cEnchantments
cEnderCrystal
cEntity
cEntityEffect
cExpBottleEntity
cExpOrb
cFallingBlock
cFile
cFireChargeEntity
cFireworkEntity
cFloater
cFlowerPotEntity
cFurnaceEntity
cGhastFireballEntity
cHangingEntity
cHopperEntity
cIniFile
cInventory
cItem
cItemFrame
cItemGrid
cItems
cJson
cJukeboxEntity
cLeashKnot
cLineBlockTracer
cLuaWindow
cMap
cMapManager
cMobHeadEntity
cMobSpawnerEntity
cMojangAPI
cMonster
cNetwork
cNoteEntity
cObjective
cPainting
cPawn
cPickup
cPlayer
cPlugin
cPluginLua
cPluginManager
cProjectileEntity
cRankManager
cRoot
cScoreboard
cServer
cServerHandle
cSignEntity
cSplashPotionEntity
cStringCompression
cTCPLink
cTeam
cThrownEggEntity
cThrownEnderPearlEntity
cThrownSnowballEntity
cTNTEntity
cUDPEndpoint
cUrlClient
cUrlParser
CustomStatistic
cUUID
cWebAdmin
cWindow
cWitherSkullEntity
cWorld
EffectID
HTTPFormData
HTTPRequest
HTTPTemplateRequest
ItemCategory
lxp
SmokeDirection
sqlite3
StatisticsManager
TakeDamageInfo
tolua
Vector3d
Vector3f
Vector3i
Globals

Contents


cJson class

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

NameParametersReturn valueNotes
Parse
InputStringstring
table
(STATIC) Parses the Json in the input string into a Lua table. Returns nil and detailed error message if parsing fails.
Serialize
tabletable
optionstable
string
(STATIC) 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.
Returns nil and error message if the table cannot be serialized (eg. contains both an array part and a dictionary part).

Serializer options

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

Code example: Parsing a Json string

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)

Code example: Serializing into a Json string

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 ""))
Generated by APIDump on 2022-10-28 17:00:47, Build ID Unknown, Commit approx: 21ec3ebe26bff24b5fc6d96f86a441c9c9628247