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

Control over labels #14

Closed
ahankinson opened this issue Feb 15, 2016 · 1 comment
Closed

Control over labels #14

ahankinson opened this issue Feb 15, 2016 · 1 comment

Comments

@ahankinson
Copy link
Contributor

There are cases where an application may need control over the output label where it might differ from the attribute name.

The case I am working on is a JSON-LD serializer, where labels can start with the @ prefix: @id, @type, etc. This is not a valid attribute name in Python, but it should be reflected in the serialized output.

I've tried to get around this with custom subclasses, but adherence to the attribute name as output label is fairly deep in the functioning of the serializer.

So what I propose is to add an (optional) label to serializer fields, label, that would get carried along in the _compile_field_to_tuple tuple. If label is not None, the _serializer method uses it as the key in the output instead of name.

If this sounds like something you would find useful, I could put together a pull request and send it upstream.

@clarkduvall
Copy link
Owner

I think this is a great idea, feel free to submit a pull request. I believe the only use of "name" in the compiled_fields is to set the key value in the returned dict, so you might be able to just replace name with label if its set.

Thanks!

ahankinson added a commit to rism-digital/serpy that referenced this issue Feb 16, 2016
This commit adds an optional parameter, label, to a Field. When serialized, the serializer will use the label if it is defined, or it will default to using the attribute name as before.

This commit includes two new unit tests for this functionality.

Fixes clarkduvall#14
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

No branches or pull requests

2 participants