return [
['{a: \'Apple\', \'b\': "Banana", c: [1, 2, 3]}', ['a' => 'Apple', 'b' => 'Banana', 'c' => [1, 2, 3]]],
['true', TRUE],
+ [' ', NULL],
['false', FALSE],
['null', NULL],
['"true"', 'true'],
["{ }", []],
[" [ ]", []],
[" [ 2 ]", [2]],
+ [
+ '{a: "parse error no closing bracket"',
+ NULL,
+ ],
[
'{a: ["foo", \'bar\'], "b": {a: [\'foo\', "bar"], b: {\'a\': ["foo", "bar"], b: {}}}}',
['a' => ['foo', 'bar'], 'b' => ['a' => ['foo', 'bar'], 'b' => ['a' => ['foo', 'bar'], 'b' => []]]],
$this->assertEquals($expectedOutput, CRM_Utils_JS::decode($input));
}
+ public static function encodeExamples() {
+ return [
+ [
+ ['a' => 'Apple', 'b' => 'Banana', 'c' => [0, -2, 3.15]],
+ "{a: 'Apple', b: 'Banana', c: [0, -2, 3.15]}",
+ ],
+ [
+ ['a' => ['foo', 'bar'], 'b' => ["'a'" => ['foo/bar&', 'bar(foo)'], 'b' => ['a' => ["fo'oo", '"bar"'], 'b' => []]]],
+ "{a: ['foo', 'bar'], b: {\"'a'\": ['foo/bar&', 'bar(foo)'], b: {a: ['fo\\'oo', '\"bar\"'], b: {}}}}",
+ ],
+ [TRUE, 'true'],
+ [' ', "' '"],
+ [FALSE, 'false'],
+ [NULL, 'null'],
+ ['true', "'true'"],
+ ['"false"', "'\"false\"'"],
+ ['0.5', "'0.5'"],
+ [0.5, '0.5'],
+ [[], "{}"],
+ ];
+ }
+
+ /**
+ * @param string $input
+ * @param string $expectedOutput
+ * @dataProvider encodeExamples
+ */
+ public function testEncode($input, $expectedOutput) {
+ $result = CRM_Utils_JS::encode($input);
+ $this->assertEquals($expectedOutput, $result);
+ $this->assertEquals($input, CRM_Utils_JS::decode($result));
+ }
+
/**
* @return array
*/