diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..739cb36 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,30 @@ +# How to use LatticeJSON from different programming languages + +This directory contains some super basic examples, which could help you to get started +working with LatticeJSON files in the programming language of your choice. + +## Python + +[example.py](./example.py): Python natively supports JSON files. Just import the `json` module. Alternatively you +can use `latticejson` package, which provides some convenience functions. You can +install it with: + + pip install latticejson + +## Julia + +[example.jl](./example.jl): Use `Pkg.add("JSON")` to install the JSON library. + +## JavaScript / NodeJS + +[example.js](./example.js): This example uses NodeJS. Run it with: + + node example.js + + +## C + +[example.c](./example.c): There are several C libraries which are able to parse JSON files. For example you could +use json-c. To compile the example.c file use: + + cc example.c -ljson-c diff --git a/examples/example.c b/examples/example.c new file mode 100644 index 0000000..ddb7d93 --- /dev/null +++ b/examples/example.c @@ -0,0 +1,35 @@ +#include +#include + +const int buffer_size = 20 * 1024; + +int main(int argc, char **argv) { + char buffer[buffer_size]; + FILE *fp = fopen("fodo.json", "r"); + fread(buffer, buffer_size, 1, fp); + fclose(fp); + + struct json_object *parsed_json; + struct json_object *elements; + struct json_object *element; + struct json_object *type; + struct json_object *attributes; + struct json_object *length; + struct json_object *sub_lattices; + struct json_object *lattice; + + const char *element_name = "Q1"; + parsed_json = json_tokener_parse(buffer); + json_object_object_get_ex(parsed_json, "elements", &elements); + json_object_object_get_ex(elements, element_name, &element); + type = json_object_array_get_idx(element, 0); + attributes = json_object_array_get_idx(element, 1); + json_object_object_get_ex(attributes, "length", &length); + + printf( + "The element %s is a %s and is %f meters long.\n", + element_name, + json_object_get_string(type), + json_object_get_double(length) + ); +} diff --git a/examples/example.jl b/examples/example.jl new file mode 100644 index 0000000..383e740 --- /dev/null +++ b/examples/example.jl @@ -0,0 +1,11 @@ +import JSON + +open("fodo.json", "r") do file + global data + data = JSON.parse(file) +end + +element_name = "Q1" +type, attributes = data["elements"][element_name] +length = attributes["length"] +println("The element $element_name is a $type and is $length meters long!") diff --git a/examples/example.js b/examples/example.js new file mode 100644 index 0000000..e164ef3 --- /dev/null +++ b/examples/example.js @@ -0,0 +1,6 @@ +const fs = require('fs'); +const data = JSON.parse(fs.readFileSync("fodo.json")); + +const elementName = "Q1"; +const [type, { length }] = data.elements[elementName]; +console.log(`The element ${elementName} is a ${type} and is ${length} meters long.`); diff --git a/examples/example.py b/examples/example.py new file mode 100644 index 0000000..e348a8b --- /dev/null +++ b/examples/example.py @@ -0,0 +1,8 @@ +import json +from pathlib import Path + +data = json.loads(Path("fodo.json").read_text()) +element_name = "Q1" +type_, attributes = data["elements"][element_name] +length = attributes["length"] +print(f"The element {element_name} is a {type_} and is {length} meters long.") diff --git a/examples/fodo.json b/examples/fodo.json new file mode 120000 index 0000000..1fc250e --- /dev/null +++ b/examples/fodo.json @@ -0,0 +1 @@ +../tests/data/fodo.json \ No newline at end of file