glean_core/metrics/recorded_experiment.rs
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5use std::collections::HashMap;
6
7use serde::{Deserialize, Serialize};
8use serde_json::{json, Map as JsonMap, Value as JsonValue};
9
10/// Deserialized experiment data.
11#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
12pub struct RecordedExperiment {
13 /// The experiment's branch as set through [`set_experiment_active`](crate::glean_set_experiment_active).
14 pub branch: String,
15 /// Any extra data associated with this experiment through [`set_experiment_active`](crate::glean_set_experiment_active).
16 /// Note: `Option` required to keep backwards-compatibility.
17 pub extra: Option<HashMap<String, String>>,
18}
19
20impl RecordedExperiment {
21 /// Gets the recorded experiment data as a JSON value.
22 ///
23 /// For JSON, we don't want to include `{"extra": null}` -- we just want to skip
24 /// `extra` entirely. Unfortunately, we can't use a serde field annotation for this,
25 /// since that would break bincode serialization, which doesn't support skipping
26 /// fields. Therefore, we use a custom serialization function just for JSON here.
27 pub fn as_json(&self) -> JsonValue {
28 let mut value = JsonMap::new();
29 value.insert("branch".to_string(), json!(self.branch));
30 if self.extra.is_some() {
31 value.insert("extra".to_string(), json!(self.extra));
32 }
33 JsonValue::Object(value)
34 }
35}