From fc7451204e9a71e448c02fded43ddb17eb020ab1 Mon Sep 17 00:00:00 2001 From: Hugo Locurcio Date: Fri, 31 Jul 2020 09:53:04 +0200 Subject: [PATCH] Improve JSON-related documentation This closes https://github.com/godotengine/godot-docs/issues/3848. (cherry picked from commit 930e10ffffa5f1117a8816cfd93a7acd4fa3942d) --- doc/classes/JSON.xml | 3 ++- doc/classes/JSONParseResult.xml | 10 ++++----- modules/gdscript/doc_classes/@GDScript.xml | 25 +++++++++++++--------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/doc/classes/JSON.xml b/doc/classes/JSON.xml index 005bbf99cc9..fa519152922 100644 --- a/doc/classes/JSON.xml +++ b/doc/classes/JSON.xml @@ -15,7 +15,7 @@ - Parses a JSON encoded string and returns a [JSONParseResult] containing the result. + Parses a JSON-encoded string and returns a [JSONParseResult] containing the result. @@ -29,6 +29,7 @@ Converts a [Variant] var to JSON text and returns the result. Useful for serializing data to store or send over the network. + [b]Note:[/b] The JSON specification does not define integer or float types, but only a [i]number[/i] type. Therefore, converting a Variant to JSON text will convert all numerical values to [float] types. diff --git a/doc/classes/JSONParseResult.xml b/doc/classes/JSONParseResult.xml index 70b99d0ded0..4f9f024a21b 100644 --- a/doc/classes/JSONParseResult.xml +++ b/doc/classes/JSONParseResult.xml @@ -15,21 +15,21 @@ The error type if the JSON source was not successfully parsed. See the [enum Error] constants. - The line number where the error occurred if JSON source was not successfully parsed. + The line number where the error occurred if the JSON source was not successfully parsed. - The error message if JSON source was not successfully parsed. See the [enum Error] constants. + The error message if the JSON source was not successfully parsed. See the [enum Error] constants. - A [Variant] containing the parsed JSON. Use [method @GDScript.typeof] or the [code]is[/code] keyword to check if it is what you expect. For example, if the JSON source starts with curly braces ([code]{}[/code]), a [Dictionary] will be returned. If the JSON source starts with braces ([code][][/code]), an [Array] will be returned. - [b]Note:[/b] The JSON specification does not define integer or float types, but only a number type. Therefore, parsing a JSON text will convert all numerical values to float types. + A [Variant] containing the parsed JSON. Use [method @GDScript.typeof] or the [code]is[/code] keyword to check if it is what you expect. For example, if the JSON source starts with curly braces ([code]{}[/code]), a [Dictionary] will be returned. If the JSON source starts with brackets ([code][][/code]), an [Array] will be returned. + [b]Note:[/b] The JSON specification does not define integer or float types, but only a [i]number[/i] type. Therefore, parsing a JSON text will convert all numerical values to [float] types. [b]Note:[/b] JSON objects do not preserve key order like Godot dictionaries, thus, you should not rely on keys being in a certain order if a dictionary is constructed from JSON. In contrast, JSON arrays retain the order of their elements: [codeblock] var p = JSON.parse('["hello", "world", "!"]') if typeof(p.result) == TYPE_ARRAY: print(p.result[0]) # Prints "hello" else: - print("unexpected results") + push_error("Unexpected results.") [/codeblock] diff --git a/modules/gdscript/doc_classes/@GDScript.xml b/modules/gdscript/doc_classes/@GDScript.xml index 40c609f65f4..ee95f81255c 100644 --- a/modules/gdscript/doc_classes/@GDScript.xml +++ b/modules/gdscript/doc_classes/@GDScript.xml @@ -734,16 +734,17 @@ - Parse JSON text to a Variant (use [method typeof] to check if it is what you expect). - Be aware that the JSON specification does not define integer or float types, but only a number type. Therefore, parsing a JSON text will convert all numerical values to [float] types. - Note that JSON objects do not preserve key order like Godot dictionaries, thus you should not rely on keys being in a certain order if a dictionary is constructed from JSON. In contrast, JSON arrays retain the order of their elements: + Parse JSON text to a Variant. (Use [method typeof] to check if the Variant's type is what you expect.) + [b]Note:[/b] The JSON specification does not define integer or float types, but only a [i]number[/i] type. Therefore, parsing a JSON text will convert all numerical values to [float] types. + [b]Note:[/b] JSON objects do not preserve key order like Godot dictionaries, thus, you should not rely on keys being in a certain order if a dictionary is constructed from JSON. In contrast, JSON arrays retain the order of their elements: [codeblock] - p = parse_json('["a", "b", "c"]') - if typeof(p) == TYPE_ARRAY: - print(p[0]) # Prints a + var p = JSON.parse('["hello", "world", "!"]') + if typeof(p.result) == TYPE_ARRAY: + print(p.result[0]) # Prints "hello" else: - print("unexpected results") + push_error("Unexpected results.") [/codeblock] + See also [JSON] for an alternative way to parse JSON text. @@ -1216,12 +1217,16 @@ - Converts a Variant [code]var[/code] to JSON text and return the result. Useful for serializing data to store or send over the network. + Converts a [Variant] [code]var[/code] to JSON text and return the result. Useful for serializing data to store or send over the network. [codeblock] + # Both numbers below are integers. a = { "a": 1, "b": 2 } b = to_json(a) print(b) # {"a":1, "b":2} + # Both numbers above are floats, even if they display without any decimal places. [/codeblock] + [b]Note:[/b] The JSON specification does not define integer or float types, but only a [i]number[/i] type. Therefore, converting a [Variant] to JSON text will convert all numerical values to [float] types. + See also [JSON] for an alternative way to convert a [Variant] to JSON text. @@ -1264,9 +1269,9 @@ j = to_json([1, 2, 3]) v = validate_json(j) if not v: - print("valid") + print("Valid JSON.") else: - prints("invalid", v) + push_error("Invalid JSON: " + v) [/codeblock]