It works great.? Thanks again
Jay
toggle quoted message
Show quoted text
On 5/18/2025 3:23 PM, jbasen via groups.io wrote:
Thanks Caleb!
I'll give that a try.
Jay
On 5/18/2025 3:20 PM, Caleb Radecky via groups.io wrote:
JsonSerializerSettings settings = new JsonSerializerSettings
{
???? NullValueHandling = NullValueHandling.Ignore
};
string json = JsonConvert.SerializeObject(account, settings);
Console.WriteLine(json);
Caleb Radecky | Manager, Online Products & Services
Crestron Silver Certified Programmer | DMC-D-4K | DMC-E-4K
ControlWorks Consulting, LLC | Crestron Services Provider | 8228 Mayfield Rd. #6B Rear Cleveland OH 44026
Direct: (+1)440.771.4809 | Cleveland: (+1)440.449.1100? | Boston: (+1)508.695.0188 | DC: (+1)202.381.9070? | London: (+44) 20 4520 4600
-----Original Message-----
From: [email protected] <[email protected]> On Behalf Of jbasen via groups.io
Sent: Sunday, May 18, 2025 16:37
To: [email protected]
Subject: [crestron] newtonsoft question
Is there a way to not serialize a property in an object that is not initialized.? Something like the [JsonIgnore] but I want to include the property if I set a value for it and leave it out of the serialization if it isn't set.? For example,
public class Account
{
? ??? public string FullName { get; set; }
? ??? public string EmailAddress { get; set; }
? ??? [Something like JsonIgnore]
? ??? public string PasswordHash { get; set; } }
Then
Account account = new Account
{
? ??? FullName = "Joe User",
? ??? EmailAddress = "joe@...",
? ??? PasswordHash = "VHdlZXQgJ1F1aWNrc2lsdmVyJyB0byBASmFtZXNOSw=="
};
string json = JsonConvert.SerializeObject(account);
Console.WriteLine(json);
// {"FullName":"Joe
User","EmailAddress":"joe@...","PasswordHash":"VHdlZXQgJ1F1aWNrc2lsdmVyJyB0byBASmFtZXNOSw=="}
However in the case where PsswordHash isn't initialized with a value
Account account = new Account
{
? ??? FullName = "Joe User",
? ??? EmailAddress = "joe@...",
};
string json = JsonConvert.SerializeObject(account);
Console.WriteLine(json);
// {"FullName":"Joe User","EmailAddress":"joe@..."}
So again, when PasswordHash isn't initialized it doesn't get included in the JSON at all.
Thanks in advance for the help
Jay