C
C#5mo ago
Mango

JsonSerializerOptions

To be continued
73 Replies
Mango
Mango5mo ago
@maxmahem post here Taking it out of #chat
maxmahem
maxmahem5mo ago
C#
builder.RegisterInstance(preConfiguredSerializationOptions).Keyed("Sprite");

public class SpriteDeserializaer([KeyFilter("Sprite")] JsonSerializationOptions options) { }
C#
builder.RegisterInstance(preConfiguredSerializationOptions).Keyed("Sprite");

public class SpriteDeserializaer([KeyFilter("Sprite")] JsonSerializationOptions options) { }
something like that. unless I'm misunderstanding you. Do you need to get multiple different options in the same class?
Mango
Mango5mo ago
No, one instance of options per attribute But I would likely use one attribute per class Hold on, I’ll have to switch computers to make a non working demo
maxmahem
maxmahem5mo ago
yeah, then something like the above should work. MS's thing uses a different syntax, but the principle is the same. The problem for me is I have a somewhat convoluted method for deserializing data back into these instances, which is tricky to do, and currently relies on inheritance, so this strategy would mess it up.
Mango
Mango5mo ago
i dont see how it does
maxmahem
maxmahem5mo ago
it's tangental to this, but I've got a whole scheme for loading data back in to like object instances from serialization... not hard to deserialize them of course, but STJ just produces a new object and I can't easily just have that object replace the current object in my IoC container, so I've got to copy the data over. And doing that currently relies on them all being sub-classes of a given type. I could probably switch over to them implementing an interface but, regardless of what I do, I can't have a JsonSerializationOption implement that interface, so there probably isn't any easy way to collect them with all my other options. anyways that's more a me problem, you aren't likely to want to do any of this 😛
Mango
Mango5mo ago
this is what i mean:
public class WeatherForecast
{
public string CityName { get; set; }
public decimal Temperature { get; set; }
}

[AttributeUsage(AttributeTargets.Class)]
public class JsonSerializerOptionsAttribute : Attribute
{
public bool AllowTrailingCommas { get; set; }
// magic
}

[Route("api/v1/[controller]")]
[ApiController]
[JsonSerializerOptions(AllowTrailingCommas = true)] // <-- custom attribute applied to class but also designed to be able to be applied on methods too
public class LegacyController : ControllerBase
{
[Route("get")]
public ActionResult<WeatherForecast> HttpGetAttribute()
{
return Ok(new WeatherForecast());
// returns { cityName = "", temperature = 32.5, }
}
}

[Route("api/[controller]")]
[ApiController]
public class NewController : ControllerBase
{
[Route("get")]
public ActionResult<WeatherForecast> HttpGetAttribute()
{
return Ok(new WeatherForecast());
// returns { cityName = "", temperature = 32.5 }
}
}
public class WeatherForecast
{
public string CityName { get; set; }
public decimal Temperature { get; set; }
}

[AttributeUsage(AttributeTargets.Class)]
public class JsonSerializerOptionsAttribute : Attribute
{
public bool AllowTrailingCommas { get; set; }
// magic
}

[Route("api/v1/[controller]")]
[ApiController]
[JsonSerializerOptions(AllowTrailingCommas = true)] // <-- custom attribute applied to class but also designed to be able to be applied on methods too
public class LegacyController : ControllerBase
{
[Route("get")]
public ActionResult<WeatherForecast> HttpGetAttribute()
{
return Ok(new WeatherForecast());
// returns { cityName = "", temperature = 32.5, }
}
}

