mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-10-23 05:51:39 +00:00
json5 work
This commit is contained in:
parent
cd63b5c9c0
commit
fdd1d145cf
22 changed files with 1977 additions and 90 deletions
|
|
@ -25,8 +25,11 @@ impl FmtProjectCommand {
|
|||
let project = Project::load_fuzzy(&vfs, &base_path)?
|
||||
.context("A project file is required to run 'rojo fmt-project'")?;
|
||||
|
||||
let serialized = serde_json::to_string_pretty(&project)
|
||||
.context("could not re-encode project file as JSON")?;
|
||||
let serialized = String::from_utf8(
|
||||
crate::json::to_vec_pretty_sorted(&project)
|
||||
.context("could not re-encode project file as JSON5")?,
|
||||
)
|
||||
.context("JSON5 output was not valid UTF-8")?;
|
||||
|
||||
fs_err::write(&project.file_location, serialized)
|
||||
.context("could not write back to project file")?;
|
||||
|
|
|
|||
|
|
@ -239,12 +239,13 @@ fn write_sourcemap(
|
|||
|
||||
if let Some(output_path) = output {
|
||||
let mut file = BufWriter::new(File::create(output_path)?);
|
||||
serde_json::to_writer(&mut file, &root_node)?;
|
||||
let json_output = json5::to_string(&root_node)?;
|
||||
file.write_all(json_output.as_bytes())?;
|
||||
file.flush()?;
|
||||
|
||||
println!("Created sourcemap at {}", output_path.display());
|
||||
} else {
|
||||
let output = serde_json::to_string(&root_node)?;
|
||||
let output = json5::to_string(&root_node)?;
|
||||
println!("{}", output);
|
||||
}
|
||||
|
||||
|
|
|
|||
369
src/json.rs
369
src/json.rs
|
|
@ -138,6 +138,368 @@ enum Json5Value {
|
|||
Object(std::collections::BTreeMap<String, Json5Value>),
|
||||
}
|
||||
|
||||
/// Error type for compact JSON5 serialization.
|
||||
#[derive(Debug)]
|
||||
struct CompactJson5Error(String);
|
||||
|
||||
impl std::fmt::Display for CompactJson5Error {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for CompactJson5Error {}
|
||||
|
||||
impl serde::ser::Error for CompactJson5Error {
|
||||
fn custom<T: std::fmt::Display>(msg: T) -> Self {
|
||||
CompactJson5Error(msg.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
/// Serialize a value to a compact JSON5 string (for HTTP transport).
|
||||
///
|
||||
/// Supports NaN/Infinity. No whitespace between elements.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns an error if the value cannot be serialized to JSON5.
|
||||
pub fn to_string_compact<T: Serialize>(value: &T) -> anyhow::Result<String> {
|
||||
let mut output = String::new();
|
||||
let mut serializer = CompactJson5Serializer { output: &mut output };
|
||||
value.serialize(&mut serializer).map_err(|e| anyhow::anyhow!("{}", e))?;
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
struct CompactJson5Serializer<'a> {
|
||||
output: &'a mut String,
|
||||
}
|
||||
|
||||
impl<'a> serde::Serializer for &mut CompactJson5Serializer<'a> {
|
||||
type Ok = ();
|
||||
type Error = CompactJson5Error;
|
||||
type SerializeSeq = Self;
|
||||
type SerializeTuple = Self;
|
||||
type SerializeTupleStruct = Self;
|
||||
type SerializeTupleVariant = Self;
|
||||
type SerializeMap = Self;
|
||||
type SerializeStruct = Self;
|
||||
type SerializeStructVariant = Self;
|
||||
|
||||
fn serialize_bool(self, v: bool) -> Result<(), Self::Error> {
|
||||
self.output.push_str(if v { "true" } else { "false" });
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_i8(self, v: i8) -> Result<(), Self::Error> {
|
||||
self.output.push_str(&v.to_string());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_i16(self, v: i16) -> Result<(), Self::Error> {
|
||||
self.output.push_str(&v.to_string());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_i32(self, v: i32) -> Result<(), Self::Error> {
|
||||
self.output.push_str(&v.to_string());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_i64(self, v: i64) -> Result<(), Self::Error> {
|
||||
self.output.push_str(&v.to_string());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_u8(self, v: u8) -> Result<(), Self::Error> {
|
||||
self.output.push_str(&v.to_string());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_u16(self, v: u16) -> Result<(), Self::Error> {
|
||||
self.output.push_str(&v.to_string());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_u32(self, v: u32) -> Result<(), Self::Error> {
|
||||
self.output.push_str(&v.to_string());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_u64(self, v: u64) -> Result<(), Self::Error> {
|
||||
self.output.push_str(&v.to_string());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_f32(self, v: f32) -> Result<(), Self::Error> {
|
||||
self.serialize_f64(v as f64)
|
||||
}
|
||||
|
||||
fn serialize_f64(self, v: f64) -> Result<(), Self::Error> {
|
||||
if v.is_nan() {
|
||||
self.output.push_str("NaN");
|
||||
} else if v.is_infinite() {
|
||||
if v.is_sign_positive() {
|
||||
self.output.push_str("Infinity");
|
||||
} else {
|
||||
self.output.push_str("-Infinity");
|
||||
}
|
||||
} else {
|
||||
self.output.push_str(&v.to_string());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_char(self, v: char) -> Result<(), Self::Error> {
|
||||
self.serialize_str(&v.to_string())
|
||||
}
|
||||
|
||||
fn serialize_str(self, v: &str) -> Result<(), Self::Error> {
|
||||
self.output.push('"');
|
||||
for c in v.chars() {
|
||||
match c {
|
||||
'"' => self.output.push_str("\\\""),
|
||||
'\\' => self.output.push_str("\\\\"),
|
||||
'\n' => self.output.push_str("\\n"),
|
||||
'\r' => self.output.push_str("\\r"),
|
||||
'\t' => self.output.push_str("\\t"),
|
||||
c if c < ' ' => self.output.push_str(&format!("\\u{:04x}", c as u32)),
|
||||
c => self.output.push(c),
|
||||
}
|
||||
}
|
||||
self.output.push('"');
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_bytes(self, v: &[u8]) -> Result<(), Self::Error> {
|
||||
use serde::ser::SerializeSeq;
|
||||
let mut seq = self.serialize_seq(Some(v.len()))?;
|
||||
for b in v {
|
||||
seq.serialize_element(b)?;
|
||||
}
|
||||
seq.end()
|
||||
}
|
||||
|
||||
fn serialize_none(self) -> Result<(), Self::Error> {
|
||||
self.serialize_unit()
|
||||
}
|
||||
|
||||
fn serialize_some<T: ?Sized + Serialize>(self, value: &T) -> Result<(), Self::Error> {
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_unit(self) -> Result<(), Self::Error> {
|
||||
self.output.push_str("null");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_unit_struct(self, _name: &'static str) -> Result<(), Self::Error> {
|
||||
self.serialize_unit()
|
||||
}
|
||||
|
||||
fn serialize_unit_variant(
|
||||
self,
|
||||
_name: &'static str,
|
||||
_variant_index: u32,
|
||||
variant: &'static str,
|
||||
) -> Result<(), Self::Error> {
|
||||
self.serialize_str(variant)
|
||||
}
|
||||
|
||||
fn serialize_newtype_struct<T: ?Sized + Serialize>(
|
||||
self,
|
||||
_name: &'static str,
|
||||
value: &T,
|
||||
) -> Result<(), Self::Error> {
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
fn serialize_newtype_variant<T: ?Sized + Serialize>(
|
||||
self,
|
||||
_name: &'static str,
|
||||
_variant_index: u32,
|
||||
variant: &'static str,
|
||||
value: &T,
|
||||
) -> Result<(), Self::Error> {
|
||||
self.output.push('{');
|
||||
self.serialize_str(variant)?;
|
||||
self.output.push(':');
|
||||
value.serialize(&mut *self)?;
|
||||
self.output.push('}');
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
|
||||
self.output.push('[');
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
|
||||
self.serialize_seq(Some(len))
|
||||
}
|
||||
|
||||
fn serialize_tuple_struct(
|
||||
self,
|
||||
_name: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeTupleStruct, Self::Error> {
|
||||
self.serialize_seq(Some(len))
|
||||
}
|
||||
|
||||
fn serialize_tuple_variant(
|
||||
self,
|
||||
_name: &'static str,
|
||||
_variant_index: u32,
|
||||
variant: &'static str,
|
||||
_len: usize,
|
||||
) -> Result<Self::SerializeTupleVariant, Self::Error> {
|
||||
self.output.push('{');
|
||||
self.serialize_str(variant)?;
|
||||
self.output.push_str(":[");
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
|
||||
self.output.push('{');
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn serialize_struct(
|
||||
self,
|
||||
_name: &'static str,
|
||||
len: usize,
|
||||
) -> Result<Self::SerializeStruct, Self::Error> {
|
||||
self.serialize_map(Some(len))
|
||||
}
|
||||
|
||||
fn serialize_struct_variant(
|
||||
self,
|
||||
_name: &'static str,
|
||||
_variant_index: u32,
|
||||
variant: &'static str,
|
||||
_len: usize,
|
||||
) -> Result<Self::SerializeStructVariant, Self::Error> {
|
||||
self.output.push('{');
|
||||
self.serialize_str(variant)?;
|
||||
self.output.push_str(":{");
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> serde::ser::SerializeSeq for &mut CompactJson5Serializer<'a> {
|
||||
type Ok = ();
|
||||
type Error = CompactJson5Error;
|
||||
|
||||
fn serialize_element<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), Self::Error> {
|
||||
if !self.output.ends_with('[') {
|
||||
self.output.push(',');
|
||||
}
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<(), Self::Error> {
|
||||
self.output.push(']');
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> serde::ser::SerializeTuple for &mut CompactJson5Serializer<'a> {
|
||||
type Ok = ();
|
||||
type Error = CompactJson5Error;
|
||||
|
||||
fn serialize_element<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), Self::Error> {
|
||||
serde::ser::SerializeSeq::serialize_element(self, value)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<(), Self::Error> {
|
||||
serde::ser::SerializeSeq::end(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> serde::ser::SerializeTupleStruct for &mut CompactJson5Serializer<'a> {
|
||||
type Ok = ();
|
||||
type Error = CompactJson5Error;
|
||||
|
||||
fn serialize_field<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), Self::Error> {
|
||||
serde::ser::SerializeSeq::serialize_element(self, value)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<(), Self::Error> {
|
||||
serde::ser::SerializeSeq::end(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> serde::ser::SerializeTupleVariant for &mut CompactJson5Serializer<'a> {
|
||||
type Ok = ();
|
||||
type Error = CompactJson5Error;
|
||||
|
||||
fn serialize_field<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), Self::Error> {
|
||||
serde::ser::SerializeSeq::serialize_element(self, value)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<(), Self::Error> {
|
||||
self.output.push_str("]}");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> serde::ser::SerializeMap for &mut CompactJson5Serializer<'a> {
|
||||
type Ok = ();
|
||||
type Error = CompactJson5Error;
|
||||
|
||||
fn serialize_key<T: ?Sized + Serialize>(&mut self, key: &T) -> Result<(), Self::Error> {
|
||||
if !self.output.ends_with('{') {
|
||||
self.output.push(',');
|
||||
}
|
||||
key.serialize(&mut **self)
|
||||
}
|
||||
|
||||
fn serialize_value<T: ?Sized + Serialize>(&mut self, value: &T) -> Result<(), Self::Error> {
|
||||
self.output.push(':');
|
||||
value.serialize(&mut **self)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<(), Self::Error> {
|
||||
self.output.push('}');
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> serde::ser::SerializeStruct for &mut CompactJson5Serializer<'a> {
|
||||
type Ok = ();
|
||||
type Error = CompactJson5Error;
|
||||
|
||||
fn serialize_field<T: ?Sized + Serialize>(
|
||||
&mut self,
|
||||
key: &'static str,
|
||||
value: &T,
|
||||
) -> Result<(), Self::Error> {
|
||||
serde::ser::SerializeMap::serialize_key(self, key)?;
|
||||
serde::ser::SerializeMap::serialize_value(self, value)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<(), Self::Error> {
|
||||
serde::ser::SerializeMap::end(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> serde::ser::SerializeStructVariant for &mut CompactJson5Serializer<'a> {
|
||||
type Ok = ();
|
||||
type Error = CompactJson5Error;
|
||||
|
||||
fn serialize_field<T: ?Sized + Serialize>(
|
||||
&mut self,
|
||||
key: &'static str,
|
||||
value: &T,
|
||||
) -> Result<(), Self::Error> {
|
||||
serde::ser::SerializeStruct::serialize_field(self, key, value)
|
||||
}
|
||||
|
||||
fn end(self) -> Result<(), Self::Error> {
|
||||
self.output.push_str("}}");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Serialize a value to a JSON5 byte vector with sorted keys.
|
||||
///
|
||||
/// Supports NaN/Infinity and sorts object keys alphabetically.
|
||||
|
|
@ -148,10 +510,11 @@ enum Json5Value {
|
|||
pub fn to_vec_pretty_sorted<T: Serialize>(value: &T) -> anyhow::Result<Vec<u8>> {
|
||||
// Serialize to json5 string (preserves NaN/Infinity)
|
||||
let json5_str = json5::to_string(value).context("Failed to serialize to JSON5")?;
|
||||
|
||||
|
||||
// Parse into Json5Value which uses BTreeMap (sorted keys)
|
||||
let sorted: Json5Value = json5::from_str(&json5_str).context("Failed to parse JSON5 for sorting")?;
|
||||
|
||||
let sorted: Json5Value =
|
||||
json5::from_str(&json5_str).context("Failed to parse JSON5 for sorting")?;
|
||||
|
||||
// Serialize back to json5 (now sorted)
|
||||
let output = json5::to_string(&sorted).context("Failed to serialize sorted JSON5")?;
|
||||
Ok(output.into_bytes())
|
||||
|
|
|
|||
|
|
@ -516,8 +516,11 @@ mod test {
|
|||
)
|
||||
.unwrap();
|
||||
|
||||
let serialized = serde_json::to_string(&project_node).unwrap();
|
||||
assert_eq!(serialized, r#"{"$path":{"optional":"../src"}}"#);
|
||||
let serialized = json5::to_string(&project_node).unwrap();
|
||||
assert_eq!(
|
||||
serialized,
|
||||
"{\n $path: {\n optional: \"../src\",\n },\n}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -529,8 +532,11 @@ mod test {
|
|||
)
|
||||
.unwrap();
|
||||
|
||||
let serialized = serde_json::to_string(&project_node).unwrap();
|
||||
assert_eq!(serialized, r#"{"$path":{"optional":"../src"}}"#);
|
||||
let serialized = json5::to_string(&project_node).unwrap();
|
||||
assert_eq!(
|
||||
serialized,
|
||||
"{\n $path: {\n optional: \"../src\",\n },\n}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -542,8 +548,9 @@ mod test {
|
|||
)
|
||||
.unwrap();
|
||||
|
||||
let serialized = serde_json::to_string(&project_node).unwrap();
|
||||
assert_eq!(serialized, r#"{"$path":"../src"}"#);
|
||||
let serialized = json5::to_string(&project_node).unwrap();
|
||||
// Backslashes normalized to forward slashes
|
||||
assert_eq!(serialized, "{\n $path: \"../src\",\n}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -243,7 +243,7 @@ fn convert_localization_csv(contents: &[u8]) -> Result<String, csv::Error> {
|
|||
}
|
||||
|
||||
let encoded =
|
||||
serde_json::to_string(&entries).expect("Could not encode JSON for localization table");
|
||||
json5::to_string(&entries).expect("Could not encode JSON5 for localization table");
|
||||
|
||||
Ok(encoded)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,5 +22,5 @@ name: foo
|
|||
class_name: LocalizationTable
|
||||
properties:
|
||||
Contents:
|
||||
String: "[{\"key\":\"Ack\",\"example\":\"An exclamation of despair\",\"source\":\"Ack!\",\"values\":{\"es\":\"¡Ay!\"}}]"
|
||||
String: "[\n {\n key: \"Ack\",\n example: \"An exclamation of despair\",\n source: \"Ack!\",\n values: {\n es: \"¡Ay!\",\n },\n },\n]"
|
||||
children: []
|
||||
|
|
|
|||
|
|
@ -29,5 +29,5 @@ name: root
|
|||
class_name: LocalizationTable
|
||||
properties:
|
||||
Contents:
|
||||
String: "[{\"key\":\"Ack\",\"example\":\"An exclamation of despair\",\"source\":\"Ack!\",\"values\":{\"es\":\"¡Ay!\"}}]"
|
||||
String: "[\n {\n key: \"Ack\",\n example: \"An exclamation of despair\",\n source: \"Ack!\",\n values: {\n es: \"¡Ay!\",\n },\n },\n]"
|
||||
children: []
|
||||
|
|
|
|||
|
|
@ -29,5 +29,5 @@ name: root
|
|||
class_name: LocalizationTable
|
||||
properties:
|
||||
Contents:
|
||||
String: "[{\"key\":\"Ack\",\"example\":\"An exclamation of despair\",\"source\":\"Ack!\",\"values\":{\"es\":\"¡Ay!\"}}]"
|
||||
String: "[\n {\n key: \"Ack\",\n example: \"An exclamation of despair\",\n source: \"Ack!\",\n values: {\n es: \"¡Ay!\",\n },\n },\n]"
|
||||
children: []
|
||||
|
|
|
|||
|
|
@ -22,5 +22,5 @@ name: foo
|
|||
class_name: LocalizationTable
|
||||
properties:
|
||||
Contents:
|
||||
String: "[{\"key\":\"Ack\",\"example\":\"An exclamation of despair\",\"source\":\"Ack!\",\"values\":{\"es\":\"¡Ay!\"}}]"
|
||||
String: "[\n {\n key: \"Ack\",\n example: \"An exclamation of despair\",\n source: \"Ack!\",\n values: {\n es: \"¡Ay!\",\n },\n },\n]"
|
||||
children: []
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ use crate::{
|
|||
ServerInfoResponse, SocketPacket, SocketPacketBody, SocketPacketType, SubscribeMessage,
|
||||
WriteRequest, WriteResponse, PROTOCOL_VERSION, SERVER_VERSION,
|
||||
},
|
||||
util::{json, json_ok},
|
||||
util::{json_response, json_ok},
|
||||
},
|
||||
web_api::{
|
||||
BufferEncode, InstanceUpdate, RefPatchRequest, RefPatchResponse, SerializeRequest,
|
||||
|
|
@ -48,7 +48,7 @@ pub async fn call(serve_session: Arc<ServeSession>, mut request: Request<Body>)
|
|||
if is_upgrade_request(&request) {
|
||||
service.handle_api_socket(&mut request).await
|
||||
} else {
|
||||
json(
|
||||
json_response(
|
||||
ErrorResponse::bad_request(
|
||||
"/api/socket must be called as a websocket upgrade request",
|
||||
),
|
||||
|
|
@ -64,7 +64,7 @@ pub async fn call(serve_session: Arc<ServeSession>, mut request: Request<Body>)
|
|||
}
|
||||
(&Method::POST, "/api/write") => service.handle_api_write(request).await,
|
||||
|
||||
(_method, path) => json(
|
||||
(_method, path) => json_response(
|
||||
ErrorResponse::not_found(format!("Route not found: {}", path)),
|
||||
StatusCode::NOT_FOUND,
|
||||
),
|
||||
|
|
@ -104,7 +104,7 @@ impl ApiService {
|
|||
let input_cursor: u32 = match argument.parse() {
|
||||
Ok(v) => v,
|
||||
Err(err) => {
|
||||
return json(
|
||||
return json_response(
|
||||
ErrorResponse::bad_request(format!("Malformed message cursor: {}", err)),
|
||||
StatusCode::BAD_REQUEST,
|
||||
);
|
||||
|
|
@ -115,7 +115,7 @@ impl ApiService {
|
|||
let (response, websocket) = match upgrade(request, None) {
|
||||
Ok(result) => result,
|
||||
Err(err) => {
|
||||
return json(
|
||||
return json_response(
|
||||
ErrorResponse::internal_error(format!("WebSocket upgrade failed: {}", err)),
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
|
|
@ -145,7 +145,7 @@ impl ApiService {
|
|||
let request: WriteRequest = match json::from_slice(&body) {
|
||||
Ok(request) => request,
|
||||
Err(err) => {
|
||||
return json(
|
||||
return json_response(
|
||||
ErrorResponse::bad_request(format!("Invalid body: {}", err)),
|
||||
StatusCode::BAD_REQUEST,
|
||||
);
|
||||
|
|
@ -153,7 +153,7 @@ impl ApiService {
|
|||
};
|
||||
|
||||
if request.session_id != session_id {
|
||||
return json(
|
||||
return json_response(
|
||||
ErrorResponse::bad_request("Wrong session ID"),
|
||||
StatusCode::BAD_REQUEST,
|
||||
);
|
||||
|
|
@ -189,7 +189,7 @@ impl ApiService {
|
|||
let requested_ids = match requested_ids {
|
||||
Ok(ids) => ids,
|
||||
Err(_) => {
|
||||
return json(
|
||||
return json_response(
|
||||
ErrorResponse::bad_request("Malformed ID list"),
|
||||
StatusCode::BAD_REQUEST,
|
||||
);
|
||||
|
|
@ -274,7 +274,7 @@ impl ApiService {
|
|||
let request: SerializeRequest = match json::from_slice(&body) {
|
||||
Ok(request) => request,
|
||||
Err(err) => {
|
||||
return json(
|
||||
return json_response(
|
||||
ErrorResponse::bad_request(format!("Invalid body: {}", err)),
|
||||
StatusCode::BAD_REQUEST,
|
||||
);
|
||||
|
|
@ -282,7 +282,7 @@ impl ApiService {
|
|||
};
|
||||
|
||||
if request.session_id != session_id {
|
||||
return json(
|
||||
return json_response(
|
||||
ErrorResponse::bad_request("Wrong session ID"),
|
||||
StatusCode::BAD_REQUEST,
|
||||
);
|
||||
|
|
@ -315,7 +315,7 @@ impl ApiService {
|
|||
|
||||
response_dom.transfer_within(child_ref, object_value);
|
||||
} else {
|
||||
json(
|
||||
json_response(
|
||||
ErrorResponse::bad_request(format!("provided id {id} is not in the tree")),
|
||||
StatusCode::BAD_REQUEST,
|
||||
);
|
||||
|
|
@ -343,7 +343,7 @@ impl ApiService {
|
|||
let request: RefPatchRequest = match json::from_slice(&body) {
|
||||
Ok(request) => request,
|
||||
Err(err) => {
|
||||
return json(
|
||||
return json_response(
|
||||
ErrorResponse::bad_request(format!("Invalid body: {}", err)),
|
||||
StatusCode::BAD_REQUEST,
|
||||
);
|
||||
|
|
@ -351,7 +351,7 @@ impl ApiService {
|
|||
};
|
||||
|
||||
if request.session_id != session_id {
|
||||
return json(
|
||||
return json_response(
|
||||
ErrorResponse::bad_request("Wrong session ID"),
|
||||
StatusCode::BAD_REQUEST,
|
||||
);
|
||||
|
|
@ -400,7 +400,7 @@ impl ApiService {
|
|||
let requested_id = match Ref::from_str(argument) {
|
||||
Ok(id) => id,
|
||||
Err(_) => {
|
||||
return json(
|
||||
return json_response(
|
||||
ErrorResponse::bad_request("Invalid instance ID"),
|
||||
StatusCode::BAD_REQUEST,
|
||||
);
|
||||
|
|
@ -412,7 +412,7 @@ impl ApiService {
|
|||
let instance = match tree.get_instance(requested_id) {
|
||||
Some(instance) => instance,
|
||||
None => {
|
||||
return json(
|
||||
return json_response(
|
||||
ErrorResponse::bad_request("Instance not found"),
|
||||
StatusCode::NOT_FOUND,
|
||||
);
|
||||
|
|
@ -422,7 +422,7 @@ impl ApiService {
|
|||
let script_path = match pick_script_path(instance) {
|
||||
Some(path) => path,
|
||||
None => {
|
||||
return json(
|
||||
return json_response(
|
||||
ErrorResponse::bad_request(
|
||||
"No appropriate file could be found to open this script",
|
||||
),
|
||||
|
|
@ -435,7 +435,7 @@ impl ApiService {
|
|||
Ok(()) => {}
|
||||
Err(error) => match error {
|
||||
OpenError::Io(io_error) => {
|
||||
return json(
|
||||
return json_response(
|
||||
ErrorResponse::internal_error(format!(
|
||||
"Attempting to open {} failed because of the following io error: {}",
|
||||
script_path.display(),
|
||||
|
|
@ -449,7 +449,7 @@ impl ApiService {
|
|||
status,
|
||||
stderr,
|
||||
} => {
|
||||
return json(
|
||||
return json_response(
|
||||
ErrorResponse::internal_error(format!(
|
||||
r#"The command '{}' to open '{}' failed with the error code '{}'.
|
||||
Error logs:
|
||||
|
|
@ -554,7 +554,8 @@ async fn handle_websocket_subscription(
|
|||
}),
|
||||
};
|
||||
|
||||
serde_json::to_string(&response)?
|
||||
// Use json5 to preserve NaN/Infinity values
|
||||
json5::to_string(&response)?
|
||||
};
|
||||
|
||||
log::debug!("Sending batch of messages over WebSocket subscription");
|
||||
|
|
|
|||
|
|
@ -1,12 +1,18 @@
|
|||
use hyper::{header::CONTENT_TYPE, Body, Response, StatusCode};
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::json as json_module;
|
||||
|
||||
pub fn json_ok<T: Serialize>(value: T) -> Response<Body> {
|
||||
json(value, StatusCode::OK)
|
||||
json_response(value, StatusCode::OK)
|
||||
}
|
||||
|
||||
pub fn json<T: Serialize>(value: T, code: StatusCode) -> Response<Body> {
|
||||
let serialized = match serde_json::to_string(&value) {
|
||||
/// Alias for backwards compatibility
|
||||
pub use json_response as json;
|
||||
|
||||
pub fn json_response<T: Serialize>(value: T, code: StatusCode) -> Response<Body> {
|
||||
// Use compact JSON5 for transport efficiency, preserves NaN/Infinity
|
||||
let serialized = match json_module::to_string_compact(&value) {
|
||||
Ok(v) => v,
|
||||
Err(err) => {
|
||||
return Response::builder()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue