naomijub / edn-rs

[DEPRECATED]: Crate to parse and emit EDN
https://crates.io/crates/edn-rs
MIT License
81 stars 14 forks source link

Convert from edn to JSON (or to any serde format) #63

Closed TheButlah closed 4 years ago

TheButlah commented 4 years ago

edn-rs currently has the ability to convert from json to edn, but not the other way around. It would be really nice to be able to convert from edn to json. Other formats would be really nice too - perhaps Edn could implement serde's Serialize trait?

P.S. is the reason serde isn't used right now because the data model of the two is different? If so, could you elaborate a bit on the details? Related: https://github.com/otaviopace/edn-derive/issues/29

naomijub commented 4 years ago

Hello The reason we did not create a function to convert from edn to json is that edn is way more complex than json, what we could do now is to map some types to a a json structure. Maybe we can have something by the end of the weekend. The current way we convert from edn to json is by attaching both derives and generating the new structure, like in brcode and below:

// pseudo-code
use edn_derive::{Deserialize, Serialize};
use serde_derive::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize};

let json_str = "{\"hello\": \"world\",\"others\":[1, 2,3, 4, 5]}";
let edn_str = "{:hello \"world\" :others [1, 2, 3, 4, 5]}"

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, SerdeDeserialize, SerdeSerialize)]
struct Hello {
   hello: String,
   others: Vec<i32>
}

let hello: Hello = edn_rs::from_str(edn_str)?;
let json = serde_json::to_string(hello)?;

assert_eq!(json, json_str);

About serde, this crate is not an extension of serde but can be used together. I promisse to get back Sunday with a to_json solution.

naomijub commented 4 years ago

Hello @TheButlah, can you check this branch out? use feature json with it please. I would love some feedback

evaporei commented 4 years ago

Hi @TheButlah :slightly_smiling_face: Love to see people using our lib :blush:

So I am a bit confused on your use case, can you show an example of code of what you want to do with the Edn structure to convert it to JSON?

TheButlah commented 4 years ago

@otaviopace See https://github.com/otaviopace/edn-derive/issues/29#issuecomment-702991382

naomijub commented 4 years ago

Here is a PR with docs that I hope can help you https://github.com/naomijub/edn-rs/pull/64

naomijub commented 4 years ago

will close this as the PR will be merged and there was no more updates. version 0.16.7 has to_json function