A dependency-free std.json example for classifying media API responses
Zhang Bo
contactus at easydown.org
Thu Nov 19 04:58:55 UTC 2026
I put together a small D example for a media-import screen: take
a normalized JSON response and select one display mode without
assuming that every successful post contains video.
The useful distinction is between a missing or wrongly typed list
and an empty, valid list. A photo-only post can be successful
with no videos. Converting a malformed field into an empty list
would hide a contract error.
The example uses only Phobos. It checks the top-level status,
validates all three list containers, and then chooses video,
images, audio, or empty. Here is the complete program:
```d
import std.json : JSONValue, JSONType, parseJSON;
import std.exception : enforce;
import std.stdio : writeln;
size_t listSize(JSONValue data, string key)
{
auto field = key in data.object;
enforce(field !is null && field.type == JSONType.array,
key ~ " must be an array");
return field.array.length;
}
string mediaKind(string raw)
{
auto root = parseJSON(raw);
enforce(root.type == JSONType.object, "expected an object");
auto status = "status" in root.object;
enforce(status !is null && status.type == JSONType.integer
&& status.integer == 200, "parse was not successful");
auto data = "data" in root.object;
enforce(data !is null && data.type == JSONType.object,
"expected a data object");
// Validate all three list containers before choosing one
display mode.
auto videos = listSize(*data, "videos");
auto images = listSize(*data, "images");
auto audios = listSize(*data, "audios");
if (videos) return "video";
if (images) return "images";
if (audios) return "audio";
return "empty";
}
void main()
{
// Synthetic fragments: items are placeholders, not full
media objects.
string[] samples = [
q{{"status":200,"data":{"videos":[{}],"images":[],"audios":[]}}},
q{{"status":200,"data":{"videos":[],"images":[{}],"audios":[]}}},
q{{"status":200,"data":{"videos":[],"images":[],"audios":[{}]}}},
q{{"status":200,"data":{"videos":[],"images":[],"audios":[]}}},
q{{"status":"200","data":{"videos":[],"images":[],"audios":[]}}},
q{{"status":200,"data":{"videos":null,"images":[],"audios":[]}}},
q{{"status":200,"data":{"videos":[],"images":[]}}}
];
string[] expected = ["video", "images", "audio", "empty",
"error", "error", "error"];
foreach (i, raw; samples)
{
string outcome;
try { outcome = mediaKind(raw); }
catch (Exception e) { outcome = "error"; }
assert(outcome == expected[i]);
writeln(i + 1, ": ", outcome);
}
}
```
I ran this with the default dmd compiler in D's online
playground. Output:
```text
1: video
2: images
3: audio
4: empty
5: error
6: error
7: error
```
A few details behind these choices:
- The associative-array `in` lookup lets us check whether a key
exists before reading its value. The `enforce` conditions
short-circuit, so a missing key is rejected before dereferencing
it.
- The status is deliberately checked as an integer. The string
`"200"` is rejected rather than silently coerced. This is an
application-payload check, separate from the HTTP response status.
- All three containers must be arrays, even when the first
contains a video. Otherwise an unexpected `images: null` could be
hidden by an early return.
- `q{...}` token strings keep the synthetic JSON readable without
escaping its quotes. The placeholder objects `{}` stand for media
items; this program validates the envelope and list containers,
not the contents of each item.
For an application, handle network and HTTP errors before passing
a successful response body to this function. Catching `Exception`
here keeps the demonstration output compact; a real application's
error path should preserve a useful diagnostic rather than
treating every failure as “no media.”
Disclosure: I build EasyDown. These field names come from its
normalized response, documented in the [EasyDown API
reference](https://easydown.org/docs/en/api). It is a paid
developer API requiring a Bearer token and account credits,
distinct from the free browser downloader. The supported input is
a public single-post media URL, such as a TikTok or Instagram
post—not a private account or an entire profile. AI assisted with
this example and write-up.
This demonstration makes no network calls, uses no API key, and
spends no credits. The synthetic fragments intentionally omit
title, thumbnail and media-item details from the full response.
The helper is not a complete schema validator or an official D
SDK. Platform-specific API endpoints may use a different response
wrapper, so map them before reusing it.
When wiring it into a UI, clear the old displayed result before a
new request and keep the original post URL as the durable
reference; returned media URLs can expire. If a result has
several video entries, they can be alternate encodings of the
same post rather than separate posts.
More information about the Digitalmars-d-announce
mailing list