Class ExpectationToCSharpSerializer

java.lang.Object
org.mockserver.serialization.code.ExpectationToCSharpSerializer

public class ExpectationToCSharpSerializer extends Object
Generates copy-paste-ready C# expectation code for the MockServer .NET client (MockServer.Client).

Following the same JSON-wrap pattern as the JavaScript/Python generators, the generated code embeds each expectation's existing JSON serialization (the same bytes produced by format=JSON) in a C# verbatim string and deserializes it with JsonSerializer.Deserialize<Expectation>(...), then passes it to client.Upsert(...).

Verified against the .NET client source:

  • new MockServerClient("localhost", 1080) (MockServer.Client)
  • public List<Expectation> Upsert(params Expectation[] expectations)
  • MockServer.Client.Models.Expectation (deserialised with System.Text.Json)
The client's own JsonSerializerOptions (CamelCase + WhenWritingNull) are private, so the generated code rebuilds an equivalent options object to deserialise with.

The JSON is embedded in a C# verbatim string (@"..."); inside a verbatim string the only character needing escaping is the double-quote, which is doubled (""). Backslashes and newlines are taken literally, matching the JSON bytes. Example output:

 using System.Text.Json;
 using System.Text.Json.Serialization;
 using MockServer.Client;
 using MockServer.Client.Models;

 var jsonOptions = new JsonSerializerOptions
 {
     PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
     DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
 };

 var client = new MockServerClient("localhost", 1080);

 client.Upsert(JsonSerializer.Deserialize<Expectation>(@"{ ... }", jsonOptions));
 
Author:
jamesdbloom
  • Constructor Details

    • ExpectationToCSharpSerializer

      public ExpectationToCSharpSerializer(ExpectationSerializer expectationSerializer)
  • Method Details