https://github.com/developerstoolbox/data-converter-package
A data converter package to convert data between JSON, YAML and XML.
https://github.com/developerstoolbox/data-converter-package
data-converter json wolfsoftware xml yaml
Last synced: 9 months ago
JSON representation
A data converter package to convert data between JSON, YAML and XML.
- Host: GitHub
- URL: https://github.com/developerstoolbox/data-converter-package
- Owner: DevelopersToolbox
- License: mit
- Created: 2024-05-30T12:09:42.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-11-11T04:47:58.000Z (about 1 year ago)
- Last Synced: 2024-11-11T05:32:28.206Z (about 1 year ago)
- Topics: data-converter, json, wolfsoftware, xml, yaml
- Language: Python
- Homepage:
- Size: 110 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Citation: CITATION.cff
- Codeowners: .github/CODEOWNERS
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
## 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:
```bash
pip install wolfsoftware.data-converter
```
## Usage
### Importing the Module
To use the `DataConverter` class, import it from the `wolfsoftware.data_converter` package:
```python
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`.
```python
# JSON data example
json_data = '{"name": "John", "age": 30}'
converter = DataConverter(json_data, data_type='json')
# XML data example
xml_data = 'John30'
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:
```python
json_output = converter.to_json()
print(json_output)
```
#### To XML
Convert the data to XML format using the `to_xml` method:
```python
xml_output = converter.to_xml()
print(xml_output)
```
#### To YAML
Convert the data to YAML format using the `to_yaml` method:
```python
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:
```python
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:
```python
try:
converter = DataConverter(data, data_type='unsupported')
except DataConverterError as e:
print(f"Error: {e}")
```