Skip to content

Add as_dict #1990

@lmazuel

Description

@lmazuel

Design:

Step 1: AzureJSONEncoder

  • Restart back from azure-core AzureJSONEncode (so we can migrate it back to azure-core later)
  • Add two kwargs exclude_readonly and exclude_none

This means this should look like this:

class AzureJSONEncoder(JSONEncoder):
   def __init__(self, *args, *, exclude_readonly: bool = False, exclude_none: bool = False, **kwargs):
         super().__init__(*args, **kwargs)
         self.exclude_readonly = exclude_readonly
         self.exclude_none = exclude_none

This design allows to call json.dumps this way:

json.dumps(my_model, exclude_readonly=True, exclude_none=True, cls=AzureJSONEncoder)

This works since json.dumps inject kwargs into the constructor of cls, so we have access to custom kwargs.

Step 2:

  • Add a as_dict in all model that is literally:
def as_dict(self, *, exclude_readonly: bool = False, exclude_none: bool = False):
   return json.dumps(self, exclude_readonly=exclude_readonly, exclude_none=exclude_none, cls=AzureJSONEncoder)

Step 3:

  • Update our operation template to call as_dict(exclude_readonly=True, exclude_none=True)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions