Garry's Mod Wiki

Revision Difference

util.TableToJSON#527729

<function name="TableToJSON" parent="util" type="libraryfunc"> <description> Converts a table to a JSON string. <warning>All integers will be converted to decimals (5 -&gt; 5.0).</warning> <warning>All keys are strings in the JSON format, so all keys will be converted to strings!</warning> <bug issue="3561">This will produce invalid JSON if the provided table contains nan or inf values.</bug> </description> <realm>Shared and Menu</realm> <args> <arg name="table" type="table">Table to convert.</arg> <arg name="prettyPrint" type="boolean" default="false">Format and indent the JSON.</arg> </args> <rets> <ret name="" type="string">A JSON formatted string containing the serialized data</ret> </rets> </function> <example> <description>Writes the positions and angles of every player to a txt document called playerstuff.txt</description> <code> local Players = {} for k, v in ipairs(ents.GetAll()) do⤶ if v:IsPlayer() then -- Is a player⤶ Players[k] = { pos = v:GetPos(), ang = v:GetAngles() }⤶ end⤶ for k, v in ipairs(player.GetAll()) do ⤶ Players[k] = { pos = v:GetPos(), ang = v:GetAngles() }⤶ end local tab = util.TableToJSON( Players ) -- Convert the player table to JSON file.CreateDir( "jsontest" ) -- Create the directory file.Write( "jsontest/playerstuff.txt", tab) -- Write to .txt </code> ⤶ </example></example>