Skip to content

Commit

Permalink
make Serializer and Deserializer pub and usable (#72)
Browse files Browse the repository at this point in the history
* make Serializer/Deserializer pub

This is required when using serde_json_core with code that is
generic over the serde implementation, i.e. code that wants to
instantiate the Serializer/Deserializer, in the way done in
`de::from_slice` and `ser::to_slice`.

This also adds public getters for the Serializer.current_length
and Deserializer.index members.

Two obsolete/unused impls on the `Unreachable` have been removed.

* renames

* improve docs

* bump MSRV to 1.56.0

there is a MSRV bump somwhere in this dependency bump:

    Updating proc-macro2 v1.0.47 -> v1.0.66
    Updating quote v1.0.23 -> v1.0.32
    Updating serde v1.0.100 -> v1.0.175
    Updating serde_derive v1.0.100 -> v1.0.175
    Updating syn v1.0.109 -> v2.0.27

* fix wrong MSRV in README and src/lib.rs
  • Loading branch information
jordens authored Jul 24, 2023
1 parent 40d3700 commit 9deb2b9
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:

include:
# Test MSRV
- rust: 1.55.0 # keep in sync with manifest rust-version
- rust: 1.56.0 # keep in sync with manifest rust-version
TARGET: x86_64-unknown-linux-gnu

# Test nightly but don't fail
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Support for serializing tuple structs. These are serialized as JSON arrays,
which matches `serde_json` behaviour.
- `Serializer` and `Deserializer` are now `pub`.
- Added `pub` `Serializer::end()` and `Deserializer::end()`.

### Changed

- Increase MSRV to 1.56.0 to work around dependency-MSRV issues (see #72)

## [v0.5.0] - 2022-11-04

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ categories = ["no-std"]
description = "serde-json for no_std programs"
documentation = "https://docs.rs/serde-json-core"
edition = "2018"
rust-version = "1.55.0" # keep in sync with ci
rust-version = "1.56.0" # keep in sync with ci, src/lib.rs, and README
keywords = ["serde", "json"]
license = "MIT OR Apache-2.0"
name = "serde-json-core"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This project is developed and maintained by the [rust-embedded-community].

## Minimum Supported Rust Version (MSRV)

This crate is guaranteed to compile on stable Rust 1.51.0 and up. It *might*
This crate is guaranteed to compile on stable Rust 1.56.0 and up. It *might*
compile with older versions but that may change in any new patch release.

## License
Expand Down
10 changes: 7 additions & 3 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,25 @@ pub enum Error {

impl serde::de::StdError for Error {}

pub(crate) struct Deserializer<'b> {
/// A structure that deserializes Rust values from JSON in a buffer.
pub struct Deserializer<'b> {
slice: &'b [u8],
index: usize,
}

impl<'a> Deserializer<'a> {
fn new(slice: &'a [u8]) -> Deserializer<'_> {
/// Create a new `Deserializer`
pub fn new(slice: &'a [u8]) -> Deserializer<'_> {
Deserializer { slice, index: 0 }
}

fn eat_char(&mut self) {
self.index += 1;
}

fn end(&mut self) -> Result<usize> {
/// Check whether there is any unexpected data left in the buffer
/// and return the amount of data consumed
pub fn end(&mut self) -> Result<usize> {
match self.parse_whitespace() {
Some(_) => Err(Error::TrailingCharacters),
None => Ok(self.index),
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
//!
//! # Minimum Supported Rust Version (MSRV)
//!
//! This crate is guaranteed to compile on stable Rust 1.51.0 and up. It *might* compile with older
//! This crate is guaranteed to compile on stable Rust 1.56.0 and up. It *might* compile with older
//! versions but that may change in any new patch release.
#![deny(missing_docs)]
Expand Down
50 changes: 11 additions & 39 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,26 @@ impl fmt::Display for Error {
}
}

pub(crate) struct Serializer<'a> {
/// A structure that serializes Rust values as JSON into a buffer.
pub struct Serializer<'a> {
buf: &'a mut [u8],
current_length: usize,
}

impl<'a> Serializer<'a> {
fn new(buf: &'a mut [u8]) -> Self {
/// Create a new `Serializer`
pub fn new(buf: &'a mut [u8]) -> Self {
Serializer {
buf,
current_length: 0,
}
}

/// Return the current amount of serialized data in the buffer
pub fn end(&self) -> usize {
self.current_length
}

fn push(&mut self, c: u8) -> Result<()> {
if self.current_length < self.buf.len() {
unsafe { self.push_unchecked(c) };
Expand Down Expand Up @@ -475,20 +482,8 @@ impl ser::Error for Error {
}
}

pub(crate) enum Unreachable {}

impl ser::SerializeTupleStruct for Unreachable {
type Ok = ();
type Error = Error;

fn serialize_field<T: ?Sized>(&mut self, _value: &T) -> Result<()> {
unreachable!()
}

fn end(self) -> Result<Self::Ok> {
unreachable!()
}
}
/// An unreachable type to fill the SerializeTupleVariant type
pub enum Unreachable {}

impl ser::SerializeTupleVariant for Unreachable {
type Ok = ();
Expand All @@ -503,29 +498,6 @@ impl ser::SerializeTupleVariant for Unreachable {
}
}

impl ser::SerializeMap for Unreachable {
type Ok = ();
type Error = Error;

fn serialize_key<T: ?Sized>(&mut self, _key: &T) -> Result<()>
where
T: ser::Serialize,
{
unreachable!()
}

fn serialize_value<T: ?Sized>(&mut self, _value: &T) -> Result<()>
where
T: ser::Serialize,
{
unreachable!()
}

fn end(self) -> Result<Self::Ok> {
unreachable!()
}
}

#[cfg(test)]
mod tests {
use serde_derive::Serialize;
Expand Down

0 comments on commit 9deb2b9

Please sign in to comment.