TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Deserializer Class Reference

Public Member Functions

bool ReadMessage (std::string json, google::protobuf::Message *message)
 
bool Key (const Ch *str, rapidjson::SizeType length, bool copy)
 
bool Null ()
 
bool Bool (bool b)
 
bool Int (int32 i)
 
bool Uint (uint32 i)
 
bool Int64 (int64 i)
 
bool Uint64 (uint64 i)
 
bool Double (double d)
 
bool String (const Ch *str, rapidjson::SizeType length, bool copy)
 
bool StartObject ()
 
bool EndObject (rapidjson::SizeType memberCount)
 
bool StartArray ()
 
bool EndArray (rapidjson::SizeType memberCount)
 
std::vector< std::string > constGetErrors () const
 

Private Member Functions

bool CheckType (google::protobuf::FieldDescriptor::CppType expectedType)
 

Private Attributes

rapidjson::Reader _reader
 
std::stack
< google::protobuf::FieldDescriptor
const * > 
_state
 
std::stack
< google::protobuf::Message * > 
_objectState
 
std::vector< std::string > _errors
 

Member Function Documentation

bool Deserializer::Bool ( bool  b)
256 {
258  return false;
259 
260  SET_FIELD(_objectState.top(), _state.top(), Bool, b);
261  return true;
262 }
bool Bool(bool b)
Definition: ProtobufJSON.cpp:255
std::stack< google::protobuf::FieldDescriptor const * > _state
Definition: ProtobufJSON.cpp:210
std::stack< google::protobuf::Message * > _objectState
Definition: ProtobufJSON.cpp:211
#define SET_FIELD(message, field, Type, val)
Definition: ProtobufJSON.cpp:247
bool CheckType(google::protobuf::FieldDescriptor::CppType expectedType)
Definition: ProtobufJSON.cpp:418

+ Here is the call graph for this function:

bool Deserializer::CheckType ( google::protobuf::FieldDescriptor::CppType  expectedType)
private
419 {
420  if (_state.top()->cpp_type() != expectedType)
421  {
422  _errors.push_back(Trinity::StringFormat("Expected field %s type to be %s but got %s instead.",
423  _state.top()->full_name().c_str(), google::protobuf::FieldDescriptor::CppTypeName(expectedType), _state.top()->cpp_type_name()));
424  return false;
425  }
426 
427  return true;
428 }
std::stack< google::protobuf::FieldDescriptor const * > _state
Definition: ProtobufJSON.cpp:210
static const char * CppTypeName(CppType cpp_type)
Definition: descriptor.h:1657
std::string StringFormat(Format &&fmt, Args &&...args)
Default TC string format function.
Definition: StringFormat.h:28
std::vector< std::string > _errors
Definition: ProtobufJSON.cpp:212

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool Deserializer::Double ( double  d)
321 {
322  google::protobuf::FieldDescriptor const* field = _state.top();
323  google::protobuf::Message* message = _objectState.top();
324  switch (field->cpp_type())
325  {
327  SET_FIELD(message, field, Float, d);
328  break;
330  SET_FIELD(message, field, Double, d);
331  break;
332  default:
333  _errors.push_back(Trinity::StringFormat("Expected field type to be float or double but got %s instead.", _state.top()->cpp_type_name()));
334  return false;
335  }
336 
337  return true;
338 }
CppType cpp_type() const
Definition: descriptor.h:1641
Definition: message.h:165
std::stack< google::protobuf::FieldDescriptor const * > _state
Definition: ProtobufJSON.cpp:210
std::stack< google::protobuf::Message * > _objectState
Definition: ProtobufJSON.cpp:211
std::string StringFormat(Format &&fmt, Args &&...args)
Default TC string format function.
Definition: StringFormat.h:28
Definition: descriptor.h:342
std::vector< std::string > _errors
Definition: ProtobufJSON.cpp:212
#define SET_FIELD(message, field, Type, val)
Definition: ProtobufJSON.cpp:247
bool Double(double d)
Definition: ProtobufJSON.cpp:320

