pianod2
multisource multiuser scriptable networked music player
Public Member Functions | Private Attributes | List of all members
UserData::JSONData Class Reference

Store key-value pairs of string keys & mixed types using Parsnip::Data. More...

#include <datastore.h>

Inheritance diagram for UserData::JSONData:
Inheritance graph
[legend]
Collaboration diagram for UserData::JSONData:
Collaboration graph
[legend]

Public Member Functions

 JSONData (const std::string &, const std::string &)
 
 JSONData (const Parsnip::Data &)
 
virtual const std::string & origin (void) const override
 Retrieve the origin (type of data) stored in this storage unit. More...
 
virtual const std::string & identity (void) const override
 Retrieve the identity (primary identifier) of the data in this storage unit. More...
 
virtual bool reconstitute (const Parsnip::Data &data) override
 Restore data from a JSON source. More...
 
virtual Parsnip::Data persist () const override
 Persist data using Parsnip. More...
 
void rename (const std::string new_id)
 
- Public Member Functions inherited from UserData::DataStore
virtual ~DataStore (void)
 
bool isSourceData (void) const
 
- Public Member Functions inherited from Parsnip::Data
 Data (std::nullptr_t null=nullptr)
 Default constructor. More...
 
 Data (const char *value, bool type_certainty=true)
 String constructor. More...
 
 Data (const std::string &value, bool type_certainty=true)
 String constructor, accepting C++ string. More...
 
template<typename T , typename = typename std::enable_if<!std::is_same<T, bool>::value && std::is_integral<T>::value, int>::type>
 Data (T value) noexcept
 Integer constructor. More...
 
 Data (double value) noexcept
 Floating point constructor. More...
 
 Data (bool value) noexcept
 Boolean constructor. More...
 
template<typename... T>
 Data (dictionary_t, T... params)
 Dictionary constructor. More...
 
template<typename... T>
 Data (list_t, T... params)
 List constructor. More...
 
 Data (Data &&from) noexcept
 Move construction. More...
 
Dataoperator= (Data &&from) noexcept
 Move assignment. More...
 
 Data (const Data &from)
 Copy constructor. More...
 
Dataoperator= (const Data &from)
 Copy assignment. More...
 
 ~Data ()
 
bool operator== (const Data &compare) const
 Compare two structures. More...
 
bool operator!= (const Data &compare) const
 
const DatamakeFlexible () const
 IF a data item is a string, change it to a flexible string. More...
 
bool isNull (void) const noexcept
 Check if data contains a value. More...
 
const StringTypeasString () const
 Retrieve string value. More...
 
long asLong (int base=0) const
 Return value of data as a long integer. More...
 
int asInteger (int base=0) const
 Return value of data as an integer. More...
 
double asDouble () const
 Return value of data as a double. If the data is a string, parse it as a float. More...
 
bool asBoolean () const
 Return value of data as a boolean. If the data is a string, translate "true" and "false". More...
 
ListType asList () const
 Return data as a list. More...
 
template<typename DataType , typename = typename std::enable_if<std::is_same<DataType, std::string>::value>::type>
const std::string & as () const
 Extract data as a string (templated version). More...
 
template<typename DataType , typename = typename std::enable_if<!std::is_same<DataType, bool>::value && std::is_integral<DataType>::value>::type>
DataType as (int base=0) const
 Extract data as an integer type, except boolean. More...
 
template<typename DataType , typename = typename std::enable_if<std::is_floating_point<DataType>::value>::type>
DataType as () const
 Extract data as a floating point type. More...
 
template<typename DataType , typename = typename std::enable_if<std::is_same<DataType, bool>::value>::type>
bool as () const
 Extract data as a boolean. More...
 
template<typename DataType , typename = typename std::enable_if<std::is_same<DataType, Data>::value>::type>
const Dataas () const
 Retrieve data as the object itself. More...
 
template<typename Container , typename = typename std::enable_if<std::is_class<Container>::value && !std::is_same<Container, Data>::value && !std::is_same<Container, std::string>::value>::type>
Container as (std::nullptr_t null=nullptr) const
 Return contents of a list as a specific datatype. More...
 
template<typename Datatype >
Datatype getOr (const std::string &name, const Datatype &defvalue) const
 Extract a value if a member exists, otherwise use a default. More...
 
StringType getOr (const std::string &name, const char *defvalue) const
 Specialization of 'getOr' that accepts a const char * default; otherwise, overload resolution chooses implicit instantiation of a Data and uses that as a default rather than implicitly instantiating the string type. More...
 
Data getOr (const std::string &name, const Data &defvalue) const
 Specialization of getOr for Data. More...
 
template<typename Datatype >
std::vector< Datatype > toList () const
 Return a list or a single item in a container. More...
 
const Dataoperator[] (const std::string &word) const
 Dictionary member accessor. More...
 
Dataoperator[] (const std::string &word)
 Dictionary member accessor. More...
 
bool contains (const std::string &word) const
 Check if a key exists in a dictionary. More...
 
