diff --git a/src/kits/shared/Json.cpp b/src/kits/shared/Json.cpp index 284c3e5406..3784740f3f 100644 --- a/src/kits/shared/Json.cpp +++ b/src/kits/shared/Json.cpp @@ -4,6 +4,8 @@ * Copyright 2014, Stephan Aßmus * Distributed under the terms of the MIT License. */ + + #include "Json.h" #include @@ -25,7 +27,8 @@ namespace BPrivate { static bool -b_jsonparse_is_hex(char c) { +b_jsonparse_is_hex(char c) +{ return isdigit(c) || (c > 0x41 && c <= 0x46) || (c > 0x61 && c <= 0x66); @@ -33,7 +36,8 @@ b_jsonparse_is_hex(char c) { static bool -b_jsonparse_all_hex(const char* c) { +b_jsonparse_all_hex(const char* c) +{ for (int i = 0; i < 4; i++) { if (!b_jsonparse_is_hex(c[i])) return false; @@ -57,27 +61,37 @@ public: { } - BJsonEventListener* Listener() const { + + BJsonEventListener* Listener() const + { return fListener; } - BDataIO* Data() const { + + BDataIO* Data() const + { return fData; } - int LineNumber() const { + + int LineNumber() const + { return fLineNumber; } - int IncrementLineNumber() { - return fLineNumber++; + + void IncrementLineNumber() + { + fLineNumber++; } + // TODO; there is considerable opportunity for performance improvements // here by buffering the input and then feeding it into the parse // algorithm character by character. - status_t NextChar(char* buffer) { + status_t NextChar(char* buffer) + { if (fHasPushbackChar) { buffer[0] = fPushbackChar; @@ -88,7 +102,9 @@ public: return Data()->ReadExactly(buffer, 1); } - void PushbackChar(char c) { + + void PushbackChar(char c) + { fPushbackChar = c; fHasPushbackChar = true; } @@ -156,14 +172,18 @@ BJson::NextChar(JsonParseContext& jsonParseContext, char* c) return true; case B_PARTIAL_READ: + { jsonParseContext.Listener()->HandleError(B_BAD_DATA, jsonParseContext.LineNumber(), "unexpected end of input"); return false; + } default: + { jsonParseContext.Listener()->HandleError(result, -1, "io related read error"); return false; + } } } @@ -272,10 +292,8 @@ BJson::ParseObjectNameValuePair(JsonParseContext& jsonParseContext) case '\"': // name of the object { if (!didParseName) { - if (!ParseString(jsonParseContext, - B_JSON_OBJECT_NAME)) { + if (!ParseString(jsonParseContext, B_JSON_OBJECT_NAME)) return false; - } didParseName = true; } else { @@ -323,7 +341,7 @@ bool BJson::ParseObject(JsonParseContext& jsonParseContext) { if (!jsonParseContext.Listener()->Handle( - BJsonEvent(B_JSON_OBJECT_START))) { + BJsonEvent(B_JSON_OBJECT_START))) { return false; } @@ -336,13 +354,16 @@ BJson::ParseObject(JsonParseContext& jsonParseContext) switch (c) { case '}': // terminate the object + { if (!jsonParseContext.Listener()->Handle( - BJsonEvent(B_JSON_OBJECT_END))) { + BJsonEvent(B_JSON_OBJECT_END))) { return false; } return true; + } case ',': // next value. + { if (firstItem) { jsonParseContext.Listener()->HandleError(B_BAD_DATA, jsonParseContext.LineNumber(), "unexpected" @@ -354,6 +375,7 @@ BJson::ParseObject(JsonParseContext& jsonParseContext) if (!ParseObjectNameValuePair(jsonParseContext)) return false; break; + } default: { @@ -379,7 +401,7 @@ bool BJson::ParseArray(JsonParseContext& jsonParseContext) { if (!jsonParseContext.Listener()->Handle( - BJsonEvent(B_JSON_ARRAY_START))) { + BJsonEvent(B_JSON_ARRAY_START))) { return false; } @@ -392,13 +414,16 @@ BJson::ParseArray(JsonParseContext& jsonParseContext) switch (c) { case ']': // terminate the array + { if (!jsonParseContext.Listener()->Handle( - BJsonEvent(B_JSON_ARRAY_END))) { + BJsonEvent(B_JSON_ARRAY_END))) { return false; } return true; + } case ',': // next value. + { if (firstItem) { jsonParseContext.Listener()->HandleError(B_BAD_DATA, jsonParseContext.LineNumber(), "unexpected" @@ -409,6 +434,7 @@ BJson::ParseArray(JsonParseContext& jsonParseContext) if (!ParseAny(jsonParseContext)) return false; break; + } default: { @@ -445,25 +471,25 @@ BJson::ParseEscapeUnicodeSequence(JsonParseContext& jsonParseContext, } if (!b_jsonparse_all_hex(buffer)) { - BString errorMessage; - errorMessage.SetToFormat( - "malformed unicode sequence [%s] in string parsing", - buffer); - jsonParseContext.Listener()->HandleError(B_BAD_DATA, - jsonParseContext.LineNumber(), errorMessage.String()); - return false; + BString errorMessage; + errorMessage.SetToFormat( + "malformed unicode sequence [%s] in string parsing", + buffer); + jsonParseContext.Listener()->HandleError(B_BAD_DATA, + jsonParseContext.LineNumber(), errorMessage.String()); + return false; } uint intValue; if (sscanf(buffer, "%4x", &intValue) != 1) { - BString errorMessage; - errorMessage.SetToFormat( - "unable to process unicode sequence [%s] in string " - " parsing", buffer); - jsonParseContext.Listener()->HandleError(B_BAD_DATA, - jsonParseContext.LineNumber(), errorMessage.String()); - return false; + BString errorMessage; + errorMessage.SetToFormat( + "unable to process unicode sequence [%s] in string " + " parsing", buffer); + jsonParseContext.Listener()->HandleError(B_BAD_DATA, + jsonParseContext.LineNumber(), errorMessage.String()); + return false; } char character[7]; @@ -511,12 +537,14 @@ BJson::ParseStringEscapeSequence(JsonParseContext& jsonParseContext, stringResult += "\""; break; case 'u': + { // unicode escape sequence. if (!ParseEscapeUnicodeSequence(jsonParseContext, - stringResult)) { + stringResult)) { return false; } break; + } default: { BString errorMessage; @@ -546,19 +574,24 @@ BJson::ParseString(JsonParseContext& jsonParseContext, switch (c) { case '"': + { // terminates the string assembled so far. jsonParseContext.Listener()->Handle( BJsonEvent(eventType, stringResult.String())); return true; + } case '\\': + { if (!ParseStringEscapeSequence(jsonParseContext, stringResult)) { return false; } break; + } default: + { uint8 uc = static_cast(c); if(uc < 0x20) { // control characters are not allowed @@ -573,6 +606,7 @@ BJson::ParseString(JsonParseContext& jsonParseContext, stringResult.Append(&c, 1); break; + } } } } @@ -585,7 +619,7 @@ BJson::ParseExpectedVerbatimStringAndRaiseEvent( json_event_type jsonEventType) { if (ParseExpectedVerbatimString(jsonParseContext, expectedString, - expectedStringLength, leadingChar)) { + expectedStringLength, leadingChar)) { if (!jsonParseContext.Listener()->Handle(BJsonEvent(jsonEventType))) return false; } @@ -695,6 +729,7 @@ BJson::ParseNumber(JsonParseContext& jsonParseContext) switch (result) { case B_OK: + { if (isdigit(c)) { value += c; break; @@ -707,7 +742,7 @@ BJson::ParseNumber(JsonParseContext& jsonParseContext) jsonParseContext.PushbackChar(c); // intentional fall through - + } case B_PARTIAL_READ: { errno = 0; @@ -723,11 +758,12 @@ BJson::ParseNumber(JsonParseContext& jsonParseContext) return true; } - default: + { jsonParseContext.Listener()->HandleError(result, -1, "io related read error"); return false; + } } } } diff --git a/src/kits/shared/JsonEvent.cpp b/src/kits/shared/JsonEvent.cpp index a63e694435..87687216a3 100644 --- a/src/kits/shared/JsonEvent.cpp +++ b/src/kits/shared/JsonEvent.cpp @@ -2,6 +2,8 @@ * Copyright 2017, Andrew Lindesay * Distributed under the terms of the MIT License. */ + + #include "JsonEvent.h" #include diff --git a/src/kits/shared/JsonEventListener.cpp b/src/kits/shared/JsonEventListener.cpp index acc8fbedc7..44b28aadd4 100644 --- a/src/kits/shared/JsonEventListener.cpp +++ b/src/kits/shared/JsonEventListener.cpp @@ -3,6 +3,7 @@ * Distributed under the terms of the MIT License. */ + #include "JsonEventListener.h" diff --git a/src/kits/shared/JsonMessageWriter.cpp b/src/kits/shared/JsonMessageWriter.cpp index 2bb44e58fa..ae08ccd123 100644 --- a/src/kits/shared/JsonMessageWriter.cpp +++ b/src/kits/shared/JsonMessageWriter.cpp @@ -3,9 +3,8 @@ * Distributed under the terms of the MIT License. */ -#include "JsonMessageWriter.h" -#include +#include "JsonMessageWriter.h" namespace BPrivate { @@ -179,19 +178,25 @@ BStackedMessageEventListener::Handle(const BJsonEvent& event) break; case B_JSON_OBJECT_START: + { SetStackedListenerOnWriter(new BStackedObjectMessageEventListener( fWriter, this)); break; + } case B_JSON_ARRAY_START: + { SetStackedListenerOnWriter(new BStackedArrayMessageEventListener( fWriter, this)); break; + } default: + { HandleError(B_NOT_ALLOWED, JSON_EVENT_LISTENER_ANY_LINE, - "unexpected type of json item to add to container"); - return false; + "unexpected type of json item to add to container"); + return false; + } } return ErrorStatus() == B_OK; @@ -304,7 +309,8 @@ BStackedMessageEventListener::SetStackedListenerOnWriter( BStackedArrayMessageEventListener::BStackedArrayMessageEventListener( BJsonMessageWriter* writer, BStackedMessageEventListener* parent) - : BStackedMessageEventListener(writer, parent, B_JSON_MESSAGE_WHAT_ARRAY) + : + BStackedMessageEventListener(writer, parent, B_JSON_MESSAGE_WHAT_ARRAY) { fCount = 0; } @@ -314,7 +320,8 @@ BStackedArrayMessageEventListener::BStackedArrayMessageEventListener( BJsonMessageWriter* writer, BStackedMessageEventListener* parent, BMessage* message) - : BStackedMessageEventListener(writer, parent, message) + : + BStackedMessageEventListener(writer, parent, message) { message->what = B_JSON_MESSAGE_WHAT_ARRAY; fCount = 0; @@ -334,11 +341,13 @@ BStackedArrayMessageEventListener::Handle(const BJsonEvent& event) switch (event.EventType()) { case B_JSON_ARRAY_END: + { if (fParent != NULL) fParent->AddMessage(fMessage); SetStackedListenerOnWriter(fParent); delete this; break; + } default: return BStackedMessageEventListener::Handle(event); @@ -370,7 +379,8 @@ BStackedArrayMessageEventListener::DidAdd() BStackedObjectMessageEventListener::BStackedObjectMessageEventListener( BJsonMessageWriter* writer, BStackedMessageEventListener* parent) - : BStackedMessageEventListener(writer, parent, B_JSON_MESSAGE_WHAT_OBJECT) + : + BStackedMessageEventListener(writer, parent, B_JSON_MESSAGE_WHAT_OBJECT) { } @@ -379,7 +389,8 @@ BStackedObjectMessageEventListener::BStackedObjectMessageEventListener( BJsonMessageWriter* writer, BStackedMessageEventListener* parent, BMessage* message) - : BStackedMessageEventListener(writer, parent, message) + : + BStackedMessageEventListener(writer, parent, message) { message->what = B_JSON_MESSAGE_WHAT_OBJECT; } @@ -398,11 +409,13 @@ BStackedObjectMessageEventListener::Handle(const BJsonEvent& event) switch (event.EventType()) { case B_JSON_OBJECT_END: + { if (fParent != NULL) fParent->AddMessage(fMessage); SetStackedListenerOnWriter(fParent); delete this; break; + } case B_JSON_OBJECT_NAME: fNextItemName.SetTo(event.Content()); @@ -479,22 +492,27 @@ BJsonMessageWriter::Handle(const BJsonEvent& event) else { switch(event.EventType()) { case B_JSON_OBJECT_START: + { SetStackedListener(new BStackedObjectMessageEventListener( this, NULL, fTopLevelMessage)); break; + } case B_JSON_ARRAY_START: + { fTopLevelMessage->what = B_JSON_MESSAGE_WHAT_ARRAY; SetStackedListener(new BStackedArrayMessageEventListener( this, NULL, fTopLevelMessage)); break; + } default: + { HandleError(B_NOT_ALLOWED, JSON_EVENT_LISTENER_ANY_LINE, "a message object can only handle an object or an array" "at the top level"); return false; - + } } } diff --git a/src/kits/shared/JsonTextWriter.cpp b/src/kits/shared/JsonTextWriter.cpp index 4f948b26f7..35c95122ff 100644 --- a/src/kits/shared/JsonTextWriter.cpp +++ b/src/kits/shared/JsonTextWriter.cpp @@ -3,6 +3,7 @@ * Distributed under the terms of the MIT License. */ + #include "JsonTextWriter.h" #include @@ -22,7 +23,8 @@ b_json_is_7bit_clean(uint8 c) static bool -b_json_is_illegal(uint8 c) { +b_json_is_illegal(uint8 c) +{ return c < 0x20 || c == 0x7f; } @@ -210,27 +212,35 @@ BJsonTextWriterStackedEventListener::Handle(const BJsonEvent& event) break; case B_JSON_OBJECT_START: + { writeResult = StreamChar('{'); if (writeResult == B_OK) { SetStackedListenerOnWriter( - new BJsonTextWriterObjectStackedEventListener(fWriter, this)); + new BJsonTextWriterObjectStackedEventListener( + fWriter, this)); } break; + } case B_JSON_ARRAY_START: + { writeResult = StreamChar('['); if (writeResult == B_OK) { SetStackedListenerOnWriter( - new BJsonTextWriterArrayStackedEventListener(fWriter, this)); + new BJsonTextWriterArrayStackedEventListener( + fWriter, this)); } break; + } default: + { HandleError(B_NOT_ALLOWED, JSON_EVENT_LISTENER_ANY_LINE, - "unexpected type of json item to add to container"); - return false; + "unexpected type of json item to add to container"); + return false; + } } if (writeResult == B_OK) @@ -313,15 +323,16 @@ BJsonTextWriterStackedEventListener::StreamStringEncoded(const char* string, status_t -BJsonTextWriterStackedEventListener::StreamQuotedEncodedString(const char* string) +BJsonTextWriterStackedEventListener::StreamQuotedEncodedString( + const char* string) { return fWriter->StreamQuotedEncodedString(string); } status_t -BJsonTextWriterStackedEventListener::StreamQuotedEncodedString(const char* string, - off_t offset, size_t length) +BJsonTextWriterStackedEventListener::StreamQuotedEncodedString( + const char* string, off_t offset, size_t length) { return fWriter->StreamQuotedEncodedString(string, offset, length); } @@ -362,7 +373,8 @@ BJsonTextWriterStackedEventListener::SetStackedListenerOnWriter( BJsonTextWriterArrayStackedEventListener::BJsonTextWriterArrayStackedEventListener( BJsonTextWriter* writer, BJsonTextWriterStackedEventListener* parent) - : BJsonTextWriterStackedEventListener(writer, parent) + : + BJsonTextWriterStackedEventListener(writer, parent) { } @@ -431,7 +443,8 @@ BJsonTextWriterArrayStackedEventListener::WillAdd() BJsonTextWriterObjectStackedEventListener::BJsonTextWriterObjectStackedEventListener( BJsonTextWriter* writer, BJsonTextWriterStackedEventListener* parent) - : BJsonTextWriterStackedEventListener(writer, parent) + : + BJsonTextWriterStackedEventListener(writer, parent) { } @@ -591,7 +604,7 @@ BJsonTextWriter::StreamStringEncoded(const char* string, status_t writeResult = B_OK; uint8* string8bit = (uint8*)string; - while (writeResult == B_OK && 0 != length) { + while (writeResult == B_OK && length != 0) { uint8 c = string8bit[offset]; const char* simpleEsc = b_json_simple_esc_sequence(c); @@ -633,11 +646,9 @@ BJsonTextWriter::StreamStringEncoded(const char* string, offset++; length--; } else { - // if the character is < 128 then it can be rendered // verbatim - check how many are like this and then // render those verbatim. - const char* stringInitial = &string[offset]; uint32 unicodeCharacter = BUnicodeChar::FromUTF8( &stringInitial); @@ -692,6 +703,4 @@ status_t BJsonTextWriter::StreamChar(char c) { return fDataIO->WriteExactly(&c, 1); -} - - +} \ No newline at end of file diff --git a/src/kits/shared/JsonWriter.cpp b/src/kits/shared/JsonWriter.cpp index 8a95270ef8..eea12b6282 100644 --- a/src/kits/shared/JsonWriter.cpp +++ b/src/kits/shared/JsonWriter.cpp @@ -3,6 +3,7 @@ * Distributed under the terms of the MIT License. */ + #include "JsonWriter.h" #include @@ -30,7 +31,6 @@ BJsonWriter::HandleError(status_t status, int32 line, if(fErrorStatus == B_OK) { if (message == NULL) message = "?"; - fErrorStatus = status; fprintf(stderr, "! json err @line %" B_PRIi32 " - %s : %s\n", line, strerror(status), message);