Let us consider a class
public class Money
{
public decimal Value { get; set; }
}
Let us say this is part of another class,
public class UserAccount
{
public Money Balance { get; set; }
}
On serialization UserAccount
produces something like,
{
"balance": {
"value": 1.0
}
}
Now I want to convert it to/from a json like this.
{
"balance": 1.0
}
This is in a way similar to how DateTime
is serialized by using Custom formatter. Json.Net and MSDN recommends custom formatter and it works fine. But is there a way to Handle this directly from the Money
or UserAccount
class?
Edit: Clarification Datetime does this quite often. For example a serialization of Datetime should look like,
{
"Hour":"00"
"Minutes" : "00"
......
}
But no, it just looks like 04-12-2021 00:00:00
Edit 2: Using a custom formatter like suggested in this MSDN Article works and I am looking to use it as last option. But this implies that every consumer of the library has to implement and looking a way to avoid it.