profile
viewpoint
David Tolnay dtolnay 0xF9BA143B95FF6D82 Menlo Park, CA

brson/stdx 1345

The missing batteries of Rust

dtolnay/cxx 1314

Safe interop between Rust and C++

dtolnay/anyhow 1045

Flexible concrete Error type built on std::error::Error

dtolnay/case-studies 798

Analysis of various tricky Rust code

dtolnay/cargo-expand 645

Subcommand to show result of macro expansion

dtolnay/async-trait 511

Type erasure for async trait methods

chyh1990/yaml-rust 370

A pure rust YAML implementation.

dtolnay/erased-serde 141

Type-erased Serialize, Serializer and Deserializer traits

dtolnay/indoc 136

Indented document literals for Rust

push eventserde-rs/docs

Serde Docs

commit sha 073e494d4bb8d1ae13b93be7dda58b4768ed95c5

Documentation for serde-rs/docs@5187b57

view details

push time in 7 hours

pull request commentrust-lang/rust

Liballoc use vec instead of vector

@bors r+ rollup

pickfire

comment created time in 19 hours

issue closedserde-rs/json

f64 serialization -> deserialize roundtrip is mutated.

I have some code that is similar to the following (edited for brevity):

#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub(in super::super) struct ChannelInfoInternals {
    id: u64,
    bandwidth: f64,
    radius: f64,
}

Which I'm testing via bolero. It produces the following test failure:

======================== Test Failure ========================

BOLERO_RANDOM_SEED=14280173725739549455

Input: 
ChannelInfoInternals {
    id: 0,
    bandwidth: 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007291122019875111,
    radius: 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005,
}

Error: 
panicked at 'assertion failed: `(left == right)`
  left: `ChannelInfoInternals { id: 0, bandwidth: 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007291122019875111, radius: 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005 }`,
 right: `ChannelInfoInternals { id: 0, bandwidth: 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000729112201987511, radius: 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005 }`: Serde_json serialized string = '"{\"id\":0,\"bandwidth\":7.291122019875111e-304,\"radius\":5e-324}\n"'', bit_network_library/src/configuration_parsing/channel_info/channel_info_internals.rs:391:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

==============================================================

when it's tested using

    #[test]
    fn serialize_json() {
        fuzz!().with_type::<ChannelInfoInternals>().for_each(|dut| {
            let mut string = serde_json::to_string(&dut)
                .expect(&format!("Failed to serialize! dut = {:?}", dut));
            string.push('\n');
            let output: ChannelInfoInternals;
            output = serde_json::from_str(&string).expect(&format!(
                "Failed to deserialize! Serialized string = {:?}",
                string
            ));
            assert_eq!(dut, &output, "Serde_json serialized string = '{:?}'", string);
        });
    }

As you can see from the encoded form, it has the very last 1 stored, it just isn't deserializing it correctly.

closed time in a day

ckaran

push eventrust-lang/api-guidelines

Rust API Guidelines (from Travis CI)

commit sha 28dcd9915ff216f575f968728abb67fec9c6820e

Deploy rust-lang/api-guidelines to github.com/rust-lang/api-guidelines.git:gh-pages

view details

push time in a day

push eventrust-lang-nursery/rust-cookbook

Rust Cookbook

commit sha 5c42d8b9fa9db1112e4d000e6facbf6c9ea83311

Build cookbook at rust-lang-nursery/rust-cookbook@bba147f

view details

push time in a day

delete branch dtolnay/rust-toolchain

delete branch : dependabot/npm_and_yarn/npm-registry-fetch-8.1.1

delete time in a day

push eventdtolnay/rust-toolchain

dependabot[bot]

commit sha 4f3ce88516b4b2d637f88a6507d611bc76a785bb

