Skip to content

Commit 7f2ccaf

Browse files
authored
Re-export serde_json::Value from core (#3296)
Resolves #1687. As long as we use `Value` from core, should `serde_json::Value` ever make a breaking change to `Value` we can effectively vendor just that while continuing to pick up changes to the de/serializers.
1 parent cdddbf5 commit 7f2ccaf

File tree

13 files changed

+81
-21
lines changed

13 files changed

+81
-21
lines changed

sdk/core/azure_core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Added `Response::to_raw_response()` function to create a `RawResponse` from cloned data.
99
- Added `UrlExt::append_path()`.
1010
- Implemented `IntoFuture` for a `Poller`. Call `await` on a Poller to get the final model, or `into_stream()` to get a `futures::Stream` to poll the operation manually.
11+
- Re-exported `serde_json::Value` as `azure_core::Value` ([#1687](https://github.com/Azure/azure-sdk-for-rust/issues/1687))
1112

1213
### Breaking Changes
1314

sdk/core/azure_core/benches/deserialization.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ criterion_main!(benchmarks);
7979
mod models {
8080
//! Copied from `azure_storage_blob::models`.
8181
//!
82-
//! Model definitions remain intact but some unused fields are replaced with `serde_json::Value`
82+
//! Model definitions remain intact but some unused fields are replaced with `azure_core::Value`
8383
//! because we still want to retain the same behavior and performance of the deserialization visitor.
8484
8585
use azure_core::{
8686
error::{Error, ErrorKind},
8787
time::OffsetDateTime,
88+
Value,
8889
};
8990
use serde::{Deserialize, Deserializer, Serialize, Serializer};
90-
use serde_json::Value;
9191
use std::{convert::Infallible, str::FromStr};
9292

9393
/// An enumeration of blobs.

sdk/core/azure_core/src/error/error_response.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub struct ErrorDetail {
7878

7979
/// Additional properties that may be returned with the error.
8080
#[serde(flatten)]
81-
pub additional_properties: HashMap<String, serde_json::Value>,
81+
pub additional_properties: HashMap<String, crate::Value>,
8282
}
8383

8484
/// Inner error information about an error returned from a service.

sdk/core/azure_core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub mod test;
2020

2121
// Re-export modules in typespec_client_core such that azure_core-based crates don't need to reference it directly.
2222
pub use typespec_client_core::{
23-
async_runtime, base64, fmt, json, sleep, stream, time, Bytes, Error, Result, Uuid,
23+
async_runtime, base64, fmt, json, sleep, stream, time, Bytes, Error, Result, Uuid, Value,
2424
};
2525

2626
/// Abstractions for distributed tracing and telemetry.

sdk/core/typespec/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ thiserror.workspace = true
2727
default = ["http", "json"]
2828
http = []
2929
json = ["dep:serde", "dep:serde_json"]
30-
xml = ["dep:quick-xml"]
30+
xml = ["dep:serde", "dep:quick-xml"]
3131

3232
[package.metadata.docs.rs]
3333
all-features = true

sdk/core/typespec_client_core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Added `PipelineOptions::retry_status_codes` for configuring which status codes should trigger a retry.
88
- Added `UrlExt::append_path()`.
99
- Added module `fmt::empty_as_null` containing some serde helpers for empty/null string values.
10+
- Re-exported `serde_json::Value` as `typespec_client_core::Value` ([#1687](https://github.com/Azure/azure-sdk-for-rust/issues/1687))
1011

1112
### Breaking Changes
1213

sdk/core/typespec_client_core/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ rand.workspace = true
2121
reqwest = { workspace = true, optional = true }
2222
rust_decimal = { workspace = true, optional = true }
2323
serde.workspace = true
24-
serde_json.workspace = true
24+
serde_json = { workspace = true, optional = true }
2525
time.workspace = true
2626
tokio = { workspace = true, optional = true }
2727
tracing.workspace = true
@@ -65,15 +65,15 @@ debug = ["typespec_macros?/debug"]
6565
decimal = ["dep:rust_decimal"]
6666
derive = ["dep:typespec_macros"]
6767
http = ["typespec/http"]
68-
json = ["typespec/json"]
68+
json = ["dep:serde_json", "typespec/json"]
6969
reqwest = ["dep:reqwest"]
7070
reqwest_deflate = ["reqwest", "reqwest/deflate"]
7171
reqwest_gzip = ["reqwest", "reqwest/gzip"]
7272
reqwest_native_tls = ["reqwest", "reqwest/native-tls"]
7373
test = [] # Enables extra tracing including error bodies that may contain PII.
7474
tokio = ["tokio/sync", "tokio/time"]
7575
wasm_bindgen = ["dep:wasm-bindgen-futures", "gloo-timers/futures"]
76-
xml = ["typespec/xml"]
76+
xml = ["dep:serde_json", "typespec/xml"] # Uses serde_json::Value for `unknown` TypeSpec scalar.
7777

7878
[[example]]
7979
name = "core_binary_data_request"

sdk/core/typespec_client_core/src/base64.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,10 @@ pub mod option {
335335

336336
#[cfg(test)]
337337
mod tests {
338-
use super::{
339-
decode, decode_url_safe, deserialize, deserialize_url_safe, encode, encode_url_safe,
340-
option, serialize, serialize_url_safe,
341-
};
338+
use super::{decode, decode_url_safe, encode, encode_url_safe};
339+
#[cfg(feature = "json")]
340+
use super::{deserialize, deserialize_url_safe, option, serialize, serialize_url_safe};
341+
#[cfg(feature = "json")]
342342
use serde::{Deserialize, Serialize};
343343

344344
#[test]
@@ -397,12 +397,14 @@ mod tests {
397397
assert_eq!(decode_url_safe(encode_url_safe(data)).unwrap(), data);
398398
}
399399

400+
#[cfg(feature = "json")]
400401
#[derive(Serialize, Deserialize)]
401402
struct TestStruct {
402403
#[serde(serialize_with = "serialize", deserialize_with = "deserialize")]
403404
data: Vec<u8>,
404405
}
405406

407+
#[cfg(feature = "json")]
406408
#[test]
407409
fn serde_standard() {
408410
let original = TestStruct {
@@ -414,6 +416,7 @@ mod tests {
414416
assert_eq!(deserialized.data, original.data);
415417
}
416418

419+
#[cfg(feature = "json")]
417420
#[derive(Serialize, Deserialize)]
418421
struct TestStructUrlSafe {
419422
#[serde(
@@ -423,6 +426,7 @@ mod tests {
423426
data: Vec<u8>,
424427
}
425428

429+
#[cfg(feature = "json")]
426430
#[test]
427431
fn serde_url_safe() {
428432
let original = TestStructUrlSafe {
@@ -434,6 +438,7 @@ mod tests {
434438
assert_eq!(deserialized.data, original.data);
435439
}
436440

441+
#[cfg(feature = "json")]
437442
#[derive(Serialize, Deserialize)]
438443
struct TestOptionalStruct {
439444
#[serde(
@@ -443,6 +448,7 @@ mod tests {
443448
data: Option<Vec<u8>>,
444449
}
445450

451+
#[cfg(feature = "json")]
446452
#[test]
447453
fn serde_option_some() {
448454
let original = TestOptionalStruct {
@@ -454,6 +460,7 @@ mod tests {
454460
assert_eq!(deserialized.data, original.data);
455461
}
456462

463+
#[cfg(feature = "json")]
457464
#[test]
458465
fn serde_option_none() {
459466
let original = TestOptionalStruct { data: None };
@@ -463,6 +470,7 @@ mod tests {
463470
assert_eq!(deserialized.data, None);
464471
}
465472

473+
#[cfg(feature = "json")]
466474
#[derive(Serialize, Deserialize)]
467475
struct TestOptionalStructUrlSafe {
468476
#[serde(
@@ -472,6 +480,7 @@ mod tests {
472480
data: Option<Vec<u8>>,
473481
}
474482

483+
#[cfg(feature = "json")]
475484
#[test]
476485
fn serde_option_url_safe_some() {
477486
let original = TestOptionalStructUrlSafe {
@@ -483,6 +492,7 @@ mod tests {
483492
assert_eq!(deserialized.data, original.data);
484493
}
485494

495+
#[cfg(feature = "json")]
486496
#[test]
487497
fn serde_option_url_safe_none() {
488498
let original = TestOptionalStructUrlSafe { data: None };

sdk/core/typespec_client_core/src/http/format.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub trait DeserializeWith<F: Format>: Sized {
4343

4444
/// Implements [`DeserializeWith<JsonFormat>`] for an arbitrary type `D`
4545
/// that implements [`serde::de::DeserializeOwned`] by deserializing the response body to the specified type using [`serde_json`].
46+
#[cfg(feature = "json")]
4647
impl<D: DeserializeOwned> DeserializeWith<JsonFormat> for D {
4748
fn deserialize_with(body: ResponseBody) -> typespec::Result<Self> {
4849
body.json()
@@ -55,9 +56,11 @@ impl<D: DeserializeOwned> DeserializeWith<JsonFormat> for D {
5556
/// This format supports deserializing response bodies to:
5657
/// * [`ResponseBody`] - The raw response body, without any deserialization.
5758
/// * Any value implementing [`serde::de::DeserializeOwned`] - Deserializes the response body to the specified type using JSON deserialization.
59+
#[cfg(feature = "json")]
5860
#[derive(Debug, Clone)]
5961
pub struct JsonFormat;
6062

63+
#[cfg(feature = "json")]
6164
impl Format for JsonFormat {
6265
fn deserialize<T: DeserializeOwned, S: AsRef<[u8]>>(body: S) -> crate::Result<T> {
6366
crate::json::from_json(body)

sdk/core/typespec_client_core/src/http/method.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
// Copyright (c) Microsoft Corporation. All rights reserved.
3131
// Licensed under the MIT License.
3232

33-
#[cfg(any(feature = "json", feature = "xml"))]
34-
use std::fmt::{self, Display};
33+
use std::fmt;
3534
use std::str::FromStr;
3635

3736
/// HTTP request methods.
@@ -172,7 +171,7 @@ mod serde {
172171
}
173172
}
174173

175-
impl Display for Method {
174+
impl fmt::Display for Method {
176175
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
177176
f.write_str(AsRef::<str>::as_ref(self))
178177
}

0 commit comments

Comments
 (0)