Tulajdonságok figyelmen kívül hagyása a System.Text.Json
Ha C# objektumokat szerializál JavaScript Object Notation (JSON) értékre, alapértelmezés szerint minden nyilvános tulajdonság szerializálva lesz. Ha nem szeretné, hogy ezek némelyike megjelenjen az eredményként kapott JSON-ban, több lehetősége is van. Ebből a cikkből megtudhatja, hogyan hagyhatja figyelmen kívül a tulajdonságokat különböző feltételek alapján:
- Egyedi tulajdonságok
- Minden írásvédett tulajdonság
- Minden null értékű tulajdonság
- Az összes alapértelmezett értéktulajdonság
Egyedi tulajdonságok figyelmen kívül hagyása
Az egyes tulajdonságok figyelmen kívül hagyásához használja a [JsonIgnore] attribútumot.
Az alábbi példa egy szerializálandó típust mutat be. A JSON-kimenetet is megjeleníti:
public class WeatherForecastWithIgnoreAttribute
{
public DateTimeOffset Date { get; set; }
public int TemperatureCelsius { get; set; }
[JsonIgnore]
public string? Summary { get; set; }
}
Public Class WeatherForecastWithIgnoreAttribute
Public Property [Date] As DateTimeOffset
Public Property TemperatureCelsius As Integer
<JsonIgnore>
Public Property Summary As String
End Class
{
"Date": "2019-08-01T00:00:00-07:00",
"TemperatureCelsius": 25,
}
A feltételes kizárást a [JsonIgnore] attribútum tulajdonságának Condition
beállításával adhatja meg. Az JsonIgnoreCondition enumerálás a következő lehetőségeket biztosítja:
Always
- A tulajdonságot a rendszer mindig figyelmen kívül hagyja. Ha nincsCondition
megadva, ezt a beállítást feltételezzük.Never
- A tulajdonság mindig szerializálva és deszerializálva van, függetlenül a , ésIgnoreReadOnlyFields
IgnoreReadOnlyProperties
aDefaultIgnoreCondition
globális beállításoktól.WhenWritingDefault
- A tulajdonság figyelmen kívül lesz hagyva a szerializáláskor, ha hivatkozástípusrólnull
, null értékű típusrólnull
vagy értéktípusróldefault
van szó.WhenWritingNull
– A tulajdonság figyelmen kívül lesz hagyva a szerializáláskor, ha hivatkozástípusrólnull
vagy null értékű típusrólnull
van szó.
Az alábbi példa a [JsonIgnore] attribútum tulajdonságának Condition
használatát mutatja be:
using System.Text.Json;
using System.Text.Json.Serialization;
namespace JsonIgnoreAttributeExample
{
public class Forecast
{
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public DateTime Date { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.Never)]
public int TemperatureC { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string? Summary { get; set; }
};
public class Program
{
public static void Main()
{
Forecast forecast = new()
{
Date = default,
Summary = null,
TemperatureC = default
};
JsonSerializerOptions options = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault
};
string forecastJson =
JsonSerializer.Serialize<Forecast>(forecast,options);
Console.WriteLine(forecastJson);
}
}
}
// Produces output like the following example:
//
//{"TemperatureC":0}
Imports System.Text.Json
Imports System.Text.Json.Serialization
Namespace JsonIgnoreAttributeExample
Public Class Forecast
<JsonIgnore(Condition:=JsonIgnoreCondition.WhenWritingDefault)>
Public Property [Date] As Date
<JsonIgnore(Condition:=JsonIgnoreCondition.Never)>
Public Property TemperatureC As Integer
<JsonIgnore(Condition:=JsonIgnoreCondition.WhenWritingNull)>
Public Property Summary As String
End Class
Public NotInheritable Class Program
Public Shared Sub Main()
Dim forecast1 As New Forecast() With {
.[Date] = CType(Nothing, Date),
.Summary = Nothing,
.TemperatureC = CType(Nothing, Integer)
}
Dim options As New JsonSerializerOptions() With {
.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault
}
Dim forecastJson As String = JsonSerializer.Serialize(forecast1, options)
Console.WriteLine(forecastJson)
End Sub
End Class
End Namespace
' Produces output like the following example:
'
'{"TemperatureC":0}
Az összes írásvédett tulajdonság figyelmen kívül hagyása
A tulajdonság írásvédett, ha nyilvános lekérőt tartalmaz, nyilvános beállítót azonban nem. Ha a szerializálás során figyelmen kívül szeretné hagyni az összes írásvédett tulajdonságot, állítsa a JsonSerializerOptions.IgnoreReadOnlyProperties következő értékre true
az alábbi példában látható módon:
var options = new JsonSerializerOptions
{
IgnoreReadOnlyProperties = true,
WriteIndented = true
};
jsonString = JsonSerializer.Serialize(weatherForecast, options);
Dim options As JsonSerializerOptions = New JsonSerializerOptions With {
.IgnoreReadOnlyProperties = True,
.WriteIndented = True
}
jsonString = JsonSerializer.Serialize(weatherForecast, options)
Az alábbi példa egy szerializálandó típust mutat be. A JSON-kimenetet is megjeleníti:
public class WeatherForecastWithROProperty
{
public DateTimeOffset Date { get; set; }
public int TemperatureCelsius { get; set; }
public string? Summary { get; set; }
public int WindSpeedReadOnly { get; private set; } = 35;
}
Public Class WeatherForecastWithROProperty
Public Property [Date] As DateTimeOffset
Public Property TemperatureCelsius As Integer
Public Property Summary As String
Private _windSpeedReadOnly As Integer
Public Property WindSpeedReadOnly As Integer
Get
Return _windSpeedReadOnly
End Get
Private Set(Value As Integer)
_windSpeedReadOnly = Value
End Set
End Property
End Class
{
"Date": "2019-08-01T00:00:00-07:00",
"TemperatureCelsius": 25,
"Summary": "Hot",
}
Ez a beállítás csak a tulajdonságokra vonatkozik. Ha figyelmen kívül szeretné hagyni az írásvédett mezőket a mezők szerializálása során, használja a JsonSerializerOptions.IgnoreReadOnlyFields globális beállítást.
Feljegyzés
Az írásvédett gyűjtemény típusú tulajdonságok akkor is szerializálva lesznek, ha JsonSerializerOptions.IgnoreReadOnlyProperties be vannak állítva true
.
Az összes null értékű tulajdonság figyelmen kívül hagyása
Az összes null értékű tulajdonság figyelmen kívül hagyásához állítsa a DefaultIgnoreCondition tulajdonságot WhenWritingNulla következő példában látható módon:
using System.Text.Json;
using System.Text.Json.Serialization;
namespace IgnoreNullOnSerialize
{
public class Forecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public string? Summary { get; set; }
};
public class Program
{
public static void Main()
{
Forecast forecast = new()
{
Date = DateTime.Now,
Summary = null,
TemperatureC = default
};
JsonSerializerOptions options = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
string forecastJson =
JsonSerializer.Serialize<Forecast>(forecast, options);
Console.WriteLine(forecastJson);
}
}
}
// Produces output like the following example:
//
//{"Date":"2020-10-30T10:11:40.2359135-07:00","TemperatureC":0}
Imports System.Text.Json
Namespace IgnoreNullOnSerialize
Public Class Forecast
Public Property [Date] As Date
Public Property TemperatureC As Integer
Public Property Summary As String
End Class
Public NotInheritable Class Program
Public Shared Sub Main()
Dim forecast1 As New Forecast() With
{
.[Date] = Date.Now,
.Summary = Nothing,
.TemperatureC = CType(Nothing, Integer)
}
Dim options As New JsonSerializerOptions
Dim forecastJson As String = JsonSerializer.Serialize(forecast1, options)
Console.WriteLine(forecastJson)
End Sub
End Class
End Namespace
' Produces output like the following example:
'
'{"Date":"2020-10-30T10:11:40.2359135-07:00","TemperatureC":0}
Az összes alapértelmezett érték tulajdonság figyelmen kívül hagyása
Az értéktípus-tulajdonságok alapértelmezett értékeinek szerializálásának megakadályozásához állítsa a DefaultIgnoreCondition tulajdonságot WhenWritingDefaulta következő példában látható módon:
using System.Text.Json;
using System.Text.Json.Serialization;
namespace IgnoreValueDefaultOnSerialize
{
public class Forecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public string? Summary { get; set; }
};
public class Program
{
public static void Main()
{
Forecast forecast = new()
{
Date = DateTime.Now,
Summary = null,
TemperatureC = default
};
JsonSerializerOptions options = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault
};
string forecastJson =
JsonSerializer.Serialize<Forecast>(forecast, options);
Console.WriteLine(forecastJson);
}
}
}
// Produces output like the following example:
//
//{ "Date":"2020-10-21T15:40:06.8920138-07:00"}
Imports System.Text.Json
Imports System.Text.Json.Serialization
Namespace IgnoreValueDefaultOnSerialize
Public Class Forecast
Public Property [Date] As Date
Public Property TemperatureC As Integer
Public Property Summary As String
End Class
Public NotInheritable Class Program
Public Shared Sub Main()
Dim forecast1 As New Forecast() With
{.[Date] = Date.Now,
.Summary = Nothing,
.TemperatureC = CType(Nothing, Integer)
}
Dim options As New JsonSerializerOptions() With {
.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault
}
Dim forecastJson As String = JsonSerializer.Serialize(forecast1, options)
Console.WriteLine(forecastJson)
End Sub
End Class
End Namespace
' Produces output like the following example:
'
'{ "Date":"2020-10-21T15:40:06.8920138-07:00"}
A WhenWritingDefault beállítás megakadályozza a null értékű referenciatípus és a null értékű érték típusú tulajdonságok szerializálását is.