diff --git a/NGitLab/Impl/Json/DynamicEnumConverterFactory.cs b/NGitLab/Impl/Json/DynamicEnumConverterFactory.cs index 6412d22a..35c66c93 100644 --- a/NGitLab/Impl/Json/DynamicEnumConverterFactory.cs +++ b/NGitLab/Impl/Json/DynamicEnumConverterFactory.cs @@ -54,7 +54,7 @@ public override DynamicEnum 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(ref reader, options); + Serializer.Deserialize(ref reader); return new DynamicEnum(enumValue); } catch @@ -72,7 +72,7 @@ public override void Write(Utf8JsonWriter writer, DynamicEnum dynamicEnum if (_enumConverter != null) _enumConverter.Write(writer, enumValue, options); else - JsonSerializer.Serialize(writer, enumValue, options); + Serializer.Serialize(writer, enumValue); } else { diff --git a/NGitLab/Impl/Json/MyJsonSerializerContext.cs b/NGitLab/Impl/Json/MyJsonSerializerContext.cs new file mode 100644 index 00000000..81440375 --- /dev/null +++ b/NGitLab/Impl/Json/MyJsonSerializerContext.cs @@ -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 +{ +} diff --git a/NGitLab/Impl/Json/Serializer.cs b/NGitLab/Impl/Json/Serializer.cs index 5c3cf403..c2e8b745 100644 --- a/NGitLab/Impl/Json/Serializer.cs +++ b/NGitLab/Impl/Json/Serializer.cs @@ -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 = @@ -22,14 +22,20 @@ internal static class Serializer new Sha1Converter(), }, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, - }; + }); public static T Deserialize(string json) { - var obj = JsonSerializer.Deserialize(json, _options); + var obj = (T)JsonSerializer.Deserialize(json, typeof(T), _defaultSerializerContext); return obj ?? throw new InvalidOperationException($"Could not deserialize: {json}"); } + public static T Deserialize(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 @@ -46,7 +52,15 @@ public static bool TryDeserializeObject(string json, out object obj) public static string Serialize(T obj) { - var str = JsonSerializer.Serialize(obj, _options); + var str = JsonSerializer.Serialize(obj, typeof(T), _defaultSerializerContext); return str; } + + public static string Serialize(Utf8JsonWriter writer, T obj) + { + var str = JsonSerializer.Serialize(obj, typeof(T), _defaultSerializerContext); + return str; + } + + } diff --git a/NGitLab/Models/Version.cs b/NGitLab/Models/GitLabVersion.cs similarity index 100% rename from NGitLab/Models/Version.cs rename to NGitLab/Models/GitLabVersion.cs diff --git a/NGitLab/Models/BuildStatus.cs b/NGitLab/Models/JobStatus.cs similarity index 100% rename from NGitLab/Models/BuildStatus.cs rename to NGitLab/Models/JobStatus.cs