Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use source generation for serialization #687

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions NGitLab/Impl/Json/DynamicEnumConverterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public override DynamicEnum<TEnum> Read(ref Utf8JsonReader reader, Type type, Js
// This will work if we have a known value.
var enumValue =
_enumConverter?.Read(ref reader, _enumType, options) ??
JsonSerializer.Deserialize<TEnum>(ref reader, options);
Serializer.Deserialize<TEnum>(ref reader);
return new DynamicEnum<TEnum>(enumValue);
}
catch
Expand All @@ -72,7 +72,7 @@ public override void Write(Utf8JsonWriter writer, DynamicEnum<TEnum> dynamicEnum
if (_enumConverter != null)
_enumConverter.Write(writer, enumValue, options);
else
JsonSerializer.Serialize(writer, enumValue, options);
Serializer.Serialize(writer, enumValue);
}
else
{
Expand Down
247 changes: 247 additions & 0 deletions NGitLab/Impl/Json/MyJsonSerializerContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
using System.Text.Json.Serialization;
using NGitLab.Models;

namespace NGitLab.Impl.Json;

[JsonSerializable(typeof(AccessControl))]
[JsonSerializable(typeof(AccessLevel))]
[JsonSerializable(typeof(AccessLevelInfo))]
[JsonSerializable(typeof(ApprovalRule))]
[JsonSerializable(typeof(ApprovalRuleCreate))]
[JsonSerializable(typeof(ApprovalRuleUpdate))]
[JsonSerializable(typeof(Assignee))]
[JsonSerializable(typeof(Author))]
[JsonSerializable(typeof(Badge))]
[JsonSerializable(typeof(BadgeCreate))]
[JsonSerializable(typeof(BadgeKind))]
[JsonSerializable(typeof(BadgeUpdate))]
[JsonSerializable(typeof(Blame))]
[JsonSerializable(typeof(BlameCommit))]
[JsonSerializable(typeof(Blob))]
[JsonSerializable(typeof(Branch))]
[JsonSerializable(typeof(BranchCreate))]
[JsonSerializable(typeof(BranchProtect))]
[JsonSerializable(typeof(Bridge))]
[JsonSerializable(typeof(Change))]
[JsonSerializable(typeof(ClusterInfo))]
[JsonSerializable(typeof(Commit))]
[JsonSerializable(typeof(CommitCherryPick))]
[JsonSerializable(typeof(CommitCreate))]
[JsonSerializable(typeof(CommitInfo))]
[JsonSerializable(typeof(CommitRefType))]
[JsonSerializable(typeof(CommitStats))]
[JsonSerializable(typeof(CommitStatus))]
[JsonSerializable(typeof(CommitStatusCreate))]
[JsonSerializable(typeof(CompareQuery))]
[JsonSerializable(typeof(CompareResults))]
[JsonSerializable(typeof(Contributor))]
[JsonSerializable(typeof(Deployment))]
[JsonSerializable(typeof(DeploymentQuery))]
[JsonSerializable(typeof(DeploymentStatus))]
[JsonSerializable(typeof(DetailedMergeStatus))]
[JsonSerializable(typeof(Diff))]
[JsonSerializable(typeof(DiffRefs))]
[JsonSerializable(typeof(EnvironmentInfo))]
[JsonSerializable(typeof(EnvironmentLastDeployment))]
[JsonSerializable(typeof(EnvironmentQuery))]
[JsonSerializable(typeof(Epic))]
[JsonSerializable(typeof(EpicCreate))]
[JsonSerializable(typeof(EpicEdit))]
[JsonSerializable(typeof(EpicQuery))]
[JsonSerializable(typeof(EpicState))]
[JsonSerializable(typeof(Event))]
[JsonSerializable(typeof(EventAction))]
[JsonSerializable(typeof(EventQuery))]
[JsonSerializable(typeof(EventTargetType))]
[JsonSerializable(typeof(FileData))]
[JsonSerializable(typeof(FileDelete))]
[JsonSerializable(typeof(FileUpsert))]
[JsonSerializable(typeof(ForkedProjectQuery))]
[JsonSerializable(typeof(ForkProject))]
[JsonSerializable(typeof(FormDataContent))]
[JsonSerializable(typeof(GitLabVersion))]
[JsonSerializable(typeof(GraphQLQuery))]
[JsonSerializable(typeof(Group))]
[JsonSerializable(typeof(GroupCreate))]
[JsonSerializable(typeof(GroupHook))]
[JsonSerializable(typeof(GroupHookUpsert))]
[JsonSerializable(typeof(GroupId))]
[JsonSerializable(typeof(GroupLabelEdit))]
[JsonSerializable(typeof(GroupMemberCreate))]
[JsonSerializable(typeof(GroupMemberUpdate))]
[JsonSerializable(typeof(GroupProjectsQuery))]
[JsonSerializable(typeof(GroupQuery))]
[JsonSerializable(typeof(GroupLabelCreate))]
[JsonSerializable(typeof(GroupUpdate))]
[JsonSerializable(typeof(Identity))]
[JsonSerializable(typeof(IIdOrPathAddressable))]
[JsonSerializable(typeof(Issue))]
[JsonSerializable(typeof(IssueClone))]
[JsonSerializable(typeof(IssueCreate))]
[JsonSerializable(typeof(IssueEdit))]
[JsonSerializable(typeof(IssueEpic))]
[JsonSerializable(typeof(IssueQuery))]
[JsonSerializable(typeof(IssueState))]
[JsonSerializable(typeof(IssueType))]
[JsonSerializable(typeof(Job))]
[JsonSerializable(typeof(JobAction))]
[JsonSerializable(typeof(JobArtifactQuery))]
[JsonSerializable(typeof(JobBasic))]
[JsonSerializable(typeof(JobQuery))]
[JsonSerializable(typeof(JobScope))]
[JsonSerializable(typeof(JobStatus))]
[JsonSerializable(typeof(Label))]
[JsonSerializable(typeof(LabelCreate))]
[JsonSerializable(typeof(LabelDelete))]
[JsonSerializable(typeof(LabelEdit))]
[JsonSerializable(typeof(LastActivityDate))]
[JsonSerializable(typeof(LineRange))]
[JsonSerializable(typeof(LintCI))]
[JsonSerializable(typeof(LintCIOptions))]
[JsonSerializable(typeof(Membership))]
[JsonSerializable(typeof(MergeRequest))]
[JsonSerializable(typeof(MergeRequestAccept))]
[JsonSerializable(typeof(MergeRequestApprovals))]
[JsonSerializable(typeof(MergeRequestApprove))]
[JsonSerializable(typeof(MergeRequestApprover))]
[JsonSerializable(typeof(MergeRequestChange))]
[JsonSerializable(typeof(MergeRequestComment))]
[JsonSerializable(typeof(MergeRequestCommentCreate))]
[JsonSerializable(typeof(MergeRequestCommentEdit))]
[JsonSerializable(typeof(MergeRequestCommentQuery))]
[JsonSerializable(typeof(MergeRequestCreate))]
[JsonSerializable(typeof(MergeRequestDiscussion))]
[JsonSerializable(typeof(MergeRequestDiscussionCreate))]
[JsonSerializable(typeof(MergeRequestDiscussionResolve))]
[JsonSerializable(typeof(MergeRequestMerge))]
[JsonSerializable(typeof(MergeRequestQuery))]
[JsonSerializable(typeof(MergeRequestRebase))]
[JsonSerializable(typeof(MergeRequestState))]
[JsonSerializable(typeof(MergeRequestUpdate))]
[JsonSerializable(typeof(MergeRequestUserInfo))]
[JsonSerializable(typeof(MergeRequestVersion))]
[JsonSerializable(typeof(Milestone))]
[JsonSerializable(typeof(MilestoneCreate))]
[JsonSerializable(typeof(MilestoneQuery))]
[JsonSerializable(typeof(MilestoneUpdate))]
[JsonSerializable(typeof(Namespace))]
[JsonSerializable(typeof(NamespaceCreate))]
[JsonSerializable(typeof(Note))]
[JsonSerializable(typeof(NoteableType))]
[JsonSerializable(typeof(ObjectType))]
[JsonSerializable(typeof(PageQuery))]
[JsonSerializable(typeof(PersonInfo))]
[JsonSerializable(typeof(Pipeline))]
[JsonSerializable(typeof(PipelineBasic))]
[JsonSerializable(typeof(PipelineBridgeQuery))]
[JsonSerializable(typeof(PipelineCreate))]
[JsonSerializable(typeof(PipelineDetailedStatus))]
[JsonSerializable(typeof(PipelineJobQuery))]
[JsonSerializable(typeof(PipelineOrderBy))]
[JsonSerializable(typeof(PipelineQuery))]
[JsonSerializable(typeof(PipelineScope))]
[JsonSerializable(typeof(PipelineSort))]
[JsonSerializable(typeof(PipelineVariable))]
[JsonSerializable(typeof(Position))]
[JsonSerializable(typeof(PositionType))]
[JsonSerializable(typeof(Project))]
[JsonSerializable(typeof(ProjectCreate))]
[JsonSerializable(typeof(ProjectHook))]
[JsonSerializable(typeof(ProjectHookUpsert))]
[JsonSerializable(typeof(ProjectId))]
[JsonSerializable(typeof(ProjectIssueNote))]
[JsonSerializable(typeof(ProjectIssueNoteCreate))]
[JsonSerializable(typeof(ProjectIssueNoteEdit))]
[JsonSerializable(typeof(ProjectLabelCreate))]
[JsonSerializable(typeof(ProjectLabelDelete))]
[JsonSerializable(typeof(ProjectLabelEdit))]
[JsonSerializable(typeof(ProjectLinks))]
[JsonSerializable(typeof(ProjectMemberCreate))]
[JsonSerializable(typeof(ProjectMemberUpdate))]
[JsonSerializable(typeof(ProjectPermission))]
[JsonSerializable(typeof(ProjectPermissions))]
[JsonSerializable(typeof(ProjectQuery))]
[JsonSerializable(typeof(ProjectStatistics))]
[JsonSerializable(typeof(ProjectUpdate))]
[JsonSerializable(typeof(ProtectedBranch))]
[JsonSerializable(typeof(ProtectedTag))]
[JsonSerializable(typeof(PushData))]
[JsonSerializable(typeof(PushDataAction))]
[JsonSerializable(typeof(QueryAssigneeId))]
[JsonSerializable(typeof(Range))]
[JsonSerializable(typeof(RangeType))]
[JsonSerializable(typeof(RebaseResult))]
[JsonSerializable(typeof(Ref))]
[JsonSerializable(typeof(RelatedMergeRequestsQuery))]
[JsonSerializable(typeof(ReleaseAssetsInfo))]
[JsonSerializable(typeof(ReleaseAssetSource))]
[JsonSerializable(typeof(ReleaseCreate))]
[JsonSerializable(typeof(ReleaseEvidence))]
[JsonSerializable(typeof(ReleaseInfo))]
[JsonSerializable(typeof(ReleaseInfoLinks))]
[JsonSerializable(typeof(ReleaseLink))]
[JsonSerializable(typeof(ReleaseLinkCreate))]
[JsonSerializable(typeof(ReleaseLinkUpdate))]
[JsonSerializable(typeof(ReleaseQuery))]
[JsonSerializable(typeof(ReleasesAccessLevel))]
[JsonSerializable(typeof(ReleaseUpdate))]
[JsonSerializable(typeof(RepositoryAccessLevel))]
[JsonSerializable(typeof(RepositoryGetTreeOptions))]
[JsonSerializable(typeof(ResourceLabelEvent))]
[JsonSerializable(typeof(ResourceLabelEventAction))]
[JsonSerializable(typeof(ResourceMilestoneEvent))]
[JsonSerializable(typeof(ResourceMilestoneEventAction))]
[JsonSerializable(typeof(ResourceStateEvent))]
[JsonSerializable(typeof(Runner))]
[JsonSerializable(typeof(RunnerId))]
[JsonSerializable(typeof(RunnerRegister))]
[JsonSerializable(typeof(RunnerScope))]
[JsonSerializable(typeof(RunnerUpdate))]
[JsonSerializable(typeof(SearchBlob))]
[JsonSerializable(typeof(Session))]
[JsonSerializable(typeof(SingleMergeRequestQuery))]
[JsonSerializable(typeof(SingleProjectQuery))]
[JsonSerializable(typeof(Snippet))]
[JsonSerializable(typeof(SnippetCreate))]
[JsonSerializable(typeof(SnippetCreateFile))]
[JsonSerializable(typeof(SnippetFile))]
[JsonSerializable(typeof(SnippetProjectCreate))]
[JsonSerializable(typeof(SnippetProjectUpdate))]
[JsonSerializable(typeof(SnippetUpdate))]
[JsonSerializable(typeof(SnippetUpdateFile))]
[JsonSerializable(typeof(SquashOption))]
[JsonSerializable(typeof(SshKey))]
[JsonSerializable(typeof(SshKeyCreate))]
[JsonSerializable(typeof(SubgroupQuery))]
[JsonSerializable(typeof(SystemHook))]
[JsonSerializable(typeof(SystemHookUpsert))]
[JsonSerializable(typeof(Tag))]
[JsonSerializable(typeof(TagCreate))]
[JsonSerializable(typeof(TagProtect))]
[JsonSerializable(typeof(TagQuery))]
[JsonSerializable(typeof(TestCases))]
[JsonSerializable(typeof(TestReport))]
[JsonSerializable(typeof(TestReportSummary))]
[JsonSerializable(typeof(TestReportSummaryTotals))]
[JsonSerializable(typeof(TestSuites))]
[JsonSerializable(typeof(TimeStats))]
[JsonSerializable(typeof(Tree))]
[JsonSerializable(typeof(Trigger))]
[JsonSerializable(typeof(TwoFactorState))]
[JsonSerializable(typeof(UploadedProjectFile))]
[JsonSerializable(typeof(User))]
[JsonSerializable(typeof(UserQuery))]
[JsonSerializable(typeof(UserToken))]
[JsonSerializable(typeof(UserTokenCreate))]
[JsonSerializable(typeof(UserUpsert))]
[JsonSerializable(typeof(Variable))]
[JsonSerializable(typeof(VariableCreate))]
[JsonSerializable(typeof(VariableType))]
[JsonSerializable(typeof(VariableUpdate))]
[JsonSerializable(typeof(VisibilityLevel))]
[JsonSerializable(typeof(WikiPage))]
[JsonSerializable(typeof(WikiPageCreate))]
[JsonSerializable(typeof(WikiPageUpdate))]
internal partial class MyJsonSerializerContext : JsonSerializerContext
{
}
22 changes: 18 additions & 4 deletions NGitLab/Impl/Json/Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace NGitLab.Impl.Json;

internal static class Serializer
{
private static readonly JsonSerializerOptions _options = new()
private static readonly MyJsonSerializerContext _defaultSerializerContext = new(new()
{
IncludeFields = true,
Converters =
Expand All @@ -22,14 +22,20 @@ internal static class Serializer
new Sha1Converter(),
},
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
};
});

public static T Deserialize<T>(string json)
{
var obj = JsonSerializer.Deserialize<T>(json, _options);
var obj = (T)JsonSerializer.Deserialize(json, typeof(T), _defaultSerializerContext);
return obj ?? throw new InvalidOperationException($"Could not deserialize: {json}");
}

public static T Deserialize<T>(ref Utf8JsonReader reader)
{
var obj = (T)JsonSerializer.Deserialize(ref reader, typeof(T), _defaultSerializerContext);
return obj ?? throw new InvalidOperationException($"Could not deserialize");
}

public static bool TryDeserializeObject(string json, out object obj)
{
try
Expand All @@ -46,7 +52,15 @@ public static bool TryDeserializeObject(string json, out object obj)

public static string Serialize<T>(T obj)
{
var str = JsonSerializer.Serialize(obj, _options);
var str = JsonSerializer.Serialize(obj, typeof(T), _defaultSerializerContext);
return str;
}

public static string Serialize<T>(Utf8JsonWriter writer, T obj)
{
var str = JsonSerializer.Serialize(obj, typeof(T), _defaultSerializerContext);
return str;
}


}
File renamed without changes.
File renamed without changes.
Loading