1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-20 01:09:37 +01:00

JSON alongside binary proto serialization test data

This makes the proto serializer characterisation test data be
accompanied by JSON data.

This is arguably useful for a reasons:

- The JSON data is human-readable while the binary data is not, so it
  provides some indication of what the test data means beyond the C++
  literals.

- The JSON data is language-agnostic, and so can be used to quickly rig
  up tests for implementation in other languages, without having source
  code literals at all (just go back and forth between the JSON and the
  binary).

- Even though we have no concrete plans to place the binary protocol 1-1
  or with JSON, it is still nice to ensure that the JSON serializers and
  binary protocols have (near) equal coverage over data types, to help
  ensure we didn't forget a JSON (de)serializer.
This commit is contained in:
John Ericson 2025-10-30 14:47:05 -04:00
parent f5390e76e4
commit 204749270b
60 changed files with 1097 additions and 42 deletions

View file

@ -153,4 +153,20 @@ BuildResult adl_serializer<BuildResult>::from_json(const json & _json)
return br;
}
KeyedBuildResult adl_serializer<KeyedBuildResult>::from_json(const json & json0)
{
auto json = getObject(json0);
return KeyedBuildResult{
adl_serializer<BuildResult>::from_json(json0),
valueAt(json, "path"),
};
}
void adl_serializer<KeyedBuildResult>::to_json(json & json, const KeyedBuildResult & kbr)
{
adl_serializer<BuildResult>::to_json(json, kbr);
json["path"] = kbr.path;
}
} // namespace nlohmann

View file

@ -178,3 +178,4 @@ struct KeyedBuildResult : BuildResult
} // namespace nix
JSON_IMPL(nix::BuildResult)
JSON_IMPL(nix::KeyedBuildResult)

View file

@ -197,3 +197,4 @@ using ValidPathInfos = std::map<StorePath, ValidPathInfo>;
} // namespace nix
JSON_IMPL(nix::UnkeyedValidPathInfo)
JSON_IMPL(nix::ValidPathInfo)

View file

@ -182,5 +182,6 @@ public:
} // namespace nix
JSON_IMPL(nix::DrvOutput)
JSON_IMPL(nix::UnkeyedRealisation)
JSON_IMPL(nix::Realisation)

View file

@ -1004,4 +1004,10 @@ const ContentAddress * getDerivationCA(const BasicDerivation & drv);
std::map<DrvOutput, StorePath>
drvOutputReferences(Store & store, const Derivation & drv, const StorePath & outputPath, Store * evalStore = nullptr);
template<>
struct json_avoids_null<TrustedFlag> : std::true_type
{};
} // namespace nix
JSON_IMPL(nix::TrustedFlag)

View file

@ -10,6 +10,7 @@
#include "nix/util/closure.hh"
#include "nix/store/filetransfer.hh"
#include "nix/util/strings.hh"
#include "nix/util/json-utils.hh"
#include <boost/unordered/unordered_flat_set.hpp>
@ -482,3 +483,19 @@ OutputPathMap resolveDerivedPath(Store & store, const DerivedPath::Built & bfd)
}
} // namespace nix
namespace nlohmann {
using namespace nix;
TrustedFlag adl_serializer<TrustedFlag>::from_json(const json & json)
{
return getBoolean(json) ? TrustedFlag::Trusted : TrustedFlag::NotTrusted;
}
void adl_serializer<TrustedFlag>::to_json(json & json, const TrustedFlag & trustedFlag)
{
json = static_cast<bool>(trustedFlag);
}
} // namespace nlohmann

View file

@ -251,4 +251,20 @@ void adl_serializer<UnkeyedValidPathInfo>::to_json(json & json, const UnkeyedVal
json = c.toJSON(nullptr, true);
}
ValidPathInfo adl_serializer<ValidPathInfo>::from_json(const json & json0)
{
auto json = getObject(json0);
return ValidPathInfo{
valueAt(json, "path"),
adl_serializer<UnkeyedValidPathInfo>::from_json(json0),
};
}
void adl_serializer<ValidPathInfo>::to_json(json & json, const ValidPathInfo & v)
{
adl_serializer<UnkeyedValidPathInfo>::to_json(json, v);
json["path"] = v.path;
}
} // namespace nlohmann

View file

@ -144,6 +144,16 @@ namespace nlohmann {
using namespace nix;
DrvOutput adl_serializer<DrvOutput>::from_json(const json & json)
{
return DrvOutput::parse(getString(json));
}
void adl_serializer<DrvOutput>::to_json(json & json, const DrvOutput & drvOutput)
{
json = drvOutput.to_string();
}
UnkeyedRealisation adl_serializer<UnkeyedRealisation>::from_json(const json & json0)
{
auto json = getObject(json0);
@ -182,14 +192,14 @@ Realisation adl_serializer<Realisation>::from_json(const json & json0)
return Realisation{
static_cast<UnkeyedRealisation>(json0),
DrvOutput::parse(valueAt(json, "id")),
valueAt(json, "id"),
};
}
void adl_serializer<Realisation>::to_json(json & json, const Realisation & r)
{
json = static_cast<const UnkeyedRealisation &>(r);
json["id"] = r.id.to_string();
json["id"] = r.id;
}
} // namespace nlohmann