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

Allow serialization of dataclass objects from and to dict #921

Closed
christianwbrock opened this issue Jan 25, 2024 · 2 comments · Fixed by #939
Closed

Allow serialization of dataclass objects from and to dict #921

christianwbrock opened this issue Jan 25, 2024 · 2 comments · Fixed by #939
Labels
enhancement New feature or request

Comments

@christianwbrock
Copy link

christianwbrock commented Jan 25, 2024

Allow serialization of dataclass objects from and to dict

Currently I know only two workarounds.
After explaining them I will ask my questions at the end ;-)

The first workarounds involves using json.loads and json.dumps:

def model_to_dict(model):
    json_serializer = JsonSerializer()
    json_string = json_serializer.render(model)
    return json.loads(json_string)

def dict_to_model(a_dict):
    json_string = json.dumps(a_dict)
    json_parser = JsonParser()
    return json_parser.parse(json_string, generated.Model)

This works but requires the extra step of creating a json string.

The second workaround exploits the fact that xsdata uses dicts internally.

For serialization this is kind of supported and documented:

def model_to_dict(model):
    result = {}

    def dict_dumper(obj, *_args, **_kwargs):
        result.update(obj)

    json_serializer = JsonSerializer(dump_factory=dict_dumper)
    json_serializer.write(None, model)

    return result

For parsing it exploits undocumented internals:

def dict_to_model(a_dict):
    class FakeSourceHavingReadAttribute:
        def read(self):
            pass

    def dict_loader(_src):
        return a_dict

    json_parser = JsonParser(load_factory=dict_loader)
    model = json_parser.parse(FakeSourceHavingReadAttribute(), generated.Model)

    return model

Two questions

  1. Is there a better way?
  2. Should we modify JsonParser to get rid of the FakeSourceHavingReadAttribute hack?
@tefra
Copy link
Owner

tefra commented Jan 25, 2024

The parser exists but it's not documented, it needs some refactoring to follow the parser interface, the serializer is also planned.

They are on top of my priorities after I finish refactoring all the docstrings/documentation.

@tefra tefra added the enhancement New feature or request label Jan 25, 2024
tefra added a commit that referenced this issue Feb 15, 2024
@tefra tefra mentioned this issue Feb 15, 2024
4 tasks
tefra added a commit that referenced this issue Feb 15, 2024
tefra added a commit that referenced this issue Feb 15, 2024
@tefra
Copy link
Owner

tefra commented Feb 15, 2024

I surfaced the dict parser as the dict decoder and extracted the dict encoder from the json serializer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants