From c18840d34e2da1b88064f719eb203b8b20c4f2ee Mon Sep 17 00:00:00 2001 From: Felix Andreas Date: Fri, 17 Apr 2020 01:14:23 +0200 Subject: [PATCH 1/8] add examples for python js julia and c --- examples/.gitignore | 1 + examples/README.md | 30 ++++++++++++++++++++++++++++++ examples/example.c | 35 +++++++++++++++++++++++++++++++++++ examples/example.jl | 11 +++++++++++ examples/example.js | 6 ++++++ examples/example.py | 8 ++++++++ examples/fodo.json | 1 + 7 files changed, 92 insertions(+) create mode 100644 examples/.gitignore create mode 100644 examples/README.md create mode 100644 examples/example.c create mode 100644 examples/example.jl create mode 100644 examples/example.js create mode 100644 examples/example.py create mode 120000 examples/fodo.json diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 0000000..f8305e7 --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1 @@ +a.out \ No newline at end of file diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..9e7ce5d --- /dev/null +++ b/examples/README.md @@ -0,0 +1,30 @@ +# How to use LatticeJSON from different programming languages + +This directory contains some super basix examples, which could help you to get started +open 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: This example uses NodeJS. Run it with: + + node example.js + + +## 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 \ No newline at end of file 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..ef8cc00 --- /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!") \ No newline at end of file diff --git a/examples/example.js b/examples/example.js new file mode 100644 index 0000000..d8c9c0d --- /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["Q1"]; +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 From 811340bdfde16d6c952fa33e4cf28d218233e0df Mon Sep 17 00:00:00 2001 From: Felix Andreas Date: Fri, 17 Apr 2020 10:40:40 +0200 Subject: [PATCH 2/8] push all --- examples/example.jl | 2 +- examples/example.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/example.jl b/examples/example.jl index ef8cc00..383e740 100644 --- a/examples/example.jl +++ b/examples/example.jl @@ -8,4 +8,4 @@ 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!") \ No newline at end of file +println("The element $element_name is a $type and is $length meters long!") diff --git a/examples/example.js b/examples/example.js index d8c9c0d..e164ef3 100644 --- a/examples/example.js +++ b/examples/example.js @@ -2,5 +2,5 @@ const fs = require('fs'); const data = JSON.parse(fs.readFileSync("fodo.json")); const elementName = "Q1"; -const [type, { length }] = data.elements["Q1"]; +const [type, { length }] = data.elements[elementName]; console.log(`The element ${elementName} is a ${type} and is ${length} meters long.`); From 026cb14b308b52a4bc36f3217833d2630b36b97f Mon Sep 17 00:00:00 2001 From: Felix Andreas Date: Fri, 17 Apr 2020 10:50:23 +0200 Subject: [PATCH 3/8] Delete .gitignore --- examples/.gitignore | 1 - 1 file changed, 1 deletion(-) delete mode 100644 examples/.gitignore diff --git a/examples/.gitignore b/examples/.gitignore deleted file mode 100644 index f8305e7..0000000 --- a/examples/.gitignore +++ /dev/null @@ -1 +0,0 @@ -a.out \ No newline at end of file From d38f30523ef2b2c811306ea29b58f64572b5cda3 Mon Sep 17 00:00:00 2001 From: Felix Andreas Date: Fri, 17 Apr 2020 10:51:08 +0200 Subject: [PATCH 4/8] Update README.md --- examples/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/README.md b/examples/README.md index 9e7ce5d..07edc0e 100644 --- a/examples/README.md +++ b/examples/README.md @@ -17,14 +17,14 @@ install it with: ## JavaScript / NodeJS -example.js: This example uses NodeJS. Run it with: +[example.js](examle.js): This example uses NodeJS. Run it with: node example.js ## C -There are several C libraries which are able to parse JSON files. For example you could +[example.c](examle.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 \ No newline at end of file + cc example.c -ljson-c From 8bcb01210771a00ee002f9c50c05fda6baf142dc Mon Sep 17 00:00:00 2001 From: Felix Andreas Date: Fri, 17 Apr 2020 11:12:37 +0200 Subject: [PATCH 5/8] Update README.md --- examples/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/README.md b/examples/README.md index 07edc0e..10a2a01 100644 --- a/examples/README.md +++ b/examples/README.md @@ -5,7 +5,7 @@ open 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 +[./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: @@ -13,18 +13,18 @@ install it with: ## Julia -[example.jl](example.jl): Use `Pkg.add("JSON")` to install the JSON library. +[./example.jl](example.jl): Use `Pkg.add("JSON")` to install the JSON library. ## JavaScript / NodeJS -[example.js](examle.js): This example uses NodeJS. Run it with: +[./example.js](examle.js): This example uses NodeJS. Run it with: node example.js ## C -[example.c](examle.c): There are several C libraries which are able to parse JSON files. For example you could +[./example.c](examle.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 From b3c86d083aeeb49b0682a9a94b62d25705ecb476 Mon Sep 17 00:00:00 2001 From: Felix Andreas Date: Fri, 17 Apr 2020 11:13:12 +0200 Subject: [PATCH 6/8] Update README.md --- examples/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/README.md b/examples/README.md index 10a2a01..0eab566 100644 --- a/examples/README.md +++ b/examples/README.md @@ -5,7 +5,7 @@ open 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 +[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: @@ -13,18 +13,18 @@ install it with: ## Julia -[./example.jl](example.jl): Use `Pkg.add("JSON")` to install the JSON library. +[example.jl](./example.jl): Use `Pkg.add("JSON")` to install the JSON library. ## JavaScript / NodeJS -[./example.js](examle.js): This example uses NodeJS. Run it with: +[example.js](./examle.js): This example uses NodeJS. Run it with: node example.js ## C -[./example.c](examle.c): There are several C libraries which are able to parse JSON files. For example you could +[example.c](./examle.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 From fb9a6e6a40eb3c85ebbcf8eaf90d56893d9e9d89 Mon Sep 17 00:00:00 2001 From: Felix Andreas Date: Fri, 17 Apr 2020 11:14:01 +0200 Subject: [PATCH 7/8] Update README.md --- examples/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/README.md b/examples/README.md index 0eab566..b74f30e 100644 --- a/examples/README.md +++ b/examples/README.md @@ -17,14 +17,14 @@ install it with: ## JavaScript / NodeJS -[example.js](./examle.js): This example uses NodeJS. Run it with: +[example.js](./example.js): This example uses NodeJS. Run it with: node example.js ## C -[example.c](./examle.c): There are several C libraries which are able to parse JSON files. For example you could +[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 From 436fb0c070c988b238614ecbd71584dfbb387017 Mon Sep 17 00:00:00 2001 From: Felix Andreas Date: Fri, 17 Apr 2020 11:18:33 +0200 Subject: [PATCH 8/8] Update README.md --- examples/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/README.md b/examples/README.md index b74f30e..739cb36 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,7 +1,7 @@ # How to use LatticeJSON from different programming languages -This directory contains some super basix examples, which could help you to get started -open LatticeJSON files in the programming language of your choice. +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