Skip to content

Commit 50bb221

Browse files
committed
Add readme and license
1 parent e22b146 commit 50bb221

3 files changed

Lines changed: 78 additions & 1 deletion

File tree

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
MIT License
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# JSONParser
2+
## About
3+
Thin wrapper around `json_decode` and `json_encode` with exceptions and some handy methods.
4+
5+
## Requirements
6+
PHP 7.4^
7+
8+
## Installation
9+
10+
You can install the package via composer:
11+
12+
```bash
13+
composer require dnonov/json-parser
14+
```
15+
16+
## Description
17+
This mainly exists because of the silent `json_decode`; if something's wrong it
18+
will return `null`. I prefer exception. I prefer nice method names as well.
19+
It is very thin wrapper around three functions in PHP standard library, but I'm
20+
tired of re-writing it every time.
21+
22+
### Here's how to use it
23+
```php
24+
use Dnonov\JsonParser\Facades\JSONParser;
25+
26+
$arrayData = JSONParser::decodeToArray("[{"one": 1, "two": 2}]");
27+
$objectData = JSONParser::decodeToObject("[{"one": 1, "two": 2}]");
28+
29+
$array = ["one" => 1, "two" => 2];
30+
$encodedArrayData = JSONParser::encode($array);
31+
32+
// Make sure file path is relative to `base_path()`.
33+
$arrayFileData = JSONParser::decodeFromFileToArray("./data.json");
34+
$objectFileData = JSONParser::decodeFromFileToObject("./data.json");
35+
36+
// There are some handy methods.
37+
$object = JSONParser::arrayToObject(["one" => 1, "two" => 2]);
38+
$array = JSONParser::objectToArray($object);
39+
```
40+
41+
### Various exceptions might be thrown during decoding and encoding.
42+
* JsonMaxDepthException
43+
* JsonInvalidOrMalformedException
44+
* JsonControlCharacterException
45+
* JsonSyntaxErrorException
46+
* JsonMalformedUTF8Exception
47+
* JsonRecursiveReferenceException
48+
* JsonInfinityOrNanDetectedException
49+
* JsonUnsupportedTypeException
50+
* JsonException
51+
* InvalidArgumentExceptions
52+
53+
Here's documentation on the equivalent errors in [PHP Doc](https://www.php.net/manual/en/json.constants.php#constant.json-throw-on-error)
54+
55+
## Contributing
56+
Bug reports and pull requests are always welcome.
57+
58+
## License
59+
The MIT License (MIT). Please see [License File](LICENSE) for more information.

src/JSONParser.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Dnonov\JsonParser\Exceptions\JsonMalformedUTF8Exception;
1111
use Dnonov\JsonParser\Exceptions\JsonMaxDepthException;
1212
use Dnonov\JsonParser\Exceptions\JsonRecursiveReferenceException;
13-
use Dnonov\JsonParser\Exceptions\JsonStateMismatchException;
1413
use Dnonov\JsonParser\Exceptions\JsonSyntaxErrorException;
1514
use Dnonov\JsonParser\Exceptions\JsonUnsupportedTypeException;
1615

0 commit comments

Comments
 (0)