Thursday, 30 July 2020

Rust + Serde + JSON

I spent a lot of time researching how to parse multi-entry JSON via Serde - I finally worked it out, so figured I'd document it here...


Haven't posted in ages. A lot of work being done under NDAs, plus a bit of family tragedy got in the way. I'm out from under NDA, and have my mojo back. Lately, I've been playing with Rust. Whenever I learn a new language or platform, I like to revisit something I've done before. Often this is a simple web page serving up a random verse from the Tao te Ching.

Normally I load the Tao te Ching into a database, and this time I started using MongoDB - I've never used it before, nor any NoSQL DBs, so I thought I'd have a play. It worked well, reasonably fast, got spammed a bit by their marketing department. It was surprising difficult to get a random entry - this might be true of any NoSQL DB - understandably, they don't seem geared up for that.

While looking for another solution, I started wondering if a DB was even the right tool - I have a static set of data, not really that big, why not keep it in a file, or even load it into RAM - enter Serde.

I got my Tao te Ching into a JSON file using OpenRefine - but couldn't get Serde to parse it. Serde wants you to define a struct that represents the JSON format. Every example I found on the web, showed how to do it with a single record, but not for multiple records. I assumed, that my struct would be used on a per-record basis. It kind of is, but you need an array to achieve that. In the end I created a struct, that had an array of another struct inside, and suddenly boom! it worked

This is the struct:




#[derive(Deserialize, Debug)]
struct Tao {
     tao: Vec,
}

#[derive(Deserialize, Debug)]
    struct Verse {
    chapter: u8,
    title: String,
    verse: String,
}

...and this is the JSON is works with:


{"tao" : [
{   "chapter" : 1,
     "title" : " TAOING ",
     "verse" : "The way you can go isn’t the real way. The name you can say isn’t the real name. Heaven and earth begin in the unnamed: name’s the mother of the ten thousand things. So the unwanted soul sees what’s hidden, and the ever-wanting soul sees only what it wants. Two things, one origin, but different in name, whose identity is mystery. Mystery of all mysteries! The door to the hidden. > **Note** UKLG: A satisfactory translation of this chapter is, I believe, perfectly impossible. It contains the book. I think of it as the Aleph, in Borges’s story: if you can see it rightly, it contains everything. "},
{   "chapter" : 2,
     "title" : " SOUL FOOD ",
     "verse" : "Everybody on earth knowing that beauty is beautiful makes ugliness. Everybody knowing that goodness is good makes wickedness. For being and nonbeing arise together; hard and easy complete each other; high and low depend on each other; note and voice make the music together; before and after follow each other. That’s why the wise soul does without doing, teaches without talking. The things of this world exist, they are; you can’t refuse them. To bear and not to own; to act and not lay claim; to do the work and let it go. for just letting it go is what makes it stay. > **Note** UKLG: One of the things I read in this chapter is that values and beliefs are not only culturally constructed but also part of the interplay of yin and yang, the great reversals that maintain the living balance of the world. To believe that our beliefs are permanent truths which encompass reality is a sad arrogance. To let go of that belief is to find safety. "},
...