Skip to content

fix: replace json with serde_json #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ libflate = "1.2.0"
libc = "^0.2.124"
prost = "0.11"
winapi = "0.3.9"
json = "0.12.4"
serde_json = "1.0.115"

[dev-dependencies]
tokio = { version = "1.18", features = ["full"] }
Expand Down
2 changes: 1 addition & 1 deletion pyroscope_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pyroscope = { path = "../", default-features = false }
pyroscope_pprofrs = { path = "../pyroscope_backends/pyroscope_pprofrs", default-features = false }
pyroscope_rbspy = { path = "../pyroscope_backends/pyroscope_rbspy", default-features = false }
pyroscope_pyspy = { path = "../pyroscope_backends/pyroscope_pyspy", default-features = false }
json = "0.12.4"
serde_json = "1.0.115"

[dependencies.clap]
version = "3.2"
Expand Down
6 changes: 3 additions & 3 deletions pyroscope_cli/src/utils/app_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl AppConfig {
.map(|s| s.to_string())
.collect::<Vec<String>>();

AppConfig::set("command_args", &json::stringify(command_args))?;
AppConfig::set("command_args", &serde_json::to_string(&command_args)?)?;
}
}
if sub_exec.is_present("log_level") {
Expand Down Expand Up @@ -314,9 +314,9 @@ fn set_http_headers(cmd: &clap::ArgMatches) -> Result<()> {
http_headers_map.insert(kv[0].to_string(), kv[1].to_string());
}
}
let http_header = json::stringify(http_headers_map);
let http_header = serde_json::to_string(&http_headers_map)?;
AppConfig::set("http_headers_json", &http_header)?;
};
}
return Ok(());
}
}
3 changes: 3 additions & 0 deletions pyroscope_cli/src/utils/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ pub enum Error {

#[error(transparent)]
PyroscopeError(#[from] pyroscope::PyroscopeError),

#[error(transparent)]
Json(#[from] serde_json::Error),
}

impl Error {
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub enum PyroscopeError {
Io(#[from] std::io::Error),

#[error(transparent)]
Json(#[from] json::JsonError),
Json(#[from] serde_json::Error),
}

impl PyroscopeError {
Expand Down
36 changes: 14 additions & 22 deletions src/pyroscope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ use crate::{
PyroscopeError,
};

use json;

use crate::backend::BackendImpl;
use crate::pyroscope::Compression::GZIP;
use crate::pyroscope::ReportEncoding::PPROF;
Expand Down Expand Up @@ -841,16 +839,13 @@ impl PyroscopeAgent<PyroscopeAgentRunning> {

pub fn parse_http_headers_json(http_headers_json: String) -> Result<HashMap<String, String>> {
let mut http_headers = HashMap::new();
let parsed = json::parse(&http_headers_json)?;
if !parsed.is_object() {
return Err(PyroscopeError::AdHoc(format!(
"expected object, got {}",
parsed
)));
}
for (k, v) in parsed.entries() {
if v.is_string() {
http_headers.insert(k.to_string(), v.to_string());
let parsed: serde_json::Value = serde_json::from_str(&http_headers_json)?;
let parsed = parsed.as_object().ok_or_else(||
PyroscopeError::AdHoc(format!("expected object, got {}", parsed))
)?;
for (k, v) in parsed {
if let Some(value) = v.as_str() {
http_headers.insert(k.to_string(), value.to_string());
} else {
return Err(PyroscopeError::AdHoc(format!(
"invalid http header value, not a string: {}",
Expand All @@ -862,17 +857,14 @@ pub fn parse_http_headers_json(http_headers_json: String) -> Result<HashMap<Stri
}

pub fn parse_vec_string_json(s: String) -> Result<Vec<String>> {
let parsed = json::parse(&s)?;
if !parsed.is_array() {
return Err(PyroscopeError::AdHoc(format!(
"expected array, got {}",
parsed
)));
}
let parsed: serde_json::Value = serde_json::from_str(&s)?;
let parsed = parsed.as_array().ok_or_else(||
PyroscopeError::AdHoc(format!("expected array, got {}", parsed))
)?;
let mut res = Vec::with_capacity(parsed.len());
for v in parsed.members() {
if v.is_string() {
res.push(v.to_string());
for v in parsed {
if let Some(s) = v.as_str() {
res.push(s.to_string());
} else {
return Err(PyroscopeError::AdHoc(format!(
"invalid element value, not a string: {}",
Expand Down
Loading