$darkmode
DENOPTIM
IAtomContainerDeserializer.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;
22
23import org.openscience.cdk.interfaces.IAtomContainer;
24import org.openscience.cdk.interfaces.IChemObjectBuilder;
25import org.openscience.cdk.silent.SilentChemObjectBuilder;
26
27import com.google.gson.JsonArray;
28import com.google.gson.JsonDeserializationContext;
29import com.google.gson.JsonDeserializer;
30import com.google.gson.JsonElement;
31import com.google.gson.JsonObject;
32import com.google.gson.JsonParseException;
33
34
42public class IAtomContainerDeserializer implements JsonDeserializer<IAtomContainer>
43{
44
45 private IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
46
47 @Override
48 public IAtomContainer deserialize(JsonElement jsonEl, Type type,
49 JsonDeserializationContext context) throws JsonParseException
50 {
51 JsonObject jo = jsonEl.getAsJsonObject();
54 return null;
55
56 IAtomContainer iac = builder.newAtomContainer();
57
58 JsonArray atomArr = jo.get(
59 IAtomContainerSerializer.ATOMSKEY).getAsJsonArray();
60 for (JsonElement e : atomArr)
61 {
62 LWAtom lwAtm = context.deserialize(e,LWAtom.class);
63 iac.addAtom(lwAtm.toIAtom());
64 }
65
66 JsonArray bondArr = jo.get(
67 IAtomContainerSerializer.BONDSKEY).getAsJsonArray();
68 for (JsonElement e : bondArr)
69 {
70 LWBond lwBnd = context.deserialize(e,LWBond.class);
71 iac.addBond(lwBnd.atomIds[0], lwBnd.atomIds[1], lwBnd.bo);
72 }
73
74 return iac;
75 }
76}
Deserialisation of collections of both light-weight atoms and bonds into a CDK IAtomContainer.
IAtomContainer deserialize(JsonElement jsonEl, Type type, JsonDeserializationContext context)
Class to serialise CDK's IAtomContainer in a simplified manner.
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
IAtom toIAtom()
Returns a CDK representation of this center.
Definition: LWAtom.java:67
A light-weight bond representation to facilitate json serialization of IBond.
Definition: LWBond.java:31
int[] atomIds
0-based index of the centers, i.e., atoms or pseudo-atoms, connected by this bond.
Definition: LWBond.java:36
IBond.Order bo
Type of bond.
Definition: LWBond.java:41