Skip to content

Commit

Permalink
Merge pull request #10 from CycloneDX/external-reference-json-bugfix
Browse files Browse the repository at this point in the history
Fix JSON serialization/deserialization of external references
  • Loading branch information
coderpatros authored Nov 3, 2020
2 parents c533fbc + 459a0b5 commit 6f68501
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CycloneDX.Json.Tests/Resources/bom.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
{
"url": "https://github.com/natemcmaster/CommandLineUtils",
"type": "website"
},
{
"url": "https://github.com/natemcmaster/CommandLineUtils/issues",
"type": "issue-tracker"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
{
"url": "https://github.com/natemcmaster/CommandLineUtils",
"type": "website"
},
{
"url": "https://github.com/natemcmaster/CommandLineUtils/issues",
"type": "issue-tracker"
}
]
},
Expand Down
17 changes: 15 additions & 2 deletions CycloneDX.Json/Converters/ExternalReferenceTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System;
using System.Diagnostics.Contracts;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using ExternalReferenceType = CycloneDX.Models.ExternalReference.ExternalReferenceType;
Expand All @@ -36,7 +37,7 @@ public override ExternalReferenceType Read(
throw new JsonException();
}

var externalReferenceTypeString = reader.GetString().Replace('-', '_');
var externalReferenceTypeString = reader.GetString().Replace("-", "");

ExternalReferenceType externalReferenceType;
var success = Enum.TryParse<ExternalReferenceType>(externalReferenceTypeString, ignoreCase: true, out externalReferenceType);
Expand All @@ -56,7 +57,19 @@ public override void Write(
JsonSerializerOptions options)
{
Contract.Requires(writer != null);
writer.WriteStringValue(value.ToString().ToLowerInvariant().Replace('_', '-'));

var s = value.ToString();
var sb = new StringBuilder();
for (var i=0; i<s.Length; i++)
{
if (i != 0 && s[i] == char.ToUpperInvariant(s[i]))
{
sb.Append('-');
}
sb.Append(char.ToLowerInvariant(s[i]));
}

writer.WriteStringValue(sb.ToString());
}
}
}

0 comments on commit 6f68501

Please sign in to comment.