libshared: Introduce JSON parser and MessageBuilder.

Based on an earlier piece of source code of mine that parsed JSON into
QObjects, this JSON parser creates a BMessage tree.

Will be used by Stephan in HaikuDepot for communication with the web app.
This commit is contained in:
Augustin Cavalier
2014-08-24 17:36:20 +02:00
committed by Stephan Aßmus
parent ffb0f5db8e
commit e4d2963e01
5 changed files with 622 additions and 0 deletions
+33
View File
@@ -0,0 +1,33 @@
/*
* Copyright 2014, Augustin Cavalier (waddlesplash)
* Distributed under the terms of the MIT License.
*/
#ifndef JSON_H
#define JSON_H
#include <Message.h>
#include <String.h>
namespace BPrivate {
class BJson {
public:
enum JsonObjectType {
JSON_TYPE_MAP = '_JTM',
JSON_TYPE_ARRAY = '_JTA'
};
public:
static status_t Parse(BMessage& message, const char* JSON);
static status_t Parse(BMessage& message, BString& JSON);
private:
static BString _Parser_ParseString(BString& JSON, int32& pos);
static double _Parser_ParseNumber(BString& JSON, int32& pos);
static bool _Parser_ParseConstant(BString& JSON, int32& pos,
const char* constant);
};
} // namespace BPrivate
#endif // JSON_H
+54
View File
@@ -0,0 +1,54 @@
/*
* Copyright 2014, Augustin Cavalier (waddlesplash)
* Distributed under the terms of the MIT License.
*/
#ifndef MESSAGE_BUILDER_H
#define MESSAGE_BUILDER_H
#include <Message.h>
#include <ObjectList.h>
#include <String.h>
namespace BPrivate {
class BMessageBuilder {
public:
BMessageBuilder(BMessage& message);
status_t PushObject(const char* name);
status_t PushObject(uint32 name);
status_t PopObject();
uint32 What();
void SetWhat(uint32 what);
uint32 CountNames(type_code type = B_ANY_TYPE);
// Copied from Message.h
status_t AddString(const char* name, const char* string);
status_t AddString(const char* name,
const BString& string);
status_t AddInt8(const char* name, int8 value);
status_t AddUInt8(const char* name, uint8 value);
status_t AddInt16(const char* name, int16 value);
status_t AddUInt16(const char* name, uint16 value);
status_t AddInt32(const char* name, int32 value);
status_t AddUInt32(const char* name, uint32 value);
status_t AddInt64(const char* name, int64 value);
status_t AddUInt64(const char* name, uint64 value);
status_t AddBool(const char* name, bool value);
status_t AddFloat(const char* name, float value);
status_t AddDouble(const char* name, double value);
status_t AddPointer(const char* name,
const void* pointer);
private:
BObjectList<BMessage> fStack;
BObjectList<BString> fNameStack;
BMessage* fCurrentMessage;
};
} // namespace BPrivate
#endif // MESSAGE_BUILDER_H