Blender V4.3
BLI_serialize.hh File Reference
#include <iosfwd>
#include "BLI_map.hh"
#include "BLI_string_ref.hh"
#include "BLI_vector.hh"

Go to the source code of this file.

Classes

class  blender::io::serialize::Value
 
class  blender::io::serialize::PrimitiveValue< T, V >
 
class  blender::io::serialize::NullValue
 
class  blender::io::serialize::StringValue
 
class  blender::io::serialize::ArrayValue
 
class  blender::io::serialize::DictionaryValue
 
class  blender::io::serialize::Formatter
 
class  blender::io::serialize::JsonFormatter
 

Namespaces

namespace  blender
 
namespace  blender::io
 
namespace  blender::io::serialize
 

Typedefs

using blender::io::serialize::IntValue = PrimitiveValue<int64_t, eValueType::Int>
 
using blender::io::serialize::DoubleValue = PrimitiveValue<double, eValueType::Double>
 
using blender::io::serialize::BooleanValue = PrimitiveValue<bool, eValueType::Boolean>
 
using blender::io::serialize::EnumValue = PrimitiveValue<int, eValueType::Enum>
 

Enumerations

enum class  blender::io::serialize::eValueType {
  blender::io::serialize::String , blender::io::serialize::Int , blender::io::serialize::Array , blender::io::serialize::Null ,
  blender::io::serialize::Boolean , blender::io::serialize::Double , blender::io::serialize::Dictionary , blender::io::serialize::Enum
}
 

Functions

void blender::io::serialize::write_json_file (StringRef path, const Value &value)
 
std::shared_ptr< Valueblender::io::serialize::read_json_file (StringRef path)
 

Detailed Description

An abstraction layer for serialization formats.

Allowing to read/write data to a serialization format like JSON.

Supported data types

The abstraction layer has a limited set of data types it supports. There are specific classes that builds up the data structure that can be (de)serialized.

  • StringValue: for strings
  • IntValue: for integer values
  • DoubleValue: for double precision floating point numbers
  • BooleanValue: for boolean values
  • ArrayValue: An array of any supported value.
  • DictionaryValue: A key value pair where keys are std::string.
  • NullValue: for null values.

Basic usage

Serializing

  • Construct a structure that needs to be serialized using the *Value classes.
  • Construct the formatter you want to use
  • Invoke the formatter.serialize method passing an output stream and the value.

The next example would format an integer value (42) as JSON the result will be stored inside out.

JsonFormatter json;
std::stringstream out;
IntValue test_value(42);
json.serialize(out, test_value);

Deserializing

std::stringstream is("42");
JsonFormatter json;
std::unique_ptr<Value> value = json.deserialize(is);

Adding a new formatter

To add a new formatter a new sub-class of Formatter must be created and the serialize/deserialize methods should be implemented.

Definition in file BLI_serialize.hh.