--------------------------------- ToJsonRecursive Recursively convert an rttr::instance to a json object - returns false if any properties fail to serialize
| 20 | // Recursively convert an rttr::instance to a json object - returns false if any properties fail to serialize |
| 21 | // |
| 22 | bool ToJsonRecursive(const rttr::instance& inst, JSON::Value*& outJVal, rttr::type const& callingType) |
| 23 | { |
| 24 | JSON::Object* outJObject = new JSON::Object(); |
| 25 | outJVal = outJObject; |
| 26 | |
| 27 | JSON::Object* appendJObject = outJObject; |
| 28 | |
| 29 | // pointers are wrapped into another object layour to allow polymorphism |
| 30 | if (callingType.is_pointer()) |
| 31 | { |
| 32 | rttr::type internalPointerType = inst.get_derived_type().get_raw_type(); |
| 33 | |
| 34 | JSON::Pair keyVal = std::make_pair(internalPointerType.get_name().to_string(), new JSON::Object()); |
| 35 | |
| 36 | outJObject->value.emplace_back(keyVal); |
| 37 | |
| 38 | appendJObject = keyVal.second->obj(); |
| 39 | } |
| 40 | |
| 41 | rttr::instance instObj = inst.get_type().get_raw_type().is_wrapper() ? inst.get_wrapped_instance() : inst; |
| 42 | |
| 43 | rttr::array_range<rttr::property> const propList = inst.get_derived_type().get_properties(); |
| 44 | |
| 45 | bool allPropertiesSerialized = true; |
| 46 | |
| 47 | for (rttr::property const& prop : propList) |
| 48 | { |
| 49 | if (prop.get_metadata("NO_SERIALIZE")) // read only should not be serialized probably |
| 50 | { |
| 51 | continue; |
| 52 | } |
| 53 | |
| 54 | rttr::variant const& propVal = prop.get_value(inst); |
| 55 | if (!propVal) |
| 56 | { |
| 57 | continue; // cannot serialize, because we cannot retrieve the value - maybe handle nullptr here |
| 58 | } |
| 59 | |
| 60 | JSON::Pair keyVal = std::make_pair(prop.get_name().to_string(), nullptr); |
| 61 | |
| 62 | if (!VariantToJsonValue(propVal, keyVal.second)) |
| 63 | { |
| 64 | LOG("ToJsonRecursive > Failed to serialize property '" + keyVal.first + std::string("' !"), LogLevel::Warning); |
| 65 | allPropertiesSerialized = false; |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | appendJObject->value.push_back(keyVal); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return allPropertiesSerialized; |
| 74 | } |
| 75 | |
| 76 | //--------------------------------- |
| 77 | // VariantToJsonValue |
no test coverage detected