Get the latest tech news

Abusing C to implement JSON parsing with struct methods


Json parsing in C using function pointers attached to a struct

The json_value struct holds the type, as defined above, a union sharing memory space for either a boolean, a string or a number, a list of json_value structures as array children or object values, a list of strings that are object keys and the length for the three aforementioned fields. Since some of the fields in json_value are heap allocated, we have to destroy / free the structure upon either no longer using it or exiting the process. Of course you have to define a function the c way (<return type> <name>(<list of params>);) and assign it to your method field - but I is not that complicated:

Get the Android app

Or read this on Hacker News

Read more on:

Photo of json

json

Photo of struct methods

struct methods

Related news:

News photo

JSON has become today's machine-readable output format (on Unix)

News photo

Parsing JSON in 500 lines of Rust

News photo

MessagePack: It's like JSON, but fast and small.