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 Default STJ Serializer #4332

Merged

Conversation

kundadebdatta
Copy link
Member

@kundadebdatta kundadebdatta commented Feb 28, 2024

Pull Request Template

Description

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
@kundadebdatta kundadebdatta added the auto-merge Enables automation to merge PRs label May 10, 2024
adityasa
adityasa previously approved these changes May 10, 2024
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.

:shipit:

@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
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
10 participants