2#ifndef __ASE_SERIALIZE_HH__
3#define __ASE_SERIALIZE_HH__
33 WritNode operator[] (
const String &fieldname) {
return recfield (fieldname,
false); }
34 WritNode front (
const String &fieldname) {
return recfield (fieldname,
true); }
42 Value::Type index ()
const {
return value_.index(); }
43 size_t count ()
const {
return value_.
count(); }
45 double as_double ()
const {
return value_.
as_double(); }
51 bool has (
const String &key)
const {
return value_.
has (key); }
53 template<
typename T>
bool operator<< (
const T &v);
54 template<
typename T>
bool operator>> (T &v);
55 template<
typename T>
bool operator& (T &v);
57 template<
class T,
class E =
void>
73 bool in_load_ =
false, in_save_ =
false, skip_zero_ =
false, skip_emptystring_ =
false, relaxed_ =
false;
76 Jsonipc::JsonValue wrapper_to_json (Wrapper*,
size_t, Jsonipc::JsonAllocator&)
override;
77 Wrapper* wrapper_from_json (
const Jsonipc::JsonValue&)
override;
82 int64 link_counter_ = 0;
85 void reset (
int mode);
94 enum Flags { NONE = 0, RELAXED = 1, SKIP_ZERO = 2, SKIP_EMPTYSTRING = 4, SKIP_DEFAULTS = SKIP_ZERO | SKIP_EMPTYSTRING };
95 friend Flags operator| (Flags a, Flags b) {
return Flags (
uint64_t (a) | b); }
96 explicit Writ (Flags flags = Flags (0));
97 template<
class T>
void save (T &source);
98 template<
class T>
bool load (T &target);
100 bool from_json (
const String &jsonstring);
103 static void blank_enum (
const String &enumname);
104 static void not_serializable (
const String &classname);
123template<
typename T,
typename =
void>
125 static_assert (!
sizeof (T),
"ENOIMPL: type serialization unimplemented");
132template<
class,
class =
void>
struct
134template<
class T>
struct
137template<
class T>
inline void
138Writ::save (T &source)
145 root_ & serializable;
152template<
class T>
inline bool
153Writ::load (T &target)
159 Serializable &serializable = target;
160 root_ & serializable;
171 return writ_.dummy();
189 return writ_.skip_emptystring_;
195 return writ_.skip_zero_;
217template<
typename T>
inline bool
221 return serialize (v,
"");
224template<
typename T>
inline bool
225WritNode::operator<< (
const T &v)
228 return serialize (
const_cast<T&
> (v),
"");
231template<
typename T>
inline bool
232WritNode::operator>> (T &v)
236 return serialize (v,
"");
239template<
typename T,
typename>
inline bool
240WritNode::serialize (T &typval,
const String &fieldname,
const StringS &typedata)
243 return WritConverter<T>::save_value (*
this, typval, typedata, fieldname);
245 return WritConverter<T>::load_value (*
this, typval, typedata, fieldname);
249template<>
inline bool
265template<
class T>
inline bool
272 array.reserve (vec.size());
275 array.push_back (Value());
276 WritNode nth_node (writ_, array.back());
282 value_.index() == Value::ARRAY)
285 vec.reserve (vec.size() + array.size());
286 for (
size_t i = 0; i < array.size(); i++)
288 vec.resize (vec.size() + 1);
289 WritNode nth_node (writ_, array[i]);
290 nth_node & vec.back();
297template<
class ...T>
inline bool
306 auto save_arg = [&] (
auto arg) {
307 array.push_back (Value());
308 WritNode nth_node (writ_, array.back());
312 (save_arg (parampack), ...);
317 value_.index() == Value::ARRAY)
321 auto load_arg = [&] (
auto &arg) {
322 if (idx >= array.size())
324 WritNode nth_node (writ_, array[idx++]);
328 (load_arg (parampack), ...);
336WritNode::serialize (Value &val,
const String &fieldname,
const StringS &typedata)
352WritNode::serialize (ValueS &vec,
const String &fieldname,
const StringS &typedata)
360 value_.index() == Value::ARRAY)
369WritNode::serialize (ValueR &rec,
const String &fieldname,
const StringS &typedata)
377 value_.index() == Value::RECORD)
389 static bool ASE_NOINLINE
395 static bool ASE_NOINLINE
406 long double limit = 0;
408 valid = valid && tmp >= limit;
410 valid = valid && tmp <= limit;
429 Writ::blank_enum (typeid_name<T>());
444 if (node.
value().index() == Value::STRING)
468 node.
value() = ValueR::empty_record;
469 serialize (obj, node);
473 Writ::not_serializable (typeid_name<T>());
476 rapidjson::Document document (rapidjson::kNullType);
477 Jsonipc::JsonValue &docroot = document;
479 node.
value() = Jsonipc::from_json<Value> (docroot);
490 if (node.
value().index() == Value::RECORD)
491 serialize (obj, node);
494 Writ::not_serializable (typeid_name<T>());
497 rapidjson::Document document (rapidjson::kNullType);
498 Jsonipc::JsonValue &docroot = document;
499 docroot = Jsonipc::to_json<Value> (node.
value(), document.GetAllocator());
514 return node.serialize (serializable);
519 if (node.
value().index() == Value::RECORD)
521 node.serialize (serializable);
529template<
class T>
inline String
533 writ.save (
const_cast<T&
> (source));
534 return writ.to_json();
537template<
class T>
inline bool
541 if (writ.from_json (jsonstring) && writ.load (target))
546template<
class T>
inline T
550 if (writ.from_json (jsonstring))
553 if (writ.load (target))
Interface for serializable objects with Reflink support.
One entry in a Writ serialization document.
bool skip_emptystring() const
Omit empty strings during in_save()
WritNode push()
Append new WritNode for serializing arrays during in_save().
Writ & writ()
Access the Writ of this node.
Value & value()
Access the Value of this node.
bool in_load() const
Return true during deserialization.
bool skip_zero() const
Omit zero integers or floats during in_save()
bool in_save() const
Return true during serialization.
WritNodeS to_nodes()
Create std::vector<WritNode> for serialized arrays during in_load().
void purge_value()
Clean up defaults in Value.
bool loadable(const String &key) const
True if in_load() && has (key).
bool operator&(T &v)
Serialization operator.
Document containing all information needed to serialize and deserialize a Value.
bool in_save() const
Return true during serialization.
static bool typedata_find_minimum(const StringS &typedata, const std::string &fieldname, long double *limit)
Find the minimum value for field of typedata.
static bool typedata_find_maximum(const StringS &typedata, const std::string &fieldname, long double *limit)
Find the maximum value for field of typedata.
static bool typedata_is_loadable(const StringS &typedata, const String &fieldname)
Check for the writable and storage flags in the hints field of typedata.
bool in_load() const
Return true during deserialization.
static bool typedata_is_storable(const StringS &typedata, const String &fieldname)
Check for the readable and storage flags in the hints field of typedata.
Keep track of temporary instances during IpcDispatcher::dispatch_message().
#define ASE_ASSERT_RETURN(expr,...)
Return from the current function if expr evaluates to false and issue an assertion warning.
The Anklang C++ API namespace.
bool json_parse(const String &jsonstring, T &target)
Parse a well formed JSON string and assign contents to target.
std::vector< String > StringS
Convenience alias for a std::vector<std::string>.
int64_t int64
A 64-bit unsigned integer, use PRI*64 in format strings.
typename ::std::enable_if< value, void >::type REQUIRESv
REQUIRESv<value> - Simplified version of std::enable_if<cond,void>::type to use SFINAE in struct temp...
std::string String
Convenience alias for std::string.
String json_stringify(const T &source, Writ::Flags flags=Writ::Flags(0))
Create JSON string from source.
Template to specialize string conversion for various data types.
Has_serialize_f<T> - Check if serialize(T&,WritNode&) is provided for T.
Value type used to interface with various property types.
double as_double() const
Convert Value to double or return 0.
String repr() const
Convert Value to a string representation, useful for debugging.
int64 as_int() const
Convert Value to int64 or return 0.
size_t count() const
Number of elements in a RECORD or ARRAY Value.
bool has(const String &key) const
Check for a named field in a RECORD.
String as_string() const
Convert Value to a string, not very useful for RECORD or ARRAY.
StringS keys() const
List the field names of a RECORD Value.
DerivesSharedPtr<T> - Check if T derives from std::shared_ptr<>.
DerivesVector<T> - Check if T derives from std::vector<>.
Jsonipc wrapper type for objects that support field-wise serialization to/from JSON.