Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ protected void setEnvironmentVars(final JsonNode node, AbstractType data) throws
JsonNode nodes = node.get("environmentVars");
List<EnvVariableChoice> environmentVars = new ArrayList<>();

ArrayNode environmentVarsNode = (nodes.isArray() ? (ArrayNode) nodes : new ArrayNode(null).add(nodes));

for (JsonNode envVarNode : environmentVarsNode) {
EnvVariableChoice envVar = objectMapper.treeToValue(envVarNode, EnvVariableChoice.class);
environmentVars.add(envVar);
if (nodes != null) {
ArrayNode environmentVarsNode = DeserializerUtils.getArrayNode(nodes, objectMapper);
for (JsonNode envVarNode : environmentVarsNode) {
EnvVariableChoice envVar = objectMapper.treeToValue(envVarNode, EnvVariableChoice.class);
environmentVars.add(envVar);
}
}

data.setEnvironmentVars(environmentVars);
}

protected void setReference(JsonNode node, String fieldName, AbstractType type)
throws JsonProcessingException
{
protected void setReference(JsonNode node, String fieldName, AbstractType type) throws JsonProcessingException {
if (node.has(fieldName)) {
JsonNode fieldNode = node.get(fieldName);
ResourceReferenceChoice reference = objectMapper.treeToValue(fieldNode, ResourceReferenceChoice.class);
Expand All @@ -62,4 +62,9 @@ protected void setReference(JsonNode node, String fieldName, AbstractType type)
}
}
}

