1 /* 2 * Copyright 2017, 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 static bool ParseEscapeUnicodeSequence( 43 JsonParseContext& jsonParseContext, 44 BString& stringResult); 45 static bool ParseStringEscapeSequence( 46 JsonParseContext& jsonParseContext, 47 BString& stringResult); 48 static bool ParseString(JsonParseContext& jsonParseContext, 49 json_event_type eventType); 50 static bool ParseExpectedVerbatimStringAndRaiseEvent( 51 JsonParseContext& jsonParseContext, 52 const char* expectedString, 53 size_t expectedStringLength, 54 char leadingChar, 55 json_event_type jsonEventType); 56 static bool ParseExpectedVerbatimString( 57 JsonParseContext& jsonParseContext, 58 const char* expectedString, 59 size_t expectedStringLength, 60 char leadingChar); 61 62 static bool IsValidNumber(BString& number); 63 static bool ParseNumber(JsonParseContext& jsonParseContext); 64 }; 65 66 } // namespace BPrivate 67 68 using BPrivate::BJson; 69 70 #endif // _JSON_H 71