mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 13:00:33 +01:00
Configurable JSON serialization support (Newtonsoft.Json vs System.Text.Json) #711
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @serber on GitHub (Aug 1, 2025).
Originally assigned to: @StefH on GitHub.
WireMock.Net forces me to use
Newtonsoft.Jsonas the only serialization option. In modern .NET applications that have migrated toSystem.Text.Json, this creates dependency conflicts, performance overhead, and inconsistent serialization behavior between production code and test mocks. When my application usesSystem.Text.Jsonwith specific naming policies and converters, WireMock'sNewtonsoft.Jsonserialization can produce different results, leading to unreliable tests that don't accurately reflect production behavior.I would like WireMock.Net to support configurable JSON serialization, allowing users to choose between
Newtonsoft.Json(current default) andSystem.Text.Json. This could be implemented through:Additional context
System.Text.Jsonoffers significantly better performance (up to 2x faster serialization)Newtonsoft.JsonSystem.Text.Jsonfor new applicationsNewtonsoft.Jsonas the default option@StefH commented on GitHub (Aug 11, 2025):
@serber
Note that it's already possible to provide the json serializer here:
https://github.com/wiremock/WireMock.Net/blob/master/src/WireMock.Net.Minimal/ResponseBuilders/Response.WithBody.cs#L217
Or do you mean something else?
@molander-dev commented on GitHub (Aug 21, 2025):
Yeah its not about the serializer used in wiremock, its that wiremock globally sets Newtonsoft for the project you install Wiremock.NET to. So if our project uses System.Text and we install the Wiremock.NET package it breaks all our code that uses the System.Text.Json.JsonSerializer
@StefH commented on GitHub (Aug 21, 2025):
Normally you would install WireMock.Net in a unit test project.
And I do not understand how adding WireMock.Net breaks / changes the json serializer used.
Can you please provide a complete example?
@molander-dev commented on GitHub (Aug 25, 2025):
Yes here's an example.
Before installing WireMock.NET package to my NUnit test project I have this helper class.
After installing the Wiremock.NET to the project this method breaks and this part will say JsonConverter is a namespace but is used as a type.
So then we need to add a custom using to keep using system.text
@tavisca-mjadhav commented on GitHub (Dec 4, 2025):
Hello @StefH
Yes I agree with @serber . Even I have a scenario where Newtonsoft.Json actually breaking during the HTTP recording phase.
In my case the my .net application is using the System.Text.Json serializer in all cases. In one of the use case, my application is making 3rd part call over the HTTPS network sending & receiving JSON data. This call is authenticated, as part of "SHA" body hash authentication mechanism, & the application computes the HASH value of the body by converting the request using System.Text.Json serializer to string. This SHA value is passed via header.
Now when I use proxy recording feature of the wiremock, to perform the recording of the outgoing call, Wiremock internally tries to parse the outgoing http request payload as JSON using newtonsoft JSON, and then it forwarded to actually 3'rd party service.
At this moment i receive unauthorized error. This is because of the difference in Hash value when HASH is computed via System.Text.Json and Newtonsoft.Json string serialization. There is different behavior used by System.Text.Json when it comes to serialize the special characters & encoding as compared to NewtonSoft.Json.
system-text-json/character-encoding .
Due to this difference in behavior, the HASH value computed before sending the call over the network & after receiving over the network for verification by the 3'rd party service is different (Though body has the same Json value).
So allowing Wiremock to choose between desired serializer will be of more helpful.
@StefH commented on GitHub (Dec 5, 2025):
@tavisca-mjadhav
Did you try this?
@tavisca-mjadhav commented on GitHub (Dec 11, 2025):
@StefH yes. I have already given it a try.
The problem is, System.Text.Json by default handles & escapes NonAscii & Html characters. But in Newtonsoft.Json Since the StringEscapeHandling is not a [Flags] enum , I can only use either
StringEscapeHandling.EscapeNonAsciiorStringEscapeHandling.EscapeHtml, but not both.Not even custom converter supports this.
Due to this, the Wiremock recorded files will either have escaped Html characters or escaped NonAscii characters but not both, which is again causing the HASH difference as mentioned in above comment.
Again there is difference in escaped values of System.Text.Json vs Newtonsoft.Json there is difference.
Output
@StefH commented on GitHub (Dec 12, 2025):
@tavisca-mjadhav
I see your point.
Maybe a solution for this would be that the proxy recording does not try to parse/write json, but just the string or just even the body as bytes.Probably need to change te recording logic and add an setting for this.
@StefH commented on GitHub (Dec 15, 2025):
@tavisca-mjadhav
A better solution would be that the JSON (de)-serialization from the mapping files is configurable between newtonsoft and systemtext. I'll take a look.