Add save to json/yaml (ctrl-w)

This commit is contained in:
2024-10-01 21:40:13 +02:00
parent 9bc83ccd56
commit 3caca13e5c
10 changed files with 101 additions and 8 deletions

View File

@@ -27,5 +27,10 @@ class ArrayNode extends Node implements CollapsibleNode
return new ArrayNode($items);
}
public function jsonSerialize(): mixed
{
return array_values($this->items);
}
}

View File

@@ -2,7 +2,9 @@
namespace NoccyLabs\JEdit\Tree;
abstract class Node
use JsonSerializable;
abstract class Node implements JsonSerializable
{
}

View File

@@ -29,5 +29,10 @@ class ObjectNode extends Node implements CollapsibleNode
return new ObjectNode($properties);
}
public function jsonSerialize(): mixed
{
return $this->properties;
}
}

View File

@@ -13,6 +13,18 @@ class Tree
return $this;
}
public function save(): mixed
{
if (!$this->root)
return null;
return json_decode(
json_encode(
$this->root
)
);
}
private function parseNode(mixed $node): Node
{
if (is_array($node) && array_is_list($node)) {

View File

@@ -7,5 +7,10 @@ class ValueNode extends Node
public function __construct(public mixed $value)
{
}
public function jsonSerialize(): mixed
{
return $this->value;
}
}