cJson


Index:
Articles
Classes
Hooks

Quick navigation:
cArrowEntity
cBeaconEntity
cBlockArea
cBlockEntity
cBlockEntityWithItems
cBlockInfo
cBoundingBox
cBrewingstandEntity
cChatColor
cChestEntity
cChunkDesc
cClientHandle
cCommandBlockEntity
cCompositeChat
cCraftingGrid
cCraftingRecipe
cCryptoHash
cCuboid
cDispenserEntity
cDropperEntity
cDropSpenserEntity
cEnchantments
cEntity
cEntityEffect
cExpBottleEntity
cFile
cFireChargeEntity
cFireworkEntity
cFloater
cFlowerPotEntity
cFurnaceEntity
cGhastFireballEntity
cHangingEntity
cHopperEntity
cIniFile
cInventory
cItem
cItemFrame
cItemGrid
cItems
cJson
cJukeboxEntity
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
cStatManager
cStringCompression
cTCPLink
cTeam
cThrownEggEntity
cThrownEnderPearlEntity
cThrownSnowballEntity
cTNTEntity
cTracer
cUDPEndpoint
cUrlClient
cUrlParser
cWebAdmin
cWindow
cWitherSkullEntity
cWorld
HTTPFormData
HTTPRequest
HTTPTemplateRequest
ItemCategory
lxp
sqlite3
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
Parses the Json in the input string into a Lua table. Returns nil and detailed error message if parsing fails.
Serialize
tabletable
optionstable
string
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 on 2017-03-27 09:00:06, Build ID Unknown, Commit approx: 96663d35b20b6d0cafdb132c920eceeff779fe5a