diff --git a/camunda/variables/tests/test_variables.py b/camunda/variables/tests/test_variables.py index 8b08577..af23fcd 100644 --- a/camunda/variables/tests/test_variables.py +++ b/camunda/variables/tests/test_variables.py @@ -51,3 +51,11 @@ def test_to_dict_returns_variables_as_dict(self): "var2": {"value": True}, "var3": {"value": "string"}}) self.assertDictEqual({"var1": 1, "var2": True, "var3": "string"}, variables.to_dict()) + + def test_json_returns_variables_as_json(self): + variables = {"list": ["a", "b", "c"], "obj": [{"z":2, "h":3}, {"t":5, "s":9}], "zobj": {"az": [0, 1, 2], "zot": [True, False, False]}} + formatted_vars = Variables.format(variables) + self.assertDictEqual({"list": {"type": "json", "value": "[\"a\", \"b\", \"c\"]"}, + "obj": {"type": "json", "value": "[{\"z\": 2, \"h\": 3}, {\"t\": 5, \"s\": 9}]"}, + "zobj": {"type": "json", "value": "{\"az\": [0, 1, 2], \"zot\": [true, false, false]}"}}, formatted_vars) + diff --git a/camunda/variables/variables.py b/camunda/variables/variables.py index 59e200b..46ea4d6 100644 --- a/camunda/variables/variables.py +++ b/camunda/variables/variables.py @@ -25,10 +25,13 @@ def format(cls, variables): """ formatted_vars = {} if variables: - formatted_vars = { - k: v if isinstance(v, dict) else {"value": v} - for k, v in variables.items() - } + for i in variables.keys(): + if type(variables[i]) in [bool, int, float, str]: + formatted_vars[i] = {"value": variables[i]} + elif type(variables[i]) == dict and "value" in variables[i] and type(variables[i]['value']) in [bool, int, float, str]: + formatted_vars[i] = variables[i] + else: + formatted_vars[i] = {"value": json.dumps(variables[i]), "type": "json"} return formatted_vars def to_dict(self): @@ -41,5 +44,8 @@ def to_dict(self): """ result = {} for k, v in self.variables.items(): - result[k] = v["value"] + if 'type' in v and v['type'] == "Json": + result[k] = json.loads(v["value"]) + else: + result[k] = v["value"] return result