$darkmode
DENOPTIM
IAtomContainerSerializer.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2022 Marco Foscato <marco.foscato@uib.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.json;
20
21import java.lang.reflect.Type;
22import java.util.ArrayList;
23import java.util.List;
24
25import org.openscience.cdk.interfaces.IAtom;
26import org.openscience.cdk.interfaces.IAtomContainer;
27import org.openscience.cdk.interfaces.IBond;
28
29import com.google.gson.JsonElement;
30import com.google.gson.JsonObject;
31import com.google.gson.JsonSerializationContext;
32import com.google.gson.JsonSerializer;
33
34import denoptim.utils.MoleculeUtils;
35
36
47public class IAtomContainerSerializer implements JsonSerializer<IAtomContainer>
48{
52 protected static final String ATOMSKEY = "atoms";
53
57 protected static final String BONDSKEY = "bonds";
58
59 @Override
60 public JsonElement serialize(IAtomContainer iac, Type typeOfSrc,
61 JsonSerializationContext context)
62 {
63 JsonObject jsonObject = new JsonObject();
64 List<LWAtom> atoms = new ArrayList<LWAtom>();
65 for (IAtom atm : iac.atoms())
66 {
67 atoms.add(new LWAtom(MoleculeUtils.getSymbolOrLabel(atm),
69 }
70 List<LWBond> bonds = new ArrayList<LWBond>();
71 for (IBond bnd : iac.bonds())
72 {
73 bonds.add(new LWBond(iac.indexOf(bnd.getAtom(0)),
74 iac.indexOf(bnd.getAtom(1)), bnd.getOrder()));
75 }
76
77 jsonObject.add(ATOMSKEY, context.serialize(atoms));
78 jsonObject.add(BONDSKEY, context.serialize(bonds));
79
80 return jsonObject;
81 }
82}
Class to serialise CDK's IAtomContainer in a simplified manner.
JsonElement serialize(IAtomContainer iac, Type typeOfSrc, JsonSerializationContext context)
static final String ATOMSKEY
String used to identify the list of atoms in json map.
static final String BONDSKEY
String used to identify the list of bonds in json map.
A light-weight atom representation to facilitate json serialization of IAtom.
Definition: LWAtom.java:37
A light-weight bond representation to facilitate json serialization of IBond.
Definition: LWBond.java:31
Utilities for molecule conversion.
static String getSymbolOrLabel(IAtom atm)
Gets either the elemental symbol (for standard atoms) of the label (for pseudo-atoms).
static Point3d getPoint3d(IAtom atm)
Return the 3D coordinates, if present.