forked from CycloneDX/cyclonedx-dotnet-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(spdx): adds support for reading ReferenceCategories with hyphens (f…
…ixes CycloneDX#267) Signed-off-by: Wessel Terpstra <wessel@wesselterpstra.com>
- Loading branch information
Showing
13 changed files
with
2,224 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/CycloneDX.Spdx/Serialization/Converters/HyphenToUnderscoreEnumConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// This file is part of CycloneDX Library for .NET | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the “License”); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an “AS IS” BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright (c) OWASP Foundation. All Rights Reserved. | ||
|
||
using System; | ||
using System.Diagnostics.Contracts; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace CycloneDX.Spdx.Serialization.Converters | ||
{ | ||
|
||
public class HyphenToUnderscoreEnumConverter<T> : JsonConverter<T> where T: struct | ||
{ | ||
public override T Read( | ||
ref Utf8JsonReader reader, | ||
Type typeToConvert, | ||
JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType == JsonTokenType.Null | ||
|| reader.TokenType != JsonTokenType.String) | ||
{ | ||
throw new JsonException(); | ||
} | ||
|
||
var enumString = reader.GetString(); | ||
|
||
T enumValue; | ||
var success = Enum.TryParse<T>(enumString.Replace("-", "_"), ignoreCase: true, out enumValue); | ||
if (success) | ||
{ | ||
return enumValue; | ||
} | ||
else | ||
{ | ||
throw new JsonException(); | ||
} | ||
} | ||
|
||
public override void Write( | ||
Utf8JsonWriter writer, | ||
T value, | ||
JsonSerializerOptions options) | ||
{ | ||
Contract.Requires(writer != null); | ||
writer.WriteStringValue(value.ToString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.