Files
GDevelop/Core/GDCore/Serialization/Serializer.h
ViktorVovk f39d8bb080 Add experimental support for "canonical" serialization of events (#8552)
- When activated, the JSON of the project is saved with properties always in alphabetical order. Fields in events are also never omitted.
- The preference is controlled via `gdevelop-settings.yaml` (already read
by the IDE on project open):

  ```yaml
  # gdevelop-settings.yaml
  preferences:
    canonicalEventSerialization: true
  ```

Only show in developer changelog
Co-authored-by: Viktor Vovk <vvovk@playtika.com>
2026-05-20 16:10:12 +02:00

90 lines
2.3 KiB
C++

/*
* GDevelop Core
* Copyright 2008-2016 Florian Rival (Florian.Rival@gmail.com). All rights
* reserved. This project is released under the MIT License.
*/
#ifndef GDCORE_SERIALIZER_H
#define GDCORE_SERIALIZER_H
#include <string>
#include "GDCore/Serialization/SerializerElement.h"
namespace gd {
/**
* \brief The class used to save/load projects and GDCore classes
* from/to XML or JSON.
*/
class GD_CORE_API Serializer {
public:
/** \name XML serialization.
* Convert a gd::SerializerElement from/to XML.
*/
///@{
/**
* \brief Escape a string for inclusion in a XML tag
*/
static gd::String ToEscapedXMLString(const gd::String& str);
///@}
/** \name JSON serialization.
* Convert a gd::SerializerElement from/to JSON.
* This uses RapidJSON for fast parsing and stringification.
* See https://github.com/miloyip/nativejson-benchmark
*/
///@{
/**
* \brief Serialize a gd::SerializerElement to a JSON string.
*/
static gd::String ToJSON(const SerializerElement& element);
/**
* \brief Construct a gd::SerializerElement from a JSON string.
*/
static SerializerElement FromJSON(const char* json);
/**
* \brief Construct a gd::SerializerElement from a JSON string.
*/
static SerializerElement FromJSON(const gd::String& json) {
return FromJSON(json.c_str());
}
///@}
/** \name Canonical serialization mode.
* When enabled, ToJSON writes object keys in alphabetical order and
* various SerializeTo helpers write default values (false, "", []) for
* properties that would otherwise be omitted.
*
* This makes git diffs minimal and shift-free when toggling boolean
* flags or adding/removing optional sub-structures (e.g. sub-events,
* local variables).
*/
///@{
/**
* \brief Enable/disable canonical serialization mode globally.
*
* Affects ToJSON (alphabetical key order) and various SerializeTo helpers
* (always writing default values for optional properties).
*/
static void SetCanonicalMode(bool canonical) { canonicalMode = canonical; }
/**
* \brief Returns true if canonical serialization mode is currently active.
*/
static bool IsCanonicalMode() { return canonicalMode; }
///@}
virtual ~Serializer(){};
private:
Serializer(){};
static bool canonicalMode;
};
} // namespace gd
#endif