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

Pass relevant JsonWriterOptions from the KiotaJsonSerializationContext.Options to the Utf8JsonWriter when writing json to enable customization. #281

Closed
andrueastman opened this issue Jul 4, 2024 · 1 comment · Fixed by #312
Labels
area:serialization Focused on functional modules of the product enhancement New feature or request WIP

Comments

@andrueastman
Copy link
Member

Related to discussion at microsoft/kiota#4898 (reply in thread)

Trying out the code below, the Encode settings seem to have no effect on the serialized payload.

        var jsonSerializationOptions = new JsonSerializerOptions
        {
            WriteIndented = true,
            Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
        };
        var kiotaJsonSerializationContext = new KiotaJsonSerializationContext(jsonSerializationOptions); // create serialization context with custom options
        SerializationWriterFactoryRegistry.DefaultInstance.ContentTypeAssociatedFactories["application/json"] = new JsonSerializationWriterFactory(kiotaJsonSerializationContext); // replace the json serializer with one with custom options

        var requestJson = await KiotaJsonSerializer.SerializeAsStringAsync(request);

This is because, the JsonSerializationWriter initializes a Utf8JsonWriter instance without propagating relelvant options to the JsonWriterOptions parameter in the constructor here.

https://github.com/microsoft/kiota-serialization-json-dotnet/blob/046356f4e43767b12cfa7b6d223671d83e63309c/src/JsonSerializationWriter.cs#L51

The line should probably be updated to something as below and relevant tests added.

writer = new Utf8JsonWriter(_stream, new JsonWriterOptions {
    Encoder = kiotaJsonSerializationContext.Options.Encoder,
    Indented = kiotaJsonSerializationContext.Options.WriteIndented
});
@andrueastman andrueastman added the enhancement New feature or request label Jul 4, 2024
@andrueastman
Copy link
Member Author

Transferring issue as part of #238

@andrueastman andrueastman transferred this issue from microsoft/kiota-serialization-json-dotnet Jul 9, 2024
@andrueastman andrueastman added the area:serialization Focused on functional modules of the product label Jul 9, 2024
@github-project-automation github-project-automation bot moved this from Todo 📃 to Done ✔️ in Kiota Aug 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:serialization Focused on functional modules of the product enhancement New feature or request WIP
Projects
Archived in project
1 participant