GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
Public Member Functions | Static Public Attributes | List of all members
gd::SerializerElement Class Reference

A generic container that can represent a value ( containing a string, double, bool or int), an object ("associative array", "dictionary") with children or an array (children indexed by numeric properties). More...

#include <SerializerElement.h>

Public Member Functions

 SerializerElement ()
 Create an empty element with no value, no children and no attributes.
 
 SerializerElement (const SerializerValue &value)
 Create an element with the specified value.
 
 SerializerElement (const gd::SerializerElement &object)
 
SerializerElementoperator= (const gd::SerializerElement &object)
 
Value

Methods related to the value of the element, if any.

void SetValue (const SerializerValue &value)
 Set the value of the element.
 
void SetValue (bool val)
 Set the value of the element, as a boolean.
 
void SetBoolValue (bool val)
 Set the value of the element, as a boolean.
 
void SetValue (const gd::String &val)
 Set the value of the element, as a string.
 
void SetStringValue (const gd::String &val)
 Set the value of the element, as a string.
 
void SetValue (int val)
 Set the value of the element, as an integer.
 
void SetIntValue (int val)
 Set the value of the element, as an integer.
 
void SetValue (unsigned int val)
 Set the value of the element, as an unsigned integer.
 
void SetValue (double val)
 Set the value of the element, as a double precision floating point number.
 
void SetDoubleValue (double val)
 Set the value of the element, as a double precision floating point number.
 
void SetValue (float val)
 Set the value of the element, as a floating point number.
 
void SetFloatValue (float val)
 Set the value of the element, as a floating point number.
 
const SerializerValueGetValue () const
 Get the value of the element, as a generic gd::SerializerValue. More...
 
bool GetBoolValue () const
 Get the value, its type being a boolean.
 
gd::String GetStringValue () const
 Get the value, its type being a gd::String.
 
int GetIntValue () const
 Get the value, its type being an int.
 
double GetDoubleValue () const
 Get the value, its type being a double.
 
bool IsValueUndefined () const
 Return true if no value was set for the element.
 
void SetMultilineStringValue (const gd::String &value)
 Save the value either as a string or as an array of strings if it has line breaks.
 
gd::String GetMultilineStringValue ()
 Read the value, either represented as a string or as an array of strings, into a string.
 
Attributes

Methods related to the attributes of the element.

Deprecated:
Prefer using AddChild/GetChild methods.

Attributes are stored differently than children elements, but are serialized to the same in JSON. Hence, the attribute getters will also search in children elements.

SerializerElementSetAttribute (const gd::String &name, bool value)
 Set the boolean value of an attribute of the element. More...
 
SerializerElementSetBoolAttribute (const gd::String &name, bool value)
 Set the boolean value of an attribute of the element. More...
 
SerializerElementSetAttribute (const gd::String &name, const gd::String &value)
 Set the string value of an attribute of the element. More...
 
SerializerElementSetStringAttribute (const gd::String &name, const gd::String &value)
 Set the string value of an attribute of the element. More...
 
SerializerElementSetAttribute (const gd::String &name, const char *value)
 Set the string value of an attribute of the element. More...
 
SerializerElementSetAttribute (const gd::String &name, int value)
 Set the integer value of an attribute of the element. More...
 
SerializerElementSetIntAttribute (const gd::String &name, int value)
 Set the integer value of an attribute of the element. More...
 
SerializerElementSetAttribute (const gd::String &name, double value)
 Set the double precision floating point number value of an attribute of the element. More...
 
SerializerElementSetDoubleAttribute (const gd::String &name, double value)
 Set the double precision floating point number value of an attribute of the element. More...
 
bool GetBoolAttribute (const gd::String &name, bool defaultValue=false, gd::String deprecatedName="") const
 
gd::String GetStringAttribute (const gd::String &name, gd::String defaultValue="", gd::String deprecatedName="") const
 
int GetIntAttribute (const gd::String &name, int defaultValue=0, gd::String deprecatedName="") const
 
double GetDoubleAttribute (const gd::String &name, double defaultValue=0.0, gd::String deprecatedName="") const
 
bool HasAttribute (const gd::String &name) const
 Return true if the specified attribute exists. More...
 
const std::map< gd::String, SerializerValue > & GetAllAttributes () const
 Return all the attributes of the element.
 
Children

Methods related to the children elements

void ConsiderAsArray () const
 Consider that the element is an array of elements, without specific name for the children element. More...
 
bool ConsideredAsArray () const
 Check if the element is considered as an array containing its children. More...
 
