There are two ways to encode values into json in string form, and they can provide different results or be wrong:
- ToJson transforms a rust value to Json, which can be encoded by a
serialize::Encoder
json::Encoder implements the Encoder trait, so you can give it arbitrary values, as long as they implement Encodable, and it will output json, or json-like text.
We need to provide a single method of encoding to Json, and make sure we use serialize correctly if we use it.
The ToJson trait expresses the restriction that only maps with string keys can be encoded to Json. This is easy to step around by using the other json encoding method, which results in invalid json.
There are two ways to encode values into json in string form, and they can provide different results or be wrong:
serialize::Encoderjson::Encoderimplements theEncodertrait, so you can give it arbitrary values, as long as they implementEncodable, and it will output json, or json-like text.We need to provide a single method of encoding to Json, and make sure we use
serializecorrectly if we use it.The ToJson trait expresses the restriction that only maps with string keys can be encoded to Json. This is easy to step around by using the other json encoding method, which results in invalid json.