Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extra::json doesn't support using Encodable to encode straight to a json value #8335

Closed
huonw opened this issue Aug 6, 2013 · 5 comments
Closed

Comments

@huonw
Copy link
Member

huonw commented Aug 6, 2013

If one wants to convert an Encodable type to a Json value, then one has to do something like (i.e. encode as JSON to a string and then parse that string):

let foo = something_to_convert_to_json;

let s = do std::io::with_str_writer |w| {
    foo.encode(&mut extra::json::Encoder(w));
};
let json = match extra::json::from_str(s) {
    Ok(j) => j,
    Err(_) => fail!("Rust output some json it doesn't understand itself.")
};
@graydon
Copy link
Contributor

graydon commented Aug 6, 2013

Yes, this struck me as very weird

@catamorphism
Copy link
Contributor

Triage bump

@seanmonstar
Copy link
Contributor

So, I started to work on this, trying to do something like this:

impl <A:Encodable> ToJson for A {
  fn to_json(&self) -> ~Json {
    ToJsonEncoder::new().json_encode(self)
  }
}

struct ToJsonEncoder {
// ...
}
impl serialize::Encoder for ToJsonEncoder {
// ...
}

The ToJsonEncoder would convert to a Json object, instead of to strings. However, since a user can write their own implementation of Encodable, it's not guaranteed that an Encodable will emit all that is needed to make JSON.

For instance:

struct Foo {
  bar: ~str,
  baz: uint
}
impl<E: Encoder> Encodable<E> for Foo {
  fn encode(&self, e: &mut E) {
    e.emit_str(self.bar);
    e.emit_uint(self.baz);
  }
}

This Foo has defined its own way to encode itself, and a ToJsonEncoder will not be able to format a String + Number as Json. The ToJsonEncoder would need to keep some state, and fail!() if the Encodable emits values in a JSON-illegal order.

This leaves me wondering if extra::serialize gives too much freedom in how it can be implemented.

bors added a commit that referenced this issue Dec 23, 2014
…richton

importing object type string key maps is still supported
writing them should be explicit, and can be done as follows

```rust
let some_tree_map : TreeMap<String, Json> = ...;
Json::Object(some_tree_map).to_writer(&mut writer);
```

related to #8335, #9028, #9142
bors added a commit that referenced this issue Jan 19, 2015
importing object type string key maps is still supported
writing them should be explicit, and can be done as follows

```rust
let some_tree_map : TreeMap<String, Json> = ...;
Json::Object(some_tree_map).to_writer(&mut writer);
```

related to #8335, #9028, #9142
@alexcrichton
Copy link
Member

cc @nick29581, could this move to rust-lang/rustc-serialize?

@rust-highfive
Copy link
Collaborator

This issue has been moved to the rustc-serialize repo: rust-lang-deprecated/rustc-serialize#46

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants