|
| 1 | +# Feature Comparison |
| 2 | + |
| 3 | +This document compares YAML, XML, JSON, and LINO data serialization |
| 4 | +formats, with a focus on cyclic reference support as requested in |
| 5 | +[issue #55](https://github.com/linksplatform/Protocols.Lino/issues/55). |
| 6 | + |
| 7 | +## Cyclic References Support |
| 8 | + |
| 9 | +### YAML |
| 10 | + |
| 11 | +**Support Level**: ❌ **Limited** - Anchors and aliases only |
| 12 | + |
| 13 | +YAML supports repeated nodes through anchors (&) and aliases (*), but |
| 14 | +has significant limitations: |
| 15 | + |
| 16 | +- **Anchors** (&) define a value that can be referenced later |
| 17 | +- **Aliases** (*) reference previously defined anchors |
| 18 | +- **Limitation**: Anchors must be defined before they can be referenced |
| 19 | +- **No True Cycles**: Cannot create A→B→A circular references directly |
| 20 | +- **Forward References**: Not supported - aliases cannot reference |
| 21 | + anchors defined later |
| 22 | + |
| 23 | +**Example**: |
| 24 | + |
| 25 | +```yaml |
| 26 | +# Valid: Simple reference |
| 27 | +defaults: &defaults |
| 28 | + timeout: 30 |
| 29 | + retries: 3 |
| 30 | + |
| 31 | +production: |
| 32 | + <<: *defaults |
| 33 | + host: prod.example.com |
| 34 | + |
| 35 | +# Invalid: Cannot create true cycles |
| 36 | +# node_a: &a |
| 37 | +# ref: *b # Error: 'b' not yet defined |
| 38 | +# node_b: &b |
| 39 | +# ref: *a |
| 40 | +``` |
| 41 | + |
| 42 | +### XML |
| 43 | + |
| 44 | +**Support Level**: ❌ **Very Limited** - Through external mechanisms only |
| 45 | + |
| 46 | +XML itself has no built-in support for cyclic references: |
| 47 | + |
| 48 | +- **XPointer/XPath**: Can reference other parts of documents, but |
| 49 | + primarily for addressing |
| 50 | +- **IDREF/ID**: Allows references within documents, but limited scope |
| 51 | +- **XInclude**: For including external documents, not for cycles |
| 52 | +- **No Native Cycles**: Standard XML serialization cannot represent |
| 53 | + object graphs with cycles |
| 54 | +- **External Solutions**: Some XML processors provide custom extensions |
| 55 | + |
| 56 | +**Example**: |
| 57 | + |
| 58 | +```xml |
| 59 | +<!-- Limited IDREF support --> |
| 60 | +<people> |
| 61 | + <person id="john" friend-ref="jane"/> |
| 62 | + <person id="jane" friend-ref="john"/> |
| 63 | +</people> |
| 64 | + |
| 65 | +<!-- But cannot serialize complex object graphs with cycles --> |
| 66 | +``` |
| 67 | + |
| 68 | +### JSON |
| 69 | + |
| 70 | +**Support Level**: ❌ **No Native Support** |
| 71 | + |
| 72 | +JSON has fundamental limitations for cyclic references: |
| 73 | + |
| 74 | +- **Tree Structure Only**: JSON represents hierarchical data, not graphs |
| 75 | +- **Serialization Issues**: `JSON.stringify()` throws errors on cycles |
| 76 | +- **JSON Schema**: Can define references ($ref) but for schema |
| 77 | + composition, not data cycles |
| 78 | +- **JSON Pointer**: For addressing within documents, not for cycles |
| 79 | +- **Workarounds**: External libraries provide cycle detection/replacement |
| 80 | + |
| 81 | +**Example**: |
| 82 | + |
| 83 | +```javascript |
| 84 | +// This fails: |
| 85 | +const obj = { name: "A" }; |
| 86 | +obj.self = obj; // Creates cycle |
| 87 | +JSON.stringify(obj); // TypeError: circular structure |
| 88 | + |
| 89 | +// JSON Schema $ref is for schema composition, not data cycles: |
| 90 | +{ |
| 91 | + "$ref": "#/definitions/person", // References schema definition |
| 92 | + "definitions": { |
| 93 | + "person": { "type": "object" } |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +### LINO (Links Notation) |
| 99 | + |
| 100 | +**Support Level**: ✅ **Full Native Support** |
| 101 | + |
| 102 | +LINO is specifically designed to represent linked data structures and |
| 103 | +naturally supports cyclic references: |
| 104 | + |
| 105 | +- **Link-Based Structure**: Every element can reference any other link by identifier |
| 106 | +- **Bidirectional Links**: Links can reference each other freely |
| 107 | +- **Complex Graphs**: Can represent any graph structure including cycles |
| 108 | +- **Natural Syntax**: Cycles emerge naturally from the link reference system |
| 109 | +- **No Special Syntax**: No additional constructs needed for cycles |
| 110 | + |
| 111 | +**Examples**: |
| 112 | + |
| 113 | +```lino |
| 114 | +// Simple bidirectional relationship |
| 115 | +john (friend: jane) |
| 116 | +jane (friend: john) |
| 117 | +
|
| 118 | +// Complex cycle in family relationships |
| 119 | +alice (mother: bob) |
| 120 | +bob (son: alice, father: carol) |
| 121 | +carol (daughter: alice, mother: bob) |
| 122 | +
|
| 123 | +// Self-reference |
| 124 | +recursive_function (calls: recursive_function) |
| 125 | +
|
| 126 | +// Multi-level cycles in data structures |
| 127 | +node_a (next: node_b) |
| 128 | +node_b (next: node_c) |
| 129 | +node_c (next: node_a, data: "cycle complete") |
| 130 | +``` |
| 131 | + |
| 132 | +## Detailed Feature Comparison |
| 133 | + |
| 134 | +| Feature | YAML | XML | JSON | LINO | |
| 135 | +|---------|------|-----|------|------| |
| 136 | +| **Cyclic References** | ❌ Limited | ❌ Very Limited | ❌ None | ✅ Full | |
| 137 | +| **Forward References** | ❌ No | ❌ Limited | ❌ No | ✅ Yes | |
| 138 | +| **Bidirectional Links** | ❌ No | ❌ Manual | ❌ No | ✅ Native | |
| 139 | +| **Graph Structures** | ❌ Trees | ❌ Trees | ❌ Trees | ✅ Full Graphs | |
| 140 | +| **Ref Syntax** | `&anchor *alias` | `id="x" ref="x"` | `$ref` | `identifier` | |
| 141 | +| **Self-Reference** | ❌ No | ❌ Manual | ❌ No | ✅ Yes | |
| 142 | +| **Complex Cycles** | ❌ No | ❌ No | ❌ No | ✅ Yes | |
| 143 | +| **Serialization** | ✅ Safe | ✅ Safe | ❌ Fails on cycles | ✅ Native | |
| 144 | + |
| 145 | +## Use Case Analysis |
| 146 | + |
| 147 | +### When Cyclic References Matter |
| 148 | + |
| 149 | +1. **Object-Relational Mapping**: Database entities with bidirectional relationships |
| 150 | +2. **Graph Algorithms**: Representing networks, social graphs, dependency graphs |
| 151 | +3. **Recursive Data Structures**: Linked lists, trees with parent pointers |
| 152 | +4. **State Machines**: States that reference each other |
| 153 | +5. **Document Cross-References**: Academic papers, legal documents |
| 154 | +6. **Family Trees**: Genealogical data with marriages and relationships |
| 155 | + |
| 156 | +### Format Recommendations |
| 157 | + |
| 158 | +- **YAML**: Good for configuration files, simple hierarchical data |
| 159 | +- **XML**: Good for document markup, when schema validation is important |
| 160 | +- **JSON**: Good for web APIs, simple data exchange, when cycles aren't needed |
| 161 | +- **LINO**: Ideal for linked data, knowledge graphs, any data with natural relationships |
| 162 | + |
| 163 | +## Technical Implementation Notes |
| 164 | + |
| 165 | +### YAML Limitations in Practice |
| 166 | + |
| 167 | +```yaml |
| 168 | +# This creates a parsing error in most YAML processors: |
| 169 | +# parent: &parent |
| 170 | +# children: |
| 171 | +# - child: &child |
| 172 | +# parent: *parent # Circular reference |
| 173 | +``` |
| 174 | + |
| 175 | +### XML Workarounds |
| 176 | + |
| 177 | +```xml |
| 178 | +<!-- Requires custom processing to resolve relationships --> |
| 179 | +<graph> |
| 180 | + <nodes> |
| 181 | + <node id="1" name="A"/> |
| 182 | + <node id="2" name="B"/> |
| 183 | + </nodes> |
| 184 | + <edges> |
| 185 | + <edge from="1" to="2"/> |
| 186 | + <edge from="2" to="1"/> <!-- Creates cycle --> |
| 187 | + </edges> |
| 188 | +</graph> |
| 189 | +``` |
| 190 | + |
| 191 | +### JSON Alternatives |
| 192 | + |
| 193 | +```javascript |
| 194 | +// Libraries like 'circular-json' provide workarounds: |
| 195 | +const CircularJSON = require('circular-json'); |
| 196 | +const obj = { name: "A" }; |
| 197 | +obj.self = obj; |
| 198 | +const serialized = CircularJSON.stringify(obj); |
| 199 | +``` |
| 200 | + |
| 201 | +### LINO Natural Cycles |
| 202 | + |
| 203 | +```lino |
| 204 | +// No special handling needed - cycles are natural: |
| 205 | +parent (children: child1 child2) |
| 206 | +child1 (parent: parent, sibling: child2) |
| 207 | +child2 (parent: parent, sibling: child1) |
| 208 | +
|
| 209 | +// Even complex multi-level cycles work seamlessly: |
| 210 | +company (employees: john jane) |
| 211 | +john (employer: company, manager: jane, reports: jane) |
| 212 | +jane (employer: company, manager: john, reports: john) |
| 213 | +``` |
| 214 | + |
| 215 | +## Conclusion |
| 216 | + |
| 217 | +For applications requiring cyclic references, LINO provides the most |
| 218 | +natural and complete solution. While YAML, XML, and JSON can handle |
| 219 | +simple hierarchical data effectively, they require workarounds or |
| 220 | +external libraries to handle cycles, often with significant complexity |
| 221 | +or limitations. |
| 222 | + |
| 223 | +LINO's link-based design makes it uniquely suited for representing |
| 224 | +interconnected data where relationships are as important as the data |
| 225 | +itself. |
0 commit comments