I'm new to Rust and want to understand its concepts, that seem to be somewhat different to other languages.
Consider the following JSON, that I want to extract all objects from:
{
"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}
}
Here's a sample PHP code that illustrates the task:
function walkObject( $name, $x, &$result ): void {
if( !is_object( $x ) ) return;
$result[] = $name;
foreach( $x as $name => $field )
walkObject( $name, $field, $result );
}
$json = file_get_contents( '/media/sf_shared/test.json' );
$object = json_decode( $json );
$result = [];
walkObject( 'root', $object, $result );
print json_encode( $result ) . PHP_EOL;
Here's its output:
["root","widget","window","image","text"]
Now when I try to achieve the same in Rust, I face the following questions:
- What is the recommended JSON parser that I should use for easy content manipulation?
- How do I "walk" the JSON tree?
- How do I test if a particular JSON element is an "object", "array", or "primitive"?