void remove (const std::string &word)
 Remove key from dictionary. More...
 
const Dataat (const std::string &word) const
 
template<typename DataType , typename = typename std::enable_if<std::is_trivial<DataType>::value>::type>
void foreach (std::function< void(const std::string &, DataType)> func) const
 Iterate over all dictionary members, calling a function with each key and value. More...
 
template<typename DataType >
void foreach (std::function< void(const std::string &, const DataType &)> func) const
 Specialization of dictionary iterator for strings and Data. More...
 
const Dataoperator[] (size_type index) const
 List member accessor. More...
 
Dataoperator[] (size_type index)
 List member accessor. More...
 
void remove (size_type index)
 Remove entry from position in list. More...
 
void push_back (const Data &value)
 List back-inserter. More...
 
void push_back (Data &&value)
 List back-inserter. More...
 
size_type size () const
 Retrieve number of entries in a list. More...
 
bool empty () const
 Empty check. More...
 
template<typename DataType , typename = typename std::enable_if<std::is_trivial<Type>::value>::type>
void foreach (std::function< void(const DataType)> func) const
 Iterate over all list members, calling a function with each value. More...
 
template<typename DataType >
void foreach (std::function< void(const DataType &)> func) const
 Specialization of list iterator for strings and Data. More...
 
Data::iterator begin () const
 Retrieve forward iterator. More...
 
Data::iterator end () const
 Retrieve end iterator. More...
 
std::string toJson (int indent=NoIndent) const
 Return serial data encoded using JSON. More...
 
std::ostream & toJson (std::ostream &target, int indent=NoIndent, bool suppress=false) const
 Write serial data encoded using JSON. More...
 
std::ostream & dumpJson (const std::string &intro, std::ostream &target=std::clog) const
 

Private Attributes

const std::string originid
 
const std::string id
 

Additional Inherited Members

- Public Types inherited from Parsnip::Data
enum class  Type {
  Null , Dictionary , List , String ,
  FlexibleString , Integer , Real , Boolean
}
 Types that may be stored in the data. More...
 
using StringType = std::string
 
using ListType = std::vector< Data >
 
using size_type = ListType::size_type
 
- Static Public Member Functions inherited from UserData::DataStore
static bool isSourceData (const std::string &origin)
 
- Static Public Member Functions inherited from Parsnip::Data
static const char * type_name (Type ty)
 Return the name corresponding to a type. More...
 
template<typename T = Data>
static Data make_dictionary (std::initializer_list< std::pair< const char *, const T >> initial_data)
 Dictionary factory. More...
 
template<typename... T>
static Data make_list (T &&... params)
 List factory. More...
 
- Static Public Attributes inherited from Parsnip::Data
static const struct Parsnip::Data::dictionary_t Dictionary
 Dictionary flag type/value. More...
 
static const struct Parsnip::Data::list_t List
 List flag type/value. More...
 
static constexpr bool Flexible {false}
 Flag to make strings flexible. More...
 
static constexpr int NoIndent {-32767}
 Prevent indentation. More...
 
- Protected Types inherited from Parsnip::Data
using DictionaryType = std::unordered_map< std::string, Data >
 
- Protected Member Functions inherited from Parsnip::Data
void release ()
 Release any stored data and restore to an "empty" state. More...
 
- Protected Attributes inherited from Parsnip::Data
Type datatype = Type::Null
 Type presently stored in object. More...
 
union Parsnip::Data::data_t data
 

Detailed Description

Store key-value pairs of string keys & mixed types using Parsnip::Data.


Constructor & Destructor Documentation

◆ JSONData() [1/2]

UserData::JSONData::JSONData ( const std::string &  _origin,
const std::string &  _id 
)

◆ JSONData() [2/2]

UserData::JSONData::JSONData ( const Parsnip::Data data)
Here is the call graph for this function:

Member Function Documentation

◆ identity()

const std::string & UserData::JSONData::identity ( void  ) const
overridevirtual

Retrieve the identity (primary identifier) of the data in this storage unit.

Implements UserData::DataStore.

Here is the caller graph for this function:

◆ origin()

const std::string & UserData::JSONData::origin ( void  ) const
overridevirtual

Retrieve the origin (type of data) stored in this storage unit.

Implements UserData::DataStore.

Here is the caller graph for this function:

◆ persist()

Parsnip::Data UserData::JSONData::persist ( ) const
overridevirtual

Persist data using Parsnip.

Returns
A Parsnip Data object suitable for serialization.

Implements UserData::DataStore.

Here is the call graph for this function:

◆ reconstitute()

bool UserData::JSONData::reconstitute ( const Parsnip::Data data)
overridevirtual

Restore data from a JSON source.

Parameters
dataData to restore from.
Returns
True on success, false otherwise.

Implements UserData::DataStore.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ rename()

void UserData::JSONData::rename ( const std::string  new_id)
Here is the caller graph for this function:

Member Data Documentation

◆ id

const std::string UserData::JSONData::id
private

◆ originid

const std::string UserData::JSONData::originid
private

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