@@ -52,19 +52,25 @@ void SetJsonContextsIfNotEmpty(nlohmann::json& json,
5252 if (!meta.has_contexts ()) {
5353 return ;
5454 }
55-
56- nlohmann::json custom_json;
57- for (auto const & kv : meta.contexts ().custom ) {
58- custom_json[kv.first ] = nlohmann::json{
59- {" value" , kv.second .value },
60- {" createTime" ,
61- google::cloud::internal::FormatRfc3339 (kv.second .create_time )},
62- {" updateTime" ,
63- google::cloud::internal::FormatRfc3339 (kv.second .update_time )},
64- };
55+ if (meta.contexts ().has_custom ()) {
56+ nlohmann::json custom_json;
57+ for (auto const & kv : meta.contexts ().custom ()) {
58+ if (kv.second .has_value ()) {
59+ nlohmann::json item;
60+ item[" value" ] = kv.second .value ().value ;
61+ item[" createTime" ] = google::cloud::internal::FormatRfc3339 (
62+ kv.second .value ().create_time );
63+ item[" updateTime" ] = google::cloud::internal::FormatRfc3339 (
64+ kv.second .value ().update_time );
65+ custom_json[kv.first ] = std::move (item);
66+ } else {
67+ custom_json[kv.first ] = nullptr ;
68+ }
69+ }
70+ json[" contexts" ] = nlohmann::json{{" custom" , std::move (custom_json)}};
71+ } else {
72+ json[" contexts" ] = nlohmann::json{{" custom" , nullptr }};
6573 }
66-
67- json[" contexts" ] = nlohmann::json{{" custom" , std::move (custom_json)}};
6874}
6975
7076Status ParseAcl (ObjectMetadata& meta, nlohmann::json const & json) {
@@ -192,19 +198,26 @@ Status ParseContexts(ObjectMetadata& meta, nlohmann::json const& json) {
192198
193199 ObjectContexts contexts;
194200 for (auto const & kv : f_custom->items ()) {
195- ObjectCustomContextPayload payload;
196- auto value = kv.value ().value (" value" , " " );
197- payload.value = value;
201+ if (kv.value ().is_null ()) {
202+ contexts.upsert_custom_context (kv.key (), absl::nullopt );
203+
204+ } else {
205+ ObjectCustomContextPayload payload;
206+ auto value = kv.value ().value (" value" , " " );
207+ payload.value = value;
198208
199- auto create_time = internal::ParseTimestampField (kv.value (), " createTime" );
200- if (!create_time) return std::move (create_time).status ();
201- payload.create_time = *create_time;
209+ auto create_time =
210+ internal::ParseTimestampField (kv.value (), " createTime" );
211+ if (!create_time) return std::move (create_time).status ();
212+ payload.create_time = *create_time;
202213
203- auto update_time = internal::ParseTimestampField (kv.value (), " updateTime" );
204- if (!update_time) return std::move (update_time).status ();
205- payload.update_time = *update_time;
214+ auto update_time =
215+ internal::ParseTimestampField (kv.value (), " updateTime" );
216+ if (!update_time) return std::move (update_time).status ();
217+ payload.update_time = *update_time;
206218
207- contexts.custom .emplace (kv.key (), std::move (payload));
219+ contexts.upsert_custom_context (kv.key (), std::move (payload));
220+ }
208221 }
209222 meta.set_contexts (std::move (contexts));
210223 return Status{};
0 commit comments