Does Meghan Markle's Paris Fashion trip spell trouble for the Royal Family? Have your say in the Palace Confidential poll
Doctors warned over pairing antidepressants with a commonly prescribed painkiller due to seizure risk
David Beckham 'tries to have his viral moment' in Victoria's documentary two years after her Rolls Royce line went viral in his Netflix series
The paint colour that could lower the price of your property, revealed by an interiors expert - and the shade you should paint your walls instead
Love Island star Georgia Harrison says MBE will drive her to 'continue to evoke change for women'
The Tories are finally listening to young people like me again so I've quit Reform: SOPHIE CORCORAN
QUENTIN LETTS: She needed to give a belter of a speech - and she did. When she made her final announcement everyone went nuts!
Pep Guardiola's time to go: Closest confidant tells him to WALK OUT on Man City - and find 'new project'
The World's Biggest Citizen Science Project
Read more of this story at Slashdot.
Now Keir Starmer wants digital ID cards to be expanded to cover Britons' everyday lives
Posh's unseen life in the Spice Girls: Victoria Beckham opens up about her struggles to perform whilst pregnant with Brooklyn and her loneliness when the group split
Oxford is still top of the class but UK universities perform their worst in a decade amid warning of 'new world order'
Victoria Beckham finally reveals why she never smiles... and it's all husband David's fault
CodeSOD: A JSON Serializer
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; }Now, it's worth noting, C# already has some pretty usable JSON serialization built-ins. None of this code needs to exist in the first place. It's parsing across a dictionary, but that dictionary is itself constructed by copying properties out of an object.
What's fun in this is because everything's shoved into the dictionary and then converted into strings (for the record, the dictionary stores objects, not strings) the only way they have for sniffing out the type of the input is to attempt to parse the input.
If it starts and ends with a (), we convert it into an array of characters. If it starts and ends with {} or parses as a double, we just shove it into the output. If it parses as a boolean, we convert it to lower case. Otherwise, we throw ""s around it and put that in the output. Notably, we don't do any further escaping on the string, which means strings containing " could do weird things in our output.
The final delight to this is the realization that their method of handling appending will include an extra comma at the end, which needs to be removed. Hence the if (dataDict.Count > 0) check at the end.
As always, if you find yourself writing code to generate code, stop. Don't do it. If you find you actually desperately need to start by building a syntax tree, and only then render it to text. If you find yourself needing to generate JSON specifically, please, I beg you, just get a library or use your language's built-ins.
[Advertisement] Plan Your .NET 9 Migration with ConfidenceYour journey to .NET 9 is more than just one decision.Avoid migration migraines with the advice in this free guide. Download Free Guide Now!