JSON
Helper class for creating and parsing JSON data.
JSON.swift:30class JSON
The JSON
enables all data types to be converted to and from a JSON string. This useful for serializing data to save to a file or send over the network.
stringify(data:indent:sortKeys:fullPrecision:)
is used to convert any data type into a JSON string.
parse(jsonText:keepText:)
is used to convert any existing JSON data into a Variant
that can be used within Godot. If successfully parsed, use data
to retrieve the Variant
, and use typeof
to check if the Variant’s type is what you expect. JSON Objects are converted into a GDictionary
, but JSON data can be used to store GArray
s, numbers, String
s and even just a boolean.
Example
Alternatively, you can parse string using the static parseString(jsonString:)
method, but it doesn’t allow to handle errors.
Trailing commas in arrays or objects are ignored, instead of causing a parser error.
New line and tab characters are accepted in string literals, and are treated like their corresponding escape sequences
\n
and\t
.Numbers are parsed using
String/toFloat()
which is generally more lax than the JSON specification.Certain errors, such as invalid Unicode sequences, do not cause a parser error. Instead, the string is cleansed and an error is logged to the console.
Superclasses
class Resource
Base class for serializable objects.
Citizens in SwiftGodot
Conformances
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Equatable
A type that can be compared for value equality.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol Identifiable<ID>
A class of types whose instances hold the value of an entity with stable identity.
protocol VariantRepresentable
Types that conform to VariantRepresentable can be stored directly in
Variant
with no conversion. These include all of the Variant types from Godot (for exampleGString
,Rect
,Plane
), Godot objects (those that subclass SwiftGodot.Object) as well as the built-in Swift types UInt8, Int64 and Double.protocol VariantStorable
Types that conform to VariantStorable can be stored in a Variant and can be extracted back out of a Variant.
Type members
static func parseString(jsonString: String
) -> Variant Attempts to parse the
jsonString
provided and returns the parsed data. Returnsnull
if parse failed.static func stringify(data: Variant, indent: String, sortKeys: Bool, fullPrecision: Bool
) -> String Converts a
Variant
var to JSON text and returns the result. Useful for serializing data to store or send over the network.class var godotClassName: StringName
Instance members
var data: Variant
Contains the parsed JSON data in
Variant
form.func getErrorLine(
) -> Int32 Returns
0
if the last call toparse(jsonText:keepText:)
was successful, or the line number where the parse failed.func getErrorMessage(
) -> String Returns an empty string if the last call to
parse(jsonText:keepText:)
was successful, or the error message if it failed.func getParsedText(
) -> String Return the text parsed by
parse(jsonText:keepText:)
as long as the function is instructed to keep it.func parse(jsonText: String, keepText: Bool
) -> GodotError Attempts to parse the
jsonText
provided.