Skip to content

Commit

Permalink
Handle escaped quotes, removed include of missing file
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-lundin committed Oct 24, 2011
1 parent 38664d5 commit 7d56019
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 33 deletions.
4 changes: 2 additions & 2 deletions Makefile
@@ -1,9 +1,9 @@
AR = ar
OBJS = jsonnode.o parser.o
CFLAGS = -Wall -pedantic -O2 -ggdb
CFLAGS = -Wall -pedantic -O2

test: $(OBJS) test.cpp
g++ $(OBJS) $(CFLAGS) test.cpp -o test.o && ./test.o
g++ $(OBJS) $(CFLAGS) test.cpp -o test.o

lib: $(OBJS)
$(AR) rc turtle_json.a $(OBJS)
Expand Down
2 changes: 0 additions & 2 deletions jsonnode.h
@@ -1,8 +1,6 @@
#ifndef JSON_NODE_H
#define JSON_NODE_H

#include "macros.h"

#include <string>
#include <map>
#include <vector>
Expand Down
27 changes: 3 additions & 24 deletions parser.cpp
Expand Up @@ -119,17 +119,18 @@ void tokenize(istream& json, vector<Token>& tokenlist)
// Look for string
else if(c == '"')
{
// TODO: Look for escaped quotes
ostringstream oss;
char prev_c='x';
do {
json.get(c);
if(!json.good())
{
throw tokenize_exception("error parsing string", t.pos);
}
if(c == '"')
if(c == '"' && prev_c != '\\')
break;
oss << c;
prev_c = c;
} while(json.good());

t.type = TOKEN_STRING;
Expand Down Expand Up @@ -327,25 +328,3 @@ void dump(JsonNode* node, ostream& os, int level)
}
}

void print_token(const Token& t)
{
if(t.type == TOKEN_OBJECT_START ) cout << "{" ;
if(t.type == TOKEN_OBJECT_END ) cout << "}" ;
if(t.type == TOKEN_ARRAY_START ) cout << "[";
if(t.type == TOKEN_ARRAY_END ) cout << "]" ;
if(t.type == TOKEN_STRING ) cout << "\"" << t.str << "\"";
if(t.type == TOKEN_NUMBER ) cout << t.number;
if(t.type == TOKEN_REAL ) cout << "TOKEN_REAL";
if(t.type == TOKEN_COMMA ) cout << ", ";
if(t.type == TOKEN_COLON) cout << ": ";
return;
if(t.type == TOKEN_OBJECT_START ) cout << "TOKEN_OBJECT_START" << endl;
if(t.type == TOKEN_OBJECT_END ) cout << "TOKEN_OBJECT_END" << endl;
if(t.type == TOKEN_ARRAY_START ) cout << "TOKEN_ARRAY_START" << endl;
if(t.type == TOKEN_ARRAY_END ) cout << "TOKEN_ARRAY_END" << endl;
if(t.type == TOKEN_STRING ) cout << "TOKEN_STRING" << endl;
if(t.type == TOKEN_NUMBER ) cout << "TOKEN_NUMBER" << endl;
if(t.type == TOKEN_REAL ) cout << "TOKEN_REAL" << endl;
if(t.type == TOKEN_COMMA ) cout << "TOKEN_COMMA" << endl;
if(t.type == TOKEN_COLON) cout << "TOKEN_COLON" << endl;
}
1 change: 0 additions & 1 deletion parser.h
Expand Up @@ -53,7 +53,6 @@ struct Token

JsonNode* parse(istream& json_stream);
void dump(JsonNode*, ostream& os, int level=0);
void print_token(const Token&);


#endif
7 changes: 3 additions & 4 deletions test.cpp
Expand Up @@ -20,7 +20,6 @@ void print_from_stream(ifstream& is, streampos at)
}
void read_and_dump()
{

cout << "--- Parsing and dumping jsonfiles/ex1.json ---" << endl;
ifstream ifs("jsonfiles/ex1.json");
if(!ifs)
Expand Down Expand Up @@ -63,7 +62,7 @@ void read_and_dump()
cout << endl << "--- Done ---" << endl;
}

void serialize_list()
void serialize_structure()
{
cout << "--- Creating json-structure and dumping to testout.json ---" << endl;
JsonNode* root = JsonNode::array_node();
Expand Down Expand Up @@ -91,8 +90,8 @@ void serialize_list()

int main(int argc, char** argv)
{

read_and_dump();
serialize_list();
serialize_structure();
return 0;
}

0 comments on commit 7d56019

Please sign in to comment.