Skip to content

Commit

Permalink
Merge pull request #1516 from BaerMitUmlaut/json-hashmap-support
Browse files Browse the repository at this point in the history
  • Loading branch information
commy2 committed Dec 17, 2021
2 parents 1f780ae + efe845b commit 47b4092
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
14 changes: 14 additions & 0 deletions addons/hashes/fnc_encodeJSON.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Description:
- STRING
- TASK
- TEAM_MEMBER
- HASHMAP
- Everything else will simply be stringified.
Parameters:
Expand Down Expand Up @@ -77,6 +78,19 @@ switch (typeName _object) do {
};
};

case "HASHMAP": {
private _json = ((_object toArray false) apply {
_x params ["_key", ["_value", objNull]];

if !(_key isEqualType "") then {
_key = str _key;
};

format ["%1: %2", [_key] call CBA_fnc_encodeJSON, [_value] call CBA_fnc_encodeJSON]
}) joinString ", ";
"{" + _json + "}"
};

default {
if !(typeName _object in (supportInfo "u:allVariables*" apply {_x splitString " " select 1})) exitWith {
[str _object] call CBA_fnc_encodeJSON
Expand Down
45 changes: 31 additions & 14 deletions addons/hashes/fnc_parseJSON.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ Description:
Deserializes a JSON string.
Parameters:
_json - String containing valid JSON. <STRING>
_useHashes - Output CBA hashes instead of namespaces
(optional, default: false) <BOOLEAN>
_json - String containing valid JSON. <STRING>
_objectType - Selects the type used for deserializing objects (optional) <BOOLEAN or NUMBER>
0, false: CBA namespace (default)
1, true: CBA hash
2: Native hash map
Returns:
_object - The deserialized JSON object or nil if JSON is invalid.
<LOCATION, ARRAY, STRING, NUMBER, BOOL, NIL>
_object - The deserialized JSON object or nil if JSON is invalid.
<LOCATION, ARRAY, STRING, NUMBER, BOOL, HASHMAP, NIL>
Examples:
(begin example)
Expand All @@ -28,18 +30,33 @@ Author:
BaerMitUmlaut
---------------------------------------------------------------------------- */
SCRIPT(parseJSON);
params ["_json", ["_useHashes", false]];
params ["_json", ["_objectType", 0]];

// Wrappers for creating "objects" and setting values on them
private ["_objectSet", "_createObject"];
if (_useHashes) then {
_createObject = CBA_fnc_hashCreate;
_objectSet = CBA_fnc_hashSet;
} else {
_createObject = CBA_fnc_createNamespace;
_objectSet = {
params ["_obj", "_key", "_val"];
_obj setVariable [_key, _val];

switch (_objectType) do {
case false;
case 0: {
_createObject = CBA_fnc_createNamespace;
_objectSet = {
params ["_obj", "_key", "_val"];
_obj setVariable [_key, _val];
};
};

case true;
case 1: {
_createObject = CBA_fnc_hashCreate;
_objectSet = CBA_fnc_hashSet;
};

case 2: {
_createObject = { createHashMap };
_objectSet = {
params ["_obj", "_key", "_val"];
_obj set [_key, _val];
};
};
};

Expand Down
6 changes: 3 additions & 3 deletions addons/hashes/test_parseJSON.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,16 @@ private _testCases = [
];

{
private _useHashes = _x;
private _objectType = _x;

{
diag_log _x;
private _input = _x;
private _object = [_x, _useHashes] call CBA_fnc_parseJSON;
private _object = [_x, _objectType] call CBA_fnc_parseJSON;
private _output = [_object] call CBA_fnc_encodeJSON;
TEST_OP(_input,==,_output,_fn);
} forEach _testCases;
} forEach [true, false];
} forEach [true, false, 0, 1, 2];

// Special test for complex object because properties are unordered
private _json = "{""OBJECT"": null, ""BOOL"": true, ""SCALAR"": 1.2, ""STRING"": ""Hello, World!"", ""ARRAY"": [], ""LOCATION"": {}}";
Expand Down

0 comments on commit 47b4092

Please sign in to comment.