Recent CodeSOD

Code Snippet Of the Day (CodeSOD) features interesting and usually incorrect code snippets taken from actual production code in a commercial and/or open source software projects.

Oct 2025

A JSON Serializer

by in CodeSOD on

Carol sends us today's nasty bit of code. It does the thing you should never do: serializes by string munging.

public string ToJSON()
{
    double unixTimestamp = ConvertToMillisecondsSinceEpoch(time);
    string JSONString = "{\"type\":\"" + type + "\",\"data\":{";
    foreach (string key in dataDict.Keys)
    {
        string value = dataDict[key].ToString();

        string valueJSONString;
        double valueNumber;
        bool valueBool;

        if (value.Length > 2 && value[0].Equals('(') && value[value.Length - 1].Equals(')')) //tuples
        {
            char[] charArray = value.ToCharArray();
            charArray[0] = '[';
            charArray[charArray.Length - 1] = ']';
            if (charArray[charArray.Length - 2].Equals(','))
                charArray[charArray.Length - 2] = ' ';
            valueJSONString = new string(charArray);
        }
        else if ((value.Length > 1 && value[0].Equals('{') && value[value.Length - 1].Equals('}')) ||
                    (double.TryParse(value, out valueNumber))) //embedded json or numbers
        {
            valueJSONString = value;
        }
        else if (bool.TryParse(value, out valueBool)) //bools
        {
            valueJSONString = value.ToLower();
        }
        else //everything else is a string
        {
            valueJSONString = "\"" + value + "\"";
        }
        JSONString = JSONString + "\"" + key + "\":" + valueJSONString + ",";
    }
    if (dataDict.Count > 0) JSONString = JSONString.Substring(0, JSONString.Length - 1);
    JSONString = JSONString + "},\"time\":" + unixTimestamp.ToString() + "}";
    return JSONString;
}

A Monthly Addition

by in CodeSOD on

In the ancient times of the late 90s, Bert worked for a software solutions company. It was the kind of company that other companies hired to do software for them, releasing custom applications for each client. Well, "each" client implies more than one client, but in this company's case, they only had one reliable client.

One day, the client said, "Hey, we have an application we built to handle scheduling helpdesk workers. Can you take a look at it and fix some problems we've got?" Bert's employer said, "Sure, no problem."


Property Flippers

by in CodeSOD on

Kleyguerth was having a hard time tracking down a bug. A _hasPicked flag was "magically" toggling itself to on. It was a bug introduced in a recent commit, but the commit in question was thousands of lines, and had the helpful comment "Fixed some stuff during the tests".

In several places, the TypeScript code checks a property like so: