rhizome-node/examples/json-ast.ts
Lentil Hoffman 5afd3232cb
feat: enable inter-plugin state sharing in CustomResolver
- Update ResolverPlugin interface to include context in update and resolve methods
- Modify CustomResolver to pass entity state and resolved values to plugins
- Update built-in plugins to accept and use the new context parameter
- Add comprehensive test for inter-plugin communication
- Add documentation for the new view composition patterns

This change enables plugins to access each other's states during both update and resolve phases, allowing for more powerful and flexible resolver compositions.
2025-06-22 20:42:05 -05:00

52 lines
1.3 KiB
TypeScript

import { jsonToAst } from '../src/utils/json-ast/index';
// Example JSON data
const exampleJson = {
name: "John Doe",
age: 42,
active: true,
scores: [95, 87, 92],
address: {
street: "123 Main St",
city: "Anytown",
coordinates: {
lat: 42.1234,
lng: -71.2345
}
},
tags: ["admin", "user", "premium"],
metadata: {
createdAt: "2023-01-01T00:00:00Z",
updatedAt: "2023-06-21T12:34:56Z"
}
};
// Convert JSON to AST with path information
const ast = jsonToAst(exampleJson, {
includePath: true,
maxDepth: 10,
// Optional filter - only include nodes with paths that include 'address'
// filter: (node) => !node.path || node.path.includes('address')
});
console.log("Original JSON:", JSON.stringify(exampleJson, null, 2));
console.log("\nAST:", JSON.stringify(ast, null, 2));
// Example of traversing the AST
function traverse(node: any, indent = 0) {
const padding = ' '.repeat(indent);
const type = node.type.toUpperCase();
const value = node.value !== undefined ? `: ${JSON.stringify(node.value)}` : '';
const path = node.path ? ` [${node.path}]` : '';
console.log(`${padding}${type}${value}${path}`);
if (node.children) {
node.children.forEach((child: any) => traverse(child, indent + 2));
}
}
console.log('\nTraversed AST:');
traverse(ast);