C
C#15mo ago
illy

❔ Deserializing JSON

I'm working on a project where I need to deserialize a JSON api response, and do things with the data. This is a little demo thing:
class Response {
[JsonProperty("success")]
[DefaultValue(false)]
public bool Success { get; private set; }

[JsonProperty("days")]
public Days Days { get; private set; }
}

class Days {
[JsonProperty("first_day")]
[DefaultValue("monday")]
public string FirstDay { get; private set; }
}
class Response {
[JsonProperty("success")]
[DefaultValue(false)]
public bool Success { get; private set; }

[JsonProperty("days")]
public Days Days { get; private set; }
}

class Days {
[JsonProperty("first_day")]
[DefaultValue("monday")]
public string FirstDay { get; private set; }
}
My issue is, I need to be able to use Response.Days.FirstDay, when my JSON response looks like this: {'success': true, 'first_night': 'friday'} Essentially, I want the DefaultValue to be present, even if the JSON doesn't contain the 'first_day' key. I'm really not sure how to do this, because I'd need to do this with dozens of attributes in about 6 subclasses, so doing it all manually would take ages and doesn't seem good for maintainability. Any help on how I can achieve this result? To get everything to 'exist' even if it doesn't exist in the JSON, just by using the default value?
6 Replies
Angius
Angius15mo ago
Well, you can have the optional property be nullable Then, whenever you access it, simply provide a fallback object.MaybeNull ?? 0
Pobiega
Pobiega15mo ago
What about using [JsonConstructor] to set the defaults?
Angius
Angius15mo ago
Or that, yeah
illy
illyOP15mo ago
How can I use that?
Pobiega
Pobiega15mo ago
public static class Program
{
public static void Run()
{
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web)
{
WriteIndented = true
};

var json =
"""
{
"success": true,
"days": {
"first_night": "friday"
}
}
""";

var deser = JsonSerializer.Deserialize<Root>(json, options);
var jsonAgain = JsonSerializer.Serialize(deser, options);
Console.WriteLine(jsonAgain);
}
}

public class Root
{
public bool Success { get; set; } = false;
public Days Days { get; set; }
}

public class Days
{
[JsonPropertyName("first_day")] public string FirstDay { get; set; } = "monday";
}
public static class Program
{
public static void Run()
{
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web)
{
WriteIndented = true
};

var json =
"""
{
"success": true,
"days": {
"first_night": "friday"
}
}
""";

var deser = JsonSerializer.Deserialize<Root>(json, options);
var jsonAgain = JsonSerializer.Serialize(deser, options);
Console.WriteLine(jsonAgain);
}
}

public class Root
{
public bool Success { get; set; } = false;
public Days Days { get; set; }
}

public class Days
{
[JsonPropertyName("first_day")] public string FirstDay { get; set; } = "monday";
}
I decided to use properties with default values instead of a constructor this outputs
{
"success": true,
"days": {
"first_day": "monday"
}
}
{
"success": true,
"days": {
"first_day": "monday"
}
}
Accord
Accord15mo 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