Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions examples/example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <stdio.h>
#include <json-c/json.h>

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)
);
}
11 changes: 11 additions & 0 deletions examples/example.jl
Original file line number Diff line number Diff line change
@@ -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!")
6 changes: 6 additions & 0 deletions examples/example.js
Original file line number Diff line number Diff line change
@@ -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.`);
8 changes: 8 additions & 0 deletions examples/example.py
Original file line number Diff line number Diff line change
@@ -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.")
1 change: 1 addition & 0 deletions examples/fodo.json