From 8cfa2736da89c4da26aa851a1ea3c1b8db09200b Mon Sep 17 00:00:00 2001 From: Darin Kelkhoff Date: Mon, 20 Mar 2023 11:15:05 -0500 Subject: [PATCH] Add jackson-dataformat-yaml and Initial checkin --- qqq-backend-core/pom.xml | 9 ++- .../qqq/backend/core/utils/YamlUtils.java | 81 +++++++++++++++++++ .../qqq/backend/core/utils/YamlUtilsTest.java | 73 +++++++++++++++++ 3 files changed, 161 insertions(+), 2 deletions(-) create mode 100644 qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/utils/YamlUtils.java create mode 100644 qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/utils/YamlUtilsTest.java diff --git a/qqq-backend-core/pom.xml b/qqq-backend-core/pom.xml index e634cde6..76d0300a 100644 --- a/qqq-backend-core/pom.xml +++ b/qqq-backend-core/pom.xml @@ -65,12 +65,17 @@ com.fasterxml.jackson.core jackson-databind - 2.14.0-rc1 + 2.14.0 com.fasterxml.jackson.datatype jackson-datatype-jsr310 - 2.13.0 + 2.14.0 + + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + 2.14.0 org.json diff --git a/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/utils/YamlUtils.java b/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/utils/YamlUtils.java new file mode 100644 index 00000000..1c7a8f53 --- /dev/null +++ b/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/utils/YamlUtils.java @@ -0,0 +1,81 @@ +/* + * QQQ - Low-code Application Framework for Engineers. + * Copyright (C) 2021-2023. Kingsrook, LLC + * 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States + * contact@kingsrook.com + * https://github.com/Kingsrook/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.kingsrook.qqq.backend.core.utils; + + +import java.util.Map; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; +import com.kingsrook.qqq.backend.core.logging.QLogger; + + +/******************************************************************************* + ** + *******************************************************************************/ +public class YamlUtils +{ + private static final QLogger LOG = QLogger.getLogger(YamlUtils.class); + + + + /******************************************************************************* + ** + *******************************************************************************/ + public static Map toMap(String yaml) throws JsonProcessingException + { + ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); + objectMapper.findAndRegisterModules(); + return (objectMapper.readValue(yaml, Map.class)); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + public static String toYaml(Object object) + { + try + { + YAMLFactory yamlFactory = new YAMLFactory() + .disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER); + + ObjectMapper objectMapper = new ObjectMapper(yamlFactory); + objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); + + // todo? objectMapper.setFilterProvider(new OmitDefaultValuesFilterProvider()); + + objectMapper.findAndRegisterModules(); + return (objectMapper.writeValueAsString(object)); + } + catch(Exception e) + { + LOG.error("Error serializing object of type [" + object.getClass().getSimpleName() + "] to yaml", e); + throw new IllegalArgumentException("Error in YAML Serialization", e); + } + } + +} diff --git a/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/utils/YamlUtilsTest.java b/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/utils/YamlUtilsTest.java new file mode 100644 index 00000000..a41afa18 --- /dev/null +++ b/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/utils/YamlUtilsTest.java @@ -0,0 +1,73 @@ +/* + * QQQ - Low-code Application Framework for Engineers. + * Copyright (C) 2021-2023. Kingsrook, LLC + * 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States + * contact@kingsrook.com + * https://github.com/Kingsrook/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.kingsrook.qqq.backend.core.utils; + + +import java.util.Map; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.kingsrook.qqq.backend.core.model.metadata.QInstance; +import org.junit.jupiter.api.Test; + + +/******************************************************************************* + ** Unit test for YamlUtils + *******************************************************************************/ +class YamlUtilsTest +{ + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void test() throws JsonProcessingException + { + String yaml = """ + orderNo: "B-9910" + date: "2019-04-18" + customerName: "Customer, Jane" + orderLines: + - item: "Copper Wire (200ft)" + quantity: 1 + unitPrice: 50.67 + - item: "Washers (1/4\\")" + quantity: 24 + unitPrice: 0.15 + """; + + Map map = YamlUtils.toMap(yaml); + System.out.println(map); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void testQInstanceToYaml() + { + QInstance qInstance = TestUtils.defineInstance(); + String yaml = YamlUtils.toYaml(qInstance); + System.out.println(yaml); + } + +} \ No newline at end of file