| 1 | use crate::error::{Error, ErrorCode, Result}; |
| 2 | use crate::map::Map; |
| 3 | use crate::value::{to_value, Value}; |
| 4 | use alloc::borrow::ToOwned; |
| 5 | use alloc::string::{String, ToString}; |
| 6 | use alloc::vec::Vec; |
| 7 | use core::fmt::Display; |
| 8 | use core::result; |
| 9 | use serde::ser::{Impossible, Serialize}; |
| 10 | |
| 11 | impl Serialize for Value { |
| 12 | #[inline ] |
| 13 | fn serialize<S>(&self, serializer: S) -> result::Result<S::Ok, S::Error> |
| 14 | where |
| 15 | S: ::serde::Serializer, |
| 16 | { |
| 17 | match self { |
| 18 | Value::Null => serializer.serialize_unit(), |
| 19 | Value::Bool(b) => serializer.serialize_bool(*b), |
| 20 | Value::Number(n) => n.serialize(serializer), |
| 21 | Value::String(s) => serializer.serialize_str(s), |
| 22 | Value::Array(v) => v.serialize(serializer), |
| 23 | #[cfg (any(feature = "std" , feature = "alloc" ))] |
| 24 | Value::Object(m) => { |
| 25 | use serde::ser::SerializeMap; |
| 26 | let mut map = tri!(serializer.serialize_map(Some(m.len()))); |
| 27 | for (k, v) in m { |
| 28 | tri!(map.serialize_entry(k, v)); |
| 29 | } |
| 30 | map.end() |
| 31 | } |
| 32 | #[cfg (not(any(feature = "std" , feature = "alloc" )))] |
| 33 | Value::Object(_) => unreachable!(), |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /// Serializer whose output is a `Value`. |
| 39 | /// |
| 40 | /// This is the serializer that backs [`serde_json::to_value`][crate::to_value]. |
| 41 | /// Unlike the main serde_json serializer which goes from some serializable |
| 42 | /// value of type `T` to JSON text, this one goes from `T` to |
| 43 | /// `serde_json::Value`. |
| 44 | /// |
| 45 | /// The `to_value` function is implementable as: |
| 46 | /// |
| 47 | /// ``` |
| 48 | /// use serde::Serialize; |
| 49 | /// use serde_json::{Error, Value}; |
| 50 | /// |
| 51 | /// pub fn to_value<T>(input: T) -> Result<Value, Error> |
| 52 | /// where |
| 53 | /// T: Serialize, |
| 54 | /// { |
| 55 | /// input.serialize(serde_json::value::Serializer) |
| 56 | /// } |
| 57 | /// ``` |
| 58 | pub struct Serializer; |
| 59 | |
| 60 | impl serde::Serializer for Serializer { |
| 61 | type Ok = Value; |
| 62 | type Error = Error; |
| 63 | |
| 64 | type SerializeSeq = SerializeVec; |
| 65 | type SerializeTuple = SerializeVec; |
| 66 | type SerializeTupleStruct = SerializeVec; |
| 67 | type SerializeTupleVariant = SerializeTupleVariant; |
| 68 | type SerializeMap = SerializeMap; |
| 69 | type SerializeStruct = SerializeMap; |
| 70 | type SerializeStructVariant = SerializeStructVariant; |
| 71 | |
| 72 | #[inline ] |
| 73 | fn serialize_bool(self, value: bool) -> Result<Value> { |
| 74 | Ok(Value::Bool(value)) |
| 75 | } |
| 76 | |
| 77 | #[inline ] |
| 78 | fn serialize_i8(self, value: i8) -> Result<Value> { |
| 79 | self.serialize_i64(value as i64) |
| 80 | } |
| 81 | |
| 82 | #[inline ] |
| 83 | fn serialize_i16(self, value: i16) -> Result<Value> { |
| 84 | self.serialize_i64(value as i64) |
| 85 | } |
| 86 | |
| 87 | #[inline ] |
| 88 | fn serialize_i32(self, value: i32) -> Result<Value> { |
| 89 | self.serialize_i64(value as i64) |
| 90 | } |
| 91 | |
| 92 | fn serialize_i64(self, value: i64) -> Result<Value> { |
| 93 | Ok(Value::Number(value.into())) |
| 94 | } |
| 95 | |
| 96 | fn serialize_i128(self, value: i128) -> Result<Value> { |
| 97 | #[cfg (feature = "arbitrary_precision" )] |
| 98 | { |
| 99 | Ok(Value::Number(value.into())) |
| 100 | } |
| 101 | |
| 102 | #[cfg (not(feature = "arbitrary_precision" ))] |
| 103 | { |
| 104 | if let Ok(value) = u64::try_from(value) { |
| 105 | Ok(Value::Number(value.into())) |
| 106 | } else if let Ok(value) = i64::try_from(value) { |
| 107 | Ok(Value::Number(value.into())) |
| 108 | } else { |
| 109 | Err(Error::syntax(ErrorCode::NumberOutOfRange, 0, 0)) |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | #[inline ] |
| 115 | fn serialize_u8(self, value: u8) -> Result<Value> { |
| 116 | self.serialize_u64(value as u64) |
| 117 | } |
| 118 | |
| 119 | #[inline ] |
| 120 | fn serialize_u16(self, value: u16) -> Result<Value> { |
| 121 | self.serialize_u64(value as u64) |
| 122 | } |
| 123 | |
| 124 | #[inline ] |
| 125 | fn serialize_u32(self, value: u32) -> Result<Value> { |
| 126 | self.serialize_u64(value as u64) |
| 127 | } |
| 128 | |
| 129 | #[inline ] |
| 130 | fn serialize_u64(self, value: u64) -> Result<Value> { |
| 131 | Ok(Value::Number(value.into())) |
| 132 | } |
| 133 | |
| 134 | fn serialize_u128(self, value: u128) -> Result<Value> { |
| 135 | #[cfg (feature = "arbitrary_precision" )] |
| 136 | { |
| 137 | Ok(Value::Number(value.into())) |
| 138 | } |
| 139 | |
| 140 | #[cfg (not(feature = "arbitrary_precision" ))] |
| 141 | { |
| 142 | if let Ok(value) = u64::try_from(value) { |
| 143 | Ok(Value::Number(value.into())) |
| 144 | } else { |
| 145 | Err(Error::syntax(ErrorCode::NumberOutOfRange, 0, 0)) |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | #[inline ] |
| 151 | fn serialize_f32(self, float: f32) -> Result<Value> { |
| 152 | Ok(Value::from(float)) |
| 153 | } |
| 154 | |
| 155 | #[inline ] |
| 156 | fn serialize_f64(self, float: f64) -> Result<Value> { |
| 157 | Ok(Value::from(float)) |
| 158 | } |
| 159 | |
| 160 | #[inline ] |
| 161 | fn serialize_char(self, value: char) -> Result<Value> { |
| 162 | let mut s = String::new(); |
| 163 | s.push(value); |
| 164 | Ok(Value::String(s)) |
| 165 | } |
| 166 | |
| 167 | #[inline ] |
| 168 | fn serialize_str(self, value: &str) -> Result<Value> { |
| 169 | Ok(Value::String(value.to_owned())) |
| 170 | } |
| 171 | |
| 172 | fn serialize_bytes(self, value: &[u8]) -> Result<Value> { |
| 173 | let vec = value.iter().map(|&b| Value::Number(b.into())).collect(); |
| 174 | Ok(Value::Array(vec)) |
| 175 | } |
| 176 | |
| 177 | #[inline ] |
| 178 | fn serialize_unit(self) -> Result<Value> { |
| 179 | Ok(Value::Null) |
| 180 | } |
| 181 | |
| 182 | #[inline ] |
| 183 | fn serialize_unit_struct(self, _name: &'static str) -> Result<Value> { |
| 184 | self.serialize_unit() |
| 185 | } |
| 186 | |
| 187 | #[inline ] |
| 188 | fn serialize_unit_variant( |
| 189 | self, |
| 190 | _name: &'static str, |
| 191 | _variant_index: u32, |
| 192 | variant: &'static str, |
| 193 | ) -> Result<Value> { |
| 194 | self.serialize_str(variant) |
| 195 | } |
| 196 | |
| 197 | #[inline ] |
| 198 | fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<Value> |
| 199 | where |
| 200 | T: ?Sized + Serialize, |
| 201 | { |
| 202 | value.serialize(self) |
| 203 | } |
| 204 | |
| 205 | fn serialize_newtype_variant<T>( |
| 206 | self, |
| 207 | _name: &'static str, |
| 208 | _variant_index: u32, |
| 209 | variant: &'static str, |
| 210 | value: &T, |
| 211 | ) -> Result<Value> |
| 212 | where |
| 213 | T: ?Sized + Serialize, |
| 214 | { |
| 215 | let mut values = Map::new(); |
| 216 | values.insert(String::from(variant), tri!(to_value(value))); |
| 217 | Ok(Value::Object(values)) |
| 218 | } |
| 219 | |
| 220 | #[inline ] |
| 221 | fn serialize_none(self) -> Result<Value> { |
| 222 | self.serialize_unit() |
| 223 | } |
| 224 | |
| 225 | #[inline ] |
| 226 | fn serialize_some<T>(self, value: &T) -> Result<Value> |
| 227 | where |
| 228 | T: ?Sized + Serialize, |
| 229 | { |
| 230 | value.serialize(self) |
| 231 | } |
| 232 | |
| 233 | fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> { |
| 234 | Ok(SerializeVec { |
| 235 | vec: Vec::with_capacity(len.unwrap_or(0)), |
| 236 | }) |
| 237 | } |
| 238 | |
| 239 | fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> { |
| 240 | self.serialize_seq(Some(len)) |
| 241 | } |
| 242 | |
| 243 | fn serialize_tuple_struct( |
| 244 | self, |
| 245 | _name: &'static str, |
| 246 | len: usize, |
| 247 | ) -> Result<Self::SerializeTupleStruct> { |
| 248 | self.serialize_seq(Some(len)) |
| 249 | } |
| 250 | |
| 251 | fn serialize_tuple_variant( |
| 252 | self, |
| 253 | _name: &'static str, |
| 254 | _variant_index: u32, |
| 255 | variant: &'static str, |
| 256 | len: usize, |
| 257 | ) -> Result<Self::SerializeTupleVariant> { |
| 258 | Ok(SerializeTupleVariant { |
| 259 | name: String::from(variant), |
| 260 | vec: Vec::with_capacity(len), |
| 261 | }) |
| 262 | } |
| 263 | |
| 264 | fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> { |
| 265 | Ok(SerializeMap::Map { |
| 266 | map: Map::new(), |
| 267 | next_key: None, |
| 268 | }) |
| 269 | } |
| 270 | |
| 271 | fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct> { |
| 272 | match name { |
| 273 | #[cfg (feature = "arbitrary_precision" )] |
| 274 | crate::number::TOKEN => Ok(SerializeMap::Number { out_value: None }), |
| 275 | #[cfg (feature = "raw_value" )] |
| 276 | crate::raw::TOKEN => Ok(SerializeMap::RawValue { out_value: None }), |
| 277 | _ => self.serialize_map(Some(len)), |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | fn serialize_struct_variant( |
| 282 | self, |
| 283 | _name: &'static str, |
| 284 | _variant_index: u32, |
| 285 | variant: &'static str, |
| 286 | _len: usize, |
| 287 | ) -> Result<Self::SerializeStructVariant> { |
| 288 | Ok(SerializeStructVariant { |
| 289 | name: String::from(variant), |
| 290 | map: Map::new(), |
| 291 | }) |
| 292 | } |
| 293 | |
| 294 | fn collect_str<T>(self, value: &T) -> Result<Value> |
| 295 | where |
| 296 | T: ?Sized + Display, |
| 297 | { |
| 298 | Ok(Value::String(value.to_string())) |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | pub struct SerializeVec { |
| 303 | vec: Vec<Value>, |
| 304 | } |
| 305 | |
| 306 | pub struct SerializeTupleVariant { |
| 307 | name: String, |
| 308 | vec: Vec<Value>, |
| 309 | } |
| 310 | |
| 311 | pub enum SerializeMap { |
| 312 | Map { |
| 313 | map: Map<String, Value>, |
| 314 | next_key: Option<String>, |
| 315 | }, |
| 316 | #[cfg (feature = "arbitrary_precision" )] |
| 317 | Number { out_value: Option<Value> }, |
| 318 | #[cfg (feature = "raw_value" )] |
| 319 | RawValue { out_value: Option<Value> }, |
| 320 | } |
| 321 | |
| 322 | pub struct SerializeStructVariant { |
| 323 | name: String, |
| 324 | map: Map<String, Value>, |
| 325 | } |
| 326 | |
| 327 | impl serde::ser::SerializeSeq for SerializeVec { |
| 328 | type Ok = Value; |
| 329 | type Error = Error; |
| 330 | |
| 331 | fn serialize_element<T>(&mut self, value: &T) -> Result<()> |
| 332 | where |
| 333 | T: ?Sized + Serialize, |
| 334 | { |
| 335 | self.vec.push(tri!(to_value(value))); |
| 336 | Ok(()) |
| 337 | } |
| 338 | |
| 339 | fn end(self) -> Result<Value> { |
| 340 | Ok(Value::Array(self.vec)) |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | impl serde::ser::SerializeTuple for SerializeVec { |
| 345 | type Ok = Value; |
| 346 | type Error = Error; |
| 347 | |
| 348 | fn serialize_element<T>(&mut self, value: &T) -> Result<()> |
| 349 | where |
| 350 | T: ?Sized + Serialize, |
| 351 | { |
| 352 | serde::ser::SerializeSeq::serialize_element(self, value) |
| 353 | } |
| 354 | |
| 355 | fn end(self) -> Result<Value> { |
| 356 | serde::ser::SerializeSeq::end(self) |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | impl serde::ser::SerializeTupleStruct for SerializeVec { |
| 361 | type Ok = Value; |
| 362 | type Error = Error; |
| 363 | |
| 364 | fn serialize_field<T>(&mut self, value: &T) -> Result<()> |
| 365 | where |
| 366 | T: ?Sized + Serialize, |
| 367 | { |
| 368 | serde::ser::SerializeSeq::serialize_element(self, value) |
| 369 | } |
| 370 | |
| 371 | fn end(self) -> Result<Value> { |
| 372 | serde::ser::SerializeSeq::end(self) |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | impl serde::ser::SerializeTupleVariant for SerializeTupleVariant { |
| 377 | type Ok = Value; |
| 378 | type Error = Error; |
| 379 | |
| 380 | fn serialize_field<T>(&mut self, value: &T) -> Result<()> |
| 381 | where |
| 382 | T: ?Sized + Serialize, |
| 383 | { |
| 384 | self.vec.push(tri!(to_value(value))); |
| 385 | Ok(()) |
| 386 | } |
| 387 | |
| 388 | fn end(self) -> Result<Value> { |
| 389 | let mut object: Map = Map::new(); |
| 390 | |
| 391 | object.insert(self.name, v:Value::Array(self.vec)); |
| 392 | |
| 393 | Ok(Value::Object(object)) |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | impl serde::ser::SerializeMap for SerializeMap { |
| 398 | type Ok = Value; |
| 399 | type Error = Error; |
| 400 | |
| 401 | fn serialize_key<T>(&mut self, key: &T) -> Result<()> |
| 402 | where |
| 403 | T: ?Sized + Serialize, |
| 404 | { |
| 405 | match self { |
| 406 | SerializeMap::Map { next_key, .. } => { |
| 407 | *next_key = Some(tri!(key.serialize(MapKeySerializer))); |
| 408 | Ok(()) |
| 409 | } |
| 410 | #[cfg (feature = "arbitrary_precision" )] |
| 411 | SerializeMap::Number { .. } => unreachable!(), |
| 412 | #[cfg (feature = "raw_value" )] |
| 413 | SerializeMap::RawValue { .. } => unreachable!(), |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | fn serialize_value<T>(&mut self, value: &T) -> Result<()> |
| 418 | where |
| 419 | T: ?Sized + Serialize, |
| 420 | { |
| 421 | match self { |
| 422 | SerializeMap::Map { map, next_key } => { |
| 423 | let key = next_key.take(); |
| 424 | // Panic because this indicates a bug in the program rather than an |
| 425 | // expected failure. |
| 426 | let key = key.expect("serialize_value called before serialize_key" ); |
| 427 | map.insert(key, tri!(to_value(value))); |
| 428 | Ok(()) |
| 429 | } |
| 430 | #[cfg (feature = "arbitrary_precision" )] |
| 431 | SerializeMap::Number { .. } => unreachable!(), |
| 432 | #[cfg (feature = "raw_value" )] |
| 433 | SerializeMap::RawValue { .. } => unreachable!(), |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | fn end(self) -> Result<Value> { |
| 438 | match self { |
| 439 | SerializeMap::Map { map, .. } => Ok(Value::Object(map)), |
| 440 | #[cfg (feature = "arbitrary_precision" )] |
| 441 | SerializeMap::Number { .. } => unreachable!(), |
| 442 | #[cfg (feature = "raw_value" )] |
| 443 | SerializeMap::RawValue { .. } => unreachable!(), |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | struct MapKeySerializer; |
| 449 | |
| 450 | fn key_must_be_a_string() -> Error { |
| 451 | Error::syntax(code:ErrorCode::KeyMustBeAString, line:0, column:0) |
| 452 | } |
| 453 | |
| 454 | fn float_key_must_be_finite() -> Error { |
| 455 | Error::syntax(code:ErrorCode::FloatKeyMustBeFinite, line:0, column:0) |
| 456 | } |
| 457 | |
| 458 | impl serde::Serializer for MapKeySerializer { |
| 459 | type Ok = String; |
| 460 | type Error = Error; |
| 461 | |
| 462 | type SerializeSeq = Impossible<String, Error>; |
| 463 | type SerializeTuple = Impossible<String, Error>; |
| 464 | type SerializeTupleStruct = Impossible<String, Error>; |
| 465 | type SerializeTupleVariant = Impossible<String, Error>; |
| 466 | type SerializeMap = Impossible<String, Error>; |
| 467 | type SerializeStruct = Impossible<String, Error>; |
| 468 | type SerializeStructVariant = Impossible<String, Error>; |
| 469 | |
| 470 | #[inline ] |
| 471 | fn serialize_unit_variant( |
| 472 | self, |
| 473 | _name: &'static str, |
| 474 | _variant_index: u32, |
| 475 | variant: &'static str, |
| 476 | ) -> Result<String> { |
| 477 | Ok(variant.to_owned()) |
| 478 | } |
| 479 | |
| 480 | #[inline ] |
| 481 | fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<String> |
| 482 | where |
| 483 | T: ?Sized + Serialize, |
| 484 | { |
| 485 | value.serialize(self) |
| 486 | } |
| 487 | |
| 488 | fn serialize_bool(self, value: bool) -> Result<String> { |
| 489 | Ok(if value { "true" } else { "false" }.to_owned()) |
| 490 | } |
| 491 | |
| 492 | fn serialize_i8(self, value: i8) -> Result<String> { |
| 493 | Ok(itoa::Buffer::new().format(value).to_owned()) |
| 494 | } |
| 495 | |
| 496 | fn serialize_i16(self, value: i16) -> Result<String> { |
| 497 | Ok(itoa::Buffer::new().format(value).to_owned()) |
| 498 | } |
| 499 | |
| 500 | fn serialize_i32(self, value: i32) -> Result<String> { |
| 501 | Ok(itoa::Buffer::new().format(value).to_owned()) |
| 502 | } |
| 503 | |
| 504 | fn serialize_i64(self, value: i64) -> Result<String> { |
| 505 | Ok(itoa::Buffer::new().format(value).to_owned()) |
| 506 | } |
| 507 | |
| 508 | fn serialize_i128(self, value: i128) -> Result<String> { |
| 509 | Ok(itoa::Buffer::new().format(value).to_owned()) |
| 510 | } |
| 511 | |
| 512 | fn serialize_u8(self, value: u8) -> Result<String> { |
| 513 | Ok(itoa::Buffer::new().format(value).to_owned()) |
| 514 | } |
| 515 | |
| 516 | fn serialize_u16(self, value: u16) -> Result<String> { |
| 517 | Ok(itoa::Buffer::new().format(value).to_owned()) |
| 518 | } |
| 519 | |
| 520 | fn serialize_u32(self, value: u32) -> Result<String> { |
| 521 | Ok(itoa::Buffer::new().format(value).to_owned()) |
| 522 | } |
| 523 | |
| 524 | fn serialize_u64(self, value: u64) -> Result<String> { |
| 525 | Ok(itoa::Buffer::new().format(value).to_owned()) |
| 526 | } |
| 527 | |
| 528 | fn serialize_u128(self, value: u128) -> Result<String> { |
| 529 | Ok(itoa::Buffer::new().format(value).to_owned()) |
| 530 | } |
| 531 | |
| 532 | fn serialize_f32(self, value: f32) -> Result<String> { |
| 533 | if value.is_finite() { |
| 534 | Ok(ryu::Buffer::new().format_finite(value).to_owned()) |
| 535 | } else { |
| 536 | Err(float_key_must_be_finite()) |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | fn serialize_f64(self, value: f64) -> Result<String> { |
| 541 | if value.is_finite() { |
| 542 | Ok(ryu::Buffer::new().format_finite(value).to_owned()) |
| 543 | } else { |
| 544 | Err(float_key_must_be_finite()) |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | #[inline ] |
| 549 | fn serialize_char(self, value: char) -> Result<String> { |
| 550 | Ok({ |
| 551 | let mut s = String::new(); |
| 552 | s.push(value); |
| 553 | s |
| 554 | }) |
| 555 | } |
| 556 | |
| 557 | #[inline ] |
| 558 | fn serialize_str(self, value: &str) -> Result<String> { |
| 559 | Ok(value.to_owned()) |
| 560 | } |
| 561 | |
| 562 | fn serialize_bytes(self, _value: &[u8]) -> Result<String> { |
| 563 | Err(key_must_be_a_string()) |
| 564 | } |
| 565 | |
| 566 | fn serialize_unit(self) -> Result<String> { |
| 567 | Err(key_must_be_a_string()) |
| 568 | } |
| 569 | |
| 570 | fn serialize_unit_struct(self, _name: &'static str) -> Result<String> { |
| 571 | Err(key_must_be_a_string()) |
| 572 | } |
| 573 | |
| 574 | fn serialize_newtype_variant<T>( |
| 575 | self, |
| 576 | _name: &'static str, |
| 577 | _variant_index: u32, |
| 578 | _variant: &'static str, |
| 579 | _value: &T, |
| 580 | ) -> Result<String> |
| 581 | where |
| 582 | T: ?Sized + Serialize, |
| 583 | { |
| 584 | Err(key_must_be_a_string()) |
| 585 | } |
| 586 | |
| 587 | fn serialize_none(self) -> Result<String> { |
| 588 | Err(key_must_be_a_string()) |
| 589 | } |
| 590 | |
| 591 | fn serialize_some<T>(self, _value: &T) -> Result<String> |
| 592 | where |
| 593 | T: ?Sized + Serialize, |
| 594 | { |
| 595 | Err(key_must_be_a_string()) |
| 596 | } |
| 597 | |
| 598 | fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> { |
| 599 | Err(key_must_be_a_string()) |
| 600 | } |
| 601 | |
| 602 | fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> { |
| 603 | Err(key_must_be_a_string()) |
| 604 | } |
| 605 | |
| 606 | fn serialize_tuple_struct( |
| 607 | self, |
| 608 | _name: &'static str, |
| 609 | _len: usize, |
| 610 | ) -> Result<Self::SerializeTupleStruct> { |
| 611 | Err(key_must_be_a_string()) |
| 612 | } |
| 613 | |
| 614 | fn serialize_tuple_variant( |
| 615 | self, |
| 616 | _name: &'static str, |
| 617 | _variant_index: u32, |
| 618 | _variant: &'static str, |
| 619 | _len: usize, |
| 620 | ) -> Result<Self::SerializeTupleVariant> { |
| 621 | Err(key_must_be_a_string()) |
| 622 | } |
| 623 | |
| 624 | fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> { |
| 625 | Err(key_must_be_a_string()) |
| 626 | } |
| 627 | |
| 628 | fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> { |
| 629 | Err(key_must_be_a_string()) |
| 630 | } |
| 631 | |
| 632 | fn serialize_struct_variant( |
| 633 | self, |
| 634 | _name: &'static str, |
| 635 | _variant_index: u32, |
| 636 | _variant: &'static str, |
| 637 | _len: usize, |
| 638 | ) -> Result<Self::SerializeStructVariant> { |
| 639 | Err(key_must_be_a_string()) |
| 640 | } |
| 641 | |
| 642 | fn collect_str<T>(self, value: &T) -> Result<String> |
| 643 | where |
| 644 | T: ?Sized + Display, |
| 645 | { |
| 646 | Ok(value.to_string()) |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | impl serde::ser::SerializeStruct for SerializeMap { |
| 651 | type Ok = Value; |
| 652 | type Error = Error; |
| 653 | |
| 654 | fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()> |
| 655 | where |
| 656 | T: ?Sized + Serialize, |
| 657 | { |
| 658 | match self { |
| 659 | SerializeMap::Map { .. } => serde::ser::SerializeMap::serialize_entry(self, key, value), |
| 660 | #[cfg (feature = "arbitrary_precision" )] |
| 661 | SerializeMap::Number { out_value } => { |
| 662 | if key == crate::number::TOKEN { |
| 663 | *out_value = Some(tri!(value.serialize(NumberValueEmitter))); |
| 664 | Ok(()) |
| 665 | } else { |
| 666 | Err(invalid_number()) |
| 667 | } |
| 668 | } |
| 669 | #[cfg (feature = "raw_value" )] |
| 670 | SerializeMap::RawValue { out_value } => { |
| 671 | if key == crate::raw::TOKEN { |
| 672 | *out_value = Some(tri!(value.serialize(RawValueEmitter))); |
| 673 | Ok(()) |
| 674 | } else { |
| 675 | Err(invalid_raw_value()) |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | fn end(self) -> Result<Value> { |
| 682 | match self { |
| 683 | SerializeMap::Map { .. } => serde::ser::SerializeMap::end(self), |
| 684 | #[cfg (feature = "arbitrary_precision" )] |
| 685 | SerializeMap::Number { out_value, .. } => { |
| 686 | Ok(out_value.expect("number value was not emitted" )) |
| 687 | } |
| 688 | #[cfg (feature = "raw_value" )] |
| 689 | SerializeMap::RawValue { out_value, .. } => { |
| 690 | Ok(out_value.expect("raw value was not emitted" )) |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | impl serde::ser::SerializeStructVariant for SerializeStructVariant { |
| 697 | type Ok = Value; |
| 698 | type Error = Error; |
| 699 | |
| 700 | fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()> |
| 701 | where |
| 702 | T: ?Sized + Serialize, |
| 703 | { |
| 704 | self.map.insert(k:String::from(key), v:tri!(to_value(value))); |
| 705 | Ok(()) |
| 706 | } |
| 707 | |
| 708 | fn end(self) -> Result<Value> { |
| 709 | let mut object: Map = Map::new(); |
| 710 | |
| 711 | object.insert(self.name, v:Value::Object(self.map)); |
| 712 | |
| 713 | Ok(Value::Object(object)) |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | #[cfg (feature = "arbitrary_precision" )] |
| 718 | struct NumberValueEmitter; |
| 719 | |
| 720 | #[cfg (feature = "arbitrary_precision" )] |
| 721 | fn invalid_number() -> Error { |
| 722 | Error::syntax(ErrorCode::InvalidNumber, 0, 0) |
| 723 | } |
| 724 | |
| 725 | #[cfg (feature = "arbitrary_precision" )] |
| 726 | impl serde::ser::Serializer for NumberValueEmitter { |
| 727 | type Ok = Value; |
| 728 | type Error = Error; |
| 729 | |
| 730 | type SerializeSeq = Impossible<Value, Error>; |
| 731 | type SerializeTuple = Impossible<Value, Error>; |
| 732 | type SerializeTupleStruct = Impossible<Value, Error>; |
| 733 | type SerializeTupleVariant = Impossible<Value, Error>; |
| 734 | type SerializeMap = Impossible<Value, Error>; |
| 735 | type SerializeStruct = Impossible<Value, Error>; |
| 736 | type SerializeStructVariant = Impossible<Value, Error>; |
| 737 | |
| 738 | fn serialize_bool(self, _v: bool) -> Result<Value> { |
| 739 | Err(invalid_number()) |
| 740 | } |
| 741 | |
| 742 | fn serialize_i8(self, _v: i8) -> Result<Value> { |
| 743 | Err(invalid_number()) |
| 744 | } |
| 745 | |
| 746 | fn serialize_i16(self, _v: i16) -> Result<Value> { |
| 747 | Err(invalid_number()) |
| 748 | } |
| 749 | |
| 750 | fn serialize_i32(self, _v: i32) -> Result<Value> { |
| 751 | Err(invalid_number()) |
| 752 | } |
| 753 | |
| 754 | fn serialize_i64(self, _v: i64) -> Result<Value> { |
| 755 | Err(invalid_number()) |
| 756 | } |
| 757 | |
| 758 | fn serialize_u8(self, _v: u8) -> Result<Value> { |
| 759 | Err(invalid_number()) |
| 760 | } |
| 761 | |
| 762 | fn serialize_u16(self, _v: u16) -> Result<Value> { |
| 763 | Err(invalid_number()) |
| 764 | } |
| 765 | |
| 766 | fn serialize_u32(self, _v: u32) -> Result<Value> { |
| 767 | Err(invalid_number()) |
| 768 | } |
| 769 | |
| 770 | fn serialize_u64(self, _v: u64) -> Result<Value> { |
| 771 | Err(invalid_number()) |
| 772 | } |
| 773 | |
| 774 | fn serialize_f32(self, _v: f32) -> Result<Value> { |
| 775 | Err(invalid_number()) |
| 776 | } |
| 777 | |
| 778 | fn serialize_f64(self, _v: f64) -> Result<Value> { |
| 779 | Err(invalid_number()) |
| 780 | } |
| 781 | |
| 782 | fn serialize_char(self, _v: char) -> Result<Value> { |
| 783 | Err(invalid_number()) |
| 784 | } |
| 785 | |
| 786 | fn serialize_str(self, value: &str) -> Result<Value> { |
| 787 | let n = tri!(value.to_owned().parse()); |
| 788 | Ok(Value::Number(n)) |
| 789 | } |
| 790 | |
| 791 | fn serialize_bytes(self, _value: &[u8]) -> Result<Value> { |
| 792 | Err(invalid_number()) |
| 793 | } |
| 794 | |
| 795 | fn serialize_none(self) -> Result<Value> { |
| 796 | Err(invalid_number()) |
| 797 | } |
| 798 | |
| 799 | fn serialize_some<T>(self, _value: &T) -> Result<Value> |
| 800 | where |
| 801 | T: ?Sized + Serialize, |
| 802 | { |
| 803 | Err(invalid_number()) |
| 804 | } |
| 805 | |
| 806 | fn serialize_unit(self) -> Result<Value> { |
| 807 | Err(invalid_number()) |
| 808 | } |
| 809 | |
| 810 | fn serialize_unit_struct(self, _name: &'static str) -> Result<Value> { |
| 811 | Err(invalid_number()) |
| 812 | } |
| 813 | |
| 814 | fn serialize_unit_variant( |
| 815 | self, |
| 816 | _name: &'static str, |
| 817 | _variant_index: u32, |
| 818 | _variant: &'static str, |
| 819 | ) -> Result<Value> { |
| 820 | Err(invalid_number()) |
| 821 | } |
| 822 | |
| 823 | fn serialize_newtype_struct<T>(self, _name: &'static str, _value: &T) -> Result<Value> |
| 824 | where |
| 825 | T: ?Sized + Serialize, |
| 826 | { |
| 827 | Err(invalid_number()) |
| 828 | } |
| 829 | |
| 830 | fn serialize_newtype_variant<T>( |
| 831 | self, |
| 832 | _name: &'static str, |
| 833 | _variant_index: u32, |
| 834 | _variant: &'static str, |
| 835 | _value: &T, |
| 836 | ) -> Result<Value> |
| 837 | where |
| 838 | T: ?Sized + Serialize, |
| 839 | { |
| 840 | Err(invalid_number()) |
| 841 | } |
| 842 | |
| 843 | fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> { |
| 844 | Err(invalid_number()) |
| 845 | } |
| 846 | |
| 847 | fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> { |
| 848 | Err(invalid_number()) |
| 849 | } |
| 850 | |
| 851 | fn serialize_tuple_struct( |
| 852 | self, |
| 853 | _name: &'static str, |
| 854 | _len: usize, |
| 855 | ) -> Result<Self::SerializeTupleStruct> { |
| 856 | Err(invalid_number()) |
| 857 | } |
| 858 | |
| 859 | fn serialize_tuple_variant( |
| 860 | self, |
| 861 | _name: &'static str, |
| 862 | _variant_index: u32, |
| 863 | _variant: &'static str, |
| 864 | _len: usize, |
| 865 | ) -> Result<Self::SerializeTupleVariant> { |
| 866 | Err(invalid_number()) |
| 867 | } |
| 868 | |
| 869 | fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> { |
| 870 | Err(invalid_number()) |
| 871 | } |
| 872 | |
| 873 | fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> { |
| 874 | Err(invalid_number()) |
| 875 | } |
| 876 | |
| 877 | fn serialize_struct_variant( |
| 878 | self, |
| 879 | _name: &'static str, |
| 880 | _variant_index: u32, |
| 881 | _variant: &'static str, |
| 882 | _len: usize, |
| 883 | ) -> Result<Self::SerializeStructVariant> { |
| 884 | Err(invalid_number()) |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | #[cfg (feature = "raw_value" )] |
| 889 | struct RawValueEmitter; |
| 890 | |
| 891 | #[cfg (feature = "raw_value" )] |
| 892 | fn invalid_raw_value() -> Error { |
| 893 | Error::syntax(ErrorCode::ExpectedSomeValue, 0, 0) |
| 894 | } |
| 895 | |
| 896 | #[cfg (feature = "raw_value" )] |
| 897 | impl serde::ser::Serializer for RawValueEmitter { |
| 898 | type Ok = Value; |
| 899 | type Error = Error; |
| 900 | |
| 901 | type SerializeSeq = Impossible<Value, Error>; |
| 902 | type SerializeTuple = Impossible<Value, Error>; |
| 903 | type SerializeTupleStruct = Impossible<Value, Error>; |
| 904 | type SerializeTupleVariant = Impossible<Value, Error>; |
| 905 | type SerializeMap = Impossible<Value, Error>; |
| 906 | type SerializeStruct = Impossible<Value, Error>; |
| 907 | type SerializeStructVariant = Impossible<Value, Error>; |
| 908 | |
| 909 | fn serialize_bool(self, _v: bool) -> Result<Value> { |
| 910 | Err(invalid_raw_value()) |
| 911 | } |
| 912 | |
| 913 | fn serialize_i8(self, _v: i8) -> Result<Value> { |
| 914 | Err(invalid_raw_value()) |
| 915 | } |
| 916 | |
| 917 | fn serialize_i16(self, _v: i16) -> Result<Value> { |
| 918 | Err(invalid_raw_value()) |
| 919 | } |
| 920 | |
| 921 | fn serialize_i32(self, _v: i32) -> Result<Value> { |
| 922 | Err(invalid_raw_value()) |
| 923 | } |
| 924 | |
| 925 | fn serialize_i64(self, _v: i64) -> Result<Value> { |
| 926 | Err(invalid_raw_value()) |
| 927 | } |
| 928 | |
| 929 | fn serialize_u8(self, _v: u8) -> Result<Value> { |
| 930 | Err(invalid_raw_value()) |
| 931 | } |
| 932 | |
| 933 | fn serialize_u16(self, _v: u16) -> Result<Value> { |
| 934 | Err(invalid_raw_value()) |
| 935 | } |
| 936 | |
| 937 | fn serialize_u32(self, _v: u32) -> Result<Value> { |
| 938 | Err(invalid_raw_value()) |
| 939 | } |
| 940 | |
| 941 | fn serialize_u64(self, _v: u64) -> Result<Value> { |
| 942 | Err(invalid_raw_value()) |
| 943 | } |
| 944 | |
| 945 | fn serialize_f32(self, _v: f32) -> Result<Value> { |
| 946 | Err(invalid_raw_value()) |
| 947 | } |
| 948 | |
| 949 | fn serialize_f64(self, _v: f64) -> Result<Value> { |
| 950 | Err(invalid_raw_value()) |
| 951 | } |
| 952 | |
| 953 | fn serialize_char(self, _v: char) -> Result<Value> { |
| 954 | Err(invalid_raw_value()) |
| 955 | } |
| 956 | |
| 957 | fn serialize_str(self, value: &str) -> Result<Value> { |
| 958 | crate::from_str(value) |
| 959 | } |
| 960 | |
| 961 | fn serialize_bytes(self, _value: &[u8]) -> Result<Value> { |
| 962 | Err(invalid_raw_value()) |
| 963 | } |
| 964 | |
| 965 | fn serialize_none(self) -> Result<Value> { |
| 966 | Err(invalid_raw_value()) |
| 967 | } |
| 968 | |
| 969 | fn serialize_some<T>(self, _value: &T) -> Result<Value> |
| 970 | where |
| 971 | T: ?Sized + Serialize, |
| 972 | { |
| 973 | Err(invalid_raw_value()) |
| 974 | } |
| 975 | |
| 976 | fn serialize_unit(self) -> Result<Value> { |
| 977 | Err(invalid_raw_value()) |
| 978 | } |
| 979 | |
| 980 | fn serialize_unit_struct(self, _name: &'static str) -> Result<Value> { |
| 981 | Err(invalid_raw_value()) |
| 982 | } |
| 983 | |
| 984 | fn serialize_unit_variant( |
| 985 | self, |
| 986 | _name: &'static str, |
| 987 | _variant_index: u32, |
| 988 | _variant: &'static str, |
| 989 | ) -> Result<Value> { |
| 990 | Err(invalid_raw_value()) |
| 991 | } |
| 992 | |
| 993 | fn serialize_newtype_struct<T>(self, _name: &'static str, _value: &T) -> Result<Value> |
| 994 | where |
| 995 | T: ?Sized + Serialize, |
| 996 | { |
| 997 | Err(invalid_raw_value()) |
| 998 | } |
| 999 | |
| 1000 | fn serialize_newtype_variant<T>( |
| 1001 | self, |
| 1002 | _name: &'static str, |
| 1003 | _variant_index: u32, |
| 1004 | _variant: &'static str, |
| 1005 | _value: &T, |
| 1006 | ) -> Result<Value> |
| 1007 | where |
| 1008 | T: ?Sized + Serialize, |
| 1009 | { |
| 1010 | Err(invalid_raw_value()) |
| 1011 | } |
| 1012 | |
| 1013 | fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> { |
| 1014 | Err(invalid_raw_value()) |
| 1015 | } |
| 1016 | |
| 1017 | fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> { |
| 1018 | Err(invalid_raw_value()) |
| 1019 | } |
| 1020 | |
| 1021 | fn serialize_tuple_struct( |
| 1022 | self, |
| 1023 | _name: &'static str, |
| 1024 | _len: usize, |
| 1025 | ) -> Result<Self::SerializeTupleStruct> { |
| 1026 | Err(invalid_raw_value()) |
| 1027 | } |
| 1028 | |
| 1029 | fn serialize_tuple_variant( |
| 1030 | self, |
| 1031 | _name: &'static str, |
| 1032 | _variant_index: u32, |
| 1033 | _variant: &'static str, |
| 1034 | _len: usize, |
| 1035 | ) -> Result<Self::SerializeTupleVariant> { |
| 1036 | Err(invalid_raw_value()) |
| 1037 | } |
| 1038 | |
| 1039 | fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> { |
| 1040 | Err(invalid_raw_value()) |
| 1041 | } |
| 1042 | |
| 1043 | fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> { |
| 1044 | Err(invalid_raw_value()) |
| 1045 | } |
| 1046 | |
| 1047 | fn serialize_struct_variant( |
| 1048 | self, |
| 1049 | _name: &'static str, |
| 1050 | _variant_index: u32, |
| 1051 | _variant: &'static str, |
| 1052 | _len: usize, |
| 1053 | ) -> Result<Self::SerializeStructVariant> { |
| 1054 | Err(invalid_raw_value()) |
| 1055 | } |
| 1056 | |
| 1057 | fn collect_str<T>(self, value: &T) -> Result<Self::Ok> |
| 1058 | where |
| 1059 | T: ?Sized + Display, |
| 1060 | { |
| 1061 | self.serialize_str(&value.to_string()) |
| 1062 | } |
| 1063 | } |
| 1064 | |