JSONParser

class manim_chemistry.utils.parsers.json_parser.JSONParser(filename: str | bytes | PathLike)[source]

Parses JSON files

Examples

parsed_json = JSONParser(filename="acetone_2d.json")
print(parsed_json.atoms_data)
print(parsed_json.bonds_data)
>>> {
    1: {"element": "O", "coords": array([3.732, 0.75, 0.0])},
    2: {"element": "C", "coords": array([2.866, 0.25, 0.0])},
    3: {"element": "C", "coords": array([2.0, 0.75, 0.0])},
    4: {"element": "C", "coords": array([2.866, -0.75, 0.0])},
    5: {"element": "H", "coords": array([2.31, 1.2869, 0.0])},
    6: {"element": "H", "coords": array([1.4631, 1.06, 0.0])},
    7: {"element": "H", "coords": array([1.69, 0.2131, 0.0])},
    8: {"element": "H", "coords": array([2.246, -0.75, 0.0])},
    9: {"element": "H", "coords": array([2.866, -1.37, 0.0])},
    10: {"element": "H", "coords": array([3.486, -0.75, 0.0])},
}
>>> {
    0: {"from_atom_index": 1, "to_atom_index": 2, "bond_type": 2},
    1: {"from_atom_index": 2, "to_atom_index": 3, "bond_type": 1},
    2: {"from_atom_index": 2, "to_atom_index": 4, "bond_type": 1},
    3: {"from_atom_index": 3, "to_atom_index": 5, "bond_type": 1},
    4: {"from_atom_index": 3, "to_atom_index": 6, "bond_type": 1},
    5: {"from_atom_index": 3, "to_atom_index": 7, "bond_type": 1},
    6: {"from_atom_index": 4, "to_atom_index": 8, "bond_type": 1},
    7: {"from_atom_index": 4, "to_atom_index": 9, "bond_type": 1},
    8: {"from_atom_index": 4, "to_atom_index": 10, "bond_type": 1},
}
inherited-members:

static data_parser(data: Any) Tuple[Dict, Dict] | List[Tuple[Dict, Dict]][source]

Parses the atoms and bonds data and returns a tuple of dictionaries with each data. The Json format usually follows the structure:

{
<Origin of the file>: [
{

<Molecule data>

}

]

}

The atom data follows the structure:

{<atom_index>: {“element”: <atom_element>, “position”: [<x_pos>, <y_pos>, <z_pos>]}}

The bond data follows the structure:

{<bond_index>: {“from_atom_index”: <from_atom_index>, “to_atom_index”: <to_atom_index>, “bond_type”: <bond_type>}}

static read_file(filename: str | bytes | PathLike) Any[source]

Reads the file and converts it to a string.

Returns:

str: String with the file data.