Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make pretty print indentation configurable #780

Merged
merged 1 commit into from
May 14, 2023

Conversation

janhavlin
Copy link
Contributor

📒 Description

Hello, I'd like to be able to customize the indentation when pretty print is enabled. So this patch is adding a setting SerializerConfig.pretty_print_indent, allowing to set indentation string per level when pretty_print is True.

Simple example:

from xsdata.formats.dataclass.serializers.config import SerializerConfig
from xsdata.formats.dataclass.serializers import XmlSerializer
from xsdata.formats.dataclass.serializers.writers.lxml import LxmlEventWriter
from xsdata.formats.dataclass.serializers.writers.native import XmlEventWriter
import dataclasses

@dataclasses.dataclass
class Bar:
    nested_a: int
    nested_b: int

@dataclasses.dataclass
class Foo:
    foo: int
    bar: Bar

f = Foo(1, Bar(2, 3))

print(XmlSerializer(
    config=SerializerConfig(pretty_print=True, pretty_print_indent="            "),
    writer=LxmlEventWriter
).render(f))

print(XmlSerializer(
    config=SerializerConfig(pretty_print=True, pretty_print_indent=" "),
    writer=XmlEventWriter
).render(f))

Which results in:

<?xml version="1.0" encoding="UTF-8"?>
<Foo>
            <foo>1</foo>
            <bar>
                        <nested_a>2</nested_a>
                        <nested_b>3</nested_b>
            </bar>
</Foo>

<?xml version="1.0" encoding="UTF-8"?>
<Foo>
 <foo>1</foo>
 <bar>
  <nested_a>2</nested_a>
  <nested_b>3</nested_b>
 </bar>
</Foo>

🛫 Checklist

@tefra
Copy link
Owner

tefra commented May 7, 2023

Hi @janhavlin can you also take care of the JsonSerializer?

@janhavlin janhavlin force-pushed the config-pretty-print-indent branch from a087f6f to 9fdde1c Compare May 9, 2023 08:21
@janhavlin
Copy link
Contributor Author

Sure! Pushed an additional change.

@codecov
Copy link

codecov bot commented May 14, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.02 🎉

Comparison is base (8e2e446) 99.96% compared to head (9fdde1c) 99.98%.

❗ Current head 9fdde1c differs from pull request most recent head 2529a2f. Consider uploading reports for the commit 2529a2f to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #780      +/-   ##
==========================================
+ Coverage   99.96%   99.98%   +0.02%     
==========================================
  Files         104      101       -3     
  Lines        9102     8916     -186     
  Branches     2035     1980      -55     
==========================================
- Hits         9099     8915     -184     
+ Partials        3        1       -2     
Impacted Files Coverage Δ
xsdata/formats/dataclass/serializers/config.py 100.00% <100.00%> (ø)
xsdata/formats/dataclass/serializers/json.py 100.00% <100.00%> (ø)
...data/formats/dataclass/serializers/writers/lxml.py 100.00% <100.00%> (ø)
...ta/formats/dataclass/serializers/writers/native.py 100.00% <100.00%> (ø)

... and 18 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@tefra tefra force-pushed the config-pretty-print-indent branch from 9fdde1c to 2529a2f Compare May 14, 2023 14:27
Copy link
Owner

@tefra tefra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great thank you for contributing!

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@tefra tefra merged commit 25f9bd4 into tefra:master May 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants