Skip to content

Commit face91b

Browse files
authored
fix: replace json with serde_json (#180)
@mati865 [fix: update time crate to fix build error with new Rust](860f7b8) [860f7b8](860f7b8) @mati865 [fix: replace json with serde_json](158839a) [158839a](158839a) @mati865 [refactor: avoid unwrapping serde_json methods](2fd0e79) [2fd0e79](2fd0e79)
1 parent fe3cfac commit face91b

File tree

7 files changed

+29
-40
lines changed

7 files changed

+29
-40
lines changed

Cargo.lock

+6-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ libflate = "1.2.0"
6363
libc = "^0.2.124"
6464
prost = "0.11"
6565
winapi = "0.3.9"
66-
json = "0.12.4"
66+
serde_json = "1.0.115"
6767

6868
[dev-dependencies]
6969
tokio = { version = "1.18", features = ["full"] }

pyroscope_cli/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pyroscope = { path = "../", default-features = false }
3838
pyroscope_pprofrs = { path = "../pyroscope_backends/pyroscope_pprofrs", default-features = false }
3939
pyroscope_rbspy = { path = "../pyroscope_backends/pyroscope_rbspy", default-features = false }
4040
pyroscope_pyspy = { path = "../pyroscope_backends/pyroscope_pyspy", default-features = false }
41-
json = "0.12.4"
41+
serde_json = "1.0.115"
4242

4343
[dependencies.clap]
4444
version = "3.2"

pyroscope_cli/src/utils/app_config.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ impl AppConfig {
172172
.map(|s| s.to_string())
173173
.collect::<Vec<String>>();
174174

175-
AppConfig::set("command_args", &json::stringify(command_args))?;
175+
AppConfig::set("command_args", &serde_json::to_string(&command_args)?)?;
176176
}
177177
}
178178
if sub_exec.is_present("log_level") {
@@ -314,9 +314,9 @@ fn set_http_headers(cmd: &clap::ArgMatches) -> Result<()> {
314314
http_headers_map.insert(kv[0].to_string(), kv[1].to_string());
315315
}
316316
}
317-
let http_header = json::stringify(http_headers_map);
317+
let http_header = serde_json::to_string(&http_headers_map)?;
318318
AppConfig::set("http_headers_json", &http_header)?;
319319
};
320320
}
321321
return Ok(());
322-
}
322+
}

pyroscope_cli/src/utils/error.rs

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ pub enum Error {
2929

3030
#[error(transparent)]
3131
PyroscopeError(#[from] pyroscope::PyroscopeError),
32+
33+
#[error(transparent)]
34+
Json(#[from] serde_json::Error),
3235
}
3336

3437
impl Error {

src/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub enum PyroscopeError {
3131
Io(#[from] std::io::Error),
3232

3333
#[error(transparent)]
34-
Json(#[from] json::JsonError),
34+
Json(#[from] serde_json::Error),
3535
}
3636

3737
impl PyroscopeError {

src/pyroscope.rs

+14-22
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ use crate::{
1818
PyroscopeError,
1919
};
2020

21-
use json;
22-
2321
use crate::backend::BackendImpl;
2422
use crate::pyroscope::Compression::GZIP;
2523
use crate::pyroscope::ReportEncoding::PPROF;
@@ -841,16 +839,13 @@ impl PyroscopeAgent<PyroscopeAgentRunning> {
841839

842840
pub fn parse_http_headers_json(http_headers_json: String) -> Result<HashMap<String, String>> {
843841
let mut http_headers = HashMap::new();
844-
let parsed = json::parse(&http_headers_json)?;
845-
if !parsed.is_object() {
846-
return Err(PyroscopeError::AdHoc(format!(
847-
"expected object, got {}",
848-
parsed
849-
)));
850-
}
851-
for (k, v) in parsed.entries() {
852-
if v.is_string() {
853-
http_headers.insert(k.to_string(), v.to_string());
842+
let parsed: serde_json::Value = serde_json::from_str(&http_headers_json)?;
843+
let parsed = parsed.as_object().ok_or_else(||
844+
PyroscopeError::AdHoc(format!("expected object, got {}", parsed))
845+
)?;
846+
for (k, v) in parsed {
847+
if let Some(value) = v.as_str() {
848+
http_headers.insert(k.to_string(), value.to_string());
854849
} else {
855850
return Err(PyroscopeError::AdHoc(format!(
856851
"invalid http header value, not a string: {}",
@@ -862,17 +857,14 @@ pub fn parse_http_headers_json(http_headers_json: String) -> Result<HashMap<Stri
862857
}
863858

864859
pub fn parse_vec_string_json(s: String) -> Result<Vec<String>> {
865-
let parsed = json::parse(&s)?;
866-
if !parsed.is_array() {
867-
return Err(PyroscopeError::AdHoc(format!(
868-
"expected array, got {}",
869-
parsed
870-
)));
871-
}
860+
let parsed: serde_json::Value = serde_json::from_str(&s)?;
861+
let parsed = parsed.as_array().ok_or_else(||
862+
PyroscopeError::AdHoc(format!("expected array, got {}", parsed))
863+
)?;
872864
let mut res = Vec::with_capacity(parsed.len());
873-
for v in parsed.members() {
874-
if v.is_string() {
875-
res.push(v.to_string());
865+
for v in parsed {
866+
if let Some(s) = v.as_str() {
867+
res.push(s.to_string());
876868
} else {
877869
return Err(PyroscopeError::AdHoc(format!(
878870
"invalid element value, not a string: {}",

0 commit comments

Comments
 (0)