JSON Parser : code style corrections
This commit is contained in:
parent
795c2826bb
commit
55f5f25940
@ -4,6 +4,8 @@
|
||||
* Copyright 2014, Stephan Aßmus <superstippi@gmx.de>
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
|
||||
#include "Json.h"
|
||||
|
||||
#include <cstdio>
|
||||
@ -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,15 +172,19 @@ 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 {
|
||||
@ -336,13 +354,16 @@ BJson::ParseObject(JsonParseContext& jsonParseContext)
|
||||
|
||||
switch (c) {
|
||||
case '}': // terminate the object
|
||||
{
|
||||
if (!jsonParseContext.Listener()->Handle(
|
||||
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:
|
||||
{
|
||||
@ -392,13 +414,16 @@ BJson::ParseArray(JsonParseContext& jsonParseContext)
|
||||
|
||||
switch (c) {
|
||||
case ']': // terminate the array
|
||||
{
|
||||
if (!jsonParseContext.Listener()->Handle(
|
||||
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:
|
||||
{
|
||||
@ -511,12 +537,14 @@ BJson::ParseStringEscapeSequence(JsonParseContext& jsonParseContext,
|
||||
stringResult += "\"";
|
||||
break;
|
||||
case 'u':
|
||||
{
|
||||
// unicode escape sequence.
|
||||
if (!ParseEscapeUnicodeSequence(jsonParseContext,
|
||||
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<uint8>(c);
|
||||
|
||||
if(uc < 0x20) { // control characters are not allowed
|
||||
@ -575,6 +608,7 @@ BJson::ParseString(JsonParseContext& jsonParseContext,
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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,13 +758,14 @@ BJson::ParseNumber(JsonParseContext& jsonParseContext)
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
jsonParseContext.Listener()->HandleError(result, -1,
|
||||
"io related read error");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace BPrivate
|
@ -2,6 +2,8 @@
|
||||
* Copyright 2017, Andrew Lindesay <apl@lindesay.co.nz>
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
|
||||
#include "JsonEvent.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
@ -3,6 +3,7 @@
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
|
||||
#include "JsonEventListener.h"
|
||||
|
||||
|
||||
|
@ -3,9 +3,8 @@
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
#include "JsonMessageWriter.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include "JsonMessageWriter.h"
|
||||
|
||||
|
||||
namespace BPrivate {
|
||||
@ -179,20 +178,26 @@ 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;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
|
||||
#include "JsonTextWriter.h"
|
||||
|
||||
#include <stdio.h>
|
||||
@ -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,28 +212,36 @@ 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;
|
||||
}
|
||||
}
|
||||
|
||||
if (writeResult == B_OK)
|
||||
DidAdd();
|
||||
@ -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);
|
||||
@ -693,5 +704,3 @@ BJsonTextWriter::StreamChar(char c)
|
||||
{
|
||||
return fDataIO->WriteExactly(&c, 1);
|
||||
}
|
||||
|
||||
|
||||
|
@ -3,6 +3,7 @@
|
||||
* Distributed under the terms of the MIT License.
|
||||
*/
|
||||
|
||||
|
||||
#include "JsonWriter.h"
|
||||
|
||||
#include <stdio.h>
|
||||
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user