Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Compiler and flags
CXX = c++
CXXFLAGS = -std=c++98 -Iincludes -Wall -Wextra
LDFLAGS =

# Source files and object files
SRCS = $(wildcard src/*.cpp) main.cpp schema.cpp
OBJS = $(SRCS:.cpp=.o)

# Target executable
TARGET = json_validator

# Default rule
all: $(TARGET)

# Linking rule
$(TARGET): $(OBJS)
$(CXX) $(LDFLAGS) -o $(TARGET) $(OBJS)

# Compilation rule
%.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@

# Clean rule
clean:
rm -f $(OBJS) $(TARGET)

.PHONY: all clean
8 changes: 8 additions & 0 deletions includes/schema.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef SCHEMA_HPP
#define SCHEMA_HPP

#include "JsonValidator.hpp"

extern ArrayValidator ServerSchema;

#endif
Binary file added json_validator
Binary file not shown.
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "Json.hpp"
#include "JsonValidator.hpp"
#include "schema.cpp"
#include "schema.hpp"
#include <iostream>

int main() {
Expand Down
Binary file added main.o
Binary file not shown.
2 changes: 1 addition & 1 deletion schema.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "JsonValidator.hpp"
#include "schema.hpp"

ObjectValidator locationSchema = obj()
.property("allowed_methods",arr().item(
Expand Down
Binary file added schema.o
Binary file not shown.
Binary file added src/AJsonValue.o
Binary file not shown.
Binary file added src/Json.o
Binary file not shown.
Binary file added src/JsonTypes.o
Binary file not shown.
32 changes: 14 additions & 18 deletions src/JsonValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,14 @@ ObjectValidator::ObjectValidator()
}

ObjectValidator::ObjectValidator(const ObjectValidator &obj)
: allowAdditional_(obj.allowAdditional_), matchMode_(obj.matchMode_),
emptyCheck(obj.emptyCheck), key_validator(NULL), val_validator(NULL),
last_(NULL) {
: AJsonValidator(obj), allowAdditional_(obj.allowAdditional_),
matchMode_(obj.matchMode_), emptyCheck(obj.emptyCheck),
key_validator(NULL), val_validator(NULL), last_(NULL) {
if (obj.val_validator)
val_validator = obj.val_validator->clone();
if (obj.key_validator)
key_validator = dynamic_cast<StringValidator *>(obj.key_validator->clone());
optional_ = obj.optional_;
exceptedType_ = OBJECT;
hasDefault_ = obj.hasDefault_;
if (obj.defaultValue_)
defaultValue_ = dynamic_cast<JsonObject *>(obj.defaultValue_->clone());
ValidatorMap::const_iterator it = obj.properties_.begin();
Expand Down Expand Up @@ -269,9 +267,9 @@ ArrayValidator::ArrayValidator()
}

ArrayValidator::ArrayValidator(const ArrayValidator &obj)
: validator_(NULL), min_(obj.min_), max_(obj.max_), defaultValue_(NULL) {
: AJsonValidator(obj), validator_(NULL), min_(obj.min_), max_(obj.max_),
defaultValue_(NULL) {
exceptedType_ = ARRAY;
hasDefault_ = obj.hasDefault_;
if (obj.defaultValue_)
defaultValue_ = dynamic_cast<JsonArray *>(obj.defaultValue_->clone());
if (obj.validator_) {
Expand Down Expand Up @@ -375,7 +373,8 @@ ArrayValidator::~ArrayValidator() {

ORValidator::ORValidator() {}

ORValidator::ORValidator(const ORValidator &obj) : msg_(obj.msg_) {
ORValidator::ORValidator(const ORValidator &obj)
: AJsonValidator(obj), msg_(obj.msg_) {
for (unsigned long i = 0; i < obj.conditions_.size(); i++) {
conditions_.push_back(obj.conditions_[i]->clone());
}
Expand Down Expand Up @@ -444,11 +443,10 @@ StringValidator::StringValidator()
}

StringValidator::StringValidator(const StringValidator &obj)
: min_(obj.min_), max_(obj.max_), checkMin(obj.checkMin),
checkMax(obj.checkMax), defaultValue_(obj.defaultValue_) {
optional_ = obj.optional_;
: AJsonValidator(obj), min_(obj.min_), max_(obj.max_),
checkMin(obj.checkMin), checkMax(obj.checkMax),
defaultValue_(obj.defaultValue_) {
exceptedType_ = STRING;
hasDefault_ = obj.hasDefault_;
std::map<std::string, funcCheck>::const_iterator it = obj.checkers.begin();
for (; it != obj.checkers.end(); it++) {
funcCheck copied = it->second;
Expand Down Expand Up @@ -625,11 +623,10 @@ NumberValidator::NumberValidator()
}

NumberValidator::NumberValidator(const NumberValidator &obj)
: min_(obj.min_), max_(obj.max_), checkMin(obj.checkMin),
checkMax(obj.checkMax), defaultValue_(obj.defaultValue_) {
optional_ = obj.optional_;
: AJsonValidator(obj), min_(obj.min_), max_(obj.max_),
checkMin(obj.checkMin), checkMax(obj.checkMax),
defaultValue_(obj.defaultValue_) {
exceptedType_ = NUMBER;
hasDefault_ = obj.hasDefault_;
}

AJsonValidator *NumberValidator::clone() const {
Expand Down Expand Up @@ -711,8 +708,7 @@ NumberValidator::~NumberValidator() {}

BoolValidator::BoolValidator() {}

BoolValidator::BoolValidator(const BoolValidator &obj) {
hasDefault_ = obj.hasDefault_;
BoolValidator::BoolValidator(const BoolValidator &obj) : AJsonValidator(obj) {
defaultValue_ = obj.get_default();
}

Expand Down
Binary file added src/JsonValidator.o
Binary file not shown.
67 changes: 41 additions & 26 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,34 @@ token extract_decimal(char &c, std::istream &in) {

token extract_json_types(char &c, std::istream &in) {
token tk;
tk.token.push_back(c);
int i = 1;
int len = (c == 'f' ? 5 : 4);
while (i < len) {
if (in.eof())
break;
in.get(c);
tk.token.push_back(c);
i++;
tk.type = TK_UNDEFINED;
std::string expected;

if (c == 't')
expected = "true";
else if (c == 'f')
expected = "false";
else if (c == 'n')
expected = "null";

tk.token += c;
for (size_t i = 1; i < expected.length(); ++i) {
if (!in.get(c) || c != expected[i]) {
// Restore the read characters to the stream to not mess up the parsing
// for other tokens
for (size_t j = 0; j < tk.token.length(); j++) {
in.unget();
}
return tk;
}
tk.token += c;
}
if (tk.token == "true" || tk.token == "false")

if (expected == "true" || expected == "false")
tk.type = TK_BOOLEAN;
else if (tk.token == "null")
else if (expected == "null")
tk.type = TK_NIL;
else
tk.type = TK_UNDEFINED;

return tk;
}

Expand Down Expand Up @@ -150,31 +162,34 @@ void Tokenizer::parse(std::istream &in, std::vector<token> &tokens) {
}

std::string match_token_name(token_type type) {
if (type == CB_OPEN)
switch (type) {
case CB_OPEN:
return "CB_OPEN";
else if (type == CB_CLOSE)
case CB_CLOSE:
return "CB_CLOSE";
else if (type == SB_OPEN)
case SB_OPEN:
return "SB_OPEN";
else if (type == SB_CLOSE)
case SB_CLOSE:
return "SB_CLOSE";
else if (type == TK_STRING)
case TK_STRING:
return "STRING";
else if (type == TK_DOUBLE)
case TK_DOUBLE:
return "DOUBLE";
else if (type == TK_NUMBER)
case TK_NUMBER:
return "NUMBER";
else if (type == COMMA)
case COMMA:
return "COMMA";
else if (type == COLON)
case COLON:
return "COLON";
else if (type == TK_BOOLEAN)
case TK_BOOLEAN:
return "BOOLEAN";
else if (type == TK_NIL)
case TK_NIL:
return "NULL";
else if (type == END)
case END:
return "END";
return "TK_UNDEFINED";
default:
return "TK_UNDEFINED";
}
}

std::ostream &operator<<(std::ostream &os, token &tk) {
Expand Down
Binary file added src/parser.o
Binary file not shown.
Binary file added src/utils.o
Binary file not shown.
Binary file added src/validators.o
Binary file not shown.