João Menighin
João Menighin

Reputation: 3225

Newtonsoft.Json serialize double formated as string

I have the following situation:

To achieve this what I am currently doing is the following class:

public class BankAccount
{
    public string Amount { get; set; } // This is serialized

    // This property is used to do the calculation
    [JsonIgnore]
    public double DoubleAmount { get; set; }

    public void FormatNumbers() {
        // This method is called after I finish doing the calculations
        // with my object and what it basically does is read DoubleAmount,
        // format it and put the value on the Amount string.
    }
}

The thing is this class doesn't feel right. I shouldn't have to call my FormatNumbers... I can somehow update my Amount everytime I update my DoubleAmount, but still it feels weird.

Anyway, do you guys know of any other better way of achieving this? Feel free to suggest anything. Thanks!

Upvotes: 0

Views: 6463

Answers (2)

Robert Synoradzki
Robert Synoradzki

Reputation: 2026

Don't use a method which you have to remember to use, because this violates C in ACID set of rules. C stands for "consistency". If you have a formatting method, this is possible:

account.DoubleAmount = 100000;
account.FormatNumbers();
Console.Write(account.Amount); // "100k" = ok
account.DoubleAmount = 0;
Console.Write(account.Amount); // "100k" = inconsistent = very bad

Use custom getter instead:

public class BankAccount
{
    [JsonIgnore]
    public double DoubleAmount { get; set; }

    public string FormattedAmount
    {
        get
        {
            return (this.DoubleAmount / 1000).ToString() + "k"; // example
        }
    }
}

If you use C# 6.0, this code becomes shorter:

public class BankAccount
{
    [JsonIgnore]
    public double DoubleAmount { get; set; }
    public string FormattedAmount => $"{this.DoubleAmount / 1000}k";
}

Still, you should only serialise (store offline) raw, unformatted values (double), and format (to custom string) only on the fly, at runtime, just when you need to display them.

Upvotes: 4

glenebob
glenebob

Reputation: 1973

Example usage of JsonConverter. Note that the example converter here just does a default double/string conversion. You'll need to implement the actual conversion you want. This approach works for serialization and deserialization, assuming you implement the conversions correctly.

public class BankAccount
{
    [JsonConverter(typeof(DoubleJsonConverter))]
    public double DoubleAmount { get; set; }
}

public class DoubleJsonConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType.IsSubclassOf(typeof(double));
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        return double.Parse((string)reader.Value);
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        writer.WriteValue($"{value}");
    }
}

Upvotes: 5

Related Questions