Comments (10)
I suggest using Json::from_str or from_reader since your issue is Json-specific and and afaik not what Encodable/Decodable are to be used for.
from rustc-serialize.
@oli-obk Issue is with limitation in the Decoder trait interface which doesn't allow to access underneath object for custom manipulation.
I tried using Json::from_str which doesn't work due to json string can have variable type of the value. e.g alert key value could be string or dictionary as per Apple Push Notification specifications.
e.g { "alert" : "Hello world!!" }
or { "alert" : {"body": "Hello, world!", "action-loc-key": "Open"} }
I think this can be resolve by 3 way.
-
rustc_serialize::Decoder trait expose a method to return underline type. Not sure Rust would alllow this. e.g
fn read<T>(&mut self)-> DecodeResult<T> {}
-
Modify read_str() in json.rs as below which will return string representation of the object
fn read_str(&mut self) -> DecodeResult<string::String> { //expect!(self.pop(), String) match self.pop() { Json::String(v) => Ok(v), value => Ok(value.to_string()), } }
-
Add read_json() method for Decoder implementation for Json
`
impl Decoder {
fn pop(&mut self) -> Json {
self.stack.pop().unwrap()
}
// new method
pub fn read_json(&mut self) -> Json {
self.stack.pop().unwrap()
}
}
`
from rustc-serialize.
even if you use the 3rd version. The Decodable has no access to the actual decoder, only to the trait. Without negative trait bounds this won't work (not sure if it works with them)
The 1st version requires specialization of generics (which would allow a full rewrite to a nicer interface to the serialization lib).
The 2nd version breaks the contract with the function and would only serve your special case.
from rustc-serialize.
@oli-obk Thanks for the details. I am new to Rust and trying to understand.
Regarding 2nd version, instead of modifying existing read_str()
, we might want to add read_as_string()
which sets expectation of getting converted string response.
from rustc-serialize.
read_as_string() would not make sense in a binary decoder though. As the Binary might be something not representable as a string.
Basically you should assume that Decoder can only decode types with a derived RustcEncodable
trait.
from rustc-serialize.
@oli-obk I guess it would be same issue for existing read_str()
method in Decoder trait for binary decoder.
Don't you think suggested option 2 is better which will return string representation of object rather than returning error at runtime.
I wish Rust supported selective override of trait method. :)
from rustc-serialize.
@alexcrichton do you think some support will be added to either pop json object or read_str() will stringify the object and return as string. If you advise me one of these option, I can submit a PR.
from rustc-serialize.
was there any resolution on this?
from rustc-serialize.
No but I had forked and implemented custom function.
from rustc-serialize.
I'm going to close this now that this crate is deprecated in favor of serde. We're discontinuing feature development in rustc-serialize but will still continue to merge bug fixes if they arise.
from rustc-serialize.
Related Issues (20)
- getting the hex outta here HOT 2
- Segfault on Decode HOT 2
- "no method named `into_object` found for type `rustc_serialize::json::Json` in the current scope" HOT 2
- Implement Encodable/Decodable traits for [T; 32+] HOT 3
- Segfault with only safe code HOT 4
- missing fields
- JSON decoding errors when reading string from stream HOT 2
- JSON Parsing Tests by seriot.ch HOT 1
- RFC: Allow formatting JSON with given maximum indentation level HOT 2
- 0.3.20 is not backward-compatible HOT 3
- Crates.io documentation link is broken HOT 1
- Crate publish HOT 2
- Implement ToHex (and friends) for AsRef HOT 1
- SocketAddr/IpAddr HOT 4
- from_hex should accept odd length slices HOT 1
- Add a comparison with serde to the documentation HOT 5
- Documentation links are broken HOT 1
- Add `#[deprecated]` to the crate root (lib.rs) so that everyone using this get warnings.
- Base64: invalid trailing characters when last line ending is near trailer
- Yank pre-0.3.20 versions
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rustc-serialize.