GithubHelp home page GithubHelp logo

Comments (10)

oli-obk avatar oli-obk commented on July 21, 2024

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.

rohitjoshi avatar rohitjoshi commented on July 21, 2024

@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.

  1. 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> {}

  2. 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()),
      }
     }
    
  3. 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.

oli-obk avatar oli-obk commented on July 21, 2024

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.

rohitjoshi avatar rohitjoshi commented on July 21, 2024

@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.

oli-obk avatar oli-obk commented on July 21, 2024

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.

rohitjoshi avatar rohitjoshi commented on July 21, 2024

@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.

rohitjoshi avatar rohitjoshi commented on July 21, 2024

@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.

softprops avatar softprops commented on July 21, 2024

was there any resolution on this?

from rustc-serialize.

rohitjoshi avatar rohitjoshi commented on July 21, 2024

No but I had forked and implemented custom function.

from rustc-serialize.

alexcrichton avatar alexcrichton commented on July 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.