You basically always do this check before calling addPair, except in the one case where you worry about it being a number.  Why not do it in addPair instead, and not repeat this code everywhere?  Do you not always need things to be strings when they go into addPair?  I'd just have addPair return early if it isn't a string, and not bother with all these checks.