|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (C) 2023 the Eclipse BaSyx Authors |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | + * a copy of this software and associated documentation files (the |
| 6 | + * "Software"), to deal in the Software without restriction, including |
| 7 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 8 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | + * permit persons to whom the Software is furnished to do so, subject to |
| 10 | + * the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be |
| 13 | + * included in all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 19 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 20 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 21 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | + * |
| 23 | + * SPDX-License-Identifier: MIT |
| 24 | + ******************************************************************************/ |
| 25 | +package org.eclipse.basyx.aas.factory.json; |
| 26 | + |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.stream.Collectors; |
| 30 | + |
| 31 | +import org.eclipse.basyx.aas.metamodel.map.AssetAdministrationShell; |
| 32 | +import org.eclipse.basyx.aas.metamodel.map.descriptor.AASDescriptor; |
| 33 | +import org.eclipse.basyx.submodel.metamodel.api.ISubmodel; |
| 34 | +import org.eclipse.basyx.submodel.metamodel.api.reference.IKey; |
| 35 | +import org.eclipse.basyx.submodel.metamodel.map.Submodel; |
| 36 | +import org.eclipse.basyx.submodel.metamodel.map.reference.Key; |
| 37 | +import org.eclipse.basyx.vab.coder.json.serialization.DefaultTypeFactory; |
| 38 | +import org.eclipse.basyx.vab.coder.json.serialization.GSONTools; |
| 39 | + |
| 40 | +/** |
| 41 | + * Serializes and deserializes shells and submodels to and from json |
| 42 | + * |
| 43 | + * @author jungjan |
| 44 | + * |
| 45 | + */ |
| 46 | +public class BidirectionalJSONConverter { |
| 47 | + private static GSONTools gsonTools = new GSONTools(new DefaultTypeFactory()); |
| 48 | + |
| 49 | + public static String serializeSubmodel(ISubmodel submodel) { |
| 50 | + return gsonTools.serialize(submodel); |
| 51 | + } |
| 52 | + |
| 53 | + @SuppressWarnings("unchecked") |
| 54 | + public static ISubmodel deserializeSubmodel(String jsonSubmodel) { |
| 55 | + return Submodel.createAsFacade((Map<String, Object>) gsonTools.deserialize(jsonSubmodel)); |
| 56 | + } |
| 57 | + |
| 58 | + public static String serializeShell(AssetAdministrationShell shell) { |
| 59 | + return gsonTools.serialize(shell); |
| 60 | + } |
| 61 | + |
| 62 | + @SuppressWarnings("unchecked") |
| 63 | + public static AssetAdministrationShell deserializeShell(String jsonShell) { |
| 64 | + return AssetAdministrationShell.createAsFacade((Map<String, Object>) gsonTools.deserialize(jsonShell)); |
| 65 | + } |
| 66 | + |
| 67 | + public static <T> String serializeObject(T object) { |
| 68 | + return gsonTools.serialize(object); |
| 69 | + } |
| 70 | + |
| 71 | + @SuppressWarnings("unchecked") |
| 72 | + public static <T> T deserializeJSON(String json) { |
| 73 | + T retrieved = (T) gsonTools.deserialize(json); |
| 74 | + return retrieved; |
| 75 | + } |
| 76 | + |
| 77 | + @SuppressWarnings("unchecked") |
| 78 | + public static AASDescriptor deserializeAASDescriptor(String json) { |
| 79 | + return AASDescriptor.createAsFacade((Map<String, Object>) gsonTools.deserialize(json)); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * |
| 84 | + * @param submodel |
| 85 | + * @return the semanticId of a submodel serialized according to the following schema: |
| 86 | + * {@code {type:<type>;value:<value>;idType:<idType>[/]}}. |
| 87 | + * Example: {@code type:Submodel;value:a value;idType:Custom/type:Submodel;value:another value;idType:Custom} |
| 88 | + */ |
| 89 | + public static String semanticIdAsSString(ISubmodel submodel) { |
| 90 | + if (submodel.getSemanticId() == null) { |
| 91 | + return null; |
| 92 | + } |
| 93 | + List<IKey> keys = submodel.getSemanticId().getKeys(); |
| 94 | + return semanticIdKeysToString(keys); |
| 95 | + } |
| 96 | + |
| 97 | + public static String semanticIdKeysToString(List<IKey> keys) { |
| 98 | + return keys.stream().map(k -> |
| 99 | + Key.TYPE + ":" + k.getType() + ";" |
| 100 | + /* + Key.LOCAL + ":" + k.isLocal() + ";" */ //ignoring local since it won't be relevant for V3 |
| 101 | + + Key.VALUE + ":" + k.getValue() + ";" |
| 102 | + + Key.IDTYPE + ":" + k.getIdType()) |
| 103 | + .collect(Collectors.joining("/")); |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments