diff --git a/cli/src/main/java/com/devonfw/tools/ide/json/DependencyInfoDeserializer.java b/cli/src/main/java/com/devonfw/tools/ide/json/DependencyInfoDeserializer.java new file mode 100644 index 000000000..f8ae53948 --- /dev/null +++ b/cli/src/main/java/com/devonfw/tools/ide/json/DependencyInfoDeserializer.java @@ -0,0 +1,49 @@ +package com.devonfw.tools.ide.json; + +import java.io.IOException; + +import com.devonfw.tools.ide.url.model.file.json.DependencyInfo; +import com.devonfw.tools.ide.version.VersionRange; +import com.fasterxml.jackson.core.JacksonException; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; + +/** + * {@link JsonDeserializer} for {@link DependencyInfo}. + */ +public class DependencyInfoDeserializer extends JsonDeserializer { + + @Override + public DependencyInfo deserialize(JsonParser p, DeserializationContext context) throws IOException, JacksonException { + + JsonToken token = p.getCurrentToken(); + if (token == JsonToken.START_OBJECT) { + token = p.nextToken(); + String tool = null; + VersionRange versionRange = null; + while (token == JsonToken.FIELD_NAME) { + String property = p.currentName(); + if (property.equals("tool")) { + token = p.nextToken(); + assert token == JsonToken.VALUE_STRING; + tool = p.getValueAsString(); + } else if (property.equals("versionRange")) { + token = p.nextToken(); + assert (token == JsonToken.START_OBJECT) || (token == JsonToken.VALUE_STRING); + versionRange = p.readValueAs(VersionRange.class); + } else { + // ignore unknown property + // currently cannot log here due to https://github.com/devonfw/IDEasy/issues/404 + } + token = p.nextToken(); + } + if ((tool != null) && (versionRange != null)) { + return new DependencyInfo(tool, versionRange); + } + } + throw new IllegalArgumentException("Invalid JSON for DependencyInfo!"); + } + +} diff --git a/cli/src/main/java/com/devonfw/tools/ide/json/JsonMapping.java b/cli/src/main/java/com/devonfw/tools/ide/json/JsonMapping.java index 36629d24a..a1b710740 100644 --- a/cli/src/main/java/com/devonfw/tools/ide/json/JsonMapping.java +++ b/cli/src/main/java/com/devonfw/tools/ide/json/JsonMapping.java @@ -1,5 +1,6 @@ package com.devonfw.tools.ide.json; +import com.devonfw.tools.ide.url.model.file.json.DependencyInfo; import com.devonfw.tools.ide.version.VersionIdentifier; import com.devonfw.tools.ide.version.VersionRange; import com.fasterxml.jackson.annotation.JsonInclude; @@ -28,6 +29,7 @@ public static ObjectMapper create() { SimpleModule customModule = new SimpleModule(); customModule.addDeserializer(VersionIdentifier.class, new VersionIdentifierDeserializer()); customModule.addDeserializer(VersionRange.class, new VersionRangeDeserializer()); + customModule.addDeserializer(DependencyInfo.class, new DependencyInfoDeserializer()); customModule.addKeyDeserializer(VersionRange.class, new VersionRangeKeyDeserializer()); mapper = mapper.registerModule(customModule); return mapper; diff --git a/cli/src/test/java/com/devonfw/tools/ide/json/JsonMappingTest.java b/cli/src/test/java/com/devonfw/tools/ide/json/JsonMappingTest.java index d2308bd7d..b618dc899 100644 --- a/cli/src/test/java/com/devonfw/tools/ide/json/JsonMappingTest.java +++ b/cli/src/test/java/com/devonfw/tools/ide/json/JsonMappingTest.java @@ -5,6 +5,10 @@ import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; +import com.devonfw.tools.ide.url.model.file.json.DependencyInfo; +import com.devonfw.tools.ide.version.BoundaryType; +import com.devonfw.tools.ide.version.VersionIdentifier; +import com.devonfw.tools.ide.version.VersionRange; import com.fasterxml.jackson.databind.ObjectMapper; /** @@ -26,12 +30,12 @@ public class JsonMappingTest extends Assertions { @Test public void testReadInstant() throws Exception { - // given + // arrange String value = INSTANT_VALUE_JSON; - // when + // act ObjectMapper mapper = JsonMapping.create(); Instant instant = mapper.readValue(value, Instant.class); - // then + // assert assertThat(instant).isEqualTo(INSTANT_VALUE); } @@ -43,13 +47,27 @@ public void testReadInstant() throws Exception { @Test public void testWriteInstant() throws Exception { - // given + // arrange Instant value = INSTANT_VALUE; - // when + // act ObjectMapper mapper = JsonMapping.create(); String json = mapper.writeValueAsString(value); - // then + // assert assertThat(json).isEqualTo(INSTANT_VALUE_JSON); } + @Test + public void testReadDependencies() throws Exception { + + // arrange + String json = "{\"tool\": \"java\",\"versionRange\": \"[11,21_35]\"}"; + VersionRange expectedVersionRange = VersionRange.of(VersionIdentifier.of("11"), VersionIdentifier.of("21_35"), BoundaryType.CLOSED); + // act + ObjectMapper mapper = JsonMapping.create(); + DependencyInfo dependencyInfo = mapper.readValue(json, DependencyInfo.class); + // assert + assertThat(dependencyInfo.tool()).isEqualTo("java"); + assertThat(dependencyInfo.versionRange()).isEqualTo(expectedVersionRange); + } + }