|
| 1 | +# flutter_json_render |
| 2 | + |
| 3 | +A Flutter-first implementation of the [vercel-labs/json-render](https://github.com/vercel-labs/json-render) concept. |
| 4 | + |
| 5 | +`flutter_json_render` renders flat JSON specs into Flutter widgets through an explicit registry and catalog. |
| 6 | + |
| 7 | +- Guardrailed component/action model |
| 8 | +- Flat and model-friendly spec shape (`root + elements`) |
| 9 | +- Dynamic prop resolution (`$state`, `$item`, `$index`, `$cond`) |
| 10 | +- Visibility and repeat support |
| 11 | +- JSONL streaming patch compiler for progressive UI updates |
| 12 | + |
| 13 | +## Install |
| 14 | + |
| 15 | +```bash |
| 16 | +flutter pub add flutter_json_render |
| 17 | +``` |
| 18 | + |
| 19 | +## Core Concepts |
| 20 | + |
| 21 | +- `JsonCatalog`: declares what components/actions are allowed |
| 22 | +- `JsonRegistry`: maps component type -> Flutter widget builder, action -> handler |
| 23 | +- `JsonRenderer`: renders `JsonRenderSpec` safely |
| 24 | +- `JsonSpecStreamCompiler`: compiles streamed JSONL specs/patches |
| 25 | + |
| 26 | +## Quick Start |
| 27 | + |
| 28 | +```dart |
| 29 | +import 'package:flutter/material.dart'; |
| 30 | +import 'package:flutter_json_render/flutter_json_render.dart'; |
| 31 | +
|
| 32 | +final catalog = JsonCatalog( |
| 33 | + components: { |
| 34 | + ...standardComponentDefinitions, |
| 35 | + 'Panel': const JsonComponentDefinition( |
| 36 | + description: 'Simple card container', |
| 37 | + props: { |
| 38 | + 'title': JsonPropDefinition(type: 'string', required: true), |
| 39 | + }, |
| 40 | + ), |
| 41 | + }, |
| 42 | + actions: { |
| 43 | + ...standardActionDefinitions, |
| 44 | + 'increment': const JsonActionDefinition(description: 'Increase count'), |
| 45 | + }, |
| 46 | +); |
| 47 | +
|
| 48 | +final registry = defineRegistry( |
| 49 | + components: { |
| 50 | + ...standardComponentBuilders(), |
| 51 | + 'Panel': (ctx) => Card( |
| 52 | + child: Padding( |
| 53 | + padding: const EdgeInsets.all(12), |
| 54 | + child: Column( |
| 55 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 56 | + children: [ |
| 57 | + Text(ctx.props['title']?.toString() ?? 'Panel'), |
| 58 | + const SizedBox(height: 8), |
| 59 | + ...ctx.children, |
| 60 | + ], |
| 61 | + ), |
| 62 | + ), |
| 63 | + ), |
| 64 | + }, |
| 65 | + actions: { |
| 66 | + 'increment': (ctx) { |
| 67 | + ctx.setStateModel((prev) { |
| 68 | + final next = <String, dynamic>{...prev}; |
| 69 | + next['count'] = ((prev['count'] as num?) ?? 0) + 1; |
| 70 | + return next; |
| 71 | + }); |
| 72 | + }, |
| 73 | + }, |
| 74 | +); |
| 75 | +
|
| 76 | +final spec = JsonRenderSpec.fromJson({ |
| 77 | + 'root': 'panel', |
| 78 | + 'state': {'count': 1}, |
| 79 | + 'elements': { |
| 80 | + 'panel': { |
| 81 | + 'type': 'Panel', |
| 82 | + 'props': {'title': 'Counter'}, |
| 83 | + 'children': ['value', 'button'], |
| 84 | + }, |
| 85 | + 'value': { |
| 86 | + 'type': 'Text', |
| 87 | + 'props': {'text': {'$state': '/count'}}, |
| 88 | + }, |
| 89 | + 'button': { |
| 90 | + 'type': 'Button', |
| 91 | + 'props': {'label': 'Increment'}, |
| 92 | + 'on': { |
| 93 | + 'press': {'action': 'increment'} |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +}); |
| 98 | +
|
| 99 | +Widget app() { |
| 100 | + return MaterialApp( |
| 101 | + home: Scaffold( |
| 102 | + body: Center( |
| 103 | + child: JsonRenderer(spec: spec, registry: registry), |
| 104 | + ), |
| 105 | + ), |
| 106 | + ); |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +## Stream JSONL Patches |
| 111 | + |
| 112 | +```dart |
| 113 | +final compiler = JsonSpecStreamCompiler(); |
| 114 | +
|
| 115 | +compiler.push('{"root":"root","elements":{"root":{"type":"Text","props":{"text":"loading"}}}}\n'); |
| 116 | +compiler.push('{"op":"replace","path":"/elements/root/props/text","value":"ready"}\n'); |
| 117 | +
|
| 118 | +final spec = compiler.getResult(); |
| 119 | +``` |
| 120 | + |
| 121 | +Supported stream line shapes: |
| 122 | + |
| 123 | +- full spec object |
| 124 | +- single patch op object |
| 125 | +- patch op array |
| 126 | +- wrapped patch object: `{ "patch": [ ... ] }` |
| 127 | + |
| 128 | +## Validate Specs |
| 129 | + |
| 130 | +```dart |
| 131 | +final result = validateSpec(spec, catalog: catalog, strictCatalog: false); |
| 132 | +if (!result.isValid) { |
| 133 | + for (final issue in result.issues) { |
| 134 | + debugPrint('[${issue.severity}] ${issue.message}'); |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +## Built-In Components |
| 140 | + |
| 141 | +- `Text` |
| 142 | +- `Column` |
| 143 | +- `Row` |
| 144 | +- `Container` |
| 145 | +- `Center` |
| 146 | +- `SizedBox` |
| 147 | +- `Button` |
| 148 | +- `Image` |
| 149 | + |
| 150 | +## Example App |
| 151 | + |
| 152 | +A full multi-scenario showcase app is included in `/example`: |
| 153 | + |
| 154 | +```bash |
| 155 | +cd example |
| 156 | +flutter run |
| 157 | +``` |
| 158 | + |
| 159 | +Included scenarios: |
| 160 | + |
| 161 | +- Counter + visibility conditions |
| 162 | +- Repeat + `$item`/`$index` actions |
| 163 | +- Dynamic props via `$cond` |
| 164 | +- Async action flow |
| 165 | +- Streamed JSONL patch simulation |
| 166 | + |
| 167 | +## pub.dev Release Checklist |
| 168 | + |
| 169 | +```bash |
| 170 | +flutter analyze |
| 171 | +flutter test |
| 172 | +dart pub publish --dry-run |
| 173 | +``` |
| 174 | + |
| 175 | +## License |
| 176 | + |
| 177 | +MIT |
0 commit comments