mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-10-25 04:54:07 +00:00
Refactor JSON handling and improve code clarity
- Updated error messages in Http.jsonDecode for better context. - Enhanced serialization error handling in CompactJson5Serializer. - Reformatted struct definitions in tests for improved readability. - Adjusted assertions in tests for better clarity and consistency. - Refactored project file name checks for improved readability.
This commit is contained in:
parent
fdd1d145cf
commit
2b716351c1
4 changed files with 48 additions and 15 deletions
|
|
@ -72,7 +72,7 @@ function Http.jsonDecode(source)
|
||||||
if success then
|
if success then
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
print("[HTTP Response:json()] Native decode failed, using JSON5 for size:", #self.body)
|
print("[Http.jsonDecode] Native decode failed, using JSON5 for size:", #source)
|
||||||
return JSON5Decoder.Decode(source)
|
return JSON5Decoder.Decode(source)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
47
src/json.rs
47
src/json.rs
|
|
@ -165,8 +165,12 @@ impl serde::ser::Error for CompactJson5Error {
|
||||||
/// Returns an error if the value cannot be serialized to JSON5.
|
/// Returns an error if the value cannot be serialized to JSON5.
|
||||||
pub fn to_string_compact<T: Serialize>(value: &T) -> anyhow::Result<String> {
|
pub fn to_string_compact<T: Serialize>(value: &T) -> anyhow::Result<String> {
|
||||||
let mut output = String::new();
|
let mut output = String::new();
|
||||||
let mut serializer = CompactJson5Serializer { output: &mut output };
|
let mut serializer = CompactJson5Serializer {
|
||||||
value.serialize(&mut serializer).map_err(|e| anyhow::anyhow!("{}", e))?;
|
output: &mut output,
|
||||||
|
};
|
||||||
|
value
|
||||||
|
.serialize(&mut serializer)
|
||||||
|
.map_err(|e| anyhow::anyhow!("{}", e))?;
|
||||||
Ok(output)
|
Ok(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -764,7 +768,9 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_deserialize_infinity() {
|
fn test_deserialize_infinity() {
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct TestFloat { x: f64 }
|
struct TestFloat {
|
||||||
|
x: f64,
|
||||||
|
}
|
||||||
let result: TestFloat = from_str(r#"{ "x": Infinity }"#).unwrap();
|
let result: TestFloat = from_str(r#"{ "x": Infinity }"#).unwrap();
|
||||||
assert!(result.x.is_infinite() && result.x.is_sign_positive());
|
assert!(result.x.is_infinite() && result.x.is_sign_positive());
|
||||||
}
|
}
|
||||||
|
|
@ -772,7 +778,9 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_deserialize_negative_infinity() {
|
fn test_deserialize_negative_infinity() {
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct TestFloat { x: f64 }
|
struct TestFloat {
|
||||||
|
x: f64,
|
||||||
|
}
|
||||||
let result: TestFloat = from_str(r#"{ "x": -Infinity }"#).unwrap();
|
let result: TestFloat = from_str(r#"{ "x": -Infinity }"#).unwrap();
|
||||||
assert!(result.x.is_infinite() && result.x.is_sign_negative());
|
assert!(result.x.is_infinite() && result.x.is_sign_negative());
|
||||||
}
|
}
|
||||||
|
|
@ -780,7 +788,9 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_deserialize_nan() {
|
fn test_deserialize_nan() {
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct TestFloat { x: f64 }
|
struct TestFloat {
|
||||||
|
x: f64,
|
||||||
|
}
|
||||||
let result: TestFloat = from_str(r#"{ "x": NaN }"#).unwrap();
|
let result: TestFloat = from_str(r#"{ "x": NaN }"#).unwrap();
|
||||||
assert!(result.x.is_nan());
|
assert!(result.x.is_nan());
|
||||||
}
|
}
|
||||||
|
|
@ -956,7 +966,9 @@ mod tests {
|
||||||
|
|
||||||
assert_eq!(result.normal, 42.5);
|
assert_eq!(result.normal, 42.5);
|
||||||
assert!(result.infinity.is_infinite() && result.infinity.is_sign_positive());
|
assert!(result.infinity.is_infinite() && result.infinity.is_sign_positive());
|
||||||
assert!(result.negative_infinity.is_infinite() && result.negative_infinity.is_sign_negative());
|
assert!(
|
||||||
|
result.negative_infinity.is_infinite() && result.negative_infinity.is_sign_negative()
|
||||||
|
);
|
||||||
assert!(result.nan_value.is_nan());
|
assert!(result.nan_value.is_nan());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -994,7 +1006,9 @@ mod tests {
|
||||||
|
|
||||||
assert_eq!(deserialized.normal, original.normal);
|
assert_eq!(deserialized.normal, original.normal);
|
||||||
assert!(deserialized.infinity.is_infinite() && deserialized.infinity.is_sign_positive());
|
assert!(deserialized.infinity.is_infinite() && deserialized.infinity.is_sign_positive());
|
||||||
assert!(deserialized.neg_infinity.is_infinite() && deserialized.neg_infinity.is_sign_negative());
|
assert!(
|
||||||
|
deserialized.neg_infinity.is_infinite() && deserialized.neg_infinity.is_sign_negative()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -1047,15 +1061,22 @@ mod tests {
|
||||||
let deserialized: CFrameData = from_str(&json_str).unwrap();
|
let deserialized: CFrameData = from_str(&json_str).unwrap();
|
||||||
|
|
||||||
// Verify position
|
// Verify position
|
||||||
assert!(deserialized.position[0].is_infinite() && deserialized.position[0].is_sign_positive());
|
assert!(
|
||||||
|
deserialized.position[0].is_infinite() && deserialized.position[0].is_sign_positive()
|
||||||
|
);
|
||||||
assert!(deserialized.position[1].is_nan());
|
assert!(deserialized.position[1].is_nan());
|
||||||
assert!(deserialized.position[2].is_infinite() && deserialized.position[2].is_sign_negative());
|
assert!(
|
||||||
|
deserialized.position[2].is_infinite() && deserialized.position[2].is_sign_negative()
|
||||||
|
);
|
||||||
|
|
||||||
// Verify orientation
|
// Verify orientation
|
||||||
assert_eq!(deserialized.orientation[0][0], 1.0);
|
assert_eq!(deserialized.orientation[0][0], 1.0);
|
||||||
assert!(deserialized.orientation[0][2].is_nan());
|
assert!(deserialized.orientation[0][2].is_nan());
|
||||||
assert!(deserialized.orientation[1][1].is_infinite());
|
assert!(deserialized.orientation[1][1].is_infinite());
|
||||||
assert!(deserialized.orientation[2][0].is_infinite() && deserialized.orientation[2][0].is_sign_negative());
|
assert!(
|
||||||
|
deserialized.orientation[2][0].is_infinite()
|
||||||
|
&& deserialized.orientation[2][0].is_sign_negative()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -1093,7 +1114,11 @@ mod tests {
|
||||||
let deserialized: MetaFile = from_str(&json_str).unwrap();
|
let deserialized: MetaFile = from_str(&json_str).unwrap();
|
||||||
|
|
||||||
assert_eq!(deserialized.class_name, "Humanoid");
|
assert_eq!(deserialized.class_name, "Humanoid");
|
||||||
assert!(deserialized.properties.get("Transparency").unwrap().is_nan());
|
assert!(deserialized
|
||||||
|
.properties
|
||||||
|
.get("Transparency")
|
||||||
|
.unwrap()
|
||||||
|
.is_nan());
|
||||||
assert!(deserialized.properties.get("Health").unwrap().is_infinite());
|
assert!(deserialized.properties.get("Health").unwrap().is_infinite());
|
||||||
assert_eq!(*deserialized.properties.get("WalkSpeed").unwrap(), 16.0);
|
assert_eq!(*deserialized.properties.get("WalkSpeed").unwrap(), 16.0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,11 @@ use crate::{
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Represents 'default' project names that act as `init` files
|
/// Represents 'default' project names that act as `init` files
|
||||||
pub static DEFAULT_PROJECT_NAMES: [&str; 3] = ["default.project.json", "default.project.jsonc", "default.project.json5"];
|
pub static DEFAULT_PROJECT_NAMES: [&str; 3] = [
|
||||||
|
"default.project.json",
|
||||||
|
"default.project.jsonc",
|
||||||
|
"default.project.json5",
|
||||||
|
];
|
||||||
|
|
||||||
/// Error type returned by any function that handles projects.
|
/// Error type returned by any function that handles projects.
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error)]
|
||||||
|
|
@ -146,7 +150,11 @@ impl Project {
|
||||||
pub fn is_project_file(path: &Path) -> bool {
|
pub fn is_project_file(path: &Path) -> bool {
|
||||||
path.file_name()
|
path.file_name()
|
||||||
.and_then(|name| name.to_str())
|
.and_then(|name| name.to_str())
|
||||||
.map(|name| name.ends_with(".project.json") || name.ends_with(".project.jsonc") || name.ends_with(".project.json5"))
|
.map(|name| {
|
||||||
|
name.ends_with(".project.json")
|
||||||
|
|| name.ends_with(".project.jsonc")
|
||||||
|
|| name.ends_with(".project.json5")
|
||||||
|
})
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ use crate::{
|
||||||
ServerInfoResponse, SocketPacket, SocketPacketBody, SocketPacketType, SubscribeMessage,
|
ServerInfoResponse, SocketPacket, SocketPacketBody, SocketPacketType, SubscribeMessage,
|
||||||
WriteRequest, WriteResponse, PROTOCOL_VERSION, SERVER_VERSION,
|
WriteRequest, WriteResponse, PROTOCOL_VERSION, SERVER_VERSION,
|
||||||
},
|
},
|
||||||
util::{json_response, json_ok},
|
util::{json_ok, json_response},
|
||||||
},
|
},
|
||||||
web_api::{
|
web_api::{
|
||||||
BufferEncode, InstanceUpdate, RefPatchRequest, RefPatchResponse, SerializeRequest,
|
BufferEncode, InstanceUpdate, RefPatchRequest, RefPatchResponse, SerializeRequest,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue