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

[Internal] SystemTextJsonSerializer: Adds STJ Serializer #4332

Merged

Conversation

kundadebdatta
Copy link
Member

@kundadebdatta kundadebdatta commented Feb 28, 2024

Pull Request Template

Description

NOTE: this PR has INTERNAL details for usage please check PR-4589 which explains the steps to use the CosmosSystemTextJsonSerializer publicly by setting UseSystemTextJsonSerializerWithOptions parameter in the CosmosClientOpions for both preview and GA contract. ]

This PR adds a Default System.Text.JSON Implementation for CosmosLinqSerializer. This default serializer can be used along with the CosmosClientOptions to set the runtime serializer. Please take a look at the example below for usage:

        CosmosClientOptions clientOptions = new CosmosClientOptions()
        {
            Serializer = new CosmosDefaultSystemTextJsonSerializer(
                new System.Text.Json.JsonSerializerOptions())
        };

Note that this is just a default implementation which handles the basic scenarios. Any JsonSerializerOptions passed in here may not going to be reflected in SerializeMemberName().

To handle special cases, please create a custom serializer which inherits from the CosmosDefaultSystemTextJsonSerializer and overrides the SerializeMemberName() method to add any special handling.

Below is an example of a custom serializer, which is inherits the CosmosDefaultSystemTextJsonSerializer to cover the special handling for camel case naming property.

    public class CustomSystemTextJsonSerializer : CosmosDefaultSystemTextJsonSerializer
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="CustomSystemTextJsonSerializer "/> class.
        /// </summary>
        /// <param name="jsonSerializerOptions"></param>
        public CustomSystemTextJsonSerializer (JsonSerializerOptions jsonSerializerOptions)
            : base(jsonSerializerOptions)
        {
        }

        /// <inheritdoc/>
        public override string SerializeMemberName(MemberInfo memberInfo)
        {
            System.Text.Json.Serialization.JsonExtensionDataAttribute jsonExtensionDataAttribute =
                memberInfo.GetCustomAttribute<System.Text.Json.Serialization.JsonExtensionDataAttribute>(true);
            if (jsonExtensionDataAttribute != null)
            {
                return null;
            }

            JsonPropertyNameAttribute jsonPropertyNameAttribute = memberInfo.GetCustomAttribute<JsonPropertyNameAttribute>(true);
            if (!string.IsNullOrEmpty(jsonPropertyNameAttribute?.Name))
            {
                return jsonPropertyNameAttribute.Name;
            }

            return System.Text.Json.JsonNamingPolicy.CamelCase.ConvertName(memberInfo.Name);
        }
    }

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)

Closing issues

To automatically close an issue: closes #4330

@kundadebdatta kundadebdatta requested a review from a team as a code owner March 11, 2024 19:07
Copy link
Contributor

@adityasa adityasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🕐

@kundadebdatta kundadebdatta self-assigned this Mar 19, 2024
@Arcalise08
Copy link

Any update on this PR? It's been a few weeks and this is a much-anticipated change.

@kirankumarkolli
Copy link
Member

Any update on this PR? It's been a few weeks and this is a much-anticipated change.

@Arcalise08 due to some reprioritizations this work was paused. We expect it to resume it after 2 weeks.
We will try to opportunistically accelerate its close as soon as possible.

@kundadebdatta kundadebdatta changed the title SystemTextJsonSerializer: Adds Default STJ Serializer [Internal] SystemTextJsonSerializer: Adds Default STJ Serializer Jun 17, 2024
Copy link
Contributor

@aavasthy aavasthy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@kundadebdatta kundadebdatta added auto-merge Enables automation to merge PRs and removed auto-merge Enables automation to merge PRs labels Jun 18, 2024
@kirankumarkolli kirankumarkolli added auto-merge Enables automation to merge PRs and removed auto-merge Enables automation to merge PRs labels Jun 18, 2024
@kirankumarkolli kirankumarkolli merged commit 035d229 into master Jun 18, 2024
21 checks passed
@kirankumarkolli kirankumarkolli deleted the users/kundadebdatta/4330_add_default_stj_serializer branch June 18, 2024 02:17
@itsawindup
Copy link

Forgive me if I am being a bit slow, but since the change from public to internal in commit 1ff1f06, I can't see a way to use this in my own code. Have I missed something obvious?

@bartelink
Copy link
Contributor

@kirankumarkolli @kundadebdatta Please follow up by adding a quick summary as to the final state of play here.
Thousands of people will hit this issue via search, and this saga had lots of twists and turns along the way!

@kirankumarkolli
Copy link
Member

@bartelink apologies, we change the usage model towards the end of the PR stage and missed to reflect that in the description.
@kundadebdatta can you please refresh the description.

@kundadebdatta
Copy link
Member Author

@kirankumarkolli @kundadebdatta Please follow up by adding a quick summary as to the final state of play here. Thousands of people will hit this issue via search, and this saga had lots of twists and turns along the way!

Thanks for bringing this up @bartelink ! I have updated the summary to explain how to use this serializer publicly by default. For your reference:

There is a separate PR-4589 which explains the steps to use the CosmosSystemTextJsonSerializer publicly by setting UseSystemTextJsonSerializerWithOptions parameter in the CosmosClientOpions for both preview and GA contract.

I hope this will be helpful! Please feel free to reach out, if you have any questions.

@bartelink
Copy link
Contributor

bartelink commented Sep 9, 2024

I guess the problem is that over the years there's been lots of PRs and/or snippets, and it's very easy to land on an issue like this which does not point to a final summary of how it worked out in the end.

I know this PR has [Internal] in the title, but the average person without context won't appreciate what that implies, and will instead focus on the "Adds Default STJ Serializer" part of the title and say "OK, it was merged some time ago and I'm on latest, and I can't see this CosmosDefaultSystemTextJsonSerializer anywhere".


So the problem is that someoe scanning even the current overview might conclude that there is a CosmosDefaultSystemTextJsonSerializer. For very good reasons I agree with, that has has wound up not being public. So I guess I was hoping that the last piece of info on this thread (and maybe a prominent note in the overview) could summarise the key points:


Maybe something like this, but written in proper English ;) ?

To apply the CosmosDefaultSystemTextJsonSerializer in the overview of this issue on 3.43.0 or later, you set the value of the CosmosClientOptions.UseSystemTextJsonSerializerWithOptions property to an instance of System.Text.Json.JsonSerializerOptions , e.g.:

 var opts = new CosmosClientOptions { UseSystemTextJsonSerializerWithOptions = System.Text.Json.JsonSerializerOptions.Default };
 var client = new CosmosClient(...., opts);

@kirankumarkolli kirankumarkolli changed the title [Internal] SystemTextJsonSerializer: Adds Default STJ Serializer [Internal] SystemTextJsonSerializer: Adds STJ Serializer Sep 10, 2024
@kirankumarkolli
Copy link
Member

Updated the title to remove default and also the updated PR description to reference the PR which made the contracts public.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-merge Enables automation to merge PRs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a Default System.Text.JSON Implementation for CosmosLinqSerializer