|
2 | 2 | #include "nix/store/store-api.hh"
|
3 | 3 | #include "nix/util/closure.hh"
|
4 | 4 | #include "nix/util/signature/local-keys.hh"
|
| 5 | +#include "nix/util/json-utils.hh" |
5 | 6 | #include <nlohmann/json.hpp>
|
6 | 7 |
|
7 | 8 | namespace nix {
|
@@ -60,54 +61,9 @@ void Realisation::closure(Store & store, const std::set<Realisation> & startOutp
|
60 | 61 | });
|
61 | 62 | }
|
62 | 63 |
|
63 |
| -nlohmann::json Realisation::toJSON() const |
64 |
| -{ |
65 |
| - auto jsonDependentRealisations = nlohmann::json::object(); |
66 |
| - for (auto & [depId, depOutPath] : dependentRealisations) |
67 |
| - jsonDependentRealisations.emplace(depId.to_string(), depOutPath.to_string()); |
68 |
| - return nlohmann::json{ |
69 |
| - {"id", id.to_string()}, |
70 |
| - {"outPath", outPath.to_string()}, |
71 |
| - {"signatures", signatures}, |
72 |
| - {"dependentRealisations", jsonDependentRealisations}, |
73 |
| - }; |
74 |
| -} |
75 |
| - |
76 |
| -Realisation Realisation::fromJSON(const nlohmann::json & json, const std::string & whence) |
77 |
| -{ |
78 |
| - auto getOptionalField = [&](std::string fieldName) -> std::optional<std::string> { |
79 |
| - auto fieldIterator = json.find(fieldName); |
80 |
| - if (fieldIterator == json.end()) |
81 |
| - return std::nullopt; |
82 |
| - return {*fieldIterator}; |
83 |
| - }; |
84 |
| - auto getField = [&](std::string fieldName) -> std::string { |
85 |
| - if (auto field = getOptionalField(fieldName)) |
86 |
| - return *field; |
87 |
| - else |
88 |
| - throw Error("Drv output info file '%1%' is corrupt, missing field %2%", whence, fieldName); |
89 |
| - }; |
90 |
| - |
91 |
| - StringSet signatures; |
92 |
| - if (auto signaturesIterator = json.find("signatures"); signaturesIterator != json.end()) |
93 |
| - signatures.insert(signaturesIterator->begin(), signaturesIterator->end()); |
94 |
| - |
95 |
| - std::map<DrvOutput, StorePath> dependentRealisations; |
96 |
| - if (auto jsonDependencies = json.find("dependentRealisations"); jsonDependencies != json.end()) |
97 |
| - for (auto & [jsonDepId, jsonDepOutPath] : jsonDependencies->get<StringMap>()) |
98 |
| - dependentRealisations.insert({DrvOutput::parse(jsonDepId), StorePath(jsonDepOutPath)}); |
99 |
| - |
100 |
| - return Realisation{ |
101 |
| - .id = DrvOutput::parse(getField("id")), |
102 |
| - .outPath = StorePath(getField("outPath")), |
103 |
| - .signatures = signatures, |
104 |
| - .dependentRealisations = dependentRealisations, |
105 |
| - }; |
106 |
| -} |
107 |
| - |
108 | 64 | std::string Realisation::fingerprint() const
|
109 | 65 | {
|
110 |
| - auto serialized = toJSON(); |
| 66 | + nlohmann::json serialized = *this; |
111 | 67 | serialized.erase("signatures");
|
112 | 68 | return serialized.dump();
|
113 | 69 | }
|
@@ -183,3 +139,43 @@ RealisedPath::Set RealisedPath::closure(Store & store) const
|
183 | 139 | }
|
184 | 140 |
|
185 | 141 | } // namespace nix
|
| 142 | + |
| 143 | +namespace nlohmann { |
| 144 | + |
| 145 | +using namespace nix; |
| 146 | + |
| 147 | +Realisation adl_serializer<Realisation>::from_json(const json & json0) |
| 148 | +{ |
| 149 | + auto json = getObject(json0); |
| 150 | + |
| 151 | + StringSet signatures; |
| 152 | + if (auto signaturesOpt = optionalValueAt(json, "signatures")) |
| 153 | + signatures = *signaturesOpt; |
| 154 | + |
| 155 | + std::map<DrvOutput, StorePath> dependentRealisations; |
| 156 | + if (auto jsonDependencies = optionalValueAt(json, "dependentRealisations")) |
| 157 | + for (auto & [jsonDepId, jsonDepOutPath] : getObject(*jsonDependencies)) |
| 158 | + dependentRealisations.insert({DrvOutput::parse(jsonDepId), jsonDepOutPath}); |
| 159 | + |
| 160 | + return Realisation{ |
| 161 | + .id = DrvOutput::parse(valueAt(json, "id")), |
| 162 | + .outPath = valueAt(json, "outPath"), |
| 163 | + .signatures = signatures, |
| 164 | + .dependentRealisations = dependentRealisations, |
| 165 | + }; |
| 166 | +} |
| 167 | + |
| 168 | +void adl_serializer<Realisation>::to_json(json & json, Realisation r) |
| 169 | +{ |
| 170 | + auto jsonDependentRealisations = nlohmann::json::object(); |
| 171 | + for (auto & [depId, depOutPath] : r.dependentRealisations) |
| 172 | + jsonDependentRealisations.emplace(depId.to_string(), depOutPath); |
| 173 | + json = { |
| 174 | + {"id", r.id.to_string()}, |
| 175 | + {"outPath", r.outPath}, |
| 176 | + {"signatures", r.signatures}, |
| 177 | + {"dependentRealisations", jsonDependentRealisations}, |
| 178 | + }; |
| 179 | +} |
| 180 | + |
| 181 | +} // namespace nlohmann |
0 commit comments