ToJSON: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (tweak syntax)
mNo edit summary
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
{{Server client function}}
{{Server client function}}
{{New feature/item|3.0120|1.2||
Available client side in 1.2 and onwards.
}}
This function converts a '''single''' value (preferably a Lua table) into a [[JSON]] encoded string. You can use this to store the data and then load it again using [[fromJSON]].
This function converts a '''single''' value (preferably a Lua table) into a [[JSON]] encoded string. You can use this to store the data and then load it again using [[fromJSON]].


Line 10: Line 6:
<!-- NOTE: don't use 'special' names for variable names, e.g. you shouldn't be writing things like 'player player, vehicle vehicle', instead write something like 'player thePlayer, vehicle vehicleToGetInto'. This is less confusing and prevents the syntax highlighting being odd -->
<!-- NOTE: don't use 'special' names for variable names, e.g. you shouldn't be writing things like 'player player, vehicle vehicle', instead write something like 'player thePlayer, vehicle vehicleToGetInto'. This is less confusing and prevents the syntax highlighting being odd -->
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
string toJSON ( var value, [ bool compact = false ][, string prettyType = "none" ] )
string toJSON ( var value [, bool compact = false ][, string prettyType = "none" ] )
</syntaxhighlight>
</syntaxhighlight>


Line 21: Line 17:
*'''compact:''' a [[boolean]] representing whether the string will contain whitespaces. To remove whitespaces from JSON string, use ''true''. String will contain whitespaces per default.
*'''compact:''' a [[boolean]] representing whether the string will contain whitespaces. To remove whitespaces from JSON string, use ''true''. String will contain whitespaces per default.
}}
}}
{{New feature/item|3.0160|1.6|8046|
{{New feature/item|3.0154|1.5.3|8046|
*'''prettyType:''' a type [[string]] from below:
*'''prettyType:''' a type [[string]] from below:
** none
** none

Revision as of 11:33, 25 September 2018

This function converts a single value (preferably a Lua table) into a JSON encoded string. You can use this to store the data and then load it again using fromJSON.

Syntax

string toJSON ( var value [, bool compact = false ][, string prettyType = "none" ] )

Required Arguments

  • var: An argument of any type. Arguments that are elements will be stored as element IDs that are liable to change between sessions. As such, do not save elements across sessions as you will get unpredictable results.

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • compact: a boolean representing whether the string will contain whitespaces. To remove whitespaces from JSON string, use true. String will contain whitespaces per default.
  • prettyType: a type string from below:
    • none
    • spaces
    • tabs

Returns

Returns a JSON formatted string.

Example

This example shows how you can encode an array. The string json should equal "[ { "1": "dogs", "mouse": "food", "cat": "hungry", "birds": 4 } ]" after executed.

local json = toJSON ( { "dogs", cat = "hungry", mouse = "food", birds = 4 } )

Requirements

Minimum server version 1.0
Minimum client version 1.1.1-9.03316

Note: Using this feature requires the resource to have the above minimum version declared in the meta.xml <min_mta_version> section. e.g. <min_mta_version server="1.0" client="1.1.1-9.03316" />

See Also