xref: /haiku/headers/private/shared/Json.h (revision 4c07199d8201fcf267e90be0d24b76799d03cea6)
1 /*
2  * Copyright 2017-2023, Andrew Lindesay <apl@lindesay.co.nz>
3  * Copyright 2014, Augustin Cavalier (waddlesplash)
4  * Distributed under the terms of the MIT License.
5  */
6 #ifndef _JSON_H
7 #define _JSON_H
8 
9 
10 #include "JsonEventListener.h"
11 
12 #include <Message.h>
13 #include <String.h>
14 
15 
16 namespace BPrivate {
17 
18 class JsonParseContext;
19 
20 class BJson {
21 
22 public:
23 	static	status_t			Parse(const char* JSON, BMessage& message);
24 	static	status_t			Parse(const char* JSON, size_t length,
25 									BMessage& message);
26 	static	status_t			Parse(const BString& JSON, BMessage& message);
27 	static	void				Parse(BDataIO* data,
28 									BJsonEventListener* listener);
29 
30 private:
31 	static	bool				NextChar(JsonParseContext& jsonParseContext,
32 									char* c);
33 	static	bool				NextNonWhitespaceChar(
34 									JsonParseContext& jsonParseContext,
35 									char* c);
36 
37 	static	bool				ParseAny(JsonParseContext& jsonParseContext);
38 	static	bool				ParseObjectNameValuePair(
39 									JsonParseContext& jsonParseContext);
40 	static	bool				ParseObject(JsonParseContext& jsonParseContext);
41 	static	bool				ParseArray(JsonParseContext& jsonParseContext);
42 
43 	static	bool				ParseEscapeUnicodeSequence(JsonParseContext& jsonParseContext);
44 	static	bool				ParseStringEscapeSequence(JsonParseContext& jsonParseContext);
45 	static	bool				ParseString(JsonParseContext& jsonParseContext,
46 									json_event_type eventType);
47 
48 	static	bool				ParseExpectedVerbatimStringAndRaiseEvent(
49 									JsonParseContext& jsonParseContext,
50 									const char* expectedString,
51 									size_t expectedStringLength,
52 									char leadingChar,
53 									json_event_type jsonEventType);
54 	static	bool				ParseExpectedVerbatimString(
55 									JsonParseContext& jsonParseContext,
56 									const char* expectedString,
57 									size_t expectedStringLength,
58 									char leadingChar);
59 
60 	static bool					IsValidNumber(const char* value);
61 	static bool					ParseNumber(JsonParseContext& jsonParseContext);
62 };
63 
64 } // namespace BPrivate
65 
66 using BPrivate::BJson;
67 
68 #endif	// _JSON_H
69