-
Notifications
You must be signed in to change notification settings - Fork 418
added JSON util to use elsewhere #657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,114 @@ | ||||||||||||||
| <?php | ||||||||||||||
|
|
||||||||||||||
| declare(strict_types=1); | ||||||||||||||
|
|
||||||||||||||
| namespace flight\util; | ||||||||||||||
|
|
||||||||||||||
| use Exception; | ||||||||||||||
| use JsonException; | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Json utility class for encoding and decoding JSON data. | ||||||||||||||
| * | ||||||||||||||
| * This class provides centralized JSON handling for the FlightPHP framework, | ||||||||||||||
| * with consistent error handling and default options. | ||||||||||||||
| */ | ||||||||||||||
| class Json | ||||||||||||||
| { | ||||||||||||||
| /** | ||||||||||||||
| * Default JSON encoding options | ||||||||||||||
| */ | ||||||||||||||
| public const DEFAULT_ENCODE_OPTIONS = JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR; | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Default JSON decoding options | ||||||||||||||
| */ | ||||||||||||||
| public const DEFAULT_DECODE_OPTIONS = JSON_THROW_ON_ERROR; | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Encodes data to JSON string. | ||||||||||||||
| * | ||||||||||||||
| * @param mixed $data Data to encode | ||||||||||||||
| * @param int $options JSON encoding options (bitmask) | ||||||||||||||
| * @param int $depth Maximum depth | ||||||||||||||
| * | ||||||||||||||
| * @return string JSON encoded string | ||||||||||||||
| * @throws Exception If encoding fails | ||||||||||||||
| */ | ||||||||||||||
| public static function encode($data, int $options = 0, int $depth = 512): string | ||||||||||||||
| { | ||||||||||||||
| $options = $options | self::DEFAULT_ENCODE_OPTIONS; // Ensure default options are applied | ||||||||||||||
| try { | ||||||||||||||
| return json_encode($data, $options, $depth); | ||||||||||||||
| } catch (JsonException $e) { | ||||||||||||||
| throw new Exception('JSON encoding failed: ' . $e->getMessage(), $e->getCode(), $e); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Decodes JSON string to PHP data. | ||||||||||||||
| * | ||||||||||||||
| * @param string $json JSON string to decode | ||||||||||||||
| * @param bool $associative Whether to return associative arrays instead of objects | ||||||||||||||
| * @param int $depth Maximum decoding depth | ||||||||||||||
| * @param int $options JSON decoding options (bitmask) | ||||||||||||||
| * | ||||||||||||||
| * @return mixed Decoded data | ||||||||||||||
| * @throws Exception If decoding fails | ||||||||||||||
| */ | ||||||||||||||
| public static function decode(string $json, bool $associative = false, int $depth = 512, int $options = 0) | ||||||||||||||
| { | ||||||||||||||
| $options = $options | self::DEFAULT_DECODE_OPTIONS; // Ensure default options are applied | ||||||||||||||
n0nag0n marked this conversation as resolved.
Show resolved
Hide resolved
n0nag0n marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
| try { | ||||||||||||||
| return json_decode($json, $associative, $depth, $options); | ||||||||||||||
| } catch (JsonException $e) { | ||||||||||||||
| throw new Exception('JSON decoding failed: ' . $e->getMessage(), $e->getCode(), $e); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Checks if a string is valid JSON. | ||||||||||||||
| * | ||||||||||||||
| * @param string $json String to validate | ||||||||||||||
| * | ||||||||||||||
| * @return bool True if valid JSON, false otherwise | ||||||||||||||
| */ | ||||||||||||||
| public static function isValid(string $json): bool | ||||||||||||||
| { | ||||||||||||||
| try { | ||||||||||||||
| json_decode($json, false, 512, JSON_THROW_ON_ERROR); | ||||||||||||||
| return true; | ||||||||||||||
| } catch (JsonException $e) { | ||||||||||||||
| return false; | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Gets the last JSON error message. | ||||||||||||||
| * | ||||||||||||||
| * @return string Error message or empty string if no error | ||||||||||||||
| */ | ||||||||||||||
| public static function getLastError(): string | ||||||||||||||
| { | ||||||||||||||
| $error = json_last_error(); | ||||||||||||||
| if ($error === JSON_ERROR_NONE) { | ||||||||||||||
| return ''; | ||||||||||||||
| } | ||||||||||||||
| return json_last_error_msg(); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Pretty prints JSON data. | ||||||||||||||
| * | ||||||||||||||
| * @param mixed $data Data to encode | ||||||||||||||
| * @param int $additionalOptions Additional options to merge with pretty print | ||||||||||||||
| * | ||||||||||||||
| * @return string Pretty formatted JSON string | ||||||||||||||
| * @throws Exception If encoding fails | ||||||||||||||
| */ | ||||||||||||||
| public static function prettyPrint($data, int $additionalOptions = 0): string | ||||||||||||||
| { | ||||||||||||||
| $options = self::DEFAULT_ENCODE_OPTIONS | JSON_PRETTY_PRINT | $additionalOptions; | ||||||||||||||
| return self::encode($data, $options); | ||||||||||||||
|
||||||||||||||
| return self::encode($data, $options); | |
| try { | |
| return json_encode($data, $options, 512); | |
| } catch (JsonException $e) { | |
| throw new Exception('JSON pretty print encoding failed: ' . $e->getMessage(), $e->getCode(), $e); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.