serde_token
provides a utility function tokenize
for transcoding a Serde deserializer into a futures::Sink
of Token
s.
Install from Crates.io:
[dependencies]
serde_token = "0.0.2"
use futures::{unsync::mpsc::unbounded, Future, Sink, Stream};
use serde_json::Deserializer;
use serde_token::{tokenize, Token};
let mut de = Deserializer::from_str(r#" [ {"a":false}, "hello", 3 ] "#);
let (token_sink, token_stream) = unbounded::<Token>();
tokenize(&mut de, token_sink).unwrap();
let expected = token_stream.collect().wait().unwrap();
assert_eq!(expected, vec![
Token::Seq { len: None },
Token::Map { len: None },
Token::Str("a"),
Token::Bool(false),
Token::MapEnd,
Token::Str("hello"),
Token::U64(3),
Token::SeqEnd,
])
tokenize
takes in a serde::Deserializer
, which will walk thru the encoded input, and a futures::Sink
, which will be sent the deserialized tokens. Two things to note:
- Under the hood, we use
serde_transcode
to directly map the givenDeserializer
to theToken
Serializer
. However,serde_transcode
"drives" the transcoding process withdeserialize_any
, meaning that encoding formats that require type hints cannot be tokenized without an intermediary step (not provided by this library). - Because of the aforementioned limitation, the
Tokenizer
struct, which is private and can only be used with the providedDeserializer
, usesunsafe
twice toserialize
tokens that borrow their underlying data from theDeserializer
(Token::Str(&'de str)
andToken::Bytes(&'de [u8])
). However, this should be safe because the tokens share the same lifetime as the providedDeserializer
and should thus expire no later than theDeserializer
itself.
- look into how we use the
Sink
:- should probably periodically
poll_complete
in case the sink is bounded - add support for back-pressure
- handle
Async::NotReady
appropriately
- should probably periodically
- provide better guarads around our use of
unsafe
Version | Change Summary |
---|---|
v0.0.2 | adds README |
v0.0.1 | initial release |
- Fork it https://github.com/your_username/serde_token/fork
- Create your feature branch (
git checkout -b feature/fooBar
) - Commit your changes (
git commit -am 'Add some fooBar'
) - Push to the branch (
git push origin feature/fooBar
) - Create a new Pull Request
- Sunny G - @sunny-g
MIT