Skip to content

Commit

Permalink
Fix encoder.
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollo3zehn committed Oct 26, 2022
1 parent 03763b8 commit bcf6937
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/remoting/python-remoting/nexus_remoting/_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from dataclasses import dataclass, field
from datetime import datetime, timedelta
from enum import Enum
from typing import Any, Callable, Optional, Type, TypeVar, Union, cast
from typing import (Any, Callable, ClassVar, Optional, Type, TypeVar, Union,
cast)
from uuid import UUID

T = TypeVar("T")
Expand Down Expand Up @@ -127,18 +128,33 @@ def _decode(typeCls: Type[T], data: Any, options: JsonEncoderOptions) -> T:
elif dataclasses.is_dataclass(typeCls):

parameters = {}
type_hints = typing.get_type_hints(typeCls)

for key, value in data.items():

type_hints = typing.get_type_hints(typeCls)
key = options.property_name_decoder(key)
parameter_type = typing.cast(Type, type_hints.get(key))

if (parameter_type is not None):
value = JsonEncoder._decode(parameter_type, value, options)
parameters[key] = value

return typeCls(**parameters)
# ensure default values if JSON does not serialize default fields
for key, value in type_hints.items():
if not key in parameters and not typing.get_origin(value) == ClassVar:

if (value == int):
parameters[key] = 0

elif (value == float):
parameters[key] = 0.0

else:
parameters[key] = None

instance = typeCls(**parameters)

return instance

# registered decoders
for base in typeCls.__mro__[:-1]:
Expand Down

0 comments on commit bcf6937

Please sign in to comment.