+ Here is the call graph for this function:

bool Deserializer::EndArray ( rapidjson::SizeType  memberCount)
431 {
432  _state.pop();
433  return true;
434 }
std::stack< google::protobuf::FieldDescriptor const * > _state
Definition: ProtobufJSON.cpp:210
bool Deserializer::EndObject ( rapidjson::SizeType  memberCount)
393 {
394  if (!_state.empty() && !_state.top()->is_repeated())
395  _state.pop();
396 
397  _objectState.pop();
398  return true;
399 }
std::stack< google::protobuf::FieldDescriptor const * > _state
Definition: ProtobufJSON.cpp:210
std::stack< google::protobuf::Message * > _objectState
Definition: ProtobufJSON.cpp:211
std::vector<std::string> const& Deserializer::GetErrors ( ) const
inline
204 { return _errors; }
std::vector< std::string > _errors
Definition: ProtobufJSON.cpp:212

+ Here is the caller graph for this function:

bool Deserializer::Int ( int32  i)
265 {
267  return false;
268 
269  SET_FIELD(_objectState.top(), _state.top(), Int32, i);
270  return true;
271 }
std::stack< google::protobuf::FieldDescriptor const * > _state
Definition: ProtobufJSON.cpp:210
std::stack< google::protobuf::Message * > _objectState
Definition: ProtobufJSON.cpp:211
int Int32
Definition: bzlib_private.h:44
#define SET_FIELD(message, field, Type, val)
Definition: ProtobufJSON.cpp:247
bool CheckType(google::protobuf::FieldDescriptor::CppType expectedType)
Definition: ProtobufJSON.cpp:418

+ Here is the call graph for this function:

bool Deserializer::Int64 ( int64  i)
303 {
305  return false;
306 
307  SET_FIELD(_objectState.top(), _state.top(), Int64, i);
308  return true;
309 }
bool Int64(int64 i)
Definition: ProtobufJSON.cpp:302
std::stack< google::protobuf::FieldDescriptor const * > _state
Definition: ProtobufJSON.cpp:210
std::stack< google::protobuf::Message * > _objectState
Definition: ProtobufJSON.cpp:211
#define SET_FIELD(message, field, Type, val)
Definition: ProtobufJSON.cpp:247
bool CheckType(google::protobuf::FieldDescriptor::CppType expectedType)
Definition: ProtobufJSON.cpp:418

+ Here is the call graph for this function:

bool Deserializer::Key ( const Ch *  str,
rapidjson::SizeType  length,
bool  copy 
)
229 {
230  google::protobuf::FieldDescriptor const* field = _objectState.top()->GetDescriptor()->FindFieldByName(str);
231  if (!field)
232  {
233  _errors.push_back(Trinity::StringFormat("Message %s has no field %s.", _objectState.top()->GetTypeName().c_str(), str));
234  return false;
235  }
236 
237  _state.push(field);
238  return true;
239 }
std::stack< google::protobuf::FieldDescriptor const * > _state
Definition: ProtobufJSON.cpp:210
std::stack< google::protobuf::Message * > _objectState
Definition: ProtobufJSON.cpp:211
std::string StringFormat(Format &&fmt, Args &&...args)
Default TC string format function.
Definition: StringFormat.h:28
Definition: descriptor.h:342
std::vector< std::string > _errors
Definition: ProtobufJSON.cpp:212

+ Here is the call graph for this function:

