Skip to content

Commit d4abdeb

Browse files
committed
FIX: Upd json conversion
1 parent 545ebd2 commit d4abdeb

4 files changed

Lines changed: 16 additions & 4 deletions

File tree

HlebBootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function __construct(?string $publicPath = null, array $config = [], ?Log
109109

110110
// The current version of the framework.
111111
// Текущая версия фреймворка.
112-
\defined('HLEB_CORE_VERSION') or \define('HLEB_CORE_VERSION', '2.1.19');
112+
\defined('HLEB_CORE_VERSION') or \define('HLEB_CORE_VERSION', '2.1.20');
113113

114114
$this->logger = $logger;
115115

HttpMethods/Specifier/DataType.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,17 @@ public function asPositiveFloat(int $precision = 5, int $mode = PHP_ROUND_HALF_U
166166
* The value of $default is assumed to be a safe value.
167167
* If there is no value, you can also set the exception class $exc, which is called
168168
* instead of the default value; if true, the standard error class will be called.
169+
* (!) Is not suitable for producing a valid JSON string, as it escapes consecutive double quotes.
170+
* To get the original JSON string, you can use value(), and to convert it
171+
* to an array with escaped values, use asArray().
169172
*
170173
* Возвращает значение приведенное к типу `string`, если значение не существует - возвращает $default.
171174
* Предполагается, что значением $default является безопасное значение.
172175
* При отсутствии значения также можно задать класс исключения $exc, вызываемый вместо
173176
* дефолтного значения; при true будет вызван стандартный класс ошибки.
177+
* (!) Не подходит для получения валидной JSON-строки, так как экранирует двойные кавычки подряд.
178+
* Для получения оригинальной JSON-строки можно использовать value(),
179+
* а преобразованный в массив c экранированными значениями - asArray().
174180
*/
175181
public function asString(string|null $default = null, bool|string $exc = false): string|null
176182
{
@@ -238,7 +244,7 @@ public function asArray(array $default = [], bool|string $exc = false): array
238244
(\str_starts_with(\ltrim($this->value), '{') || \str_starts_with(\ltrim($this->value), '['))
239245
) {
240246
try {
241-
$value = \json_decode(\trim($value), true, JSON_THROW_ON_ERROR);
247+
$value = \json_decode(\trim($value), true, 512, JSON_THROW_ON_ERROR);
242248
} catch (\JsonException) {
243249
}
244250
if (!\is_array($value)) {

Init/Review/basic.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ function env_array(string $name, #[SensitiveParameter] array $default): array
210210
return $default;
211211
}
212212
if (str_starts_with($env, '{') && str_ends_with($env,'}')) {
213-
return json_decode($env, true, JSON_THROW_ON_ERROR);
213+
return json_decode($env, true, 512, JSON_THROW_ON_ERROR);
214214
}
215215
throw new RuntimeException("The value of the environment variable `{$name}` is expected to be an JSON string!");
216216
}

Main/Console/Specifiers/LightDataType.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,17 @@ public function asPositiveFloat(int $precision = 5, int $mode = PHP_ROUND_HALF_U
152152
* The value of $default is assumed to be a safe value.
153153
* If there is no value, you can also set the exception class $exc, which is called
154154
* instead of the default value; if true, the standard error class will be called.
155+
* (!) Is not suitable for producing a valid JSON string, as it escapes consecutive double quotes.
156+
* To get the original JSON string, you can use value(), and to convert it
157+
* to an array with escaped values, use asArray().
155158
*
156159
* Возвращает значение приведенное к типу `string`, если значение не существует - возвращает $default.
157160
* Предполагается, что значением $default является безопасное значение.
158161
* При отсутствии значения также можно задать класс исключения $exc, вызываемый вместо
159162
* дефолтного значения; при true будет вызван стандартный класс ошибки.
163+
* (!) Не подходит для получения валидной JSON-строки, так как экранирует двойные кавычки подряд.
164+
* Для получения оригинальной JSON-строки можно использовать value(),
165+
* а преобразованный в массив c экранированными значениями - asArray().
160166
*/
161167
public function asString(string|null $default = null, bool|string $exc = false): string|null
162168
{
@@ -219,7 +225,7 @@ public function asArray(array $default = [], bool|string $exc = false): array
219225
) {
220226
$value = $this->value;
221227
try {
222-
$value = \json_decode(\trim($value), true, JSON_THROW_ON_ERROR);
228+
$value = \json_decode(\trim($value), true, 512, JSON_THROW_ON_ERROR);
223229
} catch (\JsonException) {
224230
}
225231
if (!\is_array($value)) {

0 commit comments

Comments
 (0)