cosmos-rust
cosmos-rust copied to clipboard
adding support for json
messages in the tx are encoded with protobuf,
need to know class first to decode by typeurl
if those messages can be parsed into serde_json, it would be easy to display how about adding serde json support in compiling rust files from proto files? (in prost)
We'd definitely like to add JSON support. Some complicating factors are a planned transition to "Proto JSON" from the previous "Amino JSON" was put on hiatus, or at least that's my understanding, but it may be out-of-date.
It'd be good to examine current and future strategies for JSON support in tendermint-rs so we can align with what they're doing, and ideally reuse some of their work:
https://github.com/informalsystems/tendermint-rs/issues/654
@tony-iqlusion Can we use pbjson for this?
@devashishdxt unfortunately it seems like the JSON that's required (e.g. by ibc-rs/Hermes) differs from the standard Protobuf JSON, or at least that's my understanding.
See ample discussion on https://github.com/informalsystems/tendermint-rs/issues/654
What if we add struct attributes ? See this link : https://github.com/tokio-rs/prost/issues/75#issuecomment-365371756
If there's consensus that Cosmos SDK will eventually migrate to Protobuf JSON, then I'm fine with using something like pbjson like @devashishdxt recommended.
relevant cosmwasm discussion https://github.com/CosmWasm/cosmwasm/issues/1448
Any updates on this? I'm trying to build a cosmos-ledger library and running into a wall. I need to be ale to convert an Any into JSON to display on the ledger, and can't figure out how to move forwards.
As a side note, is there currently any workaround I can use to get this functionality? I've been trying for an hour or two and I can't seem to come up with a way to get json from an Any, or any of the other message types for that matter.
Any contains a serialized proto, so you won't ever be able to just "convert it into JSON".
Instead you'll first need to convert it to the appropriate concrete type for a type_url, and then once you have an actual protobuf type, that type can potentially be serialized by pbjson.
Again, that's something we can potentially support, although AFAIK everything else is currently using the legacy Amino JSON so it won't provide interop. But it would work for your kind of use case, or if someone wanted to build a JSON RPC API.
Yes sorry I wasn't completely clear, I don't need to convert to json for Any, but rather for all of types that are Msg. Even just #[derive(Serialize)] would work for my use case. Would it be useful to submit a PR with Serialize, Deserialize, derived on all of the Msg types in cosmrs? Could put it behind a feature gate as well.
No, we don't want to add a bespoke JSON format. It really needs to be pbjson or Amino JSON, and I would generally prefer not to support the latter except as a last resort if a Protobuf JSON migration upstream falls through as it will require a lot of manual work
Gotcha, so am I stuck with re-deriving all the Msg structs myself? Or do you see another way for me to achieve that?
A PR adding pbjson support seems reasonable to me
I have no experience with that library and I'm not really sure what that would entail. Which types in particular should pbjson be applied to? Would this just apply to the types in cosmrs? or are you talking about all the proto definitions?
It automatically builds serde-serializable structs for all types in the Protobuf schema, whose structure conforms to the Protobuf JSON Mapping
https://docs.rs/pbjson-build/latest/pbjson_build/