Be careful with booleans.

by Grank June 16, 2009 12:08

Here's a quick one: (I realise I haven't put up an entry in months, so it's time to start pointing out at least little anecdotes rather than not posting anything.)

Be careful with booleans.

Why?  Well, just have a look at this conversation with .NET from my immediate window:

(1 == 1)

true

(1 == 1).ToString()

"True"

SerializeToString((1==1))

"<boolean>true</boolean>"

My point is, if you serialize a boolean to an xml string, it will be a lower case string value, which is the standard for XML and javascript and a good thing.

But if you call ToString() on a boolean, your string output will be Capitalized.  That might not be the behavior you're expecting.

So if you're, for example, serializing an object to XML to manually call some third-party service, and it doesn't like your boolean value on the other end, you might find that someone was calling ToString() on it before it was serialized.  A conceivable scenario is if you're auto-generating .NET class code from a schema XSD which is in turn auto-generated from an example XML fragment...  When you do that, it doesn't have any way to know from the XML that something is clearly a bool, so the onus is on you to manually edit the generated XSD to change xs:string to xs:boolean wherever appropriate before running the code generation step.  If you don't, you'll end up with objects that have string properties where they should have boolean properties.  The correct solution when you notice that is to go change the XSD and regenerate the class, but sometimes people just call ToString() on their bool instead.  Then they're confused when "True" is treated as false or other unexpected behavior.

Bonus: this will also happen to you if you add a web reference to webservices that don't provide a typed schema (old-ass enterprisey software, it happens all the time) so every parameter is a string to the wsdl parser.  Then of course you're going to have a generated web method with parameters that are all strings, and you're going to be tempted to just call ToString() on a boolean value for something that's clearly a flag, and probably end up with unexpected results.  In a pinch, if you happen to notice that, you can do .ToString().ToLower(), but I'd recommend making a habit out of never calling web methods outside of a webservice manager class in which you can encapsulate those kinds of behaviors anyway.

 

P.S.  If you're curious about that SerializeToString method, it's just a convenient wrapper for XmlSerializer.

        public static string SerializeToString(object obj)
        {
            var serializer = new XmlSerializer(obj.GetType());

            using (var writer = new StringWriter())
            {
                serializer.Serialize(writer, obj);
                var doc = new XmlDocument();
                doc.LoadXml(writer.ToString());

                // ChildNodes to scrape the <xml> tag off the top
                return doc.ChildNodes[1].OuterXml;
            }
        }

 

Tags: , , , , , ,

Comments

Add comment


(Will show your Gravatar icon)

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen | Modified by Mooglegiant

About The Author

I'm a software developer and musician in Edmonton, AB.  I write mostly web-based software, primarily on the Microsoft stack.  I have an MCPD and several MCTS, but I've only been at this whole developer thing for a few years, and the truth is that I'm still learning more than knowing.  So these are my adventures and experiments and some of it will probably be blatantly wrong...  Just warning ya.