protected void setSourceAndTarget(JsonNode node, AbstractType type) throws JsonProcessingException {
setReference(node, "source", type);
setReference(node, "target", type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,31 @@ public AttachmentText deserialize(JsonParser parser, DeserializationContext cont

AttachmentText attachmentText = new AttachmentText();

JsonNode textNode = node.get("content");
if (textNode != null) {
attachmentText.setText(textNode.textValue());
}
else if (node.has("")) {
attachmentText.setText( node.get("").asText());
JsonNode contentNode = node.get("content");
if (contentNode != null) {
attachmentText.setText(contentNode.asText());
} else if (node.has("")) {
attachmentText.setText(node.get("").asText());
}

JsonNode contentTypeNode = node.get("content-type");
if (contentTypeNode == null || !contentTypeNode.isTextual()) {
contentTypeNode = node.get("contentType");
}
JsonNode contentTypeNode = getContentTypeNode(node);
if (contentTypeNode != null && contentTypeNode.isTextual()) {
attachmentText.setContentType(contentTypeNode.textValue());
attachmentText.setContentType(contentTypeNode.asText());
}

JsonNode encodingNode = node.get("encoding");
if (encodingNode != null && encodingNode.isTextual()) {
attachmentText.setEncoding(encodingNode.textValue());
attachmentText.setEncoding(encodingNode.asText());
}

return attachmentText;
}

private JsonNode getContentTypeNode(JsonNode node) {
JsonNode contentTypeNode = node.get("content-type");
if (contentTypeNode == null || !contentTypeNode.isTextual()) {
contentTypeNode = node.get("contentType");
}
return contentTypeNode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ public class CertificationLevelDeserializer
@Override
public CertificationLevel deserialize(JsonParser parser, DeserializationContext context) throws IOException {
JsonNode node = parser.getCodec().readTree(parser);

JsonNode certificationLevelNode = node.has("certificationLevel") ? node.get("certificationLevel") : node;

if (certificationLevelNode.isArray()) {
return CertificationLevel.fromString(certificationLevelNode.get(0).asText());
}
else {
return CertificationLevel.fromString(certificationLevelNode.asText());
}
String certificationLevelText = certificationLevelNode.isArray() ?
certificationLevelNode.get(0).asText() :
certificationLevelNode.asText();

return CertificationLevel.fromString(certificationLevelText);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ public DatasetChoice deserialize(JsonParser jsonParser, DeserializationContext d
DatasetChoice datasetChoice = new DatasetChoice();

if (node.has("ref")) {
String ref = node.get("ref").asText();
datasetChoice.setRef(ref);
datasetChoice.setRef(node.get("ref").asText());
} else {
ComponentData componentData = objectMapper.treeToValue(node, ComponentData.class);
datasetChoice.setComponentData(componentData);
datasetChoice.setComponentData(objectMapper.treeToValue(node, ComponentData.class));
}

return datasetChoice;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.cyclonedx.util.deserializer;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;

public class DeserializerUtils
{
public static ArrayNode getArrayNode(JsonNode nodes, final ObjectMapper objectMapper) {
if (objectMapper != null) {
return nodes.isArray() ? (ArrayNode) nodes : new ArrayNode(objectMapper.getNodeFactory()).add(nodes);
}
else {
return nodes.isArray() ? (ArrayNode) nodes : new ArrayNode(null).add(nodes);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,40 @@ public class ExpressionDeserializer
extends JsonDeserializer<Expression>
{
@Override
public Expression deserialize(
final JsonParser p, final DeserializationContext ctxt) throws IOException
{
public Expression deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException {
JsonNode node = p.getCodec().readTree(p);

boolean hasParent = node.has("expression") && node.get("expression").isObject();

if (hasParent) {
if (node.has("expression") && node.get("expression").isObject()) {
node = node.get("expression");
}

if (node.isTextual()) {
return new Expression(node.asText());
}
else {
return parseExpressionNode(node);
}
}

Expression expression = new Expression();

if (node.get("bom-ref") != null) {
expression.setBomRef(node.get("bom-ref").asText());
}
private Expression parseExpressionNode(JsonNode node) {
Expression expression = new Expression();

if (node.get("acknowledgement") != null) {
expression.setAcknowledgement(node.get("acknowledgement").asText());
}
if (node.has("bom-ref")) {
expression.setBomRef(node.get("bom-ref").asText());
}

JsonNode textNode = node.get("expression");
if (textNode != null) {
expression.setValue(textNode.textValue());
}
else if (node.has("")) {
expression.setValue(node.get("").asText());
}
if (node.has("acknowledgement")) {
expression.setAcknowledgement(node.get("acknowledgement").asText());
}

return expression;
JsonNode textNode = node.get("expression");
if (textNode != null) {
expression.setValue(textNode.asText());
}
else if (node.has("")) {
expression.setValue(node.get("").asText());
}

return expression;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public List<ExternalReference> deserialize(JsonParser parser, DeserializationCon

private List<ExternalReference> parseExternalReferences(JsonNode node, JsonParser p, DeserializationContext ctxt) throws IOException {
List<ExternalReference> references = new ArrayList<>();
ArrayNode nodes = (node.isArray() ? (ArrayNode) node : new ArrayNode(null).add(node));
ArrayNode nodes = DeserializerUtils.getArrayNode(node, null);
for (JsonNode resolvesNode : nodes) {
ExternalReference type = parseExternalReference(resolvesNode, p, ctxt);
references.add(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,26 @@ public class HashesDeserializer
@Override
public List<Hash> deserialize(JsonParser parser, DeserializationContext context) throws IOException {
JsonNode node = parser.getCodec().readTree(parser);
if (node.has("hash")) {
return parseHashes(node.get("hash"));
}
else {
return parseHashes(node);
}
return parseHashes(node.has("hash") ? node.get("hash") : node);
}

private List<Hash> parseHashes(JsonNode node) {
if (node.isEmpty()) {
return Collections.emptyList();
}

ArrayNode nodes = DeserializerUtils.getArrayNode(node, null);
List<Hash> hashes = new ArrayList<>();
ArrayNode nodes = (node.isArray() ? (ArrayNode) node : new ArrayNode(null).add(node));

for (JsonNode resolvesNode : nodes) {
Hash hash = parseHash(resolvesNode);
hashes.add(hash);
for (JsonNode hashNode : nodes) {
hashes.add(parseHash(hashNode));
}
return hashes;
}

private Hash parseHash(JsonNode node) {
String alg = null;
if (node.has("alg")) {
alg = node.get("alg").asText();
}

String value = null;
JsonNode valueNode = node.get("content");
if (valueNode != null) {
value = valueNode.textValue();
}
else if (node.has("")) {
value = node.get("").asText();
}
String alg = node.has("alg") ? node.get("alg").asText() : null;
String value = node.has("content") ? node.get("content").asText() : node.has("") ? node.get("").asText() : null;

return new Hash(alg, value);
}
Expand Down
Loading