Reputation: 480
I am using JsonSerializer to serialize/deserialize a class and it is working well.
But in this class, there is a list that I want to serialize but not for every elements in the list.
There is 3 type in this list with inheritance:
FileInformation and FolderInformation that both inherit from TreeViewElement.
How can I filter depending on the type? I want to serialize all FolderInformations Instance but not FileInformations.
Upvotes: 1
Views: 964
Reputation: 29
You can use the [JsonIgnore] Attribute, to ignore the whole property
Like this:
class Cls
{
public string prop1 { get; set; }
public string prop2 { get; set; }
[JsonIgnore]
public string prop3 { get; set; }
}
class Program
{
static void Main(string[] args)
{
var cls = new Cls
{
prop1 = "lorem",
prop2 = "ipsum",
prop3 = "dolor"
};
System.Console.WriteLine(JsonConvert.SerializeObject(cls));
//Output: {"prop1":"lorem","prop2":"ipsum"}
}
}
EDIT: For ignoring only the value.
If its only the value, needs to be ignored and not the whole property:
class Cls
{
public List<string> prop1 { get; set; }
public List<string> prop2 { get; set; }
public List<string> prop3 { get; set; }
[OnSerializing()]
internal void OnSerializingMethod(StreamingContext context)
{
prop3 = null;
}
}
class Program
{
static void Main(string[] args)
{
var cls = new Cls
{
prop1 = new List<string>{ "lorem", "ipsum", "dolor" },
prop2 = new List<string>{ "lorem", "ipsum", "dolor" },
prop3 = new List<string>{ "lorem", "ipsum", "dolor" },
};
System.Console.WriteLine(JsonConvert.SerializeObject(cls)); //Output: {"prop1":["lorem"],"prop2":["lorem"],"prop3":null}
}
}
Upvotes: 0
Reputation: 391276
You can use a JsonConverter
attribute on your list property to filter the list during serialization.
Here's an example I wrote in LINQPad:
void Main()
{
var document = new Document
{
Id = 123,
Properties = {
new Property { Name = "Filename", Value = "Mydocument.txt" },
new Property { Name = "Length", Value = "1024" },
new Property {
Name = "My secret property",
Value = "<insert world domination plans here>",
IsSerializable = false
},
}
};
var json = JsonConvert.SerializeObject(document, Formatting.Indented).Dump();
var document2 = JsonConvert.DeserializeObject<Document>(json).Dump();
}
public class Document
{
public int Id { get; set; }
[JsonConverterAttribute(typeof(PropertyListConverter))]
public List<Property> Properties { get; } = new List<Property>();
}
public class Property
{
[JsonIgnore]
public bool IsSerializable { get; set; } = true;
public string Name { get; set; }
public string Value { get; set; }
}
public class PropertyListConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(List<Property>);
}
public override object ReadJson(JsonReader reader, Type objectType,
object existingValue, JsonSerializer serializer)
{
var list = (existingValue as List<Property>) ?? new List<Property>();
list.AddRange(serializer.Deserialize<List<Property>>(reader));
return list;
}
public override void WriteJson(JsonWriter writer, object value,
JsonSerializer serializer)
{
var list = (List<Property>)value;
var filtered = list.Where(p => p.IsSerializable).ToList();
serializer.Serialize(writer, filtered);
}
}
The output:
{
"Id": 123,
"Properties": [
{
"Name": "Filename",
"Value": "Mydocument.txt"
},
{
"Name": "Length",
"Value": "1024"
}
]
}
You would have to adapt your attribute to your own types and filtering criteria but this should get you started.
Upvotes: 2