bool Deserializer::Null ( )
242 {
243  _state.pop();
244  return true;
245 }
std::stack< google::protobuf::FieldDescriptor const * > _state
Definition: ProtobufJSON.cpp:210
bool Deserializer::ReadMessage ( std::string  json,
google::protobuf::Message message 
)
216 {
217  rapidjson::StringStream ss(json.c_str());
218 
219  _objectState.push(message);
220 
221  rapidjson::ParseResult result = _reader.Parse(ss, *this);
222 
223  ASSERT(result.IsError() || (_objectState.empty() && _state.empty()));
224 
225  return !result.IsError() && _errors.empty();
226 }
GenericStringStream< UTF8<> > StringStream
String stream with UTF8 encoding.
Definition: rapidjson.h:595
std::stack< google::protobuf::FieldDescriptor const * > _state
Definition: ProtobufJSON.cpp:210
std::stack< google::protobuf::Message * > _objectState
Definition: ProtobufJSON.cpp:211
rapidjson::Reader _reader
Definition: ProtobufJSON.cpp:209
std::vector< std::string > _errors
Definition: ProtobufJSON.cpp:212
#define ASSERT
Definition: Errors.h:55

+ Here is the caller graph for this function:

bool Deserializer::StartArray ( )
402 {
403  if (_state.empty())
404  {
405  _errors.emplace_back("Root cannot be an array.");
406  return false;
407  }
408 
409  if (_state.top()->is_repeated() ^ (_state.top()->type() != google::protobuf::FieldDescriptor::TYPE_BYTES))
410  {
411  _errors.push_back(Trinity::StringFormat("Expected field %s type to be exactly an array OR bytes but it was both or none.", _state.top()->full_name().c_str()));
412  return false;
413  }
414 
415  return true;
416 }
std::stack< google::protobuf::FieldDescriptor const * > _state
Definition: ProtobufJSON.cpp:210
std::string StringFormat(Format &&fmt, Args &&...args)
Default TC string format function.
Definition: StringFormat.h:28
std::vector< std::string > _errors
Definition: ProtobufJSON.cpp:212

+ Here is the call graph for this function:

bool Deserializer::StartObject ( )
370 {
371  // not a root object
372  if (!_state.empty())
373  {
375  {
376  _errors.push_back(Trinity::StringFormat("Expected field %s to be a message but got %s instead.", _state.top()->cpp_type_name()));
377  return false;
378  }
379 
380  google::protobuf::Message* containingMessage = _objectState.top();
381  if (!_state.top()->is_repeated())
382  _objectState.push(containingMessage->GetReflection()->MutableMessage(containingMessage, _state.top()));
383  else
384  _objectState.push(containingMessage->GetReflection()->AddMessage(containingMessage, _state.top()));
385  }
386  else if (_objectState.size() != 1)
387  return false;
388 
389  return true;
390 }
virtual Message * AddMessage(Message *message, const FieldDescriptor *field, MessageFactory *factory=NULL) const =0
Definition: message.h:165
std::stack< google::protobuf::FieldDescriptor const * > _state
Definition: ProtobufJSON.cpp:210
virtual Message * MutableMessage(Message *message, const FieldDescriptor *field, MessageFactory *factory=NULL) const =0
std::stack< google::protobuf::Message * > _objectState
Definition: ProtobufJSON.cpp:211
virtual const Reflection * GetReflection() const
Definition: message.h:301
std::string StringFormat(Format &&fmt, Args &&...args)
Default TC string format function.
Definition: StringFormat.h:28
std::vector< std::string > _errors
Definition: ProtobufJSON.cpp:212

+ Here is the call graph for this function:

