2

Currently working with the most annoying web service! I'm using Newtonsoft Json.Net.

When I request data, a bool property EndOfDay is sent as true or false - deserialize works fine BUT annoyingly, when I send data to the web service, for this same field I have to send either a 0 or 1 - don't ask why as I couldn't give you a good answer - All I know is I can't change it.

Is there a way of serializing bools to 0 or 1 even though when I deserialize the strings will be true/false or should I admit defeat and have separate objects which are almost identical except for this one property - one object has a bool and the other an int?

Percy
  • 2,855
  • 2
  • 33
  • 56
  • Can't you just use an `int` for serialization/deserialization in that field? – Pikoh Nov 21 '16 at 16:16
  • As far as i'm aware, Json.Net won't deserialize a bool to an int - it comes from the web service as a bool. – Percy Nov 21 '16 at 16:17
  • 1
    Then i don't understand why you should serialize as int. Anyway, have a look at [this question](http://stackoverflow.com/q/9738324/579895) – Pikoh Nov 21 '16 at 16:18
  • when I get from the web service I receive a bool BUT when I push to web service I have to provide a 0 or 1 – Percy Nov 21 '16 at 16:19
  • Thanks for the link, I've looked at similar questions but they don't quite do what I need it to do. – Percy Nov 21 '16 at 16:21
  • Are you specifying the classes when deserializing? Maybe using a custom class to deserialize with that field as an int would solve the problem... – Pikoh Nov 21 '16 at 16:21
  • Yes I could do that as a last resort - I'm trying to avoid duplicating the class with one property difference though (if possible) – Percy Nov 21 '16 at 16:23
  • But that way you wouldn't duplicate anything, you would be just using that field always as an int. Anyway I'm not sure if it would work or would Json.Net complaint about that ... – Pikoh Nov 21 '16 at 16:24

1 Answers1

4

One way to do it is to use custom conveter:

class Test {
    [JsonConverter(typeof(StrangeBoolConverter))]
    public bool EndOfDay { get; set; }

    private class StrangeBoolConverter : JsonConverter {
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
            // write it as 1 or 0
            writer.WriteValue((bool) value ? 1 : 0);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
            // but when reading - expect "true" or "false"
            return Convert.ToBoolean(reader.Value);
        }

        public override bool CanConvert(Type objectType) {
            return objectType == typeof(bool);
        }
    }
}

Test

var test = JsonConvert.DeserializeObject<Test>("{\"EndOfDay\":\"true\"}");
var back = JsonConvert.SerializeObject(test); // {"EndOfDay": "1"}
Evk
  • 98,527
  • 8
  • 141
  • 191
  • 1
    Well..isn't that more or less the answer of the question i linked in the comments? :) – Pikoh Nov 21 '16 at 16:31
  • @Pikoh well indeed, but I didn't see your comment and that answer before you now told me :) – Evk Nov 21 '16 at 16:33
  • 1
    It's ok,I think it's probably the best answer – Pikoh Nov 21 '16 at 16:34
  • Ah - this example is actually a lot easier for me to understand than the linked one in the comments. Let me give it a shot. – Percy Nov 21 '16 at 16:36
  • @Rick well he did tailor it exactly to your application, so that helps. Good answer – Jonesopolis Nov 21 '16 at 16:37
  • cool - all working as I require. Thanks for the help guys. I didn't realise I could change the example to work differently in each "direction". – Percy Nov 21 '16 at 16:39