A data converter package to convert data between JSON, YAML and XML.
Project description
Overview
The wolfsoftware.data-converter
module provides functionalities for converting data between JSON, XML, and YAML formats.
It includes methods to convert dictionaries to and from these formats, making it a versatile tool for data transformation in Python applications.
Installation
First, install the package using pip:
pip install wolfsoftware.data-converter
Usage
Importing the Module
To use the DataConverter
class, import it from the wolfsoftware.data_converter
package:
from wolfsoftware.data_converter import DataConverter, DataConverterError
Initializing the DataConverter
The DataConverter
class requires data and its type as parameters during initialization. Supported data types are json
, xml
, dict
, and yaml
.
# JSON data example
json_data = '{"name": "John", "age": 30}'
converter = DataConverter(json_data, data_type='json')
# XML data example
xml_data = '<person><name>John</name><age>30</age></person>'
converter = DataConverter(xml_data, data_type='xml')
# Dictionary data example
dict_data = {'name': 'John', 'age': 30}
converter = DataConverter(dict_data, data_type='dict')
# YAML data example
yaml_data = 'name: John\nage: 30\n'
converter = DataConverter(yaml_data, data_type='yaml')
Converting Data
To JSON
Convert the data to JSON format using the to_json
method:
json_output = converter.to_json()
print(json_output)
To XML
Convert the data to XML format using the to_xml
method:
xml_output = converter.to_xml()
print(xml_output)
To YAML
Convert the data to YAML format using the to_yaml
method:
yaml_output = converter.to_yaml()
print(yaml_output)
Example Usage
Here's a complete example demonstrating how to convert a dictionary to JSON, XML, and YAML formats:
from wolfsoftware.data_converter import DataConverter, DataConverterError
# Dictionary data
data = {'name': 'John', 'age': 30}
# Initialize DataConverter
converter = DataConverter(data, data_type='dict')
# Convert to JSON
json_output = converter.to_json()
print("JSON Output:")
print(json_output)
# Convert to XML
xml_output = converter.to_xml()
print("XML Output:")
print(xml_output)
# Convert to YAML
yaml_output = converter.to_yaml()
print("YAML Output:")
print(yaml_output)
Handling Unsupported Data Types
If an unsupported data type is provided, a DataConverterError
will be raised:
try:
converter = DataConverter(data, data_type='unsupported')
except DataConverterError as e:
print(f"Error: {e}")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file wolfsoftware_data_converter-0.1.0.tar.gz
.
File metadata
- Download URL: wolfsoftware_data_converter-0.1.0.tar.gz
- Upload date:
- Size: 8.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f82e2398038a1b6921a509d967e8065182c12e08066d82b4afd598cafc48ae78 |
|
MD5 | 58165f8330af21e7ca98f8f4274158b5 |
|
BLAKE2b-256 | d2cbd56e32233fc6d1fb2102006b0bab4098dd054bc3671cb64b373d0990ed8f |
File details
Details for the file wolfsoftware.data_converter-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: wolfsoftware.data_converter-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 901b873da4e28c470bad8e94fb469c18f2a2d6f31ede3b0ab238db16ef6cf858 |
|
MD5 | 1e87f500e43e77b00ff6165c914ed938 |
|
BLAKE2b-256 | 983b0999688500c9ca25816166c2a7f136809ef36923b73e5ab3e7c0255bb0fe |