Bump npm-registry-fetch from 8.0.0 to 8.1.1 Bumps [npm-registry-fetch](https://github.com/npm/registry-fetch) from 8.0.0 to 8.1.1. - [Release notes](https://github.com/npm/registry-fetch/releases) - [Changelog](https://github.com/npm/npm-registry-fetch/blob/latest/CHANGELOG.md) - [Commits](https://github.com/npm/registry-fetch/compare/v8.0.0...v8.1.1) Signed-off-by: dependabot[bot] <support@github.com>

view details

David Tolnay

commit sha 90c229845287351cd14416864bde592eea3cd1b2

Merge pull request #2 from dtolnay/dependabot/npm_and_yarn/npm-registry-fetch-8.1.1 Bump npm-registry-fetch from 8.0.0 to 8.1.1

view details

push time in a day

PR merged dtolnay/rust-toolchain

Bump npm-registry-fetch from 8.0.0 to 8.1.1 dependencies

Bumps npm-registry-fetch from 8.0.0 to 8.1.1. <details> <summary>Changelog</summary> <p><em>Sourced from <a href="https://github.com/npm/npm-registry-fetch/blob/latest/CHANGELOG.md">npm-registry-fetch's changelog</a>.</em></p> <blockquote> <h3><a href="https://github.com/npm/registry-fetch/compare/v8.1.0...v8.1.1">8.1.1</a> (2020-06-30)</h3> <h2><a href="https://github.com/npm/registry-fetch/compare/v8.0.3...v8.1.0">8.1.0</a> (2020-05-20)</h2> <h3>Features</h3> <ul> <li>add npm-command HTTP header (<a href="https://github.com/npm/registry-fetch/commit/1bb4eb2c66ee8a0dc62558bdcff1b548e2bb9820">1bb4eb2</a>)</li> </ul> <h3><a href="https://github.com/npm/registry-fetch/compare/v8.0.2...v8.0.3">8.0.3</a> (2020-05-13)</h3> <h3>Bug Fixes</h3> <ul> <li>update minipass and make-fetch-happen to latest (<a href="https://github.com/npm/registry-fetch/commit/3b6c5d0d8ccd4c4a97862a65acef956f19aec127">3b6c5d0</a>), closes <a href="https://github-redirect.dependabot.com/npm/registry-fetch/issues/23">#23</a></li> </ul> <h3><a href="https://github.com/npm/registry-fetch/compare/v8.0.1...v8.0.2">8.0.2</a> (2020-05-04)</h3> <h3>Bug Fixes</h3> <ul> <li>update make-fetch-happen to 8.0.6 (<a href="https://github.com/npm/registry-fetch/commit/226df2c32e3f9ed8ceefcfdbd11efb178181b442">226df2c</a>)</li> </ul> </blockquote> </details> <details> <summary>Commits</summary> <ul> <li><a href="https://github.com/npm/npm-registry-fetch/commit/6cfc3c38dc4aad516e51ca9147a497994add9015"><code>6cfc3c3</code></a> chore(release): 8.1.1</li> <li><a href="https://github.com/npm/npm-registry-fetch/commit/18bf9b97fb1deecdba01ffb05580370846255c88"><code>18bf9b9</code></a> chore: remove auth data from logs (<a href="https://github-redirect.dependabot.com/npm/registry-fetch/issues/29">#29</a>)</li> <li><a href="https://github.com/npm/npm-registry-fetch/commit/09e540b09a951ded299ee028e7f1bd21cef5a6da"><code>09e540b</code></a> chore(release): 8.1.0</li> <li><a href="https://github.com/npm/npm-registry-fetch/commit/1bb4eb2c66ee8a0dc62558bdcff1b548e2bb9820"><code>1bb4eb2</code></a> feat: add npm-command HTTP header</li> <li><a href="https://github.com/npm/npm-registry-fetch/commit/022c9090ccf51c9cafa0f5daa81bdec44a86a7c1"><code>022c909</code></a> chore(release): 8.0.3</li> <li><a href="https://github.com/npm/npm-registry-fetch/commit/3b6c5d0d8ccd4c4a97862a65acef956f19aec127"><code>3b6c5d0</code></a> fix: update minipass and make-fetch-happen to latest</li> <li><a href="https://github.com/npm/npm-registry-fetch/commit/8196169218aca9956b6cc2dd5be3602a36bfa450"><code>8196169</code></a> chore(release): 8.0.2</li> <li><a href="https://github.com/npm/npm-registry-fetch/commit/226df2c32e3f9ed8ceefcfdbd11efb178181b442"><code>226df2c</code></a> fix: update make-fetch-happen to 8.0.6</li> <li><a href="https://github.com/npm/npm-registry-fetch/commit/aa164def0782a454fa12b3a466aa1da5f3846bce"><code>aa164de</code></a> 8.0.1</li> <li><a href="https://github.com/npm/npm-registry-fetch/commit/ad328dce029db909f9452ffb5202ab55972977c1"><code>ad328dc</code></a> update deps to fix audit</li> <li>Additional commits viewable in <a href="https://github.com/npm/registry-fetch/compare/v8.0.0...v8.1.1">compare view</a></li> </ul> </details> <details> <summary>Maintainer changes</summary> <p>This version was pushed to npm by <a href="https://www.npmjs.com/~claudiahdz">claudiahdz</a>, a new releaser for npm-registry-fetch since your current version.</p> </details> <br />

Dependabot compatibility score

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


<details> <summary>Dependabot commands and options</summary> <br />

You can trigger Dependabot actions by commenting on this PR:

  • @dependabot rebase will rebase this PR
  • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
  • @dependabot merge will merge this PR after your CI passes on it
  • @dependabot squash and merge will squash and merge this PR after your CI passes on it
  • @dependabot cancel merge will cancel a previously requested merge and block automerging
  • @dependabot reopen will reopen this PR if it is closed
  • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
  • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
  • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
  • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
  • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
  • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
  • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
  • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

You can disable automated security fix PRs for this repo from the Security Alerts page.

</details>

+42 -27

0 comment

1 changed file

dependabot[bot]

pr closed time in a day

issue commentserde-rs/json

f64 serialization -> deserialize roundtrip is mutated.

Please try with features = ["float_roundtrip"] if it isn't already enabled.

If this reproduces with float_roundtrip enabled, please provide a minimal compilable reproducer.

ckaran

comment created time in a day

push eventserde-rs/docs

Serde Docs

commit sha dc7aee1fef9852dbc7b24942f53ec7c97a6d9624

Documentation for serde-rs/docs@5187b57

view details

push time in a day

push eventdtolnay/anyhow

David Tolnay

commit sha bff844ebe9c0951a9f504bba48d6553ff01d3f53

Update ui tests to nightly-2020-07-06

view details

push time in 2 days

push eventdtolnay/quote

David Tolnay

commit sha 9b1411336906109f7ef0e44503149e01cefead37

Update ui tests to nightly-2020-07-06

view details

push time in 2 days

push eventdtolnay/thiserror

David Tolnay

commit sha de996c863dda4cd893cb4a4cadf8575a541bd691

Update ui tests to nightly-2020-07-06

view details

push time in 2 days

push eventdtolnay/ref-cast

David Tolnay

commit sha 38494e070c266ef8bdb10b3242c016a89dedc63a

Update ui tests to nightly-2020-07-06

view details

push time in 2 days

issue openedFaultyRAM/slice-cast

Undefined behavior in test suite from incorrect alignment

The following test instantiates a &[u32] that is not necessarily 4 byte aligned, which is undefined behavior. Rust references are required to be aligned always.

https://github.com/FaultyRAM/slice-cast/blob/207dfb5a4b5197eab163b0916bdc35d8ed7dd5a4/src/lib.rs#L168-L174

Other tests may also be affected; I haven't looked through them all.

FYI @FaultyRAM @jkeljo

created time in 2 days

push eventserde-rs/docs

Serde Docs

commit sha 6e0be621d396ffc8bf0993979d01e30289edbe5b

Documentation for serde-rs/docs@5187b57

view details

push time in 2 days

pull request commentrust-lang/rust

Add Integer::checked_{log,log2,log10}

@bors r+

yoshuawuyts

comment created time in 3 days

push eventyoshuawuyts/rust

David Tolnay

commit sha 6cef103a50d6fdfa068860606ffbea7f7741c8ff

Touch up checked_log PR

view details

push time in 3 days

pull request commentrust-lang/rust

Document the unsafe keyword

@bors delegate=rylev

poliorcetics

comment created time in 3 days

pull request commentrust-lang/rust

Document the unsafe keyword

I don't know who on T-doc reviews this kind of thing but let's try this:

r? @rylev

poliorcetics

comment created time in 3 days

pull request commentrust-lang/rust

Move A|Rc::as_ptr from feature(weak_into_raw) to feature(rc_as_ptr)

@bors r+

CAD97

comment created time in 3 days

push eventserde-rs/serde

David Tolnay

commit sha 9e140a2071e27f186ba6f73abd7abfdb00f8e2fd

Tweak yaml format blurb

view details

push time in 3 days

push eventserde-rs/serde-rs.github.io

David Tolnay

commit sha 8f49d48fcba001165c92e471fef85d3b029035e6

Tweak yaml format blurb

view details

push time in 3 days

issue openedspacejam/rio

Unsoundness without forget

I came across the following note in https://docs.rs/rio/0.9.3/rio/struct.Completion.html, which I found surprising and possibly disingenuous:

Safety

Never call std::mem::forget on this value. It can lead to a use-after-free bug. The fact that std::mem::forget is not marked unsafe is a bug in the Rust standard library.

In trying to understand the backstory of your position on this, I found this sequence of tweets from which I think I see the misunderstanding on your part:

  • @Lucretiel(https://twitter.com/Lucretiel/status/1256430755808509952)— Wait, does this mean that forgetting something can lead to unsoundness in rio? That would imply I can create unsoundness by, say, accidentally creating a reference cycle

  • @spacejam(https://twitter.com/sadisticsystems/status/1256519569830670336)— A reference cycle is fine and not the same as mem::forget because the compiler still enforces that any buffer passed to rio outlives the kernel's write into the buffer

  • @spacejam(https://twitter.com/sadisticsystems/status/1256519873401745413)— A reference cycle is immortal. mem::forget allows desired lifetime constraints related to Drop to be violated

  • @spacejam(https://twitter.com/sadisticsystems/status/1256520244505411586)— Immortality is safe. Violating drop-based lifetime constraints via forget can lead to use after free though

In case it helps see why these tweets are wrong, I wrote up some example code that demonstrates stack corruption (or heap corruption / use after free / however you want it) without any involvement of forget.

The same misunderstanding in rio was called out recently by someone else in https://www.reddit.com/r/rust/comments/hk8lab/ringbahn_ii_the_central_state_machine/fwt0xmt/.

Hopefully the example code will help identify a path to making a sound API for rio; otherwise it would be good to clarify the documentation to make it clear that the safety proposition is "hope that nothing else in your code interacts poorly with rio's assumptions about Rust semantics", rather than anything specific about forget.


// [dependencies]
// extreme = "666.666.666666"
// futures-util = "0.3"
// rio = { git = "https://github.com/spacejam/rio" }

#[derive(Debug)]
pub enum Buffer {
    Inline([u8; 32]),
    OutOfLine(Vec<u8>),
}

impl Buffer {
    fn as_mut(&mut self) -> &mut [u8] {
        match self {
            Buffer::Inline(buffer) => buffer,
            Buffer::OutOfLine(buffer) => buffer,
        }
    }
}

fn main() {
    let rio = rio::new().unwrap();
    let stdin = std::io::stdin();

    let mut buf = Buffer::Inline(Default::default());
    let mut slice = buf.as_mut();
    let mut completion = rio.read_at(&stdin, &mut slice, 0);
    let _ = extreme::run(async { futures_util::poll!(&mut completion) });

    struct Oops<'a>(
        rio::Completion<'a, usize>,
        std::cell::Cell<Option<std::sync::Arc<Oops<'a>>>>,
    );
    let oops = std::sync::Arc::new(Oops(completion, Default::default()));
    oops.1.set(Some(oops.clone()));
    drop(oops);

    buf = Buffer::OutOfLine(vec![0; 100]);
    std::thread::sleep(std::time::Duration::from_secs(2));
    println!("{:?}", buf);
}
$ (sleep 1; yes) | cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/repro`
Segmentation fault (core dumped)

created time in 3 days

push eventserde-rs/docs

Serde Docs

commit sha 38a8ce312a1e1637626b93e21364e33152bfb369

Documentation for serde-rs/docs@5187b57

view details

push time in 3 days

issue closeddtolnay/serde-yaml

Allow matching ErrorImpl

I'd like to be able to handle different errors. However, I can't match ErrorImpl, because serde_yaml::error is private. Is there a way to expose it?

closed time in 4 days

sthesing

issue closeddtolnay/serde-yaml

Implement From<serde_yaml::Error> for io::Error

Basically the opposite of #97, this would be quite handy to avoid needing a map_err when e.g. you're reading YAML configuration from a file on startup and want to get some error out to the user.

serde_json has this here https://docs.serde.rs/src/serde_json/error.rs.html#134-173

closed time in 4 days

TomCrypto

issue commentdtolnay/serde-yaml

Implement From<serde_yaml::Error> for io::Error

I'll give this a close since it hasn't been important enough to anyone to send a PR.

TomCrypto

comment created time in 4 days

issue closeddtolnay/serde-yaml

[Question/Feature Request] Keep arrays on serializing?

Hi,

I have in a file:

tags: [tag1, tag2]

It deserialized into a Vec<String> but when it serialize it back we got:

tags:
    - tag1
    - tag2

Is there a way to keep the first version on serializing? If not, can we have a field attribute to say so?

closed time in 4 days

Geobert

issue commentdtolnay/serde-yaml

[Question/Feature Request] Keep arrays on serializing?

I'll close and track this under #141. This would like happen only as part of switching off of yaml-rust as the yaml backend.

Geobert

comment created time in 4 days

issue commentdtolnay/serde-yaml

hint to deserialize number as a string instead?

I have not gotten a chance, but I would accept a PR if there is a fix in serde_yaml.

pwoolcoc

comment created time in 4 days

issue closeddtolnay/serde-yaml

Deserializing either single mapping or sequence of mappings as a single sequence

I've been going around in circles trying to figure this one out. I'd like to deserialize the following yaml both in the same way:

actions:
 - create:
     a: "a"
     b: "b"

and

actions:
 - create:
     - a: "a"
       b: "b"
     - a: "a"
       b: "b"

to

#[derive(Deserialize)]
struct Obj {
    actions: Vec<Action>,
}

#[derive(Deserialize)]
#[serde(rename_all = "lowercase"]
enum Action {
  Create(Create),
}

#[derive(Deserialize)]
struct Create {
  #[serde(flatten)]
  #[serde(deserialize_with = "my_custom_fn")]
  values: Vec<CreateValue>,
}

struct CreateValue {
  a: String,
  b: String,
}

When implementing a custom deserializer, the visit_map function works for the single mapping case, but throws an invalid type: sequence, expected struct CreateValue error when attempting to parse the sequence; even though I've implemented visit_seq it never seems to detect the sequence. Not sure how I should ultimately wire this up.

closed time in 4 days

anweiss

issue commentdtolnay/serde-yaml

Deserializing either single mapping or sequence of mappings as a single sequence

Hi @anweiss, sorry that no one was able to provide help here. This is a general serde question not specific to yaml, so if this is still an issue, you could try taking this question to any of the resources shown in https://github.com/serde-rs/serde#getting-help.

anweiss

comment created time in 4 days

issue commentdtolnay/serde-yaml

Control output formatting

AFAIK this is not supported by https://github.com/chyh1990/yaml-rust. It would need to be implemented there, or we would need to move to different yaml backend (with the latter option being much preferred, for many reasons).

keirlawson

comment created time in 4 days

issue commentdtolnay/request-for-implementation

Minimalist YAML parser with support for borrowed string slices

@nathanwhit it looks like the feedback I filed on https://github.com/nathanwhit/minimal-yaml were mostly resolved. Any plan on what polish or testing are missing still before it would be released?

dtolnay

comment created time in 4 days

issue openednathanwhit/minimal-yaml

Remove serde dependency

It appears that serde is only used for:

https://github.com/nathanwhit/minimal-yaml/blob/a327853c45b5960c4db5fd860cd315b93816c26f/src/lib.rs#L15-L21

It would be better to remove those impls. They don't match expectations for how minimal_yaml::Yaml corresponds with serde data; for example Yaml::Sequence <-> serde sequence.

Serde interop would be provided by a higher level library than a yaml backend.

created time in 4 days

issue openednathanwhit/minimal-yaml

Warning on unused Parser::context

When building the crate as of the current master branch, I get the following warning emitted.

$ cargo check
    Checking minimal-yaml v0.1.0
warning: associated function is never used: `context`
  --> src/parse.rs:58:8
   |
58 |     fn context(&self) -> Option<&ParseContext> {
   |        ^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

created time in 4 days

issue closeddtolnay/serde-yaml

Implementation of `serde::de::Deserialize` is not general enough

When running following code snippet:

use std::collections::HashMap;

fn main() {
    let input = r###"
        ---
        key: Key
        value: Value
    "###;
    let x: HashMap<&str, &str> = serde_yaml::from_str(input).unwrap();
   
    // let input = r###"
    // {
    //     "key": "Key",
    //     "value": "Value"
    // }
    // "###;
    // let x: HashMap<&str, &str> = serde_json::from_str(input).unwrap();

    println!("{:?}", x);
}

it will result in compile-time error:

Compiling playground v0.0.1 (/playground)
error: implementation of `serde::de::Deserialize` is not general enough
   --> src/main.rs:9:34
    |
9   |       let x: HashMap<&str, &str> = serde_yaml::from_str(input).unwrap();
    |                                    ^^^^^^^^^^^^^^^^^^^^ implementation of `serde::de::Deserialize` is not general enough
    | 
   ::: /playground/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.110/src/de/mod.rs:531:1
    |
531 | / pub trait Deserialize<'de>: Sized {
532 | |     /// Deserialize this value from the given Serde deserializer.
533 | |     ///
534 | |     /// See the [Implementing `Deserialize`][impl-deserialize] section of the
...   |
569 | |     }
570 | | }
    | |_- trait `serde::de::Deserialize` defined here
    |
    = note: `std::collections::HashMap<&str, &str>` must implement `serde::de::Deserialize<'0>`, for any lifetime `'0`...
    = note: ...but `std::collections::HashMap<&str, &str>` actually implements `serde::de::Deserialize<'1>`, for some specific lifetime `'1`

error: aborting due to previous error

error: could not compile `playground`.

However serde_json works for it.

Is it a limitation of serde_yaml?

closed time in 4 days

zhzy0077

issue commentdtolnay/serde-yaml

Implementation of `serde::de::Deserialize` is not general enough

Yes, serde_yaml does not allow deserializing &str. This is tracked in https://github.com/dtolnay/serde-yaml/issues/94.

zhzy0077

comment created time in 4 days

issue closedserde-rs/serde

Alternative fields for adjacently tagged enums

Since no one replied to my earlier issue #1516 and it just got closed after over a year, I will make a new one. It was closed with the message that I should ask in the rust discord for help. I did and was ignored, again. I assume it is currently not possible to get adjacently tagged enums to use a field from the parent struct:

// possible
{ 
  "enum": {
    "myenum": "foo",
    "rest": { ... }
  }
}

// not possible
{ 
  "enum": {
    "rest": { ... }
  }, 
  "myenum": "foo",
}

So this is a feature request to add an attribute that supports this use-case. You can find more info here: #1516.

The contribution guidelines require me to think about downsides and alternatives. 1st, the obvious downside: It's yet another attribute. 2nd, the alternative: For one case of my use-case, I was able to use untagged in combination with deny_unknown_fields. However, this did not help me in another instance, because of the structure of the data.

closed time in 4 days

greaka

issue commentserde-rs/serde

Alternative fields for adjacently tagged enums

This is substantially the same issue as #1516 so I'll close again. Stack Overflow or https://www.reddit.com/r/rust/comments/hhv4z1/hey_rustaceans_got_an_easy_question_ask_here/ would be worth trying if Discord was not able to help.

#[serde(tag = "myenum", content = "enum")] on the enum and #[serde(flatten)] on the field are going to be relevant.

greaka

comment created time in 4 days

issue closedserde-rs/json

How to Serialize/Deserialize a fn type struct field?

I have a struct like:

#[derive(Debug, Deserialize, Serialize)]
pub struct Message {
    // Message struct to store the content and details of
    // a Message object.
    pub uuid: Uuid,
    pub callback: fn(Value) -> Value,
    pub content: Value,
}

Is it possible to Serialize/Deserialize an object like this? The issue lies with the callback field because it is callable type. The error when compiling:

error[E0277]: the trait bound fn(serde_json::Value) -> serde_json::Value: serde::Deserialize<'_> is not satisfied --> src/message/message.rs:10:5 | 10 | pub callback: fn(Value) -> Value, | ^^^ the trait serde::Deserialize<'_> is not implemented for fn(serde_json::Value) -> serde_json::Value | = note: required by serde::de::SeqAccess::next_element error[E0277]: the trait bound fn(serde_json::Value) -> serde_json::Value: serde::Deserialize<'_> is not satisfied --> src/message/message.rs:10:5 | 10 | pub callback: fn(Value) -> Value, | ^^^ the trait serde::Deserialize<'_> is not implemented for fn(serde_json::Value) -> serde_json::Value | = note: required by serde::de::MapAccess::next_value error[E0277]: the trait bound fn(serde_json::Value) -> serde_json::Value: serde::Serialize is not satisfied --> src/message/message.rs:5:30 | 5 | #[derive(Debug, Deserialize, Serialize)] | ^^^^^^^^^ the trait serde::Serialize is not implemented for fn(serde_json::Value) -> serde_json::Value | = note: required by serde::ser::SerializeStruct::serialize_field

closed time in 4 days

manishgupta24

issue commentserde-rs/json

How to Serialize/Deserialize a fn type struct field?

Hi @manishgupta24, sorry that no one was able to provide help here. If this is still an issue, you could try taking this question to any of the resources shown in https://github.com/serde-rs/serde#getting-help.

manishgupta24

comment created time in 4 days

issue closedserde-rs/json

Iterate Nested Array

Hey guys, I've been trying to find an answer to my problem, tried a few different ways, but the errors keep mounting, I'll try again tomorrow. I guess I'm unsure about how to convert the serde_json::value::Value to something I can iterate over, perhaps an array...

Thanks in advance.

    use serde_json::json;
    ...
    ...
    ...

    let tbl_cc_data_config = json!({
        "key_name": "__main__",
        "fields": [
            {"Field Name": "cc_id", "Type": "VARCHAR", "Length": 36, "Constraint": "PKEY"},
            {"Field Name": "user_id", "Type": "VARCHAR", "Length": 36, "Constraint": "None"},
            {"Field Name": "user_name", "Type": "VARCHAR", "Length": 50, "Constraint": "None"}
        ]        
    });

    let table_name = &tbl_cc_data_config["table_name"];

    // Works !!!
    println!("Table Name: {}\n", table_name);

    let fields_list = &tbl_cc_data_config["fields"];

    // Works !!!
    println!("{}\n", fields_list[0]["Type"]);
   
    // Fails from here on...
    let fields_iter = fields_list.as_object();

    for (k, v) in fields_iter.iter() {
        println!("{:?} {:?}", k, v);
    }

closed time in 4 days

viper6277

issue commentserde-rs/json

Iterate Nested Array

Hi @viper6277, sorry that no one was able to provide more help here. If this is still an issue, you could try taking this question to any of the resources shown in https://github.com/serde-rs/serde#getting-help.

viper6277

comment created time in 4 days

issue closedserde-rs/json

Add object creation syntax sugar to the macro.

It would be nice if the json! macro had the same syntax present in Rust and JavaScript, namely that you can initialised a object like the following { foo, bar, baz } which is expanded to { "foo": foo, "bar": bar, "baz": baz }.

For writing one off JSON objects that don't require strict validation it can be quite annoying to have serde_json::json!({ "foo": foo, "bar": bar, "baz": baz }) for each. This should be possible to implement in the macro, though I don't know enough about the current json_internal macro to know how difficult it would be.

Current

serde_json::json!({ "foo": foo, "bar": bar, "baz": baz })

Proposed

serde_json::json!({ foo, bar, baz })

closed time in 4 days

XAMPPRocky

issue commentserde-rs/json

Add object creation syntax sugar to the macro.

I think I would prefer not to build this into serde_json; but it would be possible to create a separate crate with a more flexible json! macro.

XAMPPRocky

comment created time in 4 days

issue closedserde-rs/json

Custom handling of Enum with empty variant

I am trying to bridge between a rust binary and a Flutter (dart) app using json, but I have issues with the case of empty variants in Enums.

I have a few enums in my Rust code that contain variants without anything inside them. The following example shows two types of such variants I could think of (Unit2 has an actual unit () inside of it).

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
enum ExampleEnum {
    Unit1,
    Unit2(())
}

This is the code I use to serialize the enum:

fn main() {
    let ser_str = serde_json::to_string_pretty(&ExampleEnum::Unit1).unwrap();
    println!("{}", ser_str);
    let ser_str = serde_json::to_string_pretty(&ExampleEnum::Unit2).unwrap();
    println!("{}", ser_str);
}

Output:

"Unit1"
{
  "Unit2": null
}

Unfortunately, my code on the dart side (After managing to introduce sum-types there) expects to get something like this:

{"unit": {}}

I was wondering if there is any hope for me to convince serde_json to produce this kind of output.

Thoughts about {}

The first time I have seen the above representation (the one with the {}) I thought it was somewhat strange, but a few hours later I began to think it might be an interesting idea after all.

The first representation of Unit1, as serialized from the serde_json, is not very natual in my opinion because it is very different from the representation of any other variant that contains a type.

The second representation of Unit2 has null, which is indistinguishable from a variant with Option. In other words:

enum MyEnum {
    Unit3(Option)
}

will serialize into:

{
  "Unit3": null
}

just like Unit2.

closed time in 4 days

realcr

issue closedserde-rs/json

`trait DeserializeJson`

If there was

trait FromJSON {
   fn from_json_slice(s: &[u8]) -> Result<Self> {
      serde_json::from_slice(s)
   }
   fn from_str(s: &str) -> Result<Self> { .... }
   ...
}

impl<T> FromJSON for T where T : serde::Deserialize { } 

Then we could

fn foo(data: &[u8]) -> Result {

   let t = MyType::from_json_slice(data);
   ...

}

instead of let t : MyType = serde_json::from_slice(data)?;.

closed time in 4 days

dpc

issue commentserde-rs/json

`trait DeserializeJson`

I think I would prefer not to build this into serde_json; but it would be reasonable to make a separate crate providing that.

dpc

comment created time in 4 days

issue closedserde-rs/json

Zero-copy deserialization - support deserializing slices as tuples (offset, length)

My use-case is deserializing JSON without copy, from static buffer, and then using the results later on. Rust doesn't support moving self-referencing structs, so I can't deserialize strings as slices, I think. A natural choice is to keep deserialization results as pairs of (offset, length).

Basically, whenever serve sees my type "RelativeSlice { offset: u64, length: u64}" in the model, I want it to expect "str" in JSON and deserialize that. What is the best way to define such deserialization in serde_json?

closed time in 4 days

Pand9

issue commentserde-rs/json

Zero-copy deserialization - support deserializing slices as tuples (offset, length)

Hi @Pand9, sorry that no one was able to provide more help here. If this is still an issue, you could try taking this question to any of the resources shown in https://github.com/serde-rs/serde#getting-help.

Pand9

comment created time in 4 days

issue closedserde-rs/json

Add mechanism to allow non-pretty-printing of some fields while pretty-printing parent object

There are some cases where I believe it would be useful to pretty-print an entire object, but forego the pretty-printing for a field of that object where it is more desirable for the JSON object under that field to be in one line.

Case in point, the JSON files encoded for my Vaultfile project. The basic idea is a file where people can store shared secrets using public-key cryptography.

Since vaultfiles would be meant to be added to source control, it would be useful for them to have secret values and the different public keys on separate lines. However, pretty printing the RSA public key as well makes it a bit more of a pain since it adds ~70 new lines to the file (instead of just one, as I would prefer).

Example:

{                                                                                                                                                                                                                                             
  "keys": {
    "daniel": {
      "n": [
        142549767,
        3518821673,
        621066004,
        4102229598,
        3153653357,
        2032560477,
        2589951819,
        1867303806,
        1318670661,
        997240273,
        2425225238,
        3450276547,
        1270029200,
        3043994472,
        3888620354,
        3764135483,
        3350298362,
        2226845209,
        1251720617,
        3138297150,
        475545373,
        2992032404,
        1157448551,
        3568826980,
        3247063395,
        508676771,
        3063833647,
        1941287772,
        4086787650,
        3654192563,
        2907691465,
        2740011775,
        4114212565,
        2377598490,
        4030765140,
        183734624,
        3154235505,
        2339535548,
        695197640,
        3640636449,
        2952022944,
        235284495,
        2837702651,
        2224583432,
        2976947853,
        3323400546,
        160814843,
        2196341190,
        3743805216,
        1176620751,
        3530369584,
        2564729300,
        2915362645,
        2320657427,
        3238025292,
        3775762401,
        1436812344,
        1352758797,
        1445046480,
        3924148788,
        1940390180,
        2403721693,
        723029722,
        2628567866
      ],
      "e": [
        65537
      ]
    }
  },
  "secrets": {}
}

instead of the terser:

{
  "keys": {
    "daniel": { "n": [ 142549767, 3518821673, 621066004, 4102229598, 3153653357, 2032560477, 2589951819, 1867303806, 1318670661, 997240273, 2425225238, 3450276547, 1270029200, 3043994472, 3888620354, 3764135483, 3350298362, 2226845209, 1251720617, 3138297150, 475545373, 2992032404, 1157448551, 3568826980, 3247063395, 508676771, 3063833647, 1941287772, 4086787650, 3654192563, 2907691465, 2740011775, 4114212565, 2377598490, 4030765140, 183734624, 3154235505, 2339535548, 695197640, 3640636449, 2952022944, 235284495, 2837702651, 2224583432, 2976947853, 3323400546, 160814843, 2196341190, 3743805216, 1176620751, 3530369584, 2564729300, 2915362645, 2320657427, 3238025292, 3775762401, 1436812344, 1352758797, 1445046480, 3924148788, 1940390180, 2403721693, 723029722, 2628567866 ], "e": [ 65537 ] }                                                                                                     
  },
  "secrets": {}
}

closed time in 4 days

danielthegray

issue commentserde-rs/json

Add mechanism to allow non-pretty-printing of some fields while pretty-printing parent object

I would prefer not to build this into serde_json, but the Formatter that is used for layout by the serializer is plugable (https://docs.rs/serde_json/1.0.56/serde_json/struct.Serializer.html#method.with_formatter) and you could write one which generates the layout that you showed, either via a heuristic that applies to numeric arrays or maps with one entry, or based on key path such that the caller would indicate they want keys.daniel placed on one line.

danielthegray

comment created time in 4 days

issue closedserde-rs/json

Question: Is it possible to optin or optout when deserializing to use the default values ?

I have a json string that have missing fields, but I would like to reject it in some cases and accept in others. is it possible ? If I use serde(default) it always accept the input and create a struct with the default values, but I would like to have control over it somehow, to populate with the default values or fail.

closed time in 4 days

patrickelectric

issue commentserde-rs/json

Question: Is it possible to optin or optout when deserializing to use the default values ?

Hi @patrickelectric, sorry that no one was able to provide more help here. If this is still an issue, you could try taking this question to any of the resources shown in https://github.com/serde-rs/serde#getting-help.

patrickelectric

comment created time in 4 days

issue closedserde-rs/json

serde_json breaks type inference of user code in some cases

I'm not sure whether this is serde_json's issue or rustc's. If you think this is not related tho this repo, but rather a bug in rustc, file it to them or tell me and I'll do this.

OK, now the issue. This code does not compile unless the first line is removed:

use serde_json;

#[cfg(test)]
mod test {
    #[test]
    fn into_iter() {
        assert_eq!(10, vec![1, 2, 3, 4].iter().sum());
    }
}

The problem is that compiler cannot infer types for sum(). However, removing or putting the use serde_json; into a comment makes it compile. The problem is even more strange as it brakes similar code in other modules of the same package.

I made a demo repo that shows the problem. It brakes only with_serde json. At least importing serde and failure does not brake it.

closed time in 4 days

acanthite

issue commentserde-rs/json

serde_json breaks type inference of user code in some cases

Thanks for the report -- I'll close this in favor of https://github.com/rust-lang/rust/issues/46257 / https://github.com/serde-rs/json/issues/380.

acanthite

comment created time in 4 days

issue closedserde-rs/json

Is it possible to generate *.d.ts file as well

In contract with typescript, is it possible to provide a helper method

serde_json::schema::<MyStruct>()

To a string or some kind AST?

closed time in 4 days

clouds56

issue commentserde-rs/json

Is it possible to generate *.d.ts file as well

This is not built into serde_json, but it would be reasonable to provide as a separate crate.

clouds56

comment created time in 4 days

issue closedserde-rs/json

Deserializing empty object to None

I'm trying to use serde with an API that returns empty JSON objects ({}) instead of null when an object is not available. I was wondering if it's possible to deserialize those to None?

Something like:

#[derive(Default, Debug, Clone, PartialEq, serde_derive::Serialize, serde_derive::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Wallet {
    #[serde(deserialize_to_none_if_empty_object)]
    pub owner: Option<Owner>,
}

Thhanks.

closed time in 4 days

lucasholder

issue commentserde-rs/json

Deserializing empty object to None

Hi @lucasholder, sorry that no one was able to provide more help here. If this is still an issue, you could try taking this question to any of the resources shown in https://github.com/serde-rs/serde#getting-help.

lucasholder

comment created time in 4 days

issue closedserde-rs/json

Converting JSON structure into byte array using serde JSON.

Our use case is to get JSON data structure and serialise/deserialise it to byte array. We are using no std platform for our project in rust. What is the best way to do it using serde-json?

closed time in 4 days

aastis123

issue commentserde-rs/json

Converting JSON structure into byte array using serde JSON.

Hi @aastis123, sorry that no one was able to provide help here. If this is still an issue, you could try taking this question to any of the resources shown in https://github.com/serde-rs/serde#getting-help.

aastis123

comment created time in 4 days

issue closedserde-rs/json

Deserializing property files, "a.b.c.e=value" into json

I've been battling how to do this... It is fairly common for interfaces to dump structures into a format like (java property files): root.prop1.sub1.key1=val root.prop1.sub1.key2=val root.prop1.sub2.key2=val ... Generically, it would be: key0{<ksep>keyn}<minor_sep>Value<major_sep> where ksep = Key separator, default '.' minor_sep = Minor or Key/value separator, default '=' major_sep = Major or Record/entry separator, default '\n' What would be the best way of serializing/deserializing into json using serde ? If you have a example, I can take a crack at it...

Thanks JR

closed time in 4 days

JRAndreassen

issue commentserde-rs/json

Deserializing property files, "a.b.c.e=value" into json

Hi @JRAndreassen, sorry that no one was able to provide help here. If this is still an issue, you could try taking this question to any of the resources shown in https://github.com/serde-rs/serde#getting-help.

JRAndreassen

comment created time in 4 days

issue closedserde-rs/json

Suggestion: Improve error message for this failed parse

Minimal project with repro case

When attempting to parse a simple JSON file, the attached code fails with a somewhat cryptic error message:

serde_suggestion git:(master) ✗ cargo run
   Compiling serde_suggestion v0.1.0 (/Users/tkeating/git-repos/serde_suggestion)
    Finished dev [unoptimized + debuginfo] target(s) in 0.36s
     Running `target/debug/serde_suggestion`
Error: invalid type: map, expected unit at line 1 column 1

The problem is this line: let _json = serde_json::from_reader(file)?;

This PROBABLY ought not to compile. The problem seems to be you have to give the compiler a hint as to what it should expect here: let _json: Value = serde_json::from_reader(file)?;

Presumably, it is guessing, and its guess is wrong. Adding the type hint fixes it.

closed time in 4 days

MrTact

issue commentserde-rs/json

Suggestion: Improve error message for this failed parse

I think this is a compiler bug that it doesn't require you to specify the expected type of _json. :(

Normally it requires a type:

use std::fs::File;

fn main() {
    let file = File::open("test.json").unwrap();
    let _json = serde_json::from_reader(file).unwrap();
}
error[E0282]: type annotations needed
 --> src/main.rs:5:9
  |
5 |     let _json = serde_json::from_reader(file).unwrap();
  |         ^^^^^ consider giving `_json` a type
MrTact

comment created time in 4 days

issue closedserde-rs/json

Serializing std::fs::Metadata

Hey all,

I'm trying to add the std::fs::Metadata struct as a member of a struct for which I am deriving serialization and serde doesn't seem to like it. I'm seeing errors like

  --> src/lib.rs:43:5
   |
43 |     meta: Metadata,
   |     ^^^^ the trait `_IMPL_SERIALIZE_FOR_Fileinfo::_serde::Serialize` is not implemented for `std::fs::Metadata`
   |
   = note: required by `_IMPL_SERIALIZE_FOR_Fileinfo::_serde::ser::SerializeStruct::serialize_field`

I've made a small struct to test this

#[derive(Debug, Serialize)]
pub struct test{
    meta: Metadata,
}

Is the metadata struct simply not serializable at the moment? If so, why not? If not, am I missing something?

closed time in 4 days

darakian

issue commentserde-rs/json

Serializing std::fs::Metadata

Correct, there isn't a serde Serialize impl for std::fs::Metadata.

If you need to serialize one (keeping in mind that deserializing won't work, because there is no way to construct a std::fs::Metadata with specific values in it) then it would be reasonable to ask for that behavior from a helper crate, such as https://github.com/jonasbb/serde_with.

darakian

comment created time in 4 days

issue commentserde-rs/json

implement into_array, into_object, and into_string for serde_json::Value

I think I would prefer not to provide these in serde_json. Thanks anyway!

Maan2003

comment created time in 4 days

issue closedserde-rs/json

skip null for Value generated by json!

I know there's #[serde(skip_serializing_if = "Option::is_none")] but how would one do the same thing for values generated with json!?

closed time in 4 days

kahing

issue closedserde-rs/json

Add to_bytes and from_bytes

Seems weird that techempower did a bunch of extra code just to convert struct to bytes, it would be nice to have a feature parity to encode/decode from bytes as for string.

As well, I wonder why generic like From<Bytes> are not used for Serialization.

https://github.com/TechEmpower/FrameworkBenchmarks/pull/3752#discussion_r274387900

closed time in 4 days

pickfire

issue commentserde-rs/json

Add to_bytes and from_bytes

I think I would prefer not to build this into serde_json, but it would be possible for an external crate to provide these signatures as a convenience around the Serializer / Deserializer API exposed by serde_json.

pickfire

comment created time in 4 days

issue closedserde-rs/json

PrettyFormatter - support Windows line endings

Currently when trying to pretty print JSON, it uses Unix line endings, even on Windows. It would be useful to add an option to PrettyFormatter to choose the type of line ending. I also think it would be useful to default to the platform default, so that to_writer_pretty behaves as expected on Windows.

closed time in 4 days

jonathanmcelroy

issue commentserde-rs/json

PrettyFormatter - support Windows line endings

I think I would prefer not to build this configuration into serde_json, but it would be reasonable to provide an alternative pretty formatter in a different crate for people that need this.

jonathanmcelroy

comment created time in 4 days

issue closedserde-rs/json

Feature request: TryInto implementation for serde_json::Value

The TryInto trait allows for conversions that can fail.

Currently, there is generic casting from various values to serde_json::Value, but not the other way around. Since there are already the explicit .as_f64() casting methods, they could be used to implement the TryInto trait.

closed time in 4 days

ghost

issue commentserde-rs/json

Feature request: TryInto implementation for serde_json::Value

I think I would prefer not to provide these impls.

But fortunately serde_json::Value isn't anything special to serde_json, it is just some type with a Serialize and Deserialize impl. That means an external crate could provide an alternative Value implementation with more or different impls depending on what different people want.

ghost

comment created time in 4 days

issue closedserde-rs/json

Serialization works for PathBuf (which is built on OsString) but not for OsString itself

serde_json appears to handle PathBuf's just fine. The output for OsString looks like some internal rust representation rather than an actual string. Expected it to work for OsString because PathBuf is build on OsString

    #[test]
    fn test_osstr_ser() {
        #[derive(Serialize, Deserialize)]
        struct Foo {
            p: PathBuf,
            f: OsString
        }

        let foo = Foo {
            p: PathBuf::from("/etc/passwd"),
            f: OsString::from("passwd")
        };

        println!("output: {}", serde_json::to_string(&foo).unwrap());
    }

expected:

output: {"p":"/etc/passwd","f": "passwd"}

actual results:

output: {"p":"/etc/passwd","f":{"Unix":[112,97,115,115,119,100]}}

closed time in 4 days

paul-tcell

issue commentserde-rs/json

Serialization works for PathBuf (which is built on OsString) but not for OsString itself

https://github.com/dtolnay/request-for-implementation/issues/48 recommends how to control the bytestring JSON encoding. There are some incomplete implementations; someone would need to finish one up and publish to crates.io. But it gives the ability to serialize them in base64, or hex, or potentially utf8-with-escape-sequences.

paul-tcell

comment created time in 4 days

issue commentserde-rs/json

Struct with named fields can be deserialized from sequence

Thanks for the issues. I will close this in favor of https://github.com/serde-rs/serde/issues/1587.

portstrom

comment created time in 4 days

issue closedserde-rs/json

Floats are serialized with unnecessary decimal place

With this struct definition:

#[derive(Serialize)]
struct MyStruct {
    a: f64,
    b: f64,
}

Instantiated like this:

MyStruct {
    a: 1.0,
    b: 1.2
}

serde_json serializes the struct like this:

{
  "a": 1.0,
  "b": 1.2
}

But I'd expect the serialized output to be:

{
  "a": 1,
  "b": 1.2
}

Even though the JSON that serde produces is technically valid, it's different from how most other languages serialize floats, and it's even different from how rust's built-in formatter formats floats: format!("{}", 1.0f64) returns 1.

closed time in 4 days

nwoltman

issue commentserde-rs/json

Floats are serialized with unnecessary decimal place

This is intentional, but can be customized by providing a Formatter impl with the behavior you want. https://docs.rs/serde_json/1.0.56/serde_json/struct.Serializer.html#method.with_formatter

nwoltman

comment created time in 4 days

issue closedserde-rs/json

Deserializing internally tagged enum: error line information inaccurate

When failing to parse inner values of an internally tagged enum, the error line information points to the wrong place. The issue does not occur with externally tagged enums.

Issue submitted against serde, as I expect that it is mostly related to serde_derive, but I am submitting it here too as I don't really know where this line information is assigned or where a possible solution could be implemented.

https://github.com/serde-rs/serde/issues/1621

closed time in 4 days

karlri

issue commentserde-rs/json

Deserializing internally tagged enum: error line information inaccurate

Thanks for the issues. I will close this in favor of https://github.com/serde-rs/serde/issues/1621.

karlri

comment created time in 4 days

issue closedserde-rs/json

Question: Extending errors

Hi,

I would like to be able to get Errors which I can convert into the following:

{
  "results": [
    {
      "resultType": "SOME_ERROR",
      "description": "Json deserialize error: invalid value: floating point `-79.53480529785156`, expected an easting value between -180 and 180 at line 1 column 366"
    }
  ]
}

However this means that I need to be able send three bits of information from the error site rather than two (I need to get the standard description AND the resultType error code.

If just two are sent then I would use a resultType of "ERROR"

I am currently doing validation like this:

[serde(deny_unknown_fields)]
#[derive(Debug, Serialize, Deserialize)]
pub struct Coordinates {
    #[serde(deserialize_with = "easting_validator")]
    easting: f32,
    #[serde(deserialize_with = "northing_validator")]
    northing: f32,
}

fn easting_validator<'de, D>(d: D) -> Result<f32, D::Error>
where
    D: de::Deserializer<'de>,
{
    let value = f32::deserialize(d)?;

    if value < -10.0 || value > 10.0 {
        return Err(de::Error::invalid_value(
            de::Unexpected::Float(value.into()),
            &"an easting value between -180 and 180",
        ));
    }

    Ok(value)
}

Is there some nice way of doing this?

closed time in 4 days

jamessewell

issue commentserde-rs/json

Question: Extending errors

Hi @jamessewell, sorry that no one was able to provide more help here. If this is still an issue, you could try taking this question to any of the resources shown in https://github.com/serde-rs/serde#getting-help.

jamessewell

comment created time in 4 days

issue closedserde-rs/json

Consider adding async methods

With the upcoming stabilization of async/await, it would be nice to add a method that would allow developers to leverage the async ecosystem.

The goal seems to be to make futures (now futures-preview) the go-to library that everyone builds upon*.

Seeing as:

  • async/await is not to be stabilized before 1.39,
  • and not everyone might want to use this feature,
  • and the minimum required version of serde would have to be raised I think it would be wise to put this behind a feature flag, e.g. async or async-std

This would add methods to (names are bikesheddable):

  • serde_json::: from_async_reader, to_async_writer and to_async_writer_pretty
  • serde_json::Serializer::: new_async, pretty_async, with_async_formatter
  • serde_json::Deserializer::: new_async, from_async_reader

*) Judging from async_std::fs::File which implements async_std::io::read which is a re-export of futures::io::AsyncRead

closed time in 4 days

VictorKoenders

issue commentserde-rs/json

Consider adding async methods

I think I would prefer not to build this into serde_json, but I would be interested to see someone else develop an async-first JSON library.

VictorKoenders

comment created time in 4 days

issue closedserde-rs/serde

Custom Deserializing: Ignoring some fields

I'm using Visitor to deserialize a struct flowing this implementation https://serde.rs/deserialize-struct.html

I received more than ten fields, but I just want four fields.

How do I to ignore the others fields?

pub struct OrderStateEvent {
    side: String
}

impl<'de> Deserialize<'de> for OrderStateEvent {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        enum Field { Side };

        impl<'de> Deserialize<'de> for Field {
            fn deserialize<D>(deserializer: D) -> Result<Field, D::Error>
            where
                D: Deserializer<'de>,
            {
                struct FieldVisitor;

                impl<'de> Visitor<'de> for FieldVisitor {
                    type Value = Field;

                    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                        formatter.write_str("`Side`")
                    }

                    fn visit_str<E>(self, value: &str) -> Result<Field, E>
                    where
                        E: de::Error,
                    {
                        match value {
                            "Side" => Ok(Field::Side),
                            _ => Err(de::Error::unknown_field(value, FIELDS)),
                        }
                    }
                }

                deserializer.deserialize_identifier(FieldVisitor)
            }
        }

        struct OrderStateEventVisitor;

        impl<'de> Visitor<'de> for OrderStateEventVisitor {
            type Value = OrderStateEvent;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("struct OrderStateEvent")
            }

            fn visit_seq<V>(self, mut seq: V) -> Result<OrderStateEvent, V::Error>
            where
                V: SeqAccess<'de>,
            {
                let side = seq.next_element()?
                    .ok_or_else(|| de::Error::invalid_length(0, &self))?;
                Ok(OrderStateEvent{side})
            }

            fn visit_map<V>(self, mut map: V) -> Result<OrderStateEvent, V::Error>
            where
                V: MapAccess<'de>,
            {
                let mut side = None;
                while let Some(key) = map.next_key()? {
                    match key {
                        Field::Side => {
                            if side.is_some() {
                                return Err(de::Error::duplicate_field("side"));
                            }
                            side = Some(map.next_value()?);
                        }
                    }
                }
                let side = side.ok_or_else(|| de::Error::missing_field("side"))?;
                Ok(OrderStateEvent{side})
            }
        }

        const FIELDS: &'static [&'static str] = &["side"];
        deserializer.deserialize_struct("OrderStateEvent", FIELDS, OrderStateEventVisitor)
    }
}

closed time in 4 days

jonnovaretti

issue commentserde-rs/serde

Custom Deserializing: Ignoring some fields

Hi @jonnovaretti, sorry that no one was able to provide help here. If this is still an issue, you could try taking this question to any of the resources shown in https://github.com/serde-rs/serde#getting-help.

jonnovaretti

comment created time in 4 days

issue closedserde-rs/serde

Deserialize map value which is a string containing space to a vector of strings

Hello! I need to deserialize the following json into a struct:

{
  "blocks": {
    "field1": ["142A538F36833D1CC78B94E11C766F75818F8B940771335C6C1B8AB880C5BB1D"],
    "field2": ["4C1FEEF0BEA7F50BE35489A1233FE002B212DEA554B55B1B470D78BD8F210C74"]
  }
}

I tried this, but doesn't work:

#[derive(Deserialize, Debug)]
struct WalletPending {
    blocks: HashMap<String, Vec<Option<String>>>,
}

The arrays can have more than one element or be empty and there can be more "fields". How can I do this? Thanks!

closed time in 4 days

Fiono11

issue closedserde-rs/serde

Deserialize from sequence or map

How might I write a Deserialize implementation that allowed my type to be deserialized from a sequences of values or a map of values?

struct Values(Vec<T>);

assert_eq!(&serde_json::from_str::<Values>(r#"{ "foo": 89 }"#).0, [89]);
assert_eq!(&serde_json::from_str::<Values>(r#"[89]"#).0, [89]);

closed time in 4 days

Plecra

issue closedserde-rs/serde

duplicate json tag field for enum/struct tags

Now we can define tags on structs (which is a great thing), but in the cases you need tags both on enums and structs, there is a duplicate type tag in JSON now:

#[serde(tag = "type", rename = "s1tag")]
#[derive(Debug,Clone,Serialize,Deserialize)]
struct S1 {
    text : String
}

#[serde(tag = "type", rename = "s2tag")]
#[derive(Debug,Clone,Serialize,Deserialize)]
struct S2 {
    text : String
}

#[serde(tag = "type")]
#[derive(Debug,Clone,Serialize,Deserialize)]
enum E {
    #[serde(rename = "s1tag")]
    S1V(S1),
    #[serde(rename = "s2tag")]
    S2V(S2)
}

fn main() {

    let s1 = S1 {
        text : String::from("test str")
    };

    let s2 = S2 {
        text : String::from("test str")
    };

    let e1 = E::S1V(s1.clone());
    let e2 = E::S2V(s2.clone());

    let s1_ser = serde_json::to_string(&s1).unwrap();
    let s2_ser = serde_json::to_string(&s2).unwrap();
    let e1_ser = serde_json::to_string(&e1).unwrap();
    let e2_ser = serde_json::to_string(&e2).unwrap();

    println!("serialized = {} from {:?}", s1_ser, s1);
    println!("serialized = {} from {:?}", s2_ser, s2);
    println!("serialized = {} from {:?}", e1_ser, e1);
    println!("serialized = {} from {:?}", e2_ser, e2);

}

resulted in

serialized = {"type":"s1tag","text":"test str"} from S1 { text: "test str" }
serialized = {"type":"s2tag","text":"test str"} from S2 { text: "test str" }
serialized = {"type":"s1tag","type":"s1tag","text":"test str"} from S1V(S1 { text: "test str" })
serialized = {"type":"s2tag","type":"s2tag","text":"test str"} from S2V(S2 { text: "test str" })

I've tried to find a workaround with untagged enums like:

#[serde(untagged)]
#[derive(Debug,Clone,Serialize,Deserialize)]
enum EU {
    S1V(S1),
    S2V(S2)
}

but it won't help because you can't decode enums properly anymore (tags on structs won't be used for enum variations).

Looks like a bug, isn't?

closed time in 4 days

abdolence

issue closedserde-rs/serde

DeserializeOwned of Cow/ToOwned types

Hi

Is it possible to deserialize to the ownable version of a ToOwned type like a Cow?

(I would like to avoid duplicating a type hierarchy if possible, while still defaulting to borrowed version)

#[derive(Debug, Serialize, Deserialize)]
struct Person<'a> {
    #[serde(borrow)]
    name: Cow<'a, str>,
}

fn get_person<T>(data: String) -> Result<T, serde_json::error::Error>
where
    T: de::DeserializeOwned,
{
    let mut reader = BufReader::new(Cursor::new(&data));
    serde_json::from_reader(reader)
}

fn main() -> Result<(), Box<dyn Error>> {
    let data = r#"
        {
            "name": "John Doe"
        }"#;

   /// the borrowed version
    let p: Person = serde_json::from_str(data)?;
/// the owned version, doesn't work :(
    let p: Person = get_person(data.into())?

    Ok(())
}

thanks!

closed time in 4 days

elmarco

issue commentserde-rs/serde

DeserializeOwned of Cow/ToOwned types

Hi @elmarco, sorry that no one was able to provide help here. If this is still an issue, you could try taking this question to any of the resources shown in https://github.com/serde-rs/serde#getting-help.

elmarco

comment created time in 4 days

issue closedserde-rs/serde

Implementing Ser/Deser for specific Ser/Deser-ser. Can not call type_of::<S/D>

Hello. I need implement serialization and deserialization with specific Serializer and Deserializer(bincode). If user uses other deserializer, program wont be compiled or panic will be occured.

Where are two ways:

  • Implement ser\deser with specific Serializer or Deserializer. I do not know how to do it(allows rust do it?) And how to impl for excepted serializers(This is near to negative traits)
  • Get TypeId of Serializer or Deserializer, match with supported serializer/deserializer, then downcast(transmute), otherwise panic. But I can not do it: compiler says: the parameter type S may not live long enough but i can not mark S:Serialzier + 'static. But I can use type_name::<S>(), and it says, that S is not SupportedSerializer, it is &mut SupportedSerializer. So i need transmute it as reference.

Code:

impl Serialize for MyType{
    fn serialize<S:Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        use serde::ser::SerializeStruct;
        use std::any::TypeId;
        use std::any::Any;
        use std::any::type_name;

        //Do not works
        serializer.type_id();
        //Do not works
        TypeId::of::<S>();

        //It works
        println!("{} == {}",type_name::<S>(), type_name::<SupportedSerializer>());

        let sser:&mut SupportedSerializer= unsafe {//maybe works
            std::mem::transmute(serializer)
        };

        //Temporary: Just to compile it
        let mut s = serializer.serialize_struct("MyType", 1)?;
        s.serialize_field("content", &8usize)?;
        s.end()
    }
}

Have you ideas, how to solve this problems?

closed time in 4 days

TrionProg
more