MCPcopy Create free account
hub / github.com/apache/tvm-ffi / DecodeObjectData

Method DecodeObjectData

src/ffi/extra/serialization.cc:367–418  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

365 }
366
367 Any DecodeObjectData(int32_t type_index, const json::Value& data) {
368 static reflection::TypeAttrColumn data_from_json =
369 reflection::TypeAttrColumn(reflection::type_attr::kDataFromJson);
370 if (data_from_json[type_index] != nullptr) {
371 return data_from_json[type_index].cast<Function>()(data);
372 }
373 // otherwise, we go over the fields and create the data.
374 const TVMFFITypeInfo* type_info = TVMFFIGetTypeInfo(type_index);
375 ObjectPtr<Object> ptr = CreateEmptyObject(type_info);
376
377 auto decode_field_value = [&](const TVMFFIFieldInfo* field_info,
378 const json::Value& data) -> Any {
379 switch (field_info->field_static_type_index) {
380 case TypeIndex::kTVMFFINone: {
381 return nullptr;
382 }
383 case TypeIndex::kTVMFFIBool: {
384 return data.cast<bool>();
385 }
386 case TypeIndex::kTVMFFIInt: {
387 return data.cast<int64_t>();
388 }
389 case TypeIndex::kTVMFFIFloat: {
390 return data.cast<double>();
391 }
392 case TypeIndex::kTVMFFIDataType: {
393 return StringToDLDataType(data.cast<String>());
394 }
395 default: {
396 return GetOrDecodeNode(data.cast<int64_t>());
397 }
398 }
399 };
400
401 json::Object data_object = data.cast<json::Object>();
402 reflection::ForEachFieldInfo(type_info, [&](const TVMFFIFieldInfo* field_info) {
403 String field_name(field_info->name);
404 void* field_addr = reinterpret_cast<char*>(ptr.get()) + field_info->offset;
405 if (data_object.count(field_name) != 0) {
406 Any field_value = decode_field_value(field_info, data_object[field_name]);
407 reflection::CallFieldSetter(field_info, field_addr,
408 reinterpret_cast<const TVMFFIAny*>(&field_value));
409 } else if (field_info->flags & kTVMFFIFieldFlagBitMaskHasDefault) {
410 reflection::SetFieldToDefault(field_info, field_addr);
411 } else {
412 TVM_FFI_THROW(TypeError) << "Required field `"
413 << String(field_info->name.data, field_info->name.size)
414 << "` not set in type `" << TypeIndexToTypeKey(type_index) << "`";
415 }
416 });
417 return ObjectRef(ptr);
418 }
419
420 explicit ObjectGraphDeserializer(const json::Value& serialized) {
421 if (!serialized.as<json::Object>()) {

Callers

nothing calls this directly

Calls 12

TypeAttrColumnClass · 0.85
TVMFFIGetTypeInfoFunction · 0.85
CreateEmptyObjectFunction · 0.85
StringToDLDataTypeFunction · 0.85
ForEachFieldInfoFunction · 0.85
CallFieldSetterFunction · 0.85
SetFieldToDefaultFunction · 0.85
TypeIndexToTypeKeyFunction · 0.85
StringClass · 0.50
ObjectRefClass · 0.50
getMethod · 0.45
countMethod · 0.45

Tested by

no test coverage detected