antirez' tiny JSON selector library
Nov 27, 2023
Salvatore Sanfilippo, aka antirez released a telegram bot library in C recently. While I'm not at all interested in a telegram bot library, I always like to read Salvatore's code, which is uniformly superb.
The bit of code that caught my eye in this release is in this file, which implements a simple selector library on top of the json library he's using. In about 100 lines of code, it implements a function that parses and executes a tiny jq-like language:
/* You can select things like this:
*
* cJSON *json = cJSON_Parse(myjson_string);
* cJSON *width = cJSON_Select(json,".features.screens[*].width",4);
* cJSON *height = cJSON_Select(json,".features.screens[4].*","height");
* cJSON *price = cJSON_Select(json,".features.screens[4].price_*",
* price_type == EUR ? "eur" : "usd");
It's a really nice example of just how simple you can make a parser for a tiny language.
I think it'd be fun to re-implement this in another language to understand it better, and I might give it a go at some point.
from Hacker News https://ift.tt/0HnGT3w
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.