[Route("api/[controller]")]
[ApiController]
public class NewController : ControllerBase
{
[Route("get")]
public ActionResult<WeatherForecast> HttpGetAttribute()
{
return Ok(new WeatherForecast());
// returns { cityName = "", temperature = 32.5 }
}
}
if a class/method doesnt have that attribute, global serializer options are used. If it does have it, then the options from that attribute instance is used how would you use DI there?
maxmahem
maxmahem5mo ago
ah, I see I see. well.. if you can't use constructor injection autofac at least can do property injection, though I don't know if you can use it along with keyed services.
Mango
Mango5mo ago
well you can use DI in general but only if the attribute lets you
maxmahem
maxmahem5mo ago
I think we may be talking cross purposes though. Is there some reason you can't use ctor injection? It feels like a better way to go instead of trying to resolve what you want manually via reflection.
Mango
Mango5mo ago
so im still wondering how DI helps here what service do you use i can resolve any service i have registered in my controller no problem
maxmahem
maxmahem5mo ago
but I mean you could do...
C#
public class LegacyController([KeyFilter("LegacyOptions") JsonSerializerOptions options)

public class NewController([KeyFilter("NewOptions"] JsonSerializerOptions options)
C#
public class LegacyController([KeyFilter("LegacyOptions") JsonSerializerOptions options)

public class NewController([KeyFilter("NewOptions"] JsonSerializerOptions options)
Mango
Mango5mo ago
and do what with those services?
maxmahem
maxmahem5mo ago
pass them to whatever needs the options?
Mango
Mango5mo ago
i cant pass a service up to an attribute tho
maxmahem
maxmahem5mo ago
I'm confused, is this not where they are needed? does LegcyController not perform serialization?
Mango
Mango5mo ago
No, the framework does it after Ok() or the return in the method happens The controller itself does not do it
maxmahem
maxmahem5mo ago
ahhh I see.
Mango
Mango5mo ago
It CAN, but the framework does it
maxmahem
maxmahem5mo ago
gotcha. Hmm, well I don't know how you can instruct the frameworks automagic to behave differently.
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
maxmahem
maxmahem5mo ago
yeah I was just going to suggest you can write your own custom converter it could probably be just a passthrough and add the necessary options to the Read and Write methods kind of messy though, but it would work.
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Mango
Mango5mo ago
Is addjsonoption a method?
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
maxmahem
maxmahem5mo ago
C#
public class LegacyControlerJsonConverter : JsonConverter<LegacyController> {
public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
options.AllowTrailingCommas = true;
// I *think* this method and a matching read one exist.
reader.Deserialize<LegacyController>(options);
}

public override void Write(Utf8JsonWriter writer, LegacyController controller, JsonSerializerOptions options) {
options.AllowTrailingCommas = true;
// I *think* this method and a matching read one exist.
writer.Serialize<LegacyController>(options);
}
C#
public class LegacyControlerJsonConverter : JsonConverter<LegacyController> {
public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
options.AllowTrailingCommas = true;
// I *think* this method and a matching read one exist.
reader.Deserialize<LegacyController>(options);
}

public override void Write(Utf8JsonWriter writer, LegacyController controller, JsonSerializerOptions options) {
options.AllowTrailingCommas = true;
// I *think* this method and a matching read one exist.
writer.Serialize<LegacyController>(options);
}
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
maxmahem
maxmahem5mo ago
ngl I'm fairly lost t this point, lol.
Mango
Mango5mo ago
Lol Wat? No I want to serialize the response Not the controller itself
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
maxmahem
maxmahem5mo ago
my b. I see it doesn't make sense now. I think I finally get it.
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Mango
Mango5mo ago
I guess for both Yeah But I want to do it on a per controller or controller method basis Or maybe just controller
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Mango
Mango5mo ago
Which is easily doable with the attribute usage
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Mango
Mango5mo ago
Right, but I want the framework to do that Via an attribute I know that way is possible
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Mango
Mango5mo ago
I want to use controller attribute decoration to drive it Is that possible?
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Mango
Mango5mo ago
Because it’s easier than running serialization manually Code gold Golf
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Mango
Mango5mo ago
Because that changes it on the type itself
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Mango
Mango5mo ago
What do you mean a dot with JsonPropertyName?
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Mango
Mango5mo ago
And that sets it no matter how many endpoints I have that returns or deserializes that?
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Mango
Mango5mo ago
That isn’t what I want
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Mango
Mango5mo ago
That’s not what I want
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Mango
Mango5mo ago
I get that
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Mango
Mango5mo ago
Ok, so is it doable with .Net? The attribute method I demoed above If not, then that’s fine
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Mango
Mango5mo ago
I see
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Mango
Mango5mo ago
Makes sense
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Mango
Mango5mo ago
Ok How can I not do it that way and do it the way I want to try? If it’s possible That’s clearly the best way
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Mango
Mango5mo ago
I want to know another way
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Mango
Mango5mo ago
I meant the way you gave examples of
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Mango
Mango5mo ago
They way you showed is the best way to handle different prop names with api versioning
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Mango
Mango5mo ago
I see
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Mango
Mango5mo ago
So I’ll keep on searching then
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Mango
Mango5mo ago
Yes because I want to do something specific That isn’t being solved by your suggestions I would recommend looking at my code at the very beginning of this thread
Want results from more Discord servers?
Add your server
More Posts