2 hours ago · 10 min read2089 words · Tech · hide · 0 comments

I wrote an API Client in Rust recently for EVE-NG, a network emulator. Some of its API endpoints (that represent lists of things) return different JSON representations for the same endpoint depending on the state of the instance. As Rust is strict about types, I had to figure out how to deserialize these quirks while ensuring consistency in my program. An example of this quirk is the list of nodes in a lab. When the lab has no nodes, the data is an empty array: { "data": [], } When the lab has nodes, the representation changes from an empty array to a hash map: { "data": { "1": { "name": "R1", "status": 0, // ... }, "2": { "name": "R2", "status": 0, // ... } } } Another example is a list of interfaces for a node. A typical node returns an array for Ethernet interfaces, and an empty array for Serial interfaces as they’re unsupported: { "data": { "ethernet": [ { "name": "Gi0/0", "network_id": 1 }, { "name": "Gi0/1", "network_id": 3 } ], "serial": [], } } The node type that supports…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.