void ConsiderAsArrayOf (const gd::String &name, const gd::String &deprecatedName="") const
 Consider that the element is an array for elements with the given name. More...
 
const gd::StringConsideredAsArrayOf () const
 Return the name of the children the element is considered an array of. More...
 
SerializerElementAddChild (gd::String name)
 Add a child at the end of the children list with the given name and return a reference to it. More...
 
SerializerElementGetChild (gd::String name, std::size_t index=0, gd::String deprecatedName="") const
 Get a child of the element using its name. More...
 
SerializerElementGetChild (std::size_t index) const
 Get a child of the element using its index (when the element is considered as an array). More...
 
std::size_t GetChildrenCount (gd::String name="", gd::String deprecatedName="") const
 Get the number of children having a specific name. More...
 
bool HasChild (const gd::String &name, gd::String deprecatedName="") const
 Return true if the specified child exists. More...
 
void RemoveChild (const gd::String &name)
 Remove the child with the specified name. More...
 
const std::vector< std::pair< gd::String, std::shared_ptr< SerializerElement > > > & GetAllChildren () const
 Return all the children of the element.
 

Static Public Attributes

static SerializerElement nullElement
 

Detailed Description

A generic container that can represent a value ( containing a string, double, bool or int), an object ("associative array", "dictionary") with children or an array (children indexed by numeric properties).

It is used for serialization (to JSON or XML), or as a generic container for properties of objects (see for example gd::Behavior).

It also has specialized methods in GDevelop.js (see postjs.js) to be converted to a JavaScript object.

Note
Children are stored with their order preserved, but this also means that their access/removal is O(number of children). This class is not appropriated for a use in game where fast access is required.
See also
gd::Serializer

Constructor & Destructor Documentation

◆ SerializerElement()

gd::SerializerElement::SerializerElement ( const gd::SerializerElement object)
inline

Copy constructor.

Member Function Documentation

◆ AddChild()

SerializerElement & gd::SerializerElement::AddChild ( gd::String  name)

Add a child at the end of the children list with the given name and return a reference to it.

Parameters
nameThe name of the new child.

◆ ConsiderAsArray()

void gd::SerializerElement::ConsiderAsArray ( ) const
inline

Consider that the element is an array of elements, without specific name for the children element.

When serialized to a format accepting arrays (like JSON), the element will be serialized to an array.

◆ ConsiderAsArrayOf()

void gd::SerializerElement::ConsiderAsArrayOf ( const gd::String name,
const gd::String deprecatedName = "" 
) const
inline

Consider that the element is an array for elements with the given name.

In this case, no child with a different name should be added. When serialized to a format accepting arrays (like JSON), the element will be serialized to an array.

Parameters
nameThe name of the children.

◆ ConsideredAsArray()

bool gd::SerializerElement::ConsideredAsArray ( ) const
inline

Check if the element is considered as an array containing its children.

As possible, this should be serialized to an array (possible JSON, but not in XML).

◆ ConsideredAsArrayOf()

const gd::String& gd::SerializerElement::ConsideredAsArrayOf ( ) const
inline

Return the name of the children the element is considered an array of.

Return an empty string if the element is not considered as an array.

◆ GetBoolAttribute()

bool gd::SerializerElement::GetBoolAttribute ( const gd::String name,
bool  defaultValue = false,
gd::String  deprecatedName = "" 
) const

Get the value of an attribute being a boolean.

Parameters
nameThe name of the attribute
defaultValueThe value returned if the attribute is not found.
deprecatedNameAn alternative name for the attribute that will be used if the first one doesn't exist.

◆ GetChild() [1/2]

SerializerElement & gd::SerializerElement::GetChild ( gd::String  name,
std::size_t  index = 0,
gd::String  deprecatedName = "" 
) const

Get a child of the element using its name.

Parameters
nameThe name of the child.
nameThe index of the child, in case of an array.

◆ GetChild() [2/2]

SerializerElement & gd::SerializerElement::GetChild ( std::size_t  index) const

Get a child of the element using its index (when the element is considered as an array).

Parameters
nameThe index of the child

◆ GetChildrenCount()

std::size_t gd::SerializerElement::GetChildrenCount ( gd::String  name = "",
gd::String  deprecatedName = "" 
) const

Get the number of children having a specific name.

If no children name is specified, return the number of children being part of the array. (ConsiderAsArrayOf must have been called before). Note that unnamed children are considered to be valid element of the array.

Parameters
nameThe name of the children.
See also
SerializerElement::ConsiderAsArrayOf

◆ GetDoubleAttribute()

double gd::SerializerElement::GetDoubleAttribute ( const gd::String name,
double  defaultValue = 0.0,
gd::String  deprecatedName = "" 
) const

