NodeJsonEncoder

The NodeJsonEncoder class in UssElement is designed to handle the serialization of Node objects into a format that can be easily transferred, stored, or manipulated—JSON. This class helps in encoding a Node (including its descendants) into a standard JSON format, making it possible to pass the Node between different systems, APIs, or components without losing its structure or important data.

Constructor

public NodeJsonEncoder::__construct(NodeInterface $node)

The constructor accepts a NodeInterface object, which is the node to be serialized.


Example

use Ucscode\UssElement\Node\ElementNode;
use Ucscode\UssElement\Serializer\NodeJsonEncoder;

// Sample Node object
$node = new ElementNode('div', ['class' => 'container']);

// Initialize the encoder
$encoder = new NodeJsonEncoder($node);

// Get the JSON representation
echo $encoder->encode();

Example Output

{
    "nodeId": 1,
    "nodeType": 1,
    "nodeName": "DIV",
    "parentId": null,
    "attributes": {
        "class": "container"
    },
    "void": false,
    "visible": true,
    "meta": [],
    "childNodes": []
}

This JSON output represents the structure of the ElementNode object with its tag, attributes, content, and child nodes.


Methods

Source Code
If you find this project useful, consider leaving a on GitHub! Thank you!