import 'dart:collection'; class Node extends LinkedListEntry { Node? parent; final String type; final LinkedList children; final Map attributes; Node({ required this.type, required this.children, required this.attributes, this.parent, }); factory Node.fromJson(Map json) { assert(json['type'] is String); final jType = json['type'] as String; final jChildren = json['children'] as List?; final jAttributes = json['attributes'] != null ? Map.from(json['attributes'] as Map) : {}; final LinkedList children = LinkedList(); if (jChildren != null) { children.addAll( jChildren.map( (jnode) => Node.fromJson( Map.from(jnode), ), ), ); } return Node( type: jType, children: children, attributes: jAttributes, ); } Map toJson() { var map = { 'type': type, }; if (children.isNotEmpty) { map['children'] = children.map((node) => node.toJson()); } if (attributes.isNotEmpty) { map['attributes'] = attributes; } return map; } }