Alternative Solution for JSON.NET Attributes

Currently, in my model classes, I have some JSON.NET attributes, such as

public class MyModel { [JsonProperty("_anothername")] [JsonConverter(typeof(MyCustomConverter))] public string Name { get; set; } } 

Is there an alternative solution that does not "pollute" my model class? The contract resolver seems to work, but it's too heavy. I want something like this:

 var contract = new JsonContract<MyModel>(); contract.Property(m => m.Name) .HasAlias("_anothername") .HasConverter<MyCustomConverter>(); JsonConvert.SerializeObject(myModelInstance, contract); 

Ideally, the model classes did not know anything about JSON.NET, and the project should not have a JSON.NET link.

EDIT: Maybe some other JSON library is working, but I do not want to attract another library, because JSON.NET is widely used in this project.

UPDATE: It seems that a contract resolver is the standard way to do this, I found this library is close to my expectation. I will try to write my own version later.

+4
source share

All Articles