ToJSON: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(New page: Category:Private __NOTOC__ {{Server client function}}<!-- Change this to "Client function" or "Server function" appropriately--> <!-- Describe in plain english what this function does...)
 
(Remove obsolete Requirements section)
 
(32 intermediate revisions by 22 users not shown)
Line 1: Line 1:
[[Category:Private]]
__NOTOC__  
__NOTOC__  
{{Server client function}}<!-- Change this to "Client function" or "Server function" appropriately-->
{{Server client function}}
<!-- Describe in plain english what this function does. Don't go into details, just give an overview -->
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 fake function is for use with blah & blah and does blahblahblabhalbhl


{{Important Note|Due to technical limitations (partly of json-c) the stringified keys will be truncated to the first 255 characters}}
{{ Warning| When using [[toJSON]] for submitting data using [[fetchRemote]] for example, make sure to use '''string.sub(data, 2, -2)''' to remove the brackets as many APIs will not understand the request }}
==Syntax==  
==Syntax==  
<!-- 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">
returnType functionName ( arguments )
string toJSON ( var value [, bool compact = false ][, string prettyType = "none" ] )
</syntaxhighlight>  
</syntaxhighlight>


===Required Arguments===  
===Required Arguments===  
<!-- List each argument one per line. This should be the argument's name as in the argument list above, NOT the argument's data type -->
*'''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.
*'''argumentName:''' description


<!-- Only include this section below if there are optional arguments -->
===Optional Arguments===  
===Optional Arguments===  
{{OptionalArg}}  
{{OptionalArg}}  
*'''argumentName2:''' description
{{New feature/item|3.0150|1.5||
*'''argumentName3:''' description
*'''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.0154|1.5.3|8046|
*'''prettyType:''' a type [[string]] from below:
** none
** spaces
** tabs
}}


===Returns===
===Returns===
<!-- Make this descriptive. Explain what cases will return false. If you're unsure, add a tag to it so we can check -->
Returns a JSON formatted string.
Returns ''true'' if blah, ''false'' otherwise.


==Example==  
==Example==  
<!-- Explain what the example is in a single sentance -->
This example shows how you can encode an array. The string json should equal ''"[ { "1": "dogs", "mouse": "food", "cat": "hungry", "birds": 4 } ]" after executed.
This example does...
<!-- Add the code below, an emphasis should be on making it clear, not optimized. You could provide two versions if you wish, one clear and well commented, the other optimized -->
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
--This line does...
local json = toJSON ( { "dogs", cat = "hungry", mouse = "food", birds = 4 } )
blabhalbalhb --abababa
--This line does this...
mooo
</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See Also==
<!-- Change FunctionArea to the area that this function is in on the main function list page, e.g. Server, Player, Vehicle etc -->
{{Server_functions}}
{{FunctionArea_functions}}
 
[[Category:Needs_Example]] <!-- leave this until the example is completed. -->
[[ru:toJSON]]
[[Category:Need_Syntax]] <!-- leave this until syntax is available. Cannot document the function or event without syntax. -->
[[Category:Incomplete]] <!-- leave this unless you complete the function -->

Latest revision as of 15:32, 7 November 2024

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.


[[{{{image}}}|link=|]] Important Note: Due to technical limitations (partly of json-c) the stringified keys will be truncated to the first 255 characters
[[|link=|]] Warning: When using toJSON for submitting data using fetchRemote for example, make sure to use string.sub(data, 2, -2) to remove the brackets as many APIs will not understand the request

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 } )

See Also