Download the PHP package karriere/json-decoder without Composer

On this page you can find all versions of the php package karriere/json-decoder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package json-decoder

    Packagist Downloads

JsonDecoder for PHP

This package contains a JsonDecoder implementation that allows you to convert your JSON data into php class objects other than stdclass.

Installation

You can install the package via composer

Usage

By default the Decoder will iterate over all JSON fields defined and will try to set this values on the given class type instance. This change in behavior allows the use of json-decoder on classes that use the magic __get and __set functions like Laravel's Eloquent models.

If a property equally named like the JSON field is found or a explicit Binding is defined for the JSON field it will be decoded into the defined place. Otherwise the property will just be created and assigned (you need the #[AllowDynamicProperties] attribute if you are on PHP 8.2.).

The JsonDecoder class can receive one parameter called shouldAutoCase. If set to true it will try to find the camel-case version from either snake-case or kebap-case automatically if no other binding was registered for the field and it will use an AliasBinding if one of the variants can be found.

A simple example

Assume you have a class Person that looks like this:

The following code will transform the given JSON data into an instance of Person.

Please be aware that since PHP 8.2. dynamic properties are deprecated. So if you still wish to have the ability to make use of those dynamic properties you have to add the PHP attribute AllowDynamicProperties to your class. If you are using PHP 8.2. (and greater) and don't use the AllowDynamicProperties attribute all dynamic properties will be ignored.

More complex use case

Let's extend the previous example with a property called address. This address field should contain an instance of Address. As of version 4 you can use the introduced method scanAndRegister to automatically generate the transformer based on class annotations. Since version 5 you can also make use of the property type instead of a class annotation.

For this class definition we can decode JSON data as follows:

Defining a Transformer

If you don't use annotations or need a more flexible Transformer you can also create a custom transformer. Let's look at the previous example without annotation.

To be able to transform the address data into an Address class object you need to define a transformer for Person:

The transformer interface defines two methods:

After registering the transformer the JsonDecoder will use the defined transformer:

Handling private and protected properties

As of version 4 the JsonDecoder class will handle private and protected properties out of the box.

Transforming an array of elements

If your JSON contains an array of elements at the root level you can use the decodeMultiple method to transform the JSON data into an array of class type objects.

Documentation

Transformer Bindings

The following Binding implementations are available

FieldBinding

Defines a JSON field to property binding for the given type.

Signature:

This defines a field mapping for the property $property to a class instance of type $type with data in $jsonField.

ArrayBinding

Defines an array field binding for the given type.

Signature:

This defines a field mapping for the property $property to an array of class instance of type $type with data in $jsonField.

AliasBinding

Defines a JSON field to property binding.

Signature:

DateTimeBinding

Defines a JSON field to property binding and converts the given string to a DateTime instance.

Signature:

CallbackBinding

Defines a property binding that gets the callback result set as its value.

Signature:

License

Apache License 2.0 Please see LICENSE for more information.


All versions of json-decoder with dependencies

PHP Build Version
Package Version
Requires php Version 8.0.* | 8.1.* | 8.2.*
php-di/phpdoc-reader Version ^2.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package karriere/json-decoder contains the following files

Loading the files please wait ....