diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..e027a5a7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/vendor +phpunit.xml +composer.lock diff --git a/README.md b/README.md index 1bf6b485..de9b2027 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,10 @@ Lendable JSON ============= +Provides an object oriented interface for handling json in php. + +Instead of failing silently, this lib will throw exceptions on json errors. + +## Installation +```bash +composer require lendable/json-serializer +``` diff --git a/composer.json b/composer.json new file mode 100644 index 00000000..5836a236 --- /dev/null +++ b/composer.json @@ -0,0 +1,28 @@ +{ + "name": "lendable/json-serializer", + "description": "JSON serializer/deserializer with an OOP interface", + "type": "library", + "authors": [ + { + "name": "Lendable Ltd", + "email": "dev@lendable.co.uk" + } + ], + "autoload": { + "psr-4": { + "Lendable\\Json\\": "lib/" + } + }, + "autoload-dev": { + "psr-4": { + "Tests\\Lendable\\Json\\Unit\\": "tests/unit/" + } + }, + "require": { + "php": ">=7.1", + "ext-json": "*" + }, + "require-dev": { + "phpunit/phpunit": "^7.4" + } +} diff --git a/lib/DeserializationFailed.php b/lib/DeserializationFailed.php new file mode 100644 index 00000000..91680b99 --- /dev/null +++ b/lib/DeserializationFailed.php @@ -0,0 +1,21 @@ + + + + + + + + + + ./tests/unit/ + + + + + + ./lib/ + + + + diff --git a/tests/unit/SerializerTest.php b/tests/unit/SerializerTest.php new file mode 100644 index 00000000..5c7fcbe4 --- /dev/null +++ b/tests/unit/SerializerTest.php @@ -0,0 +1,83 @@ +serializer->serialize(['foo' => 'bar', 'baz' => [1.03, true, 'foobar']]); + + $this->assertSame('{"foo":"bar","baz":[1.03,true,"foobar"]}', $result); + } + + /** + * @test + */ + public function it_throws_when_serializing_if_an_error_encountered(): void + { + $this->expectException(SerializationFailed::class); + $this->expectExceptionMessage('Failed to serialize data to JSON. Error code: 5, error message: Malformed UTF-8 characters, possibly incorrectly encoded.'); + + $this->serializer->serialize(["\xf0\x28\x8c\xbc" => 'bar']); + } + + /** + * @test + */ + public function it_can_deserialize_from_a_json_string_to_php_scalars(): void + { + $result = $this->serializer->deserialize('{"foo":"bar","baz":[1.03,true,"foobar"]}'); + + $this->assertSame(['foo' => 'bar', 'baz' => [1.03, true, 'foobar']], $result); + } + + /** + * @test + */ + public function it_throws_when_deserializing_if_an_error_encountered(): void + { + $this->expectException(DeserializationFailed::class); + $this->expectExceptionMessage('Failed to deserialize data from JSON. Error code: 4, error message: Syntax error.'); + + $this->serializer->deserialize('{"unclosed":"bad","object":"json"'); + } + + /** + * @test + */ + public function it_throws_when_deserializing_if_the_result_is_not_an_array(): void + { + $this->expectExceptionMessage(InvalidDeserializedData::class); + $this->expectExceptionMessage('Expected array when deserializing JSON, got "boolean".'); + + $this->serializer->deserialize('true'); + } + + protected function setUp(): void + { + $this->serializer = new Serializer(); + } +}