News for hackers. Stuff that matters.
Re:The Serde Rust Framework by roca on Thursday October 14, 2021 @06:01PM (#28870769)
Fair enough. I have not found that to be true in C/C++, which is my main point of comparison. For very simple cases it's generally easier to write a few lines to a text file, maybe with some whitespace separators in the lines, than to use an "real" format.

For JS and Python, JSON.stringify/parse and json.loads/dumps are a step up, but you still end up with an untyped mess with no schema validation, which makes them only halfway solutions to me. I'm a static typing guy at heart, sue me.

Re:The Serde Rust Framework by eptcyka on Thursday October 14, 2021 @06:19PM (#28870961)
Yea, but with serde, you don't get an untyped mess. You either get the type you expected or you get an error.
Re:The Serde Rust Framework by vips7L on Thursday October 14, 2021 @07:08PM (#28871437)
Same is true in any typed language.
Re:The Serde Rust Framework by spoiler on Thursday October 14, 2021 @08:39PM (#28872337)
I don't think that's true. Or at least we have different ideas about what's being discussed...

Let's take JSON or YAML for example:

Rust's closest siblings C and C++ are typed, and you get a pretty much untyped messes when working with serialisation or deserialisation. You have to manually inspect every node or write manual ”NodeType" to struct conversation. They allow unwrapping to primitives at best (eg via template specialisation).

In Haskell it's a bit more awkward, but you can kinda pull something similar off in terms of ergonomics. I've not worked with JSON in Haskell that much that I need to look for it, but I don't remember there being something as ergonomic as serde.

Typescript doesn't support this type of validation either out of the box (there might be tools that add validation).

In Elixir there's the Poison library, or it could be done with Kernel.struct/2 (not 100% sure this will work though).

Of the "mainstream" languages Go is pretty much the only one that has as good ergonomics as Rust in that it supports it pretty much natively (via struct tags)

---

This list excludes codegen tools which can generate (de)serialisers from an external schema (a la capnp, grpc, jsonschema, etc).

Re:The Serde Rust Framework by dwohnitmok on Thursday October 14, 2021 @11:27PM (#28873873)
> In Haskell it's a bit more awkward, but you can kinda pull something similar off in terms of ergonomics. I've not worked with JSON in Haskell that much that I need to look for it, but I don't remember there being something as ergonomic as serde.

I think Haskell's most popular JSON library aeson is basically the same thing and probably a source of inspiration for serde.

  data MyCustomType = ... deriving (Generic, ToJSON, FromJSON)
or if you want more control you can use deriving-aeson

  -- I prefer snake case instead
  data MyCustomType = ...
    deriving Generic
    deriving (FromJSON, ToJSON)
    via CustomJSON '[FieldLabelModifier CamelToSnake]
Re:The Serde Rust Framework by spoiler on Friday October 15, 2021 @03:04AM (#28875143)
Ah, I've heard about aeson, but not used it. Thanks for pointing it out!
Re:The Serde Rust Framework by rudedogg on Thursday October 14, 2021 @10:08PM (#28873246)
> Of the "mainstream" languages Go is pretty much the only one that has as good ergonomics as Rust in that it supports it pretty much natively (via struct tags)

Swift does this too, using the Codable Protocol. The compiler generates the protocol conformance code for you, if you say a type is `Codable`, and the properties the type contains are all Codable types (Int, Float, String, URL, etc. conform to it), you don't have to do anything.

https://developer.apple.com/documentation/foundation/archive...

Re:The Serde Rust Framework by spoiler on Friday October 15, 2021 @03:08AM (#28875166)
Ah, I don't use Apple devices, so I've never been able to play with Swift(1). So I didn't know Swift had it too; thank you for pointing it out!

(1): which is a shame, I remember when it came out that it seemed like a nice language

Re:The Serde Rust Framework by sli on Friday October 15, 2021 @05:11AM (#28875802)
Swift is available for Windows 10 and a few Linux distros (Ubuntu, Cent, and Amazon Linux 2) according to their download page.[0] If you're still interested, go give it a shot.

[0]: https://swift.org/download/

Re:The Serde Rust Framework by UK-Al05 on Thursday October 14, 2021 @09:23PM (#28872751)
Most typed languages it's like this

var account = JsonConvert.DeserializeObject<Account>(string/stream)

That's it. No inspection or looping through trees. You can often get it to create objects via constructor for validation.

It's been like this for 15 years...

Re:The Serde Rust Framework by vips7L on Thursday October 14, 2021 @09:31PM (#28872842)
Maybe we are having different ideas about what is being discussed, but in Java and C# (arguably the most mainstream languages) you have the same experience supported natively via annotations and attributes.
Re:The Serde Rust Framework by spoiler on Friday October 15, 2021 @03:18AM (#28875236)
Ah, I stand corrected then.

I've worked with the Microsoft tech stack very very little, so I've never bothered to learn C#.

I've completely forgot about Java; I haven't used it in ages. Now that you mentioned Java, I realised Kotlin has it too.

Re:The Serde Rust Framework by dcow on Friday October 15, 2021 @02:20AM (#28874890)
Swift also has native support. I am actually not a huge fan of the ergonomics of Go’s struct tags because of how cluttered they get in practice. It feels so unstructured like the rest of the language, but maybe that makes it perfect for Go.