Download the PHP package s-mcdonald/phpjson without Composer
On this page you can find all versions of the php package s-mcdonald/phpjson. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download s-mcdonald/phpjson
More information about s-mcdonald/phpjson
Files in s-mcdonald/phpjson
Package phpjson
Short Description PHPJson: An elegant and simple JSON object serialization library for PHP.
License MIT
Informations about the package phpjson
PHPJson
A Fast and Lightweight PHP JSON Object Serialization Library.
💖 Support This Project
PHPJson
is supported by your donations! Click the Sponsor link to display funding options.
PHPJson
is a library that provides tools to work with JSON files and structures for PHP.
Its primary feature is the ability to quickly and easily serialize PHP objects into JSON and deserialize (hydrate) JSON back to PHP objects.
Other features include
- Encode
- Decode
- Minify (uglify)
- Prettify
- Json Builder
- Serialization (including Enums)
- Serialization value casting
- Hydration
- Validation
Project Objectives
- Simplify working with JSON structures.
- Enable fast and minimal-configuration serialization using attributes.
- Provide advanced serialization capabilities through traits and custom normalizers.
- Reduce duplication in business logic by streamlining common tasks.
Contents
- Usage
- Serialization
- Basic usage
- Override Json Properties
- Nested Structures
- Serializing Enums
- Casting values
- Hydration/Deserialization
- Basic Hydration
- Hydration with Setter Methods
- JsonBuilder
- Basic Builder
- Objects and Arrays
- Json Formatting
- Prettify
- Json Validate
- Installation
- Dependencies
- License
- Contribute
Usage
Serialization
Quick usage
The fastest way to serialize a class into JSON is by using the JsonProperty
attribute.
Or you can use the Json facade
Override Json Properties
You can customize property names in the JSON output by specifying your own names. This also applies when hydrating objects from JSON.
Serialize from methods
You can serialize values from getter methods, regardless of whether the method is public, protected, or private. PHPJson
will automatically extract the value.
Nested Structures
PHPJson
allows you to work seamlessly with complex, nested objects. Nested classes and their properties are serialized into valid JSON structures, matching the relationships between objects.
Serialization Using a trait
The above method utilizes the JsonProperty to serialize any object. This is by far the easiest and fasted way to convert your objects into Json. However, this will have some limitations. To overcome this, we have included two Facets called SerializesWithMapping
and SerializesToJson
. With these Facets you can customize the Serialization and export vastly more complex objects.
Serializing Enums
PHPJson
supports the serialization of both pure and backed enums.
Pure Enum
Backed Enum
Casting Values
When serializing a PHP object to JSON, you might need to cast specific property values into different types for the JSON output. You can achieve this by using the JsonProperty
attribute to specify the desired type using a JsonType
, such as StringType
or IntegerType
.
Available types are;
- StringType
- ArrayType,
- BooleanType,
- DoubleType,
- IntegerType,
- NullType,
- ObjectType
Deserialize aka Object Hydration
Basic Hydration
With PHPJson
, basic object hydration is straightforward. If your class properties match the structure and property names in your JSON, no additional attributes or mappings are required. The library will automatically map the JSON data to your class or entity.
Now deserialize the json string with the PHP class.
Hydration with Setter Methods
If your class relies on setters for processing or assigning values, PHPJson
can hydrate using setter methods, provided these conditions are met:
- The setter accepts exactly one required argument.
- The
JsonProperty
attribute is used to specify the property for hydration. - The argument type matches the data type in the JSON.
Notice how the JsonProperty is used twice here, for hydration setter methods will be the preferred hydration point, since, setUserName
can not be used for serialization, the property $userName
will be used for this.
JsonBuilder
Fluently create Json objects using PHP.
JsonBuilder Basics
JsonBuilder Objects and Arrays
Json Formatting
Prettify & Uglify
Prettify or Uglify(minify) your json values
and then the reverse
Json Validate
PHP 8.3 onwards has the json_validate function. This library duplicates this behaviour so it can bve used in PHP 8.2
Reference
Installation
Install this package via composer, or simply fork/clone the repo.
Dependencies
- None
PHP Versions
- PHP 8.2, 8.3, 8.4
License
Json is licensed under the terms of the MIT License (See LICENSE file for details).
Contribute
🙌 Want to contribute?
Check out the issues section to get started.