JSON reading fixed. I don't know why this wasn't noticed before. :-)
This commit is contained in:
parent
f91d7fa317
commit
663ffb74eb
2 changed files with 19 additions and 10 deletions
|
@ -68,6 +68,16 @@ std::string JSON::Value::string_escape(std::string val){
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Skips an std::istream forward until any of the following characters is seen: ,]}
|
||||||
|
void JSON::Value::skipToEnd(std::istream & fromstream){
|
||||||
|
while (fromstream.good()){
|
||||||
|
char peek = fromstream.peek();
|
||||||
|
if (peek == ','){return;}
|
||||||
|
if (peek == ']'){return;}
|
||||||
|
if (peek == '}'){return;}
|
||||||
|
peek = fromstream.get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Sets this JSON::Value to null;
|
/// Sets this JSON::Value to null;
|
||||||
JSON::Value::Value(){
|
JSON::Value::Value(){
|
||||||
|
@ -78,14 +88,12 @@ JSON::Value::Value(){
|
||||||
JSON::Value::Value(std::istream & fromstream){
|
JSON::Value::Value(std::istream & fromstream){
|
||||||
null();
|
null();
|
||||||
bool reading_object = false;
|
bool reading_object = false;
|
||||||
bool reading_obj_name = false;
|
|
||||||
bool reading_array = false;
|
bool reading_array = false;
|
||||||
while (fromstream.good()){
|
while (fromstream.good()){
|
||||||
int c = fromstream.peek();
|
int c = fromstream.peek();
|
||||||
switch (c){
|
switch (c){
|
||||||
case '{':
|
case '{':
|
||||||
reading_object = true;
|
reading_object = true;
|
||||||
reading_obj_name = true;
|
|
||||||
c = fromstream.get();
|
c = fromstream.get();
|
||||||
myType = OBJECT;
|
myType = OBJECT;
|
||||||
break;
|
break;
|
||||||
|
@ -98,7 +106,7 @@ JSON::Value::Value(std::istream & fromstream){
|
||||||
case '\'':
|
case '\'':
|
||||||
case '"':
|
case '"':
|
||||||
c = fromstream.get();
|
c = fromstream.get();
|
||||||
if (!reading_object || !reading_obj_name){
|
if (!reading_object){
|
||||||
myType = STRING;
|
myType = STRING;
|
||||||
strVal = read_string(c, fromstream);
|
strVal = read_string(c, fromstream);
|
||||||
return;
|
return;
|
||||||
|
@ -125,9 +133,7 @@ JSON::Value::Value(std::istream & fromstream){
|
||||||
case ',':
|
case ',':
|
||||||
if (!reading_object && !reading_array) return;
|
if (!reading_object && !reading_array) return;
|
||||||
c = fromstream.get();
|
c = fromstream.get();
|
||||||
if (reading_object){
|
if (reading_array){
|
||||||
reading_obj_name = true;
|
|
||||||
}else{
|
|
||||||
append(JSON::Value(fromstream));
|
append(JSON::Value(fromstream));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -141,18 +147,21 @@ JSON::Value::Value(std::istream & fromstream){
|
||||||
break;
|
break;
|
||||||
case 't':
|
case 't':
|
||||||
case 'T':
|
case 'T':
|
||||||
|
skipToEnd(fromstream);
|
||||||
myType = BOOL;
|
myType = BOOL;
|
||||||
intVal = 1;
|
intVal = 1;
|
||||||
return;
|
return;
|
||||||
break;
|
break;
|
||||||
case 'f':
|
case 'f':
|
||||||
case 'F':
|
case 'F':
|
||||||
|
skipToEnd(fromstream);
|
||||||
myType = BOOL;
|
myType = BOOL;
|
||||||
intVal = 0;
|
intVal = 0;
|
||||||
return;
|
return;
|
||||||
break;
|
break;
|
||||||
case 'n':
|
case 'n':
|
||||||
case 'N':
|
case 'N':
|
||||||
|
skipToEnd(fromstream);
|
||||||
myType = EMPTY;
|
myType = EMPTY;
|
||||||
return;
|
return;
|
||||||
break;
|
break;
|
||||||
|
@ -427,10 +436,9 @@ JSON::Value JSON::fromString(std::string json){
|
||||||
|
|
||||||
/// Converts a file to a JSON::Value.
|
/// Converts a file to a JSON::Value.
|
||||||
JSON::Value JSON::fromFile(std::string filename){
|
JSON::Value JSON::fromFile(std::string filename){
|
||||||
std::string Result;
|
|
||||||
std::ifstream File;
|
std::ifstream File;
|
||||||
File.open(filename.c_str());
|
File.open(filename.c_str());
|
||||||
while (File.good()){Result += File.get();}
|
JSON::Value ret(File);
|
||||||
File.close();
|
File.close();
|
||||||
return fromString(Result);
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@ namespace JSON{
|
||||||
std::string read_string(int separator, std::istream & fromstream);
|
std::string read_string(int separator, std::istream & fromstream);
|
||||||
std::string string_escape(std::string val);
|
std::string string_escape(std::string val);
|
||||||
int c2hex(int c);
|
int c2hex(int c);
|
||||||
|
static void skipToEnd(std::istream & fromstream);
|
||||||
public:
|
public:
|
||||||
//constructors
|
//constructors
|
||||||
Value();
|
Value();
|
||||||
|
|
Loading…
Add table
Reference in a new issue