2012-04-10 15:28:07 +00:00
|
|
|
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
|
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
2012-05-04 15:25:49 +00:00
|
|
|
#ifndef PLATFORM_JSON_H_
|
|
|
|
#define PLATFORM_JSON_H_
|
2012-04-10 15:28:07 +00:00
|
|
|
|
|
|
|
#include "vm/allocation.h"
|
|
|
|
#include "vm/globals.h"
|
|
|
|
|
|
|
|
namespace dart {
|
|
|
|
|
|
|
|
|
|
|
|
// A low level interface to tokenize JSON strings.
|
|
|
|
class JSONScanner : ValueObject {
|
|
|
|
public:
|
|
|
|
enum Token {
|
|
|
|
TokenIllegal = 0,
|
|
|
|
TokenLBrace,
|
|
|
|
TokenRBrace,
|
|
|
|
TokenLBrack,
|
|
|
|
TokenRBrack,
|
|
|
|
TokenColon,
|
|
|
|
TokenComma,
|
|
|
|
TokenString,
|
|
|
|
TokenInteger,
|
|
|
|
TokenTrue,
|
|
|
|
TokenFalse,
|
|
|
|
TokenNull,
|
|
|
|
TokenEOM
|
|
|
|
};
|
|
|
|
explicit JSONScanner(const char* json_text);
|
|
|
|
|
|
|
|
void SetText(const char* json_text);
|
|
|
|
void Scan();
|
|
|
|
Token CurrentToken() const { return token_; }
|
|
|
|
bool EOM() const { return token_ == TokenEOM; }
|
|
|
|
const char* TokenChars() const { return token_start_; }
|
|
|
|
int TokenLen() const { return token_length_; }
|
|
|
|
bool IsStringLiteral(const char* literal) const;
|
|
|
|
void Skip(Token matching_token);
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool IsLetter(char ch) const;
|
|
|
|
bool IsDigit(char ch) const;
|
|
|
|
bool IsLiteral(const char* literal);
|
|
|
|
void ScanNumber();
|
|
|
|
void ScanString();
|
|
|
|
void Recognize(Token t);
|
|
|
|
|
|
|
|
const char* current_pos_;
|
|
|
|
const char* token_start_;
|
|
|
|
int token_length_;
|
|
|
|
Token token_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// JSONReader is a higher level interface that allows for lookup of
|
|
|
|
// name-value pairs in JSON objects.
|
|
|
|
class JSONReader : ValueObject {
|
|
|
|
public:
|
|
|
|
enum JSONType {
|
|
|
|
kString,
|
|
|
|
kInteger,
|
|
|
|
kObject,
|
|
|
|
kArray,
|
|
|
|
kLiteral,
|
|
|
|
kNone
|
|
|
|
};
|
|
|
|
|
|
|
|
explicit JSONReader(const char* json_object);
|
|
|
|
void Set(const char* json_object);
|
|
|
|
|
|
|
|
// Returns true if a pair with the given name was found.
|
|
|
|
bool Seek(const char* name);
|
|
|
|
|
|
|
|
// Returns true if a syntax error was found.
|
|
|
|
bool Error() const { return error_; }
|
|
|
|
|
2012-05-04 15:25:49 +00:00
|
|
|
// Returns a pointer to the matching closing brace if the text starts
|
|
|
|
// with a valid JSON object. Returns NULL otherwise.
|
|
|
|
const char* EndOfObject();
|
|
|
|
|
2012-04-10 15:28:07 +00:00
|
|
|
JSONType Type() const;
|
|
|
|
const char* ValueChars() const {
|
|
|
|
return (Type() != kNone) ? scanner_.TokenChars() : NULL;
|
|
|
|
}
|
|
|
|
int ValueLen() const {
|
|
|
|
return (Type() != kNone) ? scanner_.TokenLen() : 0;
|
|
|
|
}
|
2013-08-26 20:59:16 +00:00
|
|
|
void GetRawValueChars(char* buf, intptr_t buflen) const;
|
|
|
|
void GetDecodedValueChars(char* buf, intptr_t buflen) const;
|
2012-04-10 15:28:07 +00:00
|
|
|
bool IsStringLiteral(const char* literal) const {
|
|
|
|
return scanner_.IsStringLiteral(literal);
|
|
|
|
}
|
2012-04-23 22:26:09 +00:00
|
|
|
bool IsTrue() const {
|
|
|
|
return scanner_.CurrentToken() == JSONScanner::TokenTrue;
|
|
|
|
}
|
|
|
|
bool IsFalse() const {
|
|
|
|
return scanner_.CurrentToken() == JSONScanner::TokenFalse;
|
|
|
|
}
|
|
|
|
bool IsNull() const {
|
|
|
|
return scanner_.CurrentToken() == JSONScanner::TokenNull;
|
|
|
|
}
|
2012-04-10 15:28:07 +00:00
|
|
|
|
2013-01-24 23:35:34 +00:00
|
|
|
// Debugging method to check for validity of a JSON message.
|
|
|
|
bool CheckMessage();
|
|
|
|
|
2012-04-10 15:28:07 +00:00
|
|
|
private:
|
2013-01-24 23:35:34 +00:00
|
|
|
void CheckObject();
|
|
|
|
void CheckArray();
|
|
|
|
void CheckValue();
|
|
|
|
|
2012-04-10 15:28:07 +00:00
|
|
|
JSONScanner scanner_;
|
|
|
|
const char* json_object_;
|
|
|
|
bool error_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// TextBuffer maintains a dynamic character buffer with a printf-style way to
|
|
|
|
// append text.
|
|
|
|
class TextBuffer : ValueObject {
|
|
|
|
public:
|
|
|
|
explicit TextBuffer(intptr_t buf_size);
|
|
|
|
~TextBuffer();
|
|
|
|
|
2012-09-06 00:58:25 +00:00
|
|
|
intptr_t Printf(const char* format, ...) PRINTF_ATTRIBUTE(2, 3);
|
2012-07-18 23:27:59 +00:00
|
|
|
void AddChar(char ch);
|
|
|
|
void AddUTF8(uint32_t ch);
|
|
|
|
void AddEscapedChar(uint32_t ch);
|
2013-03-27 17:30:14 +00:00
|
|
|
void AddString(const char* s);
|
|
|
|
void AddEscapedString(const char* s);
|
2012-05-10 22:04:50 +00:00
|
|
|
|
2012-04-10 15:28:07 +00:00
|
|
|
void Clear();
|
|
|
|
|
|
|
|
char* buf() { return buf_; }
|
2012-05-04 15:25:49 +00:00
|
|
|
intptr_t length() { return msg_len_; }
|
2012-04-10 15:28:07 +00:00
|
|
|
|
|
|
|
private:
|
2012-07-18 23:27:59 +00:00
|
|
|
void EnsureCapacity(intptr_t len);
|
2012-04-10 15:28:07 +00:00
|
|
|
char* buf_;
|
|
|
|
intptr_t buf_size_;
|
|
|
|
intptr_t msg_len_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dart
|
|
|
|
|
2012-05-04 15:25:49 +00:00
|
|
|
#endif // PLATFORM_JSON_H_
|