❔ How to Require Property for Serialization?

Hello, I'm working on a library for the Unity game engine that interfaces with an API. Some of the endpoints require the presence of a property for it to be a valid request. I'm using Newtonsoft.Json for my serialization/deserialization. I saw that it had a [JsonProperty(Required = Required.Always)] attribute, but apparently this only restricts deserialization. Is there any way to restrict serialization as well? Looking to throw an exception or something if developer tries to serialize an object without a required property. I'm not attached to Newtonsoft if there is another library that has this functionality. Thanks!
7 Replies
Angius
Angius17mo ago
Use a constructor for that class? I'd say use required properties, but alas, Unity is quite a few years behind And first and foremost, don't let the user just send their own stuff to that API Wrap it up fully So that the user only calls, say
var client = new SomeApiClient();
client.Send(new SomeApiData("bob", 42, false));
var client = new SomeApiClient();
client.Send(new SomeApiData("bob", 42, false));
Don't expose serialization and deserialization Provide the client, and provide whatever classes the client will work with
davejrodriguez
davejrodriguez17mo ago
Right yeah, that's the plan Good advice They say they're planning on catching up 🤞 but until then
Angius
Angius17mo ago
Required properties are a .NET 7 thing If Unity actually updates to .NET 6 sometime this decade it'll be a resounding success
davejrodriguez
davejrodriguez17mo ago
They've said they plan on getting to .NET 7.x or 8 sometime in 2024. We'll see.
Angius
Angius17mo ago
Oh they've said a lot lol How's DOTS doing mweh
davejrodriguez
davejrodriguez17mo ago
1.0 almost out lol
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ Is there a way to get Textchunk Width?something like TextChunk.GetWidth() which to get a character width..not the TextChunk.CharSpaceWidth❔ protocol for sending small amounts of data for long periods of timeWhat the title says:) I was thinking between mqtt or grpc, maybe just websockets? But the pub/sub mo❔ Efficient way to query this problemI have a logic question. Right now, I'm doing it the worst way possible... Which is s adly the only ❔ Can't build wpf project. "TestClassifier_w04izrw3_wpftmp.csproj" -- FAILED.I'm trying to build a WPF project and I keep seeing the build error: `Done building project "TestCla❔ repo doesnt connect to github on another pcmy repo (solution) is on a usb stick, i've created it on another pc and pushed it to github, now i a❔ How to get hostname string ?i need the hostname string in MappingProfile for automapper i have no access to `HttpContext.CurrenHow to get and set position of an ui element via code wpf c#Hello i want to create TextBoxes via code and set their position but i can't found how to even get tLooking for somebody who can teach me using C# language, simple things for 2D games, going to pay💰I’m looking for somebody who can teach me the C# language so I can begin creating a 2D game, I will ❔ How to handle null in InterfaceI'm trying to learn .Net Core MVC and I'm following a course where the following code gives a warninTrying to delete registry Sub key treeHey I am trying to delete this sub key tree but I keep getting errors. my code is in screen shot. a