Download the PHP package ddn/jsonobject without Composer

On this page you can find all versions of the php package ddn/jsonobject. 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 jsonobject

JsonObject

JsonObject is a PHP class to ease the usage of objects coming from a JSON definition. The idea comes from using pydantic in python, and its ability to parse and validate json data into objects.

Why JsonObject

I had to use an API from PHP, and that API returned me JSONObjects. So I needed to parse them into PHP objects that I was able to use in the app.

The workflow is

  1. retrieve a JSON object definition
  2. use JsonObject to parse the JSON definition
  3. use the resulting objects in the application

Use case

Let's take the following JSON example:

Using JsonObject, I will be able to define my data model using the following classes:

And then add the following command:

The JsonObject class will carry out with parsing the content into objects, and we would be able to use its attributes as defined:

The classes defined can also have methods that will make it easier to implement the application's data model. E.g. it would possible to define the class User like this:

Using JsonObject

The idea of the JsonObject class is to use it to parse json data into objects. So that these objects may contain other methods that will help to implement the data model of the application.

When the json object (or array) is parsed, its content is recursively parsed according to the types defined in the ATTRIBUTES constant. If the data is not valid, because it does not contain the expected values, an exception is thrown.

To use JsonObject one must subclass JsonObject and define the ATTRIBUTES constant for that class so that it defines the attributes expected for the objects of that class, along with the type of each one.

Defining the types for the attributes

The ATTRIBUTES constant is an associative array where the keys are the name for each attribute, and the values are the type for each attribute.

The possible types can be:

Optional and mandatory attributes

When defining the name of the attributes, one can add a ? at the end of the name to indicate that the attribute is optional. For example, the attribute name address? in the use-case section is optional.

Each field is considered to be mandatory so that it must exist in the parsed object (or array). Moreover, the object must be of the type defined (i.e. it must be correctly parsed by the specific type).

Mandatory attributes

Any attribute that is not optional is considered to be madatory. This is of special interest in two points:

  1. when creating the object from an external structure (either using fromArray or fromObject functions).
  2. when generating the object or array representation of the JsonObject

When creating the object from an external structure, the JsonObject will take care of every mandatory field. And if any of them is missing, an exception will raise.

In the next example, an exception will raise because the mandatory field age is not provided.

When converting the object to an array or to an object (or getting its json representation), a mandatory field will get a default value, even if not set.

So in the next example

The output will be

Because while attributes name and age are mandatory and they get their default values (i.e. 0 for numbers, empty for strings, lists or dicts), attribute birthDate is not mandatory and it has not been set, yet. So it is not generated in the output.

Setting to null on mandatory attributes

The problem of setting values to null is of special relevance when considering whether an attribute is optional or not.

One may think that, if we set a value to null it would mean to unset the value and so it should only be possible for optional values but not for mandatory values.

In JsonObject we have a different concept, because setting a property to null will mean "setting a value to null" and not unsetting the property. In order to unset the property, we should use function unset or somethink like that.

Unsetting mandatory attributes

JsonObject also enables to unset values. For an optional attribute, it means removing the value and thus it will not have any value in an array representation or an object (if retrieving the value, it will be set to null).

But for a mandatory attribute, unsetting it will mean resetting its value to the default. That means that it will be initialized to the default value of the type (i.e. 0 for numbers, empty for lists, strings or dicts, etc.) or its default value in the ATTRIBUTES constant.

Inheritance

JsonObjects are also able to inherit attributes from their parent classes. Take the following example:

In this example, class Vehicle will only have attribute brand and color, but class Car will have brand, color and wheels attributes, while class Boat will have brand, color and length attributes.

Creation of objects

Objects from children classes of JsonObject can be created using the static method ::fromArray or ::fromObject, starting from a json parsed object.

In the previous example, if we have a file car.json with the following content:

We can use the following code to get an instance of the Vehicle class:

An alternative is to instantiate objects like in the next example

* PHP 8 and over:

* previous PHP versions:

Methods for the objects

JsonObject

The JsonObject is the core class for this library. Its methods are:

JsonDict

This object is used to deal with a dictionary coming from a json definition. The JsonDict class is typed to that each of the elements must be from a given type.

The JsonDict objects can be used as array-like objects (e.g. $jsonDict["key1"]) but (at the moment of writing this text) the type of the elements inserted in the dictionary are not checked. The type is used for parsing the content when creating the dict (e.g. using fromArray static function) or to dump the content to an array or an object (e.g. using toArray function).

The methods are:

These methods are interpreted in the same way than in the case of JsonObject. And the type of the elements in the dict may refer to complex types that will be considered recursively when parsing the content.

e.g. type list[list[int]] will be used to parse [ [ 1, 2, 3], [ 4, 5, 6 ]]

JsonArray

This object is very much the same than JsonDict with the exception that the indexes must be integer numbers. In this case $value["key1"] will produce an exception.

In this case, the function to append elements to the array (i.e. []) is also implemented.

Initializing values

When defining the class, it is possible to initialize the values for the objects that are newly created, and to those attributes that are optional.

There are two ways:

 Using class properties

It is possible to initialize the value of an object by using the class properties, so if the value for an attribute is set in the class, it will be copied to the instance as an attribute, if it is defined.

E.g.

Now, the attribute sex is initialized to not revealed instead of being null.

Using the definition of the attributes

The way to make it is to define a tuple [ <type>, <default value> ] for the type of the object. Taking the next example:

The attribute sex is optional when retrieving the user data. Using this new definition for the class, if sex is not set, the value will be set to "not revealed" instead of null.

An important feature is that, if the string set as \ corresponds to a method of the object, it will be called upon getting the value (if it has not been set, yet), and the value set for that property will be the result of the call.

E.g.

In this example, if we had not set the birthDate property but it is retrieved, it will be computed by subtracting the age to the current date.

Additional tools and technical facts

Parsing a value

If wanted to parse an arbitrary object to a JsonObject, it is possible to use the function JsonObject::parse_typed_value. This is important to be able to convert from any type to a JsonObject-type.

e.g.

Will obtain an object of type JsonList<str>.

Type checking

The default behavior of this library is to ensure that the values set for the attributes match their defined type. But that means that would mean that, as a float is not an int, setting a float to 0 will fail because 0 is an integer. In that case, the user must cast the values before assigning them. To control whether to so stritcly check the type or not, it is possible to use the constant STRICT_TYPE_CHECKING.

If STRICT_TYPE_CHECKING it is set to True, the types will be strictly checked and e.g. assigning 9.3 to an int will raise an exception. If set to False, the numerical types will be converted from one to each other. So e.g. if we assign 9.3 to an int it will be automatically truncated to 9.

Other important type checking is when assigning an empty value (i.e. "" or null) to a numeric type. In that case, we have the constant STRICT_TYPE_CHECKING_EMPTY_ZERO.

If STRICT_TYPE_CHECKING_EMPTY_ZERO is set to True (the default behavior), when assigning an empty value to a numeric type, it will be considered to be 0. i.e. assigning an empty string or a null value to an int attribute, will mean to assign 0. If set to False, the library will check the types and will eventually raise an exception.

Enhanced JsonLists

Now JsonList also enables to use negative indexes, so that -1 will be the last element, -2 the penultimate, etc.

JsonList object includes functions for sorting or filtering.


All versions of jsonobject with dependencies

PHP Build Version
Package Version
Requires php Version >=7.4
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 ddn/jsonobject contains the following files

Loading the files please wait ....