Created
March 25, 2023 17:30
-
-
Save Ilchert/a64915e6f9c5856d608126fee233a973 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Security.Cryptography; | |
using System; | |
using System.Text.Json; | |
using System.Text.Json.Serialization.Metadata; | |
var a = new A { ModeA = "A", ModeB = "B" }; | |
JsonSerializerOptions optionsA = new() | |
{ | |
TypeInfoResolver = new DefaultJsonTypeInfoResolver | |
{ | |
Modifiers = | |
{ | |
p=>Modifier(p,Mode.A) | |
} | |
} | |
}; | |
JsonSerializerOptions optionsB = new() | |
{ | |
TypeInfoResolver = new DefaultJsonTypeInfoResolver | |
{ | |
Modifiers = | |
{ | |
p=>Modifier(p,Mode.B) | |
} | |
} | |
}; | |
var json = JsonSerializer.Serialize(a, optionsA); | |
Console.WriteLine(json); //{"ModeA":"A"} | |
json = JsonSerializer.Serialize(a, optionsB); | |
Console.WriteLine(json);//{"ModeB":"B"} | |
static void Modifier(JsonTypeInfo typeInfo, Mode mode) | |
{ | |
if (typeInfo.Type != typeof(A)) | |
return; | |
if (mode == Mode.A) | |
{ | |
var b = typeInfo.Properties.Single(p => p.Name == nameof(A.ModeB)); | |
typeInfo.Properties.Remove(b); | |
} | |
else if (mode == Mode.B) | |
{ | |
var b = typeInfo.Properties.Single(p => p.Name == nameof(A.ModeA)); | |
typeInfo.Properties.Remove(b); | |
} | |
else | |
throw new ArgumentException(); | |
} | |
enum Mode | |
{ | |
A, | |
B | |
} | |
class A | |
{ | |
public string ModeA { get; set; } | |
public string ModeB { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment