I have json objects that fill a struct with values, and when iterating over some of them, i noticed one that was meant to be a string (or []const u8) had came back as null-- not the string "null" but just null, and i am unsure how to handle that, as any of the members of this struct could potentially come back as null. This causes a runtime error rather than a compiler error because it is coming from an http request that is dynamic.
this is my for loop iterating over the items:
for (rresults.items) |val| {
var current_result = Result{
.Description = val.object.get("Description").?.string,
.FirstSubmitted = val.object.get("FirstSubmitted").?.integer,
.ID = val.object.get("ID").?.integer,
.LastModified = val.object.get("LastModified").?.integer,
.Maintainer = val.object.get("Maintainer").?.string,
.Name = val.object.get("Name").?.string,
.NumVotes = val.object.get("NumVotes").?.integer,
.PackageBase = val.object.get("PackageBase").?.string,
.PackageBaseID = val.object.get("PackageBaseID").?.integer,
.Popularity = val.object.get("Popularity").?.float,
.URL = val.object.get("URL").?.string,
.URLPath = val.object.get("URLPath").?.string,
.Version = val.object.get("Version").?.string,
};
// code below here is a statement printing each field of the struct to stdout
// however it makes the post too many characters to post.
}
and this is the struct definition:
const Result = struct {
Description: []const u8,
FirstSubmitted: i64,
ID: i64,
LastModified: i64,
Maintainer: []const u8,
Name: []const u8,
NumVotes: i64,
PackageBase: []const u8,
PackageBaseID: i64,
Popularity: f64,
URL: []const u8,
URLPath: []const u8,
Version: []const u8,
};