I want to hide objects (not properties) when serializing with Json.Net.
The situation is:
In a list of images, there are many images, including attributes such as Class, Name, and Layers. Within the same layer, there are many shape objects, including attributes such as DrawMode and Shape, and each shape object may be a circle, rectangle, or polygon, with coordinate information.
The whole json is:
"Objects": [
{
"Class": "A",
"Name": "image1",
"Layers": [
{
"DrawMode": 0,
"Shape": {
"$type": "Circle",
"Center": "667.126739375705,127.059044753667",
"Radius": 0.0,
"RadiusX": 86.377240728513485,
"RadiusY": 86.377240728513485
}
},
{
"DrawMode": 0,
"Shape": {
"$type": "Polygon",
"Point0": "1243.0011282437,308.048138397894",
"Points": [
"976.453553967657,452.839413313276",
"1479.93230537796,847.724708537044",
"1664.21210981572,535.107183151561"
]
}
},
{
"DrawMode": 1,
"Shape": {
"$type": "Rectangle",
"P0": "565.114704776231,725.968409176382",
"P1": "960,1068.20233170365"
}
}
]
},
{
"Class": "A",
"Name": "image2",
"Layers": [
{
"DrawMode": 0,
"Shape": {
"$type": "Circle",
"Center": "226.171493042497,502.200075216247",
"Radius": 0.0,
"RadiusX": 146.94414717221008,
"RadiusY": 146.94414717221008
}
}
]
}
]
When serializing, I want to hide all objects with the type "Circle".
If there are multiple shape objects in the Layers, including Circle and Polygon, only the shape object of type Circle will be hidden. However, if there are only shape objects of type Circle in the Layers, then the entire image object will be hidden.
So I want json is:
"Objects": [
{
"Class": "A",
"Name": "image1",
"Layers": [
{
"DrawMode": 0,
"Shape": {
"$type": "Polygon",
"Point0": "1243.0011282437,308.048138397894",
"Points": [
"976.453553967657,452.839413313276",
"1479.93230537796,847.724708537044",
"1664.21210981572,535.107183151561"
]
}
},
{
"DrawMode": 1,
"Shape": {
"$type": "Rectangle",
"P0": "565.114704776231,725.968409176382",
"P1": "960,1068.20233170365"
}
}
]
}
]
Can anyone have any idea about this problem?
Thanks!
Update Class:
[JsonObject(MemberSerialization.OptIn)]
public partial class AnnotationViewModel : ViewModelBase
{
public AnnotationViewModel()
{
}
private int _id;
public int Id
{
get => _id;
set => Set(ref _id, value);
}
private ObservableCollection<AnnotationObjectViewModel> _objects = new ObservableCollection<AnnotationObjectViewModel>();
[JsonProperty]
public ObservableCollection<AnnotationObjectViewModel> Objects
{
get => _objects;
set => Set(ref _objects, value);
}
}
[JsonObject(MemberSerialization.OptIn)]
public class AnnotationObjectViewModel : ViewModelBase
{
public AnnotationObjectViewModel()
{
}
private int _fontsize = 20;
public int Fontsize
{
get => _fontsize;
set => Set(ref _fontsize, value);
}
private string _class;
[JsonProperty]
public string Class
{
get => _class;
set => Set(ref _class, value);
}
private string _name;
[JsonProperty]
public string Name
{
get => _name;
set => Set(ref _name, value);
}
private ObservableCollection<LayerViewModel> _layers = new ObservableCollection<LayerViewModel>();
[JsonProperty]
public ObservableCollection<LayerViewModel> Layers
{
get => _layers;
set => Set(ref _layers, value);
}
}
[JsonObject(MemberSerialization.OptIn)]
public class LayerViewModel: ViewModelBase
{
private int _drawMode;
[JsonProperty]
public int DrawMode
{
get => _drawMode;
set => Set(ref _drawMode, value);
}
MShape _shape;
[JsonProperty]
public MShape Shape
{
get => _shape;
set => Set(ref _shape, value);
}
}
By the way, I know DefaultContractResolver, but I an not sure whether it can hide object