-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathproto_json.py
More file actions
237 lines (197 loc) · 8.04 KB
/
proto_json.py
File metadata and controls
237 lines (197 loc) · 8.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import uuid
from importlib.metadata import version as importlib_version
from typing import Any, Callable, Type
from google.protobuf.json_format import ( # type: ignore
_WKTJSONMETHODS,
ParseError,
_Parser,
_Printer,
)
from packaging import version
from feast.protos.feast.serving.ServingService_pb2 import FeatureList
from feast.protos.feast.types.Value_pb2 import RepeatedValue, Value
ProtoMessage = Any
JsonObject = Any
def _patch_proto_json_encoding(
proto_type: Type[ProtoMessage],
to_json_object: Callable[[_Printer, ProtoMessage], JsonObject],
from_json_object: Callable[[_Parser, JsonObject, ProtoMessage], None],
) -> None:
"""Patch Protobuf JSON Encoder / Decoder for a desired Protobuf type with to_json & from_json methods."""
to_json_fn_name = "_" + uuid.uuid4().hex
from_json_fn_name = "_" + uuid.uuid4().hex
setattr(_Printer, to_json_fn_name, to_json_object)
setattr(_Parser, from_json_fn_name, from_json_object)
_WKTJSONMETHODS[proto_type.DESCRIPTOR.full_name] = [
to_json_fn_name,
from_json_fn_name,
]
def _patch_feast_value_json_encoding():
"""Patch Protobuf JSON Encoder / Decoder with a Feast Value type.
This allows encoding the proto object as a native type, without the dummy structural wrapper.
Here's a before example:
{
"value_1": {
"int64_val": 1
},
"value_2": {
"double_list_val": [1.0, 2.0, 3.0]
},
}
And here's an after example:
{
"value_1": 1,
"value_2": [1.0, 2.0, 3.0]
}
"""
def to_json_object(printer: _Printer, message: ProtoMessage) -> JsonObject:
which = message.WhichOneof("val")
# If the Value message is not set treat as null_value when serialize
# to JSON. The parse back result will be different from original message.
if which is None or which == "null_val":
return None
elif "_list_" in which:
value = list(getattr(message, which).val)
else:
value = getattr(message, which)
return value
def from_json_object(
parser: _Parser, value: JsonObject, message: ProtoMessage
) -> None:
if value is None:
message.null_val = 0
elif isinstance(value, bool):
message.bool_val = value
elif isinstance(value, str):
message.string_val = value
elif isinstance(value, int):
message.int64_val = value
elif isinstance(value, float):
message.double_val = value
elif isinstance(value, list):
if len(value) == 0:
# Clear will mark the struct as modified so it will be created even if there are no values
message.int64_list_val.Clear()
elif isinstance(value[0], bool):
message.bool_list_val.val.extend(value)
elif isinstance(value[0], str):
message.string_list_val.val.extend(value)
elif isinstance(value[0], (float, int, type(None))):
# Identify array as ints if all of the elements are ints
if all(isinstance(item, int) for item in value):
message.int64_list_val.val.extend(value)
# If any of the elements are floats or nulls, then parse it as a float array
else:
# Convert each null as NaN.
message.double_list_val.val.extend(
[item if item is not None else float("nan") for item in value]
)
else:
raise ParseError(
"Value {0} has unexpected type {1}.".format(
value[0], type(value[0])
)
)
else:
raise ParseError(
"Value {0} has unexpected type {1}.".format(value, type(value))
)
def from_json_object_updated(
parser: _Parser, value: JsonObject, message: ProtoMessage, path: str
):
from_json_object(parser, value, message)
# https://github.com/feast-dev/feast/issues/2484 Certain feast users need a higher version of protobuf but the
# parameters of `from_json_object` changes in feast 3.20.1. This change gives users flexibility to use earlier versions.
current_version = importlib_version("protobuf")
if version.parse(current_version) < version.parse("3.20"):
_patch_proto_json_encoding(Value, to_json_object, from_json_object)
else:
_patch_proto_json_encoding(Value, to_json_object, from_json_object_updated)
def _patch_feast_repeated_value_json_encoding():
"""Patch Protobuf JSON Encoder / Decoder with a Feast RepeatedValue type.
This allows list of lists without dummy field name "val".
Here's a before example:
{
"repeated_value": [
{"val": [1,2,3]},
{"val": [4,5,6]}
]
}
And here's an after example:
{
"repeated_value": [
[1,2,3],
[4,5,6]
]
}
"""
def to_json_object(printer: _Printer, message: ProtoMessage) -> JsonObject:
return [printer._MessageToJsonObject(item) for item in message.val]
def from_json_object_updated(
parser: _Parser, value: JsonObject, message: ProtoMessage, path: str
) -> None:
array = value if isinstance(value, list) else value["val"]
for item in array:
parser.ConvertMessage(item, message.val.add(), path)
def from_json_object(
parser: _Parser, value: JsonObject, message: ProtoMessage
) -> None:
array = value if isinstance(value, list) else value["val"]
for item in array:
parser.ConvertMessage(item, message.val.add())
# https://github.com/feast-dev/feast/issues/2484 Certain feast users need a higher version of protobuf but the
# parameters of `from_json_object` changes in feast 3.20.1. This change gives users flexibility to use earlier versions.
current_version = importlib_version("protobuf")
if version.parse(current_version) < version.parse("3.20"):
_patch_proto_json_encoding(RepeatedValue, to_json_object, from_json_object)
else:
_patch_proto_json_encoding(
RepeatedValue, to_json_object, from_json_object_updated
)
def _patch_feast_feature_list_json_encoding():
"""Patch Protobuf JSON Encoder / Decoder with a Feast FeatureList type.
This allows list of lists without dummy field name "features".
Here's a before example:
{
"feature_list": {
"features": [
"feature-1",
"feature-2",
"feature-3"
]
}
}
And here's an after example:
{
"feature_list": [
"feature-1",
"feature-2",
"feature-3"
]
}
"""
def to_json_object(printer: _Printer, message: ProtoMessage) -> JsonObject:
return list(message.val)
def from_json_object(
parser: _Parser, value: JsonObject, message: ProtoMessage
) -> None:
array = value if isinstance(value, list) else value["val"]
message.val.extend(array)
def from_json_object_updated(
parser: _Parser, value: JsonObject, message: ProtoMessage, path: str
) -> None:
from_json_object(parser, value, message)
# https://github.com/feast-dev/feast/issues/2484 Certain feast users need a higher version of protobuf but the
# parameters of `from_json_object` changes in feast 3.20.1. This change gives users flexibility to use earlier versions.
current_version = importlib_version("protobuf")
if version.parse(current_version) < version.parse("3.20"):
_patch_proto_json_encoding(FeatureList, to_json_object, from_json_object)
else:
_patch_proto_json_encoding(
FeatureList, to_json_object, from_json_object_updated
)
def patch():
"""Patch Protobuf JSON Encoder / Decoder with all desired Feast types."""
_patch_feast_value_json_encoding()
_patch_feast_repeated_value_json_encoding()
_patch_feast_feature_list_json_encoding()