aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/json
diff options
context:
space:
mode:
Diffstat (limited to 'crates/shirabe/src/json')
-rw-r--r--crates/shirabe/src/json/json_file.rs19
-rw-r--r--crates/shirabe/src/json/json_manipulator.rs34
2 files changed, 27 insertions, 26 deletions
diff --git a/crates/shirabe/src/json/json_file.rs b/crates/shirabe/src/json/json_file.rs
index 78ed76d7..e31cd70d 100644
--- a/crates/shirabe/src/json/json_file.rs
+++ b/crates/shirabe/src/json/json_file.rs
@@ -14,8 +14,8 @@ use shirabe_php_shim::Catch as _;
use shirabe_php_shim::{
InvalidArgumentException, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE,
PhpMixed, RuntimeException, UnexpectedValueException, dirname, file_exists, file_get_contents,
- file_put_contents, is_dir, is_file, json_decode, json_encode_ex, mkdir, php_regex, realpath,
- str_repeat, strlen, strpos, usleep,
+ file_put_contents, is_dir, is_file, json_decode_assoc, json_decode_obj, json_encode_ex, mkdir,
+ php_regex, realpath, str_repeat, strlen, strpos, usleep,
};
use shirabe_seld_json_lint::{ParsingException, ParsingExceptionDetails};
@@ -309,7 +309,7 @@ impl JsonFile {
.into());
}
let content = file_get_contents(&self.path).unwrap_or_default();
- let data = json_decode(&content, false)?;
+ let data = json_decode_obj(&content)?;
if matches!(data, PhpMixed::Null) && content != "null" {
Self::validate_syntax(&content, Some(&self.path))?;
@@ -362,7 +362,7 @@ impl JsonFile {
};
if schema == Self::STRICT_SCHEMA && is_composer_schema_file {
- schema_data = json_decode(Self::COMPOSER_SCHEMA_JSON, false)?;
+ schema_data = json_decode_obj(Self::COMPOSER_SCHEMA_JSON)?;
if let PhpMixed::Object(map) = &mut schema_data {
map.insert("additionalProperties".to_string(), PhpMixed::Bool(false));
map.insert(
@@ -476,11 +476,12 @@ impl JsonFile {
None => return Ok(PhpMixed::Null),
Some(j) => j,
};
- let mut data = json_decode(json, true)?;
+ let mut data = json_decode_assoc(json)?;
// PHP: `null === $data && JSON_ERROR_NONE !== json_last_error()`, i.e. the decode produced
- // null because of an error rather than because the input was the literal `null`. json_decode
- // here swallows the error into PhpMixed::Null, so detect the failure by comparing the source
- // against `null`, mirroring validateSchema's own `'null' !== $content` check.
+ // null because of an error rather than because the input was the literal `null`.
+ // json_decode_assoc swallows the error into PhpMixed::Null, so detect the failure by
+ // comparing the source against `null`, mirroring validateSchema's own
+ // `'null' !== $content` check.
if matches!(data, PhpMixed::Null) && json != "null" {
// attempt resolving simple conflicts in lock files so that one can run `composer update --lock` and get a valid lock file
if let Some(file) = file
@@ -498,7 +499,7 @@ impl JsonFile {
&mut count,
);
if count == 1 {
- data = json_decode(&replaced, true)?;
+ data = json_decode_assoc(&replaced)?;
if !matches!(data, PhpMixed::Null) {
return Ok(data);
}
diff --git a/crates/shirabe/src/json/json_manipulator.rs b/crates/shirabe/src/json/json_manipulator.rs
index 766137ca..08880e80 100644
--- a/crates/shirabe/src/json/json_manipulator.rs
+++ b/crates/shirabe/src/json/json_manipulator.rs
@@ -8,8 +8,8 @@ use shirabe_pcre::{CaptureKey, Preg};
use shirabe_php_shim::{
InvalidArgumentException, LogicException, PhpMixed, addcslashes, array_key_exists, array_keys,
array_reverse, empty, explode, implode, in_array_loose, is_array, is_int, is_numeric,
- json_decode, php_regex, php_truthy, preg_quote, rtrim, str_repeat, str_replace, strlen,
- strnatcmp, strpos, substr, trim, uksort,
+ json_decode_assoc, json_decode_obj, php_regex, php_truthy, preg_quote, rtrim, str_repeat,
+ str_replace, strlen, strnatcmp, strpos, substr, trim, uksort,
};
#[derive(Debug)]
@@ -156,7 +156,7 @@ impl JsonManipulator {
}
if sort_packages {
- let mut requirements = json_decode(&links, true)?;
+ let mut requirements = json_decode_assoc(&links)?;
Self::sort_packages(&mut requirements);
links = self.format(&requirements, 0, false)?;
}
@@ -230,7 +230,7 @@ impl JsonManipulator {
}
fn do_convert_repositories_from_assoc_to_list(&mut self) -> anyhow::Result<bool> {
- let decoded = json_decode(&self.contents, false)?;
+ let decoded = json_decode_obj(&self.contents)?;
let repositories_value: Option<PhpMixed> = decoded
.as_object()
@@ -405,7 +405,7 @@ impl JsonManipulator {
let raw_repo = self.contents[repo_pos..repo_end].to_string();
// invalid match due to un-regexable content, abort
- if json_decode(&raw_repo, false)?.as_bool() == Some(false) {
+ if json_decode_obj(&raw_repo)?.as_bool() == Some(false) {
return Ok(false);
}
@@ -515,7 +515,7 @@ impl JsonManipulator {
}
fn do_remove_repository(&mut self, name: &str) -> anyhow::Result<bool> {
- let decoded = json_decode(&self.contents, false)?;
+ let decoded = json_decode_obj(&self.contents)?;
let repositories_value: Option<PhpMixed> = decoded
.as_object()
.and_then(|o| o.get("repositories").cloned());
@@ -706,7 +706,7 @@ impl JsonManipulator {
let node_end = self.contents[node.value_end..].to_string();
let mut children = self.contents[node.value_pos..node.value_end].to_string();
// invalid match due to un-regexable content, abort
- if !php_truthy(&json_decode(&children, false)?) {
+ if !php_truthy(&json_decode_obj(&children)?) {
return Ok(false);
}
@@ -723,7 +723,7 @@ impl JsonManipulator {
let content_str = children[cm.value_pos..cm.value_end].to_string();
let mut value_local = value;
if let Some(sub_name) = sub_name {
- let mut cur_val = json_decode(&content_str, true).unwrap_or(PhpMixed::Null);
+ let mut cur_val = json_decode_assoc(&content_str).unwrap_or(PhpMixed::Null);
if !is_array(&cur_val) {
cur_val = PhpMixed::Array(IndexMap::new());
}
@@ -858,7 +858,7 @@ impl JsonManipulator {
let children = self.contents[node.value_pos..node.value_end].to_string();
// invalid match due to un-regexable content, abort
- if !php_truthy(&json_decode(&children, true)?) {
+ if !php_truthy(&json_decode_assoc(&children)?) {
return Ok(false);
}
@@ -956,7 +956,7 @@ impl JsonManipulator {
// we have a subname, so we restore the rest of $name
if let Some(sub) = sub_name {
- let mut cur_val = json_decode(&children, true)?;
+ let mut cur_val = json_decode_assoc(&children)?;
if let Some(arr) = cur_val.as_array_mut() {
if let Some(inner) = arr.get_mut(&name_owned).and_then(|v| v.as_array_mut()) {
inner.shift_remove(&sub);
@@ -985,7 +985,7 @@ impl JsonManipulator {
// subkey removed when a sub_name is in play.
let mut children_final = children_clean.clone();
if let Some(ref sub) = sub_name {
- let mut cur_val = json_decode(&children, true).unwrap_or(PhpMixed::Null);
+ let mut cur_val = json_decode_assoc(&children).unwrap_or(PhpMixed::Null);
if let Some(arr) = cur_val.as_array_mut() {
if let Some(inner) = arr.get_mut(&name_owned).and_then(|v| v.as_array_mut()) {
inner.shift_remove(sub);
@@ -1035,7 +1035,7 @@ impl JsonManipulator {
let node_end = self.contents[node.value_end..].to_string();
let mut children = self.contents[node.value_pos..node.value_end].to_string();
// invalid match due to un-regexable content, abort
- if json_decode(&children, false)?.as_bool() == Some(false) {
+ if json_decode_obj(&children)?.as_bool() == Some(false) {
return Ok(false);
}
@@ -1168,7 +1168,7 @@ impl JsonManipulator {
let node_end = self.contents[node.value_end..].to_string();
let children = self.contents[node.value_pos..node.value_end].to_string();
// invalid match due to un-regexable content, abort
- if json_decode(&children, false)?.as_bool() == Some(false) {
+ if json_decode_obj(&children)?.as_bool() == Some(false) {
return Ok(false);
}
@@ -1233,7 +1233,7 @@ impl JsonManipulator {
let children = self.contents[node.value_pos..node.value_end].to_string();
// invalid match due to un-regexable content, abort
- if json_decode(&children, true)?.as_bool() == Some(false) {
+ if json_decode_assoc(&children)?.as_bool() == Some(false) {
return Ok(false);
}
@@ -1314,7 +1314,7 @@ impl JsonManipulator {
if let Some(m) = key_match {
// invalid match due to un-regexable content, abort
let key_capture = &self.contents[m.key_pos..m.value_end];
- if json_decode(&format!("{{{}}}", key_capture), false)?.is_null() {
+ if json_decode_obj(&format!("{{{}}}", key_capture))?.is_null() {
return Ok(false);
}
@@ -1396,7 +1396,7 @@ impl JsonManipulator {
if let Some(m) = key_match {
// invalid match due to un-regexable content, abort
let removal = &self.contents[m.key_pos..m.value_end];
- if json_decode(&format!("{{{}}}", removal), false)?.is_null() {
+ if json_decode_obj(&format!("{{{}}}", removal))?.is_null() {
return Ok(false);
}
@@ -1448,7 +1448,7 @@ impl JsonManipulator {
if let Some(m) = key_match {
// invalid match due to un-regexable content, abort
let removal = &self.contents[m.value_pos..m.value_end];
- if json_decode(removal, false)?.as_bool() == Some(false) {
+ if json_decode_obj(removal)?.as_bool() == Some(false) {
return Ok(false);
}