Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

any-serde

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

any-serde

  • 0.2.18
  • PyPI
  • Socket score

Maintainers
1

AnySerde

AnySerde is a Python package that simplifies the task of converting Python variables to and from serializable data. Whether you need to serialize Python objects to JSON, YAML, or other common formats, or deserialize data from these formats back into Python objects, AnySerde has got you covered.

The main difference from other serialization libraries is that AnySerde does not require any changes to your existing codebase such as adding decorators or adding parent classes.

Installation

You can easily install AnySerde using pip:

pip install any_serde

More package details on the PyPI page:

Usage

Using AnySerde is straightforward. Here's a quick overview of how to get started:

Converting Python Objects to Data

To convert a Python variable to a data format (e.g., JSON or YAML), you can use the to_data function. Here's an example with a dataclass:

from dataclasses import dataclass
import yaml
from any_serde import to_data

@dataclass
class SampleDataclass:
    foo: int
    bar: str
    baz: dict[str, list[bool]]

sample = SampleDataclass(
    foo=2,
    bar="some text",
    baz={"0": [], "8": [True, False, False]},
)
sample_data = to_data(SampleDataclass, sample)

with open("sample.yaml", "wt") as f:
    yaml.dump(sample_data, f)

This example dumps the data as yaml to a file, but you could easily use json or xml or any other format you want.

Converting Data to Python Objects

Conversely, if you have data in a specific format and want to convert it back to a Python object, you can use the from_data function. Here's a continuation of the previous example that turns the yaml file back into a python variable:

from any_serde import from_data

with open("sample.yaml", "rt") as f:
    sample_data = yaml.load(f, Loader=yaml.SafeLoader)

sample = from_data(SampleDataclass, sample_data)

Again, you can replace 'json' with the desired format.

Supported Object Types

Right now, AnySerde can convert these types to data:

  1. All python primitives except complex
  2. dictionaries
  3. lists
  4. sets
  5. tuples
  6. union types (but be careful of ambiguous serialization)
  7. Dataclasses
  8. Enums

As of writing this, AnySerde plans to support these types (but doesn't yet):

  1. NamedTuple
  2. Path
  3. Datetime
  4. all other builtin python types
  5. user-defined types (with user-defined to_data and from_data methods)

Contributing

Contributions to AnySerde are welcome! If you find a bug, have a feature request, or want to contribute code, please open an issue or submit a pull request on the GitHub repository.

License

AnySerde is available for any use (including commercial) and is distributed under the Apache License 2.0. See the LICENSE file for more details.

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc