|
13 | 13 | # under the License. |
14 | 14 |
|
15 | 15 | import io |
16 | | -import ujson |
| 16 | +import json |
17 | 17 | import typing |
18 | 18 |
|
19 | 19 |
|
@@ -117,43 +117,47 @@ def Set(self, key: str, value: object): |
117 | 117 | def MarshalJSON(self, data_marshaller: typing.Callable) -> typing.IO: |
118 | 118 | props = self.Properties() |
119 | 119 | props["data"] = data_marshaller(props.get("data")) |
120 | | - return io.StringIO(ujson.dumps(props)) |
| 120 | + return io.BytesIO(json.dumps(props).encode("utf-8")) |
121 | 121 |
|
122 | 122 | def UnmarshalJSON(self, b: typing.IO, |
123 | 123 | data_unmarshaller: typing.Callable): |
124 | | - raw_ce = ujson.load(b) |
| 124 | + raw_ce = json.load(b) |
125 | 125 | for name, value in raw_ce.items(): |
126 | 126 | if name == "data": |
127 | 127 | value = data_unmarshaller(value) |
128 | 128 | self.Set(name, value) |
129 | 129 |
|
130 | 130 | def UnmarshalBinary(self, headers: dict, body: typing.IO, |
131 | 131 | data_unmarshaller: typing.Callable): |
132 | | - props = self.Properties(with_nullable=True) |
133 | | - exts = props.get("extensions") |
134 | | - for key in props: |
135 | | - formatted_key = "ce-{0}".format(key) |
136 | | - if key != "extensions": |
137 | | - self.Set(key, headers.get("ce-{0}".format(key))) |
138 | | - if formatted_key in headers: |
139 | | - del headers[formatted_key] |
140 | | - |
141 | | - # rest of headers suppose to an extension? |
142 | | - exts.update(**headers) |
143 | | - self.Set("extensions", exts) |
| 132 | + BINARY_MAPPING = { |
| 133 | + 'content-type': 'contenttype', |
| 134 | + # TODO(someone): add Distributed Tracing. It's not clear |
| 135 | + # if this is one extension or two. |
| 136 | + # https://github.com/cloudevents/spec/blob/master/extensions/distributed-tracing.md |
| 137 | + } |
| 138 | + for header, value in headers.items(): |
| 139 | + header = header.lower() |
| 140 | + if header in BINARY_MAPPING: |
| 141 | + self.Set(BINARY_MAPPING[header], value) |
| 142 | + elif header.startswith("ce-"): |
| 143 | + self.Set(header[3:], value) |
| 144 | + |
144 | 145 | self.Set("data", data_unmarshaller(body)) |
145 | 146 |
|
146 | | - def MarshalBinary(self) -> (dict, object): |
| 147 | + def MarshalBinary( |
| 148 | + self, data_marshaller: typing.Callable) -> (dict, object): |
147 | 149 | headers = {} |
| 150 | + if self.ContentType(): |
| 151 | + headers["content-type"] = self.ContentType() |
148 | 152 | props = self.Properties() |
149 | 153 | for key, value in props.items(): |
150 | | - if key not in ["data", "extensions"]: |
| 154 | + if key not in ["data", "extensions", "contenttype"]: |
151 | 155 | if value is not None: |
152 | 156 | headers["ce-{0}".format(key)] = value |
153 | 157 |
|
154 | | - exts = props.get("extensions") |
155 | | - if len(exts) > 0: |
156 | | - headers.update(**exts) |
| 158 | + for key, value in props.get("extensions"): |
| 159 | + headers["ce-{0}".format(key)] = value |
157 | 160 |
|
158 | 161 | data, _ = self.Get("data") |
159 | | - return headers, data |
| 162 | + return headers, io.BytesIO( |
| 163 | + str(data_marshaller(data)).encode("utf-8")) |
0 commit comments