bool Deserializer::String ( const Ch *  str,
rapidjson::SizeType  length,
bool  copy 
)
341 {
342  google::protobuf::FieldDescriptor const* field = _state.top();
343  google::protobuf::Message* message = _objectState.top();
344  switch (field->cpp_type())
345  {
347  {
348  google::protobuf::EnumValueDescriptor const* enumValue = field->enum_type()->FindValueByName(str);
349  if (!enumValue)
350  {
351  _errors.push_back(Trinity::StringFormat("Field %s enum %s does not have a value named %s.", field->full_name().c_str(), field->enum_type()->full_name().c_str(), str));
352  return false;
353  }
354 
355  SET_FIELD(message, field, Enum, enumValue);
356  break;
357  }
359  SET_FIELD(message, field, String, str);
360  break;
361  default:
362  _errors.push_back(Trinity::StringFormat("Expected field type to be string or enum but got %s instead.", _state.top()->cpp_type_name()));
363  return false;
364  }
365 
366  return true;
367 }
const EnumValueDescriptor * FindValueByName(const string &name) const
const string & full_name() const
CppType cpp_type() const
Definition: descriptor.h:1641
const string & full_name() const
const EnumDescriptor * enum_type() const
Definition: descriptor.h:780
Definition: message.h:165
std::stack< google::protobuf::FieldDescriptor const * > _state
Definition: ProtobufJSON.cpp:210
bool String(const Ch *str, rapidjson::SizeType length, bool copy)
Definition: ProtobufJSON.cpp:340
std::stack< google::protobuf::Message * > _objectState
Definition: ProtobufJSON.cpp:211
std::string StringFormat(Format &&fmt, Args &&...args)
Default TC string format function.
Definition: StringFormat.h:28
Definition: descriptor.h:342
std::vector< std::string > _errors
Definition: ProtobufJSON.cpp:212
#define SET_FIELD(message, field, Type, val)
Definition: ProtobufJSON.cpp:247

+ Here is the call graph for this function:

bool Deserializer::Uint ( uint32  i)
274 {
275  google::protobuf::FieldDescriptor const* field = _state.top();
276  google::protobuf::Message* message = _objectState.top();
277  switch (field->cpp_type())
278  {
280  SET_FIELD(message, field, UInt32, i);
281  break;
283  {
285  {
286  _errors.emplace_back("Expected field type to be bytes but got string instead.");
287  return false;
288  }
289  std::string currentValue = message->GetReflection()->GetString(*message, field);
290  currentValue.append(1, (char)i);
291  message->GetReflection()->SetString(message, field, currentValue);
292  break;
293  }
294  default:
295  _errors.push_back(Trinity::StringFormat("Expected field type to be uint32 or string but got %s instead.", _state.top()->cpp_type_name()));
296  return false;
297  }
298 
299  return true;
300 }
CppType cpp_type() const
Definition: descriptor.h:1641
Definition: message.h:165
std::stack< google::protobuf::FieldDescriptor const * > _state
Definition: ProtobufJSON.cpp:210
virtual void SetString(Message *message, const FieldDescriptor *field, const string &value) const =0
std::stack< google::protobuf::Message * > _objectState
Definition: ProtobufJSON.cpp:211
virtual const Reflection * GetReflection() const
Definition: message.h:301
unsigned int UInt32
Definition: bzlib_private.h:45
std::string StringFormat(Format &&fmt, Args &&...args)
Default TC string format function.
Definition: StringFormat.h:28
Definition: descriptor.h:342
std::vector< std::string > _errors
Definition: ProtobufJSON.cpp:212
#define SET_FIELD(message, field, Type, val)
Definition: ProtobufJSON.cpp:247
virtual string GetString(const Message &message, const FieldDescriptor *field) const =0

+ Here is the call graph for this function:

bool Deserializer::Uint64 ( uint64  i)
312 {
314  return false;
315 
316  SET_FIELD(_objectState.top(), _state.top(), UInt64, i);
317  return true;
318 }
std::stack< google::protobuf::FieldDescriptor const * > _state
Definition: ProtobufJSON.cpp:210
std::stack< google::protobuf::Message * > _objectState
Definition: ProtobufJSON.cpp:211
#define SET_FIELD(message, field, Type, val)
Definition: ProtobufJSON.cpp:247
bool CheckType(google::protobuf::FieldDescriptor::CppType expectedType)
Definition: ProtobufJSON.cpp:418

+ Here is the call graph for this function:

Member Data Documentation

std::vector<std::string> Deserializer::_errors
private
std::stack<google::protobuf::Message*> Deserializer::_objectState
private
rapidjson::Reader Deserializer::_reader
private
std::stack<google::protobuf::FieldDescriptor const*> Deserializer::_state
private

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