Skip to content

Commit

Permalink
Add deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-zepol committed Mar 21, 2024
1 parent 26de50b commit 3083c5d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/cyclonedx/model/ReleaseNotes.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public ReleaseNotes() {}
@VersionFilter(versions = {"1.0", "1.1", "1.2", "1.3"})
private Date timestamp;
private List<String> aliases;
@JsonProperty("tags")
private Tags tags;
private List<Resolves> resolves;
private List<Notes> notes;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/cyclonedx/model/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class Service extends ExtensibleElement {
@VersionFilter(versions = {"1.0", "1.1", "1.2"})
private List<Property> properties;
@VersionFilter(versions = {"1.0", "1.1", "1.2", "1.3", "1.4", "1.5"})
@JsonProperty("tags")
private Tags tags;
private List<Service> services;
private ReleaseNotes releaseNotes;
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/org/cyclonedx/model/component/Tags.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import org.cyclonedx.util.deserializer.StringListDeserializer;
import org.cyclonedx.util.deserializer.TagsDeserializer;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonDeserialize(using = TagsDeserializer.class)
public class Tags
{
private List<String> tags;

@JacksonXmlElementWrapper(localName = "tags")
@JacksonXmlProperty(localName = "tag")
@JsonDeserialize(using = StringListDeserializer.class)
public Tags() {
}

public Tags(List<String> tags){
this.tags = tags;
}

public List<String> getTags() {
return tags;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.cyclonedx.util.deserializer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.cyclonedx.model.component.Tags;

public class TagsDeserializer
extends JsonDeserializer<Tags>
{

private final ObjectMapper mapper = new ObjectMapper();
@Override
public Tags deserialize(JsonParser parser, DeserializationContext context) throws IOException {
JsonNode node = parser.getCodec().readTree(parser);

if(node.has("tag")) {
return parseNode(node.get("tag"));
} else {
return parseNode(node);
}
}

private Tags parseNode(JsonNode node) {
List<String> list = new ArrayList<>();

ArrayNode nodes = (node.isArray() ? (ArrayNode) node : new ArrayNode(null).add(node));
for (JsonNode tagNode : nodes) {
list.add(tagNode.asText());
}

if(!list.isEmpty()) {
return new Tags(list);
}
return null;
}
}

0 comments on commit 3083c5d

Please sign in to comment.