Download the PHP package webfiori/jsonx without Composer
On this page you can find all versions of the php package webfiori/jsonx. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package jsonx
WebFiori Json
A helper class library for creating JSON or JSONx strings in PHP. It can be used to create well-formatted JSON strings from any variable type (strings, numbers, booleans, arrays, and even objects).
Table of Contents
- What is JSON?
- Features
- Supported PHP Versions
- Installation
- Basic Usage
- Example
- Using the Constructor
- Converting Properties Case
- Available Styles
- Letter Case Options
- Reading From Files
- Working With Objects
- Using JsonI Interface
- Auto-Mapping Objects
- Decoding JSON String
- Storing Output
- Working With Arrays
- Arrays as Objects
- JSONx
- Error Handling
- API Reference
What is JSON?
According to json.org, JSON is a data exchange format which is based partially on JavaScript. It is easy for humans to read and for machines to understand. JSON data is represented as pairs of keys and values.
Features
- Support for creating well-formatted JSON with proper indentation and escaping
- Support for creating JSONx (XML representation of JSON)
- Ability to decode JSON strings and convert them to
Json
objects - Ability to read JSON files and map JSON values to PHP data types
- Ability to manipulate JSON properties as needed
- Support for different property naming styles (camelCase, kebab-case, snake_case)
- Support for different letter cases (same, upper, lower)
- Customizable object serialization through the
JsonI
interface
Supported PHP Versions
Build Status |
---|
Installation
If you are using composer to manage your dependencies, then it is possible to install the library by including the entry "webfiori/jsonx":"*"
in the require
section of your composer.json
file to install the latest release:
Alternatively, you can install a specific version:
Another way to include the library is by going to releases and downloading the latest release, then extracting the compressed file content and adding it to your include directory.
Basic Usage
The process of using the classes is very simple. What you have to do is the following steps:
- Import (or include) the class
Json
from the namespaceWebFiori\Json
- Create an instance of the class
- Add data as needed using the various
add
methods - Output the object using
echo
command or any similar one
Example
The following code shows a very simple usage example:
The output of the code will be:
Using the Constructor
You can also add data directly using the constructor by passing an associative array:
The JSON output of this code will be:
Converting Properties Case
The library supports different property naming styles and letter cases. You can set these when creating a Json object or change them later.
Available Styles
The following property naming styles are supported:
none
: Keep the property names as they are providedcamel
: Convert property names to camelCasekebab
: Convert property names to kebab-casesnake
: Convert property names to snake_case
Letter Case Options
The following letter case options are available:
same
: Keep the letter case as providedupper
: Convert all letters to uppercaselower
: Convert all letters to lowercase
Example:
Output:
You can also change the style after creating the object:
Output:
Reading From Files
The library provides a static method to read JSON data from files:
Working With Objects
Using JsonI Interface
For custom object serialization, you can implement the JsonI
interface:
Output:
Auto-Mapping Objects
If an object doesn't implement the JsonI
interface, the library will try to map its public getter methods:
Output:
Decoding JSON String
You can decode a JSON string into a Json
object:
Storing Output
You can save the JSON output to a file:
Working With Arrays
You can add arrays to your JSON object:
Output:
Arrays as Objects
You can also represent arrays as objects:
Output:
JSONx
JSONx is an IBM standard format that represents JSON as XML. The library supports converting JSON to JSONx:
Output:
Error Handling
The library uses the JsonException
class for error handling:
API Reference
Main Classes
- Json: The main class for creating and manipulating JSON data
- JsonConverter: Handles conversion between JSON and other formats
- Property: Represents a property in a JSON object
- CaseConverter: Utility for converting between different naming styles
- JsonI: Interface for objects that can be converted to JSON
- JsonException: Exception class for JSON-related errors
- JsonTypes: Constants for JSON data types
Key Methods
Json Class
__construct(array $initialData = [], ?string $propsStyle = '', ?string $lettersCase = '', bool $isFormatted = false)
add(string $key, $value, $arrayAsObj = false): bool
addString(string $key, $val): bool
addNumber(string $key, $value): bool
addBoolean($key, $val = true): bool
addNull(string $key): bool
addArray(string $key, $value, $asObject = false): bool
addObject(string $key, &$val): bool
get($key): mixed
hasKey($key): bool
remove($keyName): ?Property
setPropsStyle(string $style, string $lettersCase = 'same'): void
setIsFormatted($bool): void
toJSONString(): string
toJSONxString(): string
toJsonFile(string $fileName, string $path, bool $override = false): void
Static Methods
Json::decode($jsonStr): Json
Json::fromJsonFile($pathToJsonFile): Json