From 89171f2be1b31a2399e3d0727b3da2a9ee5823ba Mon Sep 17 00:00:00 2001 From: Aaron Pham <29749331+aarnphm@users.noreply.github.com> Date: Mon, 5 Dec 2022 17:32:49 -0800 Subject: [PATCH] chore: clearer parsing logic Signed-off-by: Aaron Pham <29749331+aarnphm@users.noreply.github.com> --- src/bentoml/_internal/io_descriptors/json.py | 33 ++++++++++---------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/bentoml/_internal/io_descriptors/json.py b/src/bentoml/_internal/io_descriptors/json.py index 08c1dc18b59..2adbea6f631 100644 --- a/src/bentoml/_internal/io_descriptors/json.py +++ b/src/bentoml/_internal/io_descriptors/json.py @@ -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