Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenlib/intern/serialize.cc
| Show All 38 Lines | |||||
| const ArrayValue *Value::as_array_value() const | const ArrayValue *Value::as_array_value() const | ||||
| { | { | ||||
| if (type_ != eValueType::Array) { | if (type_ != eValueType::Array) { | ||||
| return nullptr; | return nullptr; | ||||
| } | } | ||||
| return static_cast<const ArrayValue *>(this); | return static_cast<const ArrayValue *>(this); | ||||
| } | } | ||||
| const ObjectValue *Value::as_object_value() const | const DictionaryValue *Value::as_dictionary_value() const | ||||
| { | { | ||||
| if (type_ != eValueType::Object) { | if (type_ != eValueType::Dictionary) { | ||||
| return nullptr; | return nullptr; | ||||
| } | } | ||||
| return static_cast<const ObjectValue *>(this); | return static_cast<const DictionaryValue *>(this); | ||||
| } | } | ||||
| static void convert_to_json(nlohmann::ordered_json &j, const Value &value); | static void convert_to_json(nlohmann::ordered_json &j, const Value &value); | ||||
| static void convert_to_json(nlohmann::ordered_json &j, const ArrayValue &value) | static void convert_to_json(nlohmann::ordered_json &j, const ArrayValue &value) | ||||
| { | { | ||||
| const ArrayValue::Items &items = value.elements(); | const ArrayValue::Items &items = value.elements(); | ||||
| /* Create a json array to store the elements. If this isn't done and items is empty it would | /* Create a json array to store the elements. If this isn't done and items is empty it would | ||||
| * return use a null value, in stead of an empty array. */ | * return use a null value, in stead of an empty array. */ | ||||
| j = "[]"_json; | j = "[]"_json; | ||||
| for (const ArrayValue::Item &item_value : items) { | for (const ArrayValue::Item &item_value : items) { | ||||
| nlohmann::ordered_json json_item; | nlohmann::ordered_json json_item; | ||||
| convert_to_json(json_item, *item_value); | convert_to_json(json_item, *item_value); | ||||
| j.push_back(json_item); | j.push_back(json_item); | ||||
| } | } | ||||
| } | } | ||||
| static void convert_to_json(nlohmann::ordered_json &j, const ObjectValue &value) | static void convert_to_json(nlohmann::ordered_json &j, const DictionaryValue &value) | ||||
| { | { | ||||
| const ObjectValue::Items &attributes = value.elements(); | const DictionaryValue::Items &attributes = value.elements(); | ||||
| /* Create a json object to store the attributes. If this isn't done and attributes is empty it | /* Create a json object to store the attributes. If this isn't done and attributes is empty it | ||||
| * would return use a null value, in stead of an empty object. */ | * would return use a null value, in stead of an empty object. */ | ||||
| j = "{}"_json; | j = "{}"_json; | ||||
| for (const ObjectValue::Item &attribute : attributes) { | for (const DictionaryValue::Item &attribute : attributes) { | ||||
| nlohmann::ordered_json json_item; | nlohmann::ordered_json json_item; | ||||
| convert_to_json(json_item, *attribute.second); | convert_to_json(json_item, *attribute.second); | ||||
| j[attribute.first] = json_item; | j[attribute.first] = json_item; | ||||
| } | } | ||||
| } | } | ||||
| static void convert_to_json(nlohmann::ordered_json &j, const Value &value) | static void convert_to_json(nlohmann::ordered_json &j, const Value &value) | ||||
| { | { | ||||
| Show All 9 Lines | switch (value.type()) { | ||||
| } | } | ||||
| case eValueType::Array: { | case eValueType::Array: { | ||||
| const ArrayValue &array = *value.as_array_value(); | const ArrayValue &array = *value.as_array_value(); | ||||
| convert_to_json(j, array); | convert_to_json(j, array); | ||||
| break; | break; | ||||
| } | } | ||||
| case eValueType::Object: { | case eValueType::Dictionary: { | ||||
| const ObjectValue &object = *value.as_object_value(); | const DictionaryValue &object = *value.as_dictionary_value(); | ||||
| convert_to_json(j, object); | convert_to_json(j, object); | ||||
| break; | break; | ||||
| } | } | ||||
| case eValueType::Null: { | case eValueType::Null: { | ||||
| j = nullptr; | j = nullptr; | ||||
| break; | break; | ||||
| } | } | ||||
| Show All 17 Lines | static std::unique_ptr<ArrayValue> convert_from_json_to_array(const nlohmann::ordered_json &j) | ||||
| for (auto element : j.items()) { | for (auto element : j.items()) { | ||||
| nlohmann::ordered_json element_json = element.value(); | nlohmann::ordered_json element_json = element.value(); | ||||
| std::unique_ptr<Value> value = convert_from_json(element_json); | std::unique_ptr<Value> value = convert_from_json(element_json); | ||||
| elements.append_as(value.release()); | elements.append_as(value.release()); | ||||
| } | } | ||||
| return array; | return array; | ||||
| } | } | ||||
| static std::unique_ptr<ObjectValue> convert_from_json_to_object(const nlohmann::ordered_json &j) | static std::unique_ptr<DictionaryValue> convert_from_json_to_object( | ||||
| const nlohmann::ordered_json &j) | |||||
| { | { | ||||
| std::unique_ptr<ObjectValue> object = std::make_unique<ObjectValue>(); | std::unique_ptr<DictionaryValue> object = std::make_unique<DictionaryValue>(); | ||||
| ObjectValue::Items &elements = object->elements(); | DictionaryValue::Items &elements = object->elements(); | ||||
| for (auto element : j.items()) { | for (auto element : j.items()) { | ||||
| std::string key = element.key(); | std::string key = element.key(); | ||||
| nlohmann::ordered_json element_json = element.value(); | nlohmann::ordered_json element_json = element.value(); | ||||
| std::unique_ptr<Value> value = convert_from_json(element_json); | std::unique_ptr<Value> value = convert_from_json(element_json); | ||||
| elements.append_as(std::pair(key, value.release())); | elements.append_as(std::pair(key, value.release())); | ||||
| } | } | ||||
| return object; | return object; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 69 Lines • Show Last 20 Lines | |||||