32 WritNode operator[] (
const String &fieldname) {
return recfield (fieldname,
false); }
33 WritNode front (
const String &fieldname) {
return recfield (fieldname,
true); }
41 Value::Type index ()
const {
return value_.index(); }
42 size_t count ()
const {
return value_.
count(); }
44 double as_double ()
const {
return value_.
as_double(); }
50 bool has (
const String &key)
const {
return value_.
has (key); }
52 template<
typename T>
bool operator<< (
const T &v);
53 template<
typename T>
bool operator>> (T &v);
54 template<
typename T>
bool operator& (T &v);
56 template<
class T,
class E =
void>
72 bool in_load_ =
false, in_save_ =
false, skip_zero_ =
false, skip_emptystring_ =
false, relaxed_ =
false;
75 Jsonipc::JsonValue wrapper_to_json (Wrapper*,
size_t, Jsonipc::JsonAllocator&)
override;
76 Wrapper* wrapper_from_json (
const Jsonipc::JsonValue&)
override;
81 int64 link_counter_ = 0;
84 void reset (
int mode);
93 enum Flags { NONE = 0, RELAXED = 1, SKIP_ZERO = 2, SKIP_EMPTYSTRING = 4, SKIP_DEFAULTS = SKIP_ZERO | SKIP_EMPTYSTRING };
94 friend Flags operator| (Flags a, Flags b) {
return Flags (
uint64_t (a) | b); }
95 explicit Writ (Flags flags = Flags (0));
96 template<
class T>
void save (T &source);
97 template<
class T>
bool load (T &target);
99 bool from_json (
const String &jsonstring);
102 static void blank_enum (
const String &enumname);
103 static void not_serializable (
const String &classname);
122template<
typename T,
typename =
void>
124 static_assert (!
sizeof (T),
"ENOIMPL: type serialization unimplemented");
131template<
class,
class =
void>
struct
133template<
class T>
struct
136template<
class T>
inline void
137Writ::save (T &source)
144 root_ & serializable;
151template<
class T>
inline bool
152Writ::load (T &target)
158 Serializable &serializable = target;
159 root_ & serializable;
170 return writ_.dummy();
188 return writ_.skip_emptystring_;
194 return writ_.skip_zero_;
216template<
typename T>
inline bool
220 return serialize (v,
"");
223template<
typename T>
inline bool
224WritNode::operator<< (
const T &v)
227 return serialize (
const_cast<T&
> (v),
"");
230template<
typename T>
inline bool
231WritNode::operator>> (T &v)
235 return serialize (v,
"");
238template<
typename T,
typename>
inline bool
239WritNode::serialize (T &typval,
const String &fieldname,
const StringS &typedata)
242 return WritConverter<T>::save_value (*
this, typval, typedata, fieldname);
244 return WritConverter<T>::load_value (*
this, typval, typedata, fieldname);
248template<>
inline bool
264template<
class T>
inline bool
271 array.reserve (vec.size());
274 array.push_back (Value());
275 WritNode nth_node (writ_, array.back());
281 value_.index() == Value::ARRAY)
284 vec.reserve (vec.size() + array.size());
285 for (
size_t i = 0; i < array.size(); i++)
287 vec.resize (vec.size() + 1);
288 WritNode nth_node (writ_, array[i]);
289 nth_node & vec.back();
296template<
class ...T>
inline bool
305 auto save_arg = [&] (
auto arg) {
306 array.push_back (Value());
307 WritNode nth_node (writ_, array.back());
311 (save_arg (parampack), ...);
316 value_.index() == Value::ARRAY)
320 auto load_arg = [&] (
auto &arg) {
321 if (idx >= array.size())
323 WritNode nth_node (writ_, array[idx++]);
327 (load_arg (parampack), ...);
335WritNode::serialize (Value &val,
const String &fieldname,
const StringS &typedata)
351WritNode::serialize (ValueS &vec,
const String &fieldname,
const StringS &typedata)
359 value_.index() == Value::ARRAY)
368WritNode::serialize (ValueR &rec,
const String &fieldname,
const StringS &typedata)
376 value_.index() == Value::RECORD)
388 static bool ASE_NOINLINE
394 static bool ASE_NOINLINE
405 long double limit = 0;
407 valid = valid && tmp >= limit;
409 valid = valid && tmp <= limit;
428 Writ::blank_enum (typeid_name<T>());
443 if (node.
value().index() == Value::STRING)
467 node.
value() = ValueR::empty_record;
468 serialize (obj, node);
472 Writ::not_serializable (typeid_name<T>());
475 rapidjson::Document document (rapidjson::kNullType);
476 Jsonipc::JsonValue &docroot = document;
478 node.
value() = Jsonipc::from_json<Value> (docroot);
489 if (node.
value().index() == Value::RECORD)
490 serialize (obj, node);
493 Writ::not_serializable (typeid_name<T>());
496 rapidjson::Document document (rapidjson::kNullType);
497 Jsonipc::JsonValue &docroot = document;
498 docroot = Jsonipc::to_json<Value> (node.
value(), document.GetAllocator());
513 return node.serialize (serializable);
518 if (node.
value().index() == Value::RECORD)
520 node.serialize (serializable);
528template<
class T>
inline String
532 writ.save (
const_cast<T&
> (source));
533 return writ.to_json();
536template<
class T>
inline bool
540 if (writ.from_json (jsonstring) && writ.load (target))
545template<
class T>
inline T
549 if (writ.from_json (jsonstring))
552 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.
Maps C++ shared_ptr instances to JSON-wrapped objects with unique IDs, supporting polymorphic upcasti...
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<>.
Registers C++ enum values with string names for JSON serialization and TypeScript binding generation.
Jsonipc wrapper type for objects that support field-wise serialization to/from JSON.