Skip to content

Commit d77649a

Browse files
committed
GRAL-2093: add JsonSerializer class
1 parent 8a32ff5 commit d77649a

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/Utils/CamelCaseHelper.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@ public static function keysToCamelCase($input)
1111
}
1212

1313
if (is_object($input)) {
14-
$className = get_class($input);
15-
$obj = new $className;
14+
// If the input object is not an instance of any SDK model then use the JsonSerializer class
15+
// so that json_encode() would use snake_case keys from the raw response.
16+
// Otherwise, use the mapped model class where the snake_case keys are assigned by generated code.
17+
if ($input instanceof \stdClass) {
18+
$obj = new JsonSerializer($input);
19+
} else {
20+
$className = get_class($input);
21+
$obj = new $className;
22+
}
1623
}
1724

1825
$arr = [];

src/Utils/JsonSerializer.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Pipedrive\Utils;
4+
5+
use JsonSerializable;
6+
7+
class JsonSerializer implements JsonSerializable
8+
{
9+
private $args;
10+
11+
public function __construct($input)
12+
{
13+
$this->args = $input;
14+
}
15+
16+
/**
17+
* Encode this object to JSON
18+
*/
19+
public function jsonSerialize()
20+
{
21+
return $this->args;
22+
}
23+
}

0 commit comments

Comments
 (0)