MCPcopy Create free account
hub / github.com/OpenFodder/openfodder / deserialize

Method deserialize

Source/Utils/dukglue/dukvalue.h:196–267  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

194public:
195 static_assert(sizeof(char) == 1, "Serialization probably broke");
196 static DukValue deserialize(duk_context* ctx, const char* data, size_t data_len) {
197 DukValue v;
198 v.mContext = ctx;
199 v.mType = *((Type*)data);
200
201 const char* data_ptr = data + sizeof(Type);
202 data_len -= sizeof(Type);
203
204 switch (v.mType) {
205 case UNDEFINED:
206 case NULLREF:
207 break;
208
209 case BOOLEAN:
210 {
211 if (data_len < 1)
212 throw DukException() << "Malformed boolean data";
213
214 v.mPOD.boolean = data[1] == 1 ? true : false;
215 break;
216 }
217
218 case NUMBER:
219 {
220 if (data_len < sizeof(double))
221 throw DukException() << "Malformed number data";
222
223 v.mPOD.number = *((double*)data_ptr);
224 break;
225 }
226
227 case STRING:
228 {
229 if (data_len < sizeof(uint32_t))
230 throw DukException() << "Malformed string data (no length)";
231 uint32_t str_len = *((uint32_t*)data_ptr);
232
233 if (data_len < sizeof(uint32_t) + str_len)
234 throw DukException() << "Malformed string data (appears truncated)";
235
236 const char* str_data = (data_ptr + sizeof(uint32_t));
237 v.mString.assign(str_data, str_len);
238 break;
239 }
240
241 case OBJECT:
242 {
243 if (data_len < sizeof(uint32_t))
244 throw DukException() << "Malformed object JSON data (no length)";
245 uint32_t json_len = *((uint32_t*)data_ptr);
246
247 if (data_len < sizeof(uint32_t) + json_len)
248 throw DukException() << "Malformed object JSON data (appears truncated)";
249
250 const char* json_data = (data_ptr + sizeof(uint32_t));
251 duk_push_lstring(ctx, json_data, json_len);
252 int rc = duk_safe_call(ctx, &json_decode_safe, NULL, 1, 1);
253 if (rc) {

Callers

nothing calls this directly

Calls 2

DukExceptionClass · 0.85
DukErrorExceptionClass · 0.85

Tested by

no test coverage detected