Get the value of an attribute being a double.

Parameters
nameThe name of the attribute
defaultValueThe value returned if the attribute is not found.
deprecatedNameAn alternative name for the attribute that will be used if the first one doesn't exist.

◆ GetIntAttribute()

int gd::SerializerElement::GetIntAttribute ( const gd::String name,
int  defaultValue = 0,
gd::String  deprecatedName = "" 
) const

Get the value of an attribute being an int.

Parameters
nameThe name of the attribute
defaultValueThe value returned if the attribute is not found.
deprecatedNameAn alternative name for the attribute that will be used if the first one doesn't exist.

◆ GetStringAttribute()

gd::String gd::SerializerElement::GetStringAttribute ( const gd::String name,
gd::String  defaultValue = "",
gd::String  deprecatedName = "" 
) const

Get the value of an attribute being a string.

Parameters
nameThe name of the attribute
defaultValueThe value returned if the attribute is not found.
deprecatedNameAn alternative name for the attribute that will be used if the first one doesn't exist.

◆ GetValue()

const SerializerValue & gd::SerializerElement::GetValue ( ) const

Get the value of the element, as a generic gd::SerializerValue.

Note
If not value was set, an attribute named "value" is searched (for backward compatiblity). If found, its value is returned.

◆ HasAttribute()

bool gd::SerializerElement::HasAttribute ( const gd::String name) const

Return true if the specified attribute exists.

Deprecated:
Use HasChild instead. This should be removed from the codebase.
Parameters
nameThe name of the attribute to find.

◆ HasChild()

bool gd::SerializerElement::HasChild ( const gd::String name,
gd::String  deprecatedName = "" 
) const

Return true if the specified child exists.

Note
Complexity is O(number of children).
Parameters
nameThe name of the child to find.

◆ operator=()

SerializerElement& gd::SerializerElement::operator= ( const gd::SerializerElement object)
inline

Assignment operator.

◆ RemoveChild()

void gd::SerializerElement::RemoveChild ( const gd::String name)

Remove the child with the specified name.

Note
Complexity is O(number of children).
Parameters
nameThe name of the child to remove.

◆ SetAttribute() [1/5]

SerializerElement & gd::SerializerElement::SetAttribute ( const gd::String name,
bool  value 
)

Set the boolean value of an attribute of the element.

Parameters
nameThe name of the attribute.
valueThe value of the attribute.

◆ SetAttribute() [2/5]

SerializerElement& gd::SerializerElement::SetAttribute ( const gd::String name,
const char *  value 
)
inline

Set the string value of an attribute of the element.

Parameters
nameThe name of the attribute.
valueThe value of the attribute.

◆ SetAttribute() [3/5]

SerializerElement & gd::SerializerElement::SetAttribute ( const gd::String name,
const gd::String value 
)

Set the string value of an attribute of the element.

Parameters
nameThe name of the attribute.
valueThe value of the attribute.

◆ SetAttribute() [4/5]

SerializerElement & gd::SerializerElement::SetAttribute ( const gd::String name,
double  value 
)

Set the double precision floating point number value of an attribute of the element.

Parameters
nameThe name of the attribute.
valueThe value of the attribute.

◆ SetAttribute() [5/5]

SerializerElement & gd::SerializerElement::SetAttribute ( const gd::String name,
int  value 
)

Set the integer value of an attribute of the element.

Parameters
nameThe name of the attribute.
valueThe value of the attribute.

◆ SetBoolAttribute()

SerializerElement& gd::SerializerElement::SetBoolAttribute ( const gd::String name,
bool  value 
)
inline

Set the boolean value of an attribute of the element.

Parameters
nameThe name of the attribute.
valueThe value of the attribute.

◆ SetDoubleAttribute()

SerializerElement& gd::SerializerElement::SetDoubleAttribute ( const gd::String name,
double  value 
)
inline

Set the double precision floating point number value of an attribute of the element.

Parameters
nameThe name of the attribute.
valueThe value of the attribute.

◆ SetIntAttribute()

SerializerElement& gd::SerializerElement::SetIntAttribute ( const gd::String name,
int  value 
)
inline

Set the integer value of an attribute of the element.

Parameters
nameThe name of the attribute.
valueThe value of the attribute.

◆ SetStringAttribute()

SerializerElement& gd::SerializerElement::SetStringAttribute ( const gd::String name,
const gd::String value 
)
inline

Set the string value of an attribute of the element.

Parameters
nameThe name of the attribute.
valueThe value of the attribute.

The documentation for this class was generated from the following files: