Skip to content

Commit

Permalink
chore: clearer parsing logic
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Pham <29749331+aarnphm@users.noreply.github.com>
  • Loading branch information
aarnphm committed Dec 6, 2022
1 parent ceeb0ef commit 89171f2
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/bentoml/_internal/io_descriptors/json.py
Expand Up @@ -401,20 +401,21 @@ def parse_dict_to_proto(
msg: _message.Message,
json_encoder: type[json.JSONEncoder] = DefaultJsonEncoder,
) -> t.Any:
# To handle None cases.
if obj is not None:
from google.protobuf.json_format import ParseDict

if isinstance(obj, (dict, str, list, float, int, bool)):
# ParseDict handles google.protobuf.Struct type
# directly if given object has a supported type
ParseDict(obj, msg)
else:
# If given object doesn't have a supported type, we will
# use given JSON encoder to convert it to dictionary
# and then parse it to google.protobuf.Struct.
# Note that if a custom JSON encoder is used, it mustn't
# take any arguments.
ParseDict(json_encoder().default(obj), msg)
# otherwise this function is an identity op for the msg if obj is None.
if obj is None:
# this function is an identity op for the msg if obj is None.
return msg

from google.protobuf.json_format import ParseDict

if isinstance(obj, (dict, str, list, float, int, bool)):
# ParseDict handles google.protobuf.Struct type
# directly if given object has a supported type
ParseDict(obj, msg)
else:
# If given object doesn't have a supported type, we will
# use given JSON encoder to convert it to dictionary
# and then parse it to google.protobuf.Struct.
# Note that if a custom JSON encoder is used, it mustn't
# take any arguments.
ParseDict(json_encoder().default(obj), msg)
return msg

0 comments on commit 89171f2

Please sign in to comment.