0

I have a custom JSON converter for a class that has several fields of various types, I have an specific rules to map negative double fields to null, but I want that rule applied based upon field name, ie:

I have class

public class sampleClass
{
    public double x { get; set; }
    public double y { get; set; }
    public double z { get; set; }
    public double w { get; set; }
}

when converting the class with the following values

sampleClass c = new sampleClass()
{
    x = -1,
    y = 2,
    z = -1,
    w = 8
};

Result would be:

{
    "x": null,
    "y": "2",
    "z": null,
    "w": "8"
}

but I'd like it to be:

{
    "x": null,
    "y": "2",
    "z": "-1",
    "w": "8"
}

That is, custom converter would apply override conversion to x but not to z based on the field name, but neither CanConvert nor WriteJson receives information about field name

Any ideas regarding this problem will be apreciated

Below I pasted the code for the custom decimal converter (name can be different) that I'm currently using

    class DecimalConverter : JsonConverter
    {
        public override void WriteJson(JsonWriter jsonWriter, object inputObject, JsonSerializer jsonSerializer)
        {
            // Set the properties of the Json Writer
            jsonWriter.Formatting = Formatting.Indented;

            // Typecast the input object
            var numericObject = inputObject as double?;
            jsonWriter.WriteValue((numericObject == -1.0) ? null : numericObject);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            double? readValue = reader.ReadAsDouble();
            return (readValue == null) ? -1 : readValue;
        }

        public override bool CanConvert(Type objectType)
        {
            return (typeof(double) == objectType);
        }
    }
Diego Satizabal
  • 119
  • 1
  • 7
  • `jsonWriter.WriteValue(numericObject == -1.0D ? jsonWriter.Path.Equals("z") ? numericObject : null : numericObject);`. Or negate the equality comparison: `jsonWriter.WriteValue(numericObject == -1.0D && !jsonWriter.Path.Equals("z") ? null : numericObject);` – Jimi Jul 22 '19 at 00:53
  • Great! that did the trick, thanks a lot! – Diego Satizabal Jul 22 '19 at 13:55
  • I'm glad it helped, but this is just an example. Properties shouldn't be hard-coded like that. You should have an object that holds the list Properties that are excluded or included in a condition. Anyway, if you just have this *case*, it may be good enough. – Jimi Jul 22 '19 at 13:59
  • 1
    Yes I did exactly that, created a list with the fields that does not need to apply the custom conversion and the look for the current field in that list, thanks again! – Diego Satizabal Jul 22 '19 at 17:52
  • Couldn't you just use `[JsonConverter]` attributes to apply the converter only to those fields that need the special conversion? – Brian Rogers Jul 26 '19 at 21:41
  • Hi Brian, I already managed to solve the problem but How would I do that? I won't complain if there's a different approach to take that works. Thanks in advance! – Diego Satizabal Jul 27 '19 at 23:03

0 Answers0