Skip to main content

CodeSOD: The JSON Template

2 days 4 hours ago

We rip on PHP a lot, but I am willing to admit that the language and ecosystem have evolved over the years. What started as an ugly templating language is now just an ugly regular language.

But what happens when you still really want to do things with templates? Allison has inherited a Python-based, WSGI application which rejects any sort of formal routing or basic web development best practices. Their way of routing requests is simply long chains of "if condition then invokeA elif otherCondition then invokeB". Sometimes, those conditions will directly set the MIME type on the HTTP response.

They do use a templating library called Mako for generating their responses. They use it for their HTML responses, obviously. They also use it for their JSON responses, generating code like this:

{ "success": true, "items": { %for item in items_available.keys(): "${item}": ${items_available[item]}${',' if not loop.last else ''} %endfor } }

The %for and matching %endfor mark the Python code off, which generates JSON via string-munging, complete with the check to make sure we're not on the last iteration of the loop.

Like so much bad code, this offers a degree of fractal wrongness. Instead of iterating over the keys and fetching the items inside the loop, you could iterate for key,value in items_available.items()- and according to the Mako docs, that for is just a regular Python for loop. That we're just outputting the contents of the dictionary is itself potentially a problem- sure, if we know the types of the dictionary, we'll know that whatever it is can be output in the body of a JSON document, but do we really think this code is using type annotations? I don't. And for a RESTful web service, I'm always going to feel weird about using a success field when ideally the HTTP status code could convey most of that information (and yes, I know there are reasons to still put status in the body, I just hate it).

Of course, the real issue is just: Python's built in JSON serialization is actually pretty advanced. And performant! You don't need any of this, you could just do something like:

return json.dumps({"success": true, "items": items_available})

No templates. No formatting. No worries about how the data gets represented. Well, still worries, because JSON serialier will throw exceptions if it doesn't know what to do with a type. But then at least you get that exception on the server side and aren't sending the client a malformed document.

In any case, this is a good demonstration that you can write bad PHP in any language.

[Advertisement] Keep all your packages and Docker containers in one place, scan for vulnerabilities, and control who can access different feeds. ProGet installs in minutes and has a powerful free version with a lot of great features that you can upgrade when ready.Learn more.
Remy Porter

Right-to-Repair Laws Gain Political Momentum Across America

2 days 7 hours ago
"California, Colorado, Minnesota, New York, Connecticut, Oregon and Washington have all passed comprehensive right-to-repair regulations," reports CNBC, "covering everything from consumer electronics and farm equipment to wheelchairs and automobiles." And the consumer movement "continues to gain political momentum" across America... As of this year, advocates are tracking 57 right-to-repair bills across 22 states. In Maine, the state senate just advanced a bill that would bring the right to repair to electronics in the state. Texas's new right-to-repair law kicks in on Sept. 1 and covers phones, laptops, and tablets, but excludes medical and farm equipment, and game consoles.... [U.S.] Senator Ben Ray Luján (D-NM) and Josh Hawley (R-Mo.) are unlikely political bedfellows but have joined together to sponsor the REPAIR Act... The REPAIR Act would require automakers to give vehicle owners, independent repair shops, and aftermarket manufacturers secure access to vehicle repair and maintenance data, preventing manufacturers from funneling consumers into their own exclusive and more expensive dealership repair networks... Hawley criticized big corporations in his arguments in favor of right-to-repair legislation. "Big corporations have a history of gatekeeping basic information that belongs to car owners, effectively forcing consumers to pay a fixed price whenever their car is in the shop," Hawley told CNBC. "The bipartisan REPAIR Act would end corporations' control over diagnostics and service information and give consumers the right to repair their own equipment at a price most feasible for them." The largest small business lobby in the U.S., the NFIB, says 89% of its members support right-to-repair legislation, making it a top legislative priority for 2026.

Read more of this story at Slashdot.

EditorDavid