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.rs6
-rw-r--r--crates/shirabe/src/json/json_formatter.rs29
-rw-r--r--crates/shirabe/src/json/json_manipulator.rs7
-rw-r--r--crates/shirabe/src/json/json_validation_exception.rs4
4 files changed, 33 insertions, 13 deletions
diff --git a/crates/shirabe/src/json/json_file.rs b/crates/shirabe/src/json/json_file.rs
index c1fa495..d08e207 100644
--- a/crates/shirabe/src/json/json_file.rs
+++ b/crates/shirabe/src/json/json_file.rs
@@ -401,7 +401,11 @@ impl JsonFile {
/// @param int $options json_encode options (defaults to JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)
/// @param string $indent Indentation string
/// @return string Encoded json
- pub fn encode(data: &PhpMixed, options: i64, indent: &str) -> String {
+ pub fn encode(data: &PhpMixed, options: i64) -> String {
+ Self::encode_with_indent(data, options, Self::INDENT_DEFAULT)
+ }
+
+ pub fn encode_with_indent(data: &PhpMixed, options: i64, indent: &str) -> String {
let json = json_encode_ex(data, options);
let json = match json {
diff --git a/crates/shirabe/src/json/json_formatter.rs b/crates/shirabe/src/json/json_formatter.rs
index 1696982..47680b1 100644
--- a/crates/shirabe/src/json/json_formatter.rs
+++ b/crates/shirabe/src/json/json_formatter.rs
@@ -1,6 +1,6 @@
//! ref: composer/src/Composer/Json/JsonFormatter.php
-use shirabe_external_packages::composer::pcre::preg::Preg;
+use shirabe_external_packages::composer::pcre::preg::{CaptureKey, Preg};
use shirabe_php_shim::{PhpMixed, function_exists, mb_convert_encoding, pack};
pub struct JsonFormatter;
@@ -44,27 +44,40 @@ impl JsonFormatter {
if unescape_unicode && function_exists("mb_convert_encoding") {
buffer = Preg::replace_callback(
r"/(\\+)u([0-9a-f]{4})/i",
- |matches: &[String]| -> String {
- let l = matches[1].len();
+ |matches: &indexmap::IndexMap<CaptureKey, String>| -> String {
+ let m0 = matches
+ .get(&CaptureKey::ByIndex(0))
+ .cloned()
+ .unwrap_or_default();
+ let m1 = matches
+ .get(&CaptureKey::ByIndex(1))
+ .cloned()
+ .unwrap_or_default();
+ let m2 = matches
+ .get(&CaptureKey::ByIndex(2))
+ .cloned()
+ .unwrap_or_default();
+ let l = m1.len();
if l % 2 != 0 {
- let code = i64::from_str_radix(&matches[2], 16).unwrap_or(0);
+ let code = i64::from_str_radix(&m2, 16).unwrap_or(0);
if code >= 0xD800 && code <= 0xDFFF {
- return matches[0].clone();
+ return m0;
}
return "\\".repeat(l - 1)
+ &mb_convert_encoding(
- pack("H*", &[PhpMixed::String(matches[2].clone())]),
+ pack("H*", &[PhpMixed::String(m2)]),
"UTF-8",
"UCS-2BE",
);
}
- matches[0].clone()
+ m0
},
&buffer,
- );
+ )
+ .unwrap_or(buffer);
}
result.push_str(&buffer);
diff --git a/crates/shirabe/src/json/json_manipulator.rs b/crates/shirabe/src/json/json_manipulator.rs
index ebbfff9..b9c6a5c 100644
--- a/crates/shirabe/src/json/json_manipulator.rs
+++ b/crates/shirabe/src/json/json_manipulator.rs
@@ -128,8 +128,7 @@ impl JsonManipulator {
JsonFile::encode(
&PhpMixed::String(str_replace("\\/", "/", &existing_owned)),
0
- )
- .unwrap_or_default(),
+ ),
m.get("separator").cloned().unwrap_or_default(),
constraint_owned
)
@@ -1024,7 +1023,7 @@ impl JsonManipulator {
if now_empty {
arr.insert(
name_owned.clone(),
- Box::new(PhpMixed::Object(ArrayObject::new())),
+ Box::new(PhpMixed::Object(ArrayObject::new(None))),
);
}
}
@@ -1068,7 +1067,7 @@ impl JsonManipulator {
if now_empty {
arr.insert(
name_capture.clone(),
- Box::new(PhpMixed::Object(ArrayObject::new())),
+ Box::new(PhpMixed::Object(ArrayObject::new(None))),
);
}
}
diff --git a/crates/shirabe/src/json/json_validation_exception.rs b/crates/shirabe/src/json/json_validation_exception.rs
index 5f3cbbf..4549a0f 100644
--- a/crates/shirabe/src/json/json_validation_exception.rs
+++ b/crates/shirabe/src/json/json_validation_exception.rs
@@ -19,6 +19,10 @@ impl JsonValidationException {
pub fn get_errors(&self) -> &Vec<String> {
&self.errors
}
+
+ pub fn get_message(&self) -> &str {
+ &self.inner.message
+ }
}
impl std::fmt::Display for JsonValidationException {