NodeJsonDecoder::decode

The decode() method converts a JSON string back into a NodeInterface object, recreating the structure of the original node, including its child nodes and attributes.

Description

public NodeJsonDecoder::decode(): NodeInterface

Parameters

None.

Return Value

Returns the root NodeInterface object reconstructed from the JSON string. This node can be used as part of the DOM hierarchy or manipulated further.

Exceptions

Throws an exception if the JSON is invalid or does not represent a valid node structure.


Example

Given the following JSON string:

{
    "nodeId": 1,
    "nodeType": 1,
    "nodeName": "DIV",
    "parentId": null,
    "attributes": {
        "class": "container"
    },
    "void": false,
    "visible": true,
    "meta": [],
    "childNodes": [
        {
            "nodeId": 2,
            "nodeType": 3,
            "nodeName": "#TEXT",
            "parentId": 1,
            "attributes": {},
            "void": false,
            "visible": true,
            "meta": {
                "data": "Hello World"
            },
            "childNodes": []
        }
    ]
}

You can decode it into a NodeInterface object as follows:

use Ucscode\UssElement\Serializer\NodeJsonDecoder;

$json = '...'; // The above JSON string

$decoder = new NodeJsonDecoder($json);
$node = $decoder->decode();

// Accessing node details
echo $node->getNodeName(); // Outputs: DIV
echo $node->getChildNodes()->first()->getNodeName(); // Outputs: #TEXT
echo $node->getInnerHtml(); // Outputs: Hello World
Source Code
If you find this project useful, consider leaving a on GitHub! Thank you!