Skip to content

Commit

Permalink
Merge pull request #337 from microsoft/feature/STJbase64
Browse files Browse the repository at this point in the history
feat: replaces convert base64 by STJ base64 for performance
  • Loading branch information
baywet authored Aug 16, 2024
2 parents 666bf27 + 966015d commit c198508
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.11.3] - 2024-08-16

### Changed

- Replaced Convert.FromBase64 by System.Text.Json GetBytesFromBase64 to improve performance (10x improvement).

## [1.11.2] - 2024-08-14

### Changed
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<!-- Common default project properties for ALL projects-->
<PropertyGroup>
<VersionPrefix>1.11.2</VersionPrefix>
<VersionPrefix>1.11.3</VersionPrefix>
<VersionSuffix></VersionSuffix>
<!-- This is overidden in test projects by setting to true-->
<IsTestProject>false</IsTestProject>
Expand Down
7 changes: 3 additions & 4 deletions src/serialization/json/JsonParseNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,9 @@ public IEnumerable<T> GetCollectionOfObjectValues<T>(ParsableFactory<T> factory)
/// <returns>The byte array value of the node.</returns>
public byte[]? GetByteArrayValue()
{
var rawValue = _jsonNode.GetString();
if(string.IsNullOrEmpty(rawValue))
return null;
return Convert.FromBase64String(rawValue);
if(_jsonNode.ValueKind is JsonValueKind.String && _jsonNode.TryGetBytesFromBase64(out var result))
return result;
return null;
}
/// <summary>
/// Gets the untyped value of the node
Expand Down
10 changes: 8 additions & 2 deletions src/serialization/json/JsonSerializationWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,14 @@ private void WriteDictionaryValue<T>(string? key, T values) where T : IDictionar
/// <param name="value">The byte array to be written.</param>
public void WriteByteArrayValue(string? key, byte[]? value)
{
if(value != null)//empty array is meaningful
WriteStringValue(key, value.Length > 0 ? Convert.ToBase64String(value) : string.Empty);
//empty array is meaningful
if(value != null)
{
if(string.IsNullOrEmpty(key))
writer.WriteBase64StringValue(value);
else
writer.WriteBase64String(key!, value);
}
}

/// <summary>
Expand Down

0 comments on commit c198508

Please sign in to comment.