Как изменить свойства json с помощью настраиваемых атрибутов?
public class Employee
{
[MyPropertyInfo(SerializationName = "Имя сотрудника")]
public string Name { get; set; }
[MyPropertyInfo(SerializationName = "Опыт работы")]
public int Experience { get; set; }
}
[AttributeUsage(AttributeTargets.Property)]
public class MyPropertyInfoAttribute : Attribute
{
public string SerializationName { get; set; }
}
public class MySerializer<T>
{
private T _obj;
public MySerializer(T obj)
{
_obj = obj;
}
public string Serialize()
{
string serializedEmployee = JsonSerializer.Serialize(_obj);
return serializedEmployee;
}
}
public class Program
{
static void Main(string[] args)
{
Employee employee = new Employee { Name = "Ivanov Ivan", Experience = 5 };
MySerializer<Employee> employeeSerializer = new MySerializer<Employee>(employee);
string serializedEmployee = employeeSerializer.Serialize();
Console.WriteLine(serializedEmployee);
}
}
На выходе сериализованная строка должна выйти Имя сотрудника Ivanov Ivan Опыт работы 5
У меня название свойств остались без изменений