-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Source Generation for json (de)serialization (#69)
- Loading branch information
Showing
7 changed files
with
91 additions
and
77 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
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
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,53 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using BuildXL.Cache.ContentStore.Hashing; | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.MSBuildCache; | ||
|
||
internal sealed class SortedDictionaryConverter : JsonConverter<SortedDictionary<string, ContentHash>> | ||
{ | ||
public override SortedDictionary<string, ContentHash>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
var contentHashConverter = (JsonConverter<ContentHash>)options.GetConverter(typeof(ContentHash)); | ||
var outputs = new SortedDictionary<string, ContentHash>(StringComparer.OrdinalIgnoreCase); | ||
while (reader.Read()) | ||
{ | ||
if (reader.TokenType == JsonTokenType.EndObject) | ||
{ | ||
break; | ||
} | ||
|
||
if (reader.TokenType != JsonTokenType.PropertyName) | ||
{ | ||
throw new JsonException($"Unexpected token: {reader.TokenType}"); | ||
} | ||
|
||
string propertyName = reader.GetString()!; | ||
if (!reader.Read()) | ||
{ | ||
throw new JsonException($"Property name '{propertyName}' does not have a value."); | ||
} | ||
|
||
ContentHash? contentHash = contentHashConverter.Read(ref reader, typeof(ContentHash), options); | ||
if (contentHash == null) | ||
{ | ||
throw new JsonException($"Property value for '{propertyName}' could not be parsed."); | ||
} | ||
|
||
outputs.Add(propertyName, contentHash.Value); | ||
} | ||
|
||
return outputs; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, SortedDictionary<string, ContentHash> value, JsonSerializerOptions options) | ||
{ | ||
var defaultConverter = (JsonConverter<IDictionary<string, ContentHash>>)options.GetConverter(typeof(IDictionary<string, ContentHash>)); | ||
defaultConverter.Write(writer, value, options); | ||
} | ||
} |
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,17 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Text.Json.Serialization; | ||
using BuildXL.Cache.ContentStore.Hashing; | ||
using Microsoft.MSBuildCache.Fingerprinting; | ||
|
||
namespace Microsoft.MSBuildCache; | ||
|
||
[JsonSourceGenerationOptions(WriteIndented = true, Converters = [typeof(ContentHashJsonConverter), typeof(SortedDictionaryConverter)])] | ||
[JsonSerializable(typeof(NodeBuildResult))] | ||
[JsonSerializable(typeof(PathSet))] | ||
[JsonSerializable(typeof(IDictionary<string, ContentHash>))] | ||
internal partial class SourceGenerationContext : JsonSerializerContext | ||
{ | ||
} |