PHP Classes
elePHPant
Icontem

PHP JSON Parser Class: Encode and decode data in JSON format

Recommend this page to a friend!
  Info   View files Example   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum (11)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2019-03-03 (21 days ago) RSS 2.0 feedStarStarStarStar 71%Total: 88 This week: 3All time: 9,230 This week: 152Up
Version License PHP version Categories
jsonparser 1.0Custom (specified...5PHP 5, Data types, Parsers
Description Author

This package can encode and decode data in JSON format.

I provides a pure PHP solution to take a value and generates a string that represents that value in JSON format.

The package can also take a JSON encoded string and decodes it to retrieve the original data value.

Innovation Award
PHP Programming Innovation award nominee
February 2019
Nominee
Vote
PHP comes with built-in support to parse and encode data in the JSON format but it provides limited support to determine if a data JSON string value is well encoded.

This package implements a JSON parse in pure PHP in such a way that is possible to determine more details about errors of a JSON string that is malformed, thus helping developers to validate JSON strings and give the precise location of any errors.

Manuel Lemos
  Performance   Level  
Name: Max Base <contact>
Classes: 2 packages by
Country: Canada Canada
Age: ???
All time rank: 409390 in Canada Canada
Week rank: 75 Up1 in Canada Canada Up
Innovation award
Innovation award
Nominee: 1x

Details

JsonParser

Tiny Library for parse JSON.

JsonParser.min.php File Size JsonParser.min.php Validation Code

Features

  • Small and light library
  • Hand-coded scanner
  • Analyze and found Errors
  • Poor speed and performance
  • No dependencies (only suitable version of the PHP)
  • Written and run on PHP 7.2.6

Type of supported values

  • [x] Integer
  • [x] Float
  • [x] Boolean (true , false)
  • [x] String
  • [ ] Char
  • [x] Sub Array
  • [ ] Sub Object

Unfinished items

  • encode() function
  • Improve speed and performance (Speed does not reach C Language!)
  • Fix bugs of arrays in arrays (Tree Array)

Usage

All public functions are accessible through the $json variable.

  • array $json->decode(string);
  • string $json->encode(array);

| Function | Return Type | Argument | | -------- | ----------- | -------- | | encode() | String | Array | | decode() | Array | String |

Samples

To view the full details, run the Example.php file.

JSON Grammar

  • json - element
  • value - object - array - string - number - "true" - "false" - "null"
  • object - '{' ws '}' - '{' members '}'
  • members - member - member ',' members
  • member - ws string ws ':' element
  • array - '[' ws ']' - '[' elements ']'
  • elements - element - element ',' elements
  • element - ws value ws
  • string - '"' characters '"'
  • characters - "" - character characters
  • character - '0020' . '10ffff' - '"' - '\' - '\' escape
  • escape - '"' - '\' - '/' - 'b' - 'n' - 'r' - 't' - 'u' hex hex hex hex
  • hex - digit - 'A' . 'F' - 'a' . 'f'
  • number - int frac exp
  • int - digit - onenine digits - '-' digit - '-' onenine digits
  • digits - digit - digit digits
  • digit - '0' - onenine
  • onenine - '1' . '9'
  • frac - "" - '.' digits
  • exp - "" - 'E' sign digits - 'e' sign digits
  • sign - "" - '+' - '-'
  • ws - "" - '0009' ws - '000A' ws - '000D' ws - '0020' ws

Performance

Competition between json_encode(...) and $json->encode(...)

The basic PHP functions are written in C itself.

Do not expect to reach it quickly.

However, there is always way for improvement.

Orginal PHP function json_encode()

1th Time : 0.0014569759368896 elapsed
2th Time : 0.0028131008148193 elapsed
3th Time : 0.0013258457183838 elapsed
4th Time : 0.0026090145111084 elapsed
-------------------------------------
Sigma Time : 0.008204937
Average Time : 0.008204937  4 = 0.002051234

View Full Source Code

Own function $json->encode()

1th Time : 0.44240999221802 elapsed
2th Time : 0.43391299247742 elapsed
3th Time : 0.44072198867798 elapsed
4th Time : 0.43309497833252 elapsed
-----------------------------------
Sigma Time : 1.750139952
Average Time : 1.750139952  4 = 0.437534988

View Full Source Code

License

JsonParser is licensed under the GNU General Public License.

  Files folder image Files  
File Role Description
Accessible without login Plain text file Example.php Example Example script
Plain text file JsonParser.min.php Class Class source
Plain text file JsonParser.php Class Class source
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file Performance-own.php Example Example script
Accessible without login Plain text file Performance-php.php Aux. Auxiliary script
Accessible without login Plain text file README.md Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:88
This week:3
All time:9,230
This week:152Up
User Ratings User Comments (10)
 All timeMonth
Utility:85%StarStarStarStarStar85%Equal
Consistency:95%StarStarStarStarStar95%Equal
Documentation:95%StarStarStarStarStar95%Equal
Examples:95%StarStarStarStarStar95%Equal
Tests:--
Videos:--
Overall:71%StarStarStarStar71%Equal
Rank:3402Up