Scripting: JSON – Arma Reforger

From Bohemia Interactive Community
Jump to navigation Jump to search
(Add Serialisation link)
m (Fix link)
 
(8 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{TOC|side}}
{{TOC|side}}
{{Wiki|Stub}}
== JSON Format ==
== JSON Format ==


Line 6: Line 5:
{{Feature|informative|
{{Feature|informative|
See:
See:
* {{Wikipedia|JSON}} for a more informative Wikipedia article
* {{Link|https://en.wikipedia.org/wiki/JSON}} for a more informative Wikipedia article
* [https://datatracker.ietf.org/doc/html/rfc7159 RFC7159] for the RFC
* {{Link|https://datatracker.ietf.org/doc/html/rfc7159|RFC7159}} for the RFC
* [https://www.ecma-international.org/publications-and-standards/standards/ecma-404/ ECMA-404] for the standard
* {{Link|https://www.ecma-international.org/publications-and-standards/standards/ecma-404/|ECMA-404}} for the standard
* https://www.json.org/ for general details and libraries
* https://www.json.org/ for general details and libraries
* [https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/JSON Mozilla's article] for more information.
* [https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/JSON Mozilla's article] for more information.
Line 83: Line 82:
* {{hl|\\}}
* {{hl|\\}}
* {{hl|\/}}
* {{hl|\/}}
* {{hl|\u####}} - see {{Wikipedia|List of Unicode characters|Unicode chars}}
* {{hl|\u####}} - see {{Link|https://en.wikipedia.org/wiki/List_of_Unicode_characters|Unicode chars}}
}}
}}


Line 132: Line 131:




== Hydration ==
== See Also ==


{{Feature|quote|Hydration refers to the process of filling an object with data.||https://stackoverflow.com/questions/6991135/what-does-it-mean-to-hydrate-an-object}}
* {{Link|Arma Reforger:JsonApiStruct Usage}} - covers JSON serialisation, deserialisation, validation
 
* {{Link|Arma Reforger:Serialisation#JSON}}
* Properties must be named the same in JSON and in the Enforce Script object. They are '''case-sensitive'''!
* A missing property will not fail the JSON hydration.
 
 
== Example ==
 
=== JSON Structure ===
 
'''MainClass.json'''
<syntaxhighlight lang="json">
{
"m_sMessage": "Hello Message",
"m_subObject": {
"integerValue": 1,
"booleanValue": true
};
}
</syntaxhighlight>
 
 
=== Script Usage ===
 
<syntaxhighlight lang="c#">
class SecondaryJSONClass : JsonApiStruct
{
void SecondaryJSONClass() {} // Important, if you have a constructor it needs to take 0 parameters, as the engine will construct this class when you parse a json file.
 
int integerValue;
bool booleanValue;
}
 
class MainJSONClass : JsonApiStruct
{
protected string m_sMessage; // names need to match the JSON property name
protected SecondaryJSONClass m_subObject;
 
void Hydrate()
{
LoadFromFile("MainClass.json"); // loads JSON in the calling class
 
Print("m_sMessage = " + m_sMessage); // Prints "Hello Message"
Print("is sub-object null: " + (m_subObject == null));
if (m_subObject)
{
Print("sub-object/integerValue = " + m_subObject.integerValue);
Print("sub-object/booleanValue = " + m_subObject.booleanValue);
}
}
}
</syntaxhighlight>
 
 
== Serialisation ==
 
{{Feature|informative|See {{Link|Arma Reforger:Serialisation#JSON}}.}}




{{GameCategory|armaR|Modding|Guidelines|Scripting}}
{{GameCategory|armaR|Modding|Guidelines|Scripting}}

Latest revision as of 19:28, 30 May 2023

JSON Format

JSON stands for JavaScript Object Notation. It is a way to present data in a simple form.

See:

The parent class is always an object (the O from JSON).

{
}

A property is defined with its name on the left of the colon, and its value on the right. Properties and strings are defined by double-quotes ".
If another property follows, a comma is required. If no other property follows, there must be no comma.

There can be no comments in a JSON file.
{
	"property": 42
}
{
	"property1": 41,
	"property2": "fourty-two"
}

Spacing does not matter outside of the quotes.

{
	"property1": "name",
	"property2": 42
}

is identical to

{"property1":"name","property2":42}

but for obvious readability reasons, tabs and spaces are welcome.

Boolean

A boolean can be either true or false and nothing else.

{
	"boolean1": true,
	"boolean2": false
}

Number

A number can be either an integer or a float. There are no quotes around it. It cannot start with a period .. Scientific notations are accepted.

{
	"number1": 42,
	"number2": 4.2,
	"number3": -33,
	"number4": 10e10
}

String

Line returns cannot happen in JSON strings. The following character escape sequences are accepted:

{
	"string1": "This is a string",
	"string2": "Is \"this\" a string?\tYes, Sir\u0021"
}

Object

{
	"object1": {
		"property1": "sub-object"
	}
}

The null value is covered.

{
	"object1": null
}

Array

An array can contain anything: direct values, or other objects with named properties.

{
	"array1": [
		4,
		5,
		6
	],
	"array2": ["This", "is", "Sparta!"],
	"array3": [
		{
			"name": "object's name",
			"value": 33
		},
		{ "name": "another object, inlined", "value": 42 }
	]
}


See Also