DotNetCore cross-platform ~ Json dynamic serialization attribute, dotnetcorejson
Back to directory
Json dynamic serialization attributes are mainly used to solve a large object. They are serialized as needed when the frontend is returned. If a property in the object is not serialized in the task, you can add the [JsonIgnore] feature, which is global filtering. However, in more cases, we need to serialize certain attributes according to certain scenarios. The following is an example.
In both B2C and C2C scenarios, the content returned by the PeopleDTO entity is different. The former returns Name and Email, and the latter only returns Name. In this case, our JsonIgnore cannot meet the requirements, therefore, we need to develop a new model.
Stupid method: write different DTO entities for different scenarios. Of course, there will be a lot of repeated code
Uncle's latest Json dynamic serialization attribute Method
SerializationFilterAttribute: implements this feature according to the scenario. It is a base class.
/// <summary>
/// Serialization identity feature
/// </summary>
[AttributeUsageAttribute(AttributeTargets.Property)]
Public abstract class SerializationFilterAttribute : Attribute
{
}
Scenario 1: B2CAttribute, which must serialize the attributes Name and Email. Therefore, it will be identified in the DTO object class.
public class B2CAttribute : SerializationFilterAttribute
{
}
Scenario 2: C2CAttribute, which only needs to serialize the Name attribute
public class C2CAttribute : SerializationFilterAttribute
{
}
In the entity, we add these two features for the corresponding attributes so that they can be returned on demand during serialization.
public class PeopleDTO
{
[C2CAttribute, B2CAttribute]
public FullNameDTO Name { get; set; }
[B2CAttribute]
public string Email { get; set; }
public DateTime CreateTime { get; set; }
}
Rewrite our previous serialization method to support on-demand serialization.
public static string SerializeToJson<T>(T obj, SerializationFilterAttribute filter)
{
dynamic d = new System.Dynamic.ExpandoObject();
foreach (var item in typeof(T).GetProperties())
{
if (item.GetCustomAttributes(false).Contains(filter))
{
(d as System.Collections.Generic.ICollection<System.Collections.Generic.KeyValuePair<string, object>>).Add(new System.Collections.Generic.KeyValuePair<string, object>(item.Name, item.GetValue(obj)));
}
}
var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };
return JsonConvert.SerializeObject(d);
}
It is also very easy to call the program. Simply enter the corresponding scenario features.
PeopleDTO peopleDTO = new PeopleDTO
{
Name = new FullNameDTO { LastName = "zhanling", FirstName = "zhang", ChinaName = "张占岭" },
Email = "zhangsan@sina.com",
CreateTime = DateTime.Now
};
var msg1 = SerializationHelper.SerializeToJson(peopleDTO, new B2CAttribute());
The returned results are as follows, only include Name and Email
The returned result is as follows, which only contains the Name and Email.
{
"Name":{
"LastName": "zhanling",
"ChinaName": "Zhang Zhanling"
},
"Email": "zhangsan@sina.com"
}
Thank you for reading this article!
Sometimes, the conversion of one kind of thinking may achieve different effects!
Back to directory