question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: 3rd party non-JSON serialization/deserialization

See original GitHub issue

Hi, author of pydantic-yaml here. I have no idea about anything Rust-related, unfortunately, but hopefully this feature request will make sense in Python land.

I’m going off this slide in this presentation by @samuelcolvin, specifically:

We could add support for other formats (e.g. yaml, toml) the only side affect would be bigger binaries.

Here’s a relevant discussion about “3rd party” deserialization from v1: https://github.com/samuelcolvin/pydantic/discussions/3025

It would be great if pydantic-core were built in a way where non-JSON formats could be added “on top” rather than necessarily being built into the core. I understand performance is a big question in this rewrite, so ideally these would be high-level interfaces that can be hacked in Python (or implemented in Rust/etc. for better performance).

From the examples available already, it’s possible that such a feature could be quite simple on the pydantic-core side - the 3rd party would create their own function a-la validate_json, possibly just calling validate_python. However, care would be needed on how format-specific details are sent between pydantic and the implementation. In V1 this is done with the Config class and special json_encoder/decoder attributes, which have been a pain to re-implement for YAML properly (without way too much hackery).

Ideally for V2, this would be something more easily addable and configurable. The alternative would be to just implement TOML, YAML etc. directly in the binary (and I wouldn’t have to keep supporting my project, ha!)

Thanks again for Pydantic!

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
samuelcolvincommented, Jun 8, 2022

Thanks for the question.

I think this is just calling validate_python, or indeed initializing the pydantic model as I guess you do now.

No changes should be required to pydantic-core to allow this.

I want to add support for line numbers in errors, but that requires a new rust JSON parser, so won’t be added until v2.1 or later.

2reactions
samuelcolvincommented, Jul 27, 2022

This sounds very hard to do in a reliable cross-platform way. Given the problems we already experience (at the scale of pydantic or watchfiles) with very subtle OS differences and wheels. I’m very unwilling to enter into this mess.

You’re effectively proposing another way link libraries that side steps both crates and python imports, are there any other examples of python libraries that use DLL/ share libraries to access functionality in other packages without using the python runtime?

(Perhaps worth remembering that I’ll probably be maintaining pydantic for the next decade at least, one “clever” idea proposed now which relies on shaky cross-platform behaviour, could cost me 100s of hours of support over the years - hence my caution)

I real question is how much faster would this be than my proposal above?

To proceed with the conversation, we need to benchmark that. Really someone need to build both solutions (and any 3rd solutions proposed) and see how they perform.

@PrettyWood @tiangolo do you have any feedback on any of this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Require that sessions are JSON serializable · Issue #2709 ...
2.0 feature request: Require that sessions are JSON serializable #2709 ... 3rd party plugins can register (de)serialization formats for non-standard types.
Read more >
Automatic JSON serialization and deserialization of objects in ...
Once you deserialize the json into a NSDictionary , you can then create and initialize your object with that dictionary instance, no need...
Read more >
How to serialize and deserialize JSON in C# - C# Corner
In this article, you will learn about JSON serialization and deserialization in C#. We can implement JSON Serialization/Deserialization by ...
Read more >
Transcoders and Non-JSON Documents | Couchbase Docs
This transcoder is particularly useful when working with third-party JSON libraries. Here we want to use System.Text.Json for serialization, instead of the ...
Read more >
Definitive Guide to Jackson ObjectMapper - Serialize and ...
Whenever there's a mismatch between the names of properties/fields in a JSON String and a POJO - you can deal with the mismatch...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found