Exchange json serializer #438

Closed
opened 2025-12-29 15:23:47 +01:00 by adam · 13 comments
Owner

Originally created by @LadislavPataki on GitHub (Aug 11, 2022).

Is there a possibility to exchange the json serializer from Json.NET to System.Text.Json? I would like to use BodyAsJson method to create response or at least extend it somehow. Could you help me with that? Thanks.

Originally created by @LadislavPataki on GitHub (Aug 11, 2022). Is there a possibility to exchange the json serializer from Json.NET to System.Text.Json? I would like to use BodyAsJson method to create response or at least extend it somehow. Could you help me with that? Thanks.
adam added the question label 2025-12-29 15:23:47 +01:00
adam closed this issue 2025-12-29 15:23:47 +01:00
Author
Owner

@StefH commented on GitHub (Aug 12, 2022):

This will be difficult because Newtonsoft is also used for replacing values in json using templating.

But it depends on how exactly you want to use BodyAsJson and what you expect from it. Maybe adding a optional parameter to that method to indicate that you want to use System.Text.Json?

@StefH commented on GitHub (Aug 12, 2022): This will be difficult because Newtonsoft is also used for replacing values in json using templating. But it depends on how exactly you want to use `BodyAsJson` and what you expect from it. Maybe adding a optional parameter to that method to indicate that you want to use System.Text.Json?
Author
Owner

@LadislavPataki commented on GitHub (Aug 12, 2022):

That would be sufficient. But this way I would have to pass the serializer options every time. For now I'm using an extension method. I was just wondering if it's even possible. Thanks for your reply.

@LadislavPataki commented on GitHub (Aug 12, 2022): That would be sufficient. But this way I would have to pass the serializer options every time. For now I'm using an extension method. I was just wondering if it's even possible. Thanks for your reply.
Author
Owner

@StefH commented on GitHub (Aug 12, 2022):

Can you provide that extension method?

@StefH commented on GitHub (Aug 12, 2022): Can you provide that extension method?
Author
Owner

@LadislavPataki commented on GitHub (Aug 12, 2022):

The extension method.

public static class JsonSerializerExtensions
{
    private static readonly JsonSerializerOptions SerializerOptions = new()
    {
        Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) },
        PropertyNameCaseInsensitive = true
    };

    public static T? Deserialize<T>(this string json) => JsonSerializer.Deserialize<T>(json, SerializerOptions);

    public static T? Deserialize<T>(this string json, JsonSerializerOptions serializerOptions) =>
        JsonSerializer.Deserialize<T>(json, serializerOptions);

    public static string Serialize<TValue>(this TValue value) => JsonSerializer.Serialize(value, SerializerOptions);

    public static string Serialize<TValue>(this TValue value, JsonSerializerOptions serializerOptions) =>
        JsonSerializer.Serialize(value, serializerOptions);
}

Usage:

WireMockServer
            .Given(Request.Create()
                .UsingPost()
                .WithPath("..."))
            .AtPriority(LowPriority)
            .RespondWith(Response.Create()
                .WithSuccess()
                .WithBody(Fixture.Create<Response>().Serialize()));
@LadislavPataki commented on GitHub (Aug 12, 2022): The extension method. ``` c# public static class JsonSerializerExtensions { private static readonly JsonSerializerOptions SerializerOptions = new() { Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) }, PropertyNameCaseInsensitive = true }; public static T? Deserialize<T>(this string json) => JsonSerializer.Deserialize<T>(json, SerializerOptions); public static T? Deserialize<T>(this string json, JsonSerializerOptions serializerOptions) => JsonSerializer.Deserialize<T>(json, serializerOptions); public static string Serialize<TValue>(this TValue value) => JsonSerializer.Serialize(value, SerializerOptions); public static string Serialize<TValue>(this TValue value, JsonSerializerOptions serializerOptions) => JsonSerializer.Serialize(value, serializerOptions); } ``` Usage: ``` c# WireMockServer .Given(Request.Create() .UsingPost() .WithPath("...")) .AtPriority(LowPriority) .RespondWith(Response.Create() .WithSuccess() .WithBody(Fixture.Create<Response>().Serialize())); ```
Author
Owner

@StefH commented on GitHub (Aug 14, 2022):

I see. I'll check if this can be added.

Note that I also started a new project https://github.com/StefH/JsonConverter which could maybe solve these issues.

@StefH commented on GitHub (Aug 14, 2022): I see. I'll check if this can be added. Note that I also started a new project https://github.com/StefH/JsonConverter which could maybe solve these issues.
Author
Owner

@StefH commented on GitHub (Aug 15, 2022):

@LadislavPataki

See this preview version (1.5.3-ci-16370) where you can provide a different JsonConverter when using WithBody:

public IResponseBuilder WithBody(object body, IJsonConverter converter, IJsonConverterOptions? options = null)

For implementations for IJsonConverter, see https://github.com/StefH/JsonConverter.

(how to install preview version: see https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions)

@StefH commented on GitHub (Aug 15, 2022): @LadislavPataki See this preview version (`1.5.3-ci-16370`) where you can provide a different JsonConverter when using `WithBody`: ``` c# public IResponseBuilder WithBody(object body, IJsonConverter converter, IJsonConverterOptions? options = null) ``` For implementations for **IJsonConverter**, see https://github.com/StefH/JsonConverter. (how to install preview version: see https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions)
Author
Owner

@StefH commented on GitHub (Aug 17, 2022):

@LadislavPataki
Did you have time to test this?

@StefH commented on GitHub (Aug 17, 2022): @LadislavPataki Did you have time to test this?
Author
Owner

@LadislavPataki commented on GitHub (Aug 18, 2022):

Hi @StefH , I'm sorry I didn't have time for this. I'll give it a try at the weekend.

@LadislavPataki commented on GitHub (Aug 18, 2022): Hi @StefH , I'm sorry I didn't have time for this. I'll give it a try at the weekend.
Author
Owner

@StefH commented on GitHub (Aug 23, 2022):

@LadislavPataki
Did you already have time to test it?

@StefH commented on GitHub (Aug 23, 2022): @LadislavPataki Did you already have time to test it?
Author
Owner

@LadislavPataki commented on GitHub (Aug 23, 2022):

Hi @StefH, I'm not able to find version (1.5.3-ci-16370). Am I doing something wrong?

image

@LadislavPataki commented on GitHub (Aug 23, 2022): Hi @StefH, I'm not able to find version (1.5.3-ci-16370). Am I doing something wrong? ![image](https://user-images.githubusercontent.com/12115874/186148764-a5e86864-2b6e-4f59-9d65-66d2991b6d5d.png)
Author
Owner

@StefH commented on GitHub (Aug 23, 2022):

That version is already deleted I think.

Please try this one:
1.5.3-ci-16393

@StefH commented on GitHub (Aug 23, 2022): That version is already deleted I think. Please try this one: 1.5.3-ci-16393
Author
Owner

@LadislavPataki commented on GitHub (Aug 23, 2022):

Thanks, the Decorator looks fine to me. This way you can exchange the Converters easily.

@LadislavPataki commented on GitHub (Aug 23, 2022): Thanks, the Decorator looks fine to me. This way you can exchange the Converters easily.
Author
Owner

@StefH commented on GitHub (Aug 23, 2022):

Yes

For all implementations, see https://github.com/StefH/JsonConverter

I'll close it this and release a new official NuGet soon.

@StefH commented on GitHub (Aug 23, 2022): Yes For all implementations, see https://github.com/StefH/JsonConverter I'll close it this and release a new official NuGet soon.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#438