From 663ffb74ebd01b78e16a5ba5d18331ee258c66ca Mon Sep 17 00:00:00 2001 From: Thulinma Date: Tue, 1 May 2012 14:28:31 +0200 Subject: [PATCH] JSON reading fixed. I don't know why this wasn't noticed before. :-) --- util/json.cpp | 28 ++++++++++++++++++---------- util/json.h | 1 + 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/util/json.cpp b/util/json.cpp index e8e12641..37ce3279 100644 --- a/util/json.cpp +++ b/util/json.cpp @@ -68,6 +68,16 @@ std::string JSON::Value::string_escape(std::string val){ 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; JSON::Value::Value(){ @@ -78,14 +88,12 @@ JSON::Value::Value(){ JSON::Value::Value(std::istream & fromstream){ null(); bool reading_object = false; - bool reading_obj_name = false; bool reading_array = false; while (fromstream.good()){ int c = fromstream.peek(); switch (c){ case '{': reading_object = true; - reading_obj_name = true; c = fromstream.get(); myType = OBJECT; break; @@ -98,7 +106,7 @@ JSON::Value::Value(std::istream & fromstream){ case '\'': case '"': c = fromstream.get(); - if (!reading_object || !reading_obj_name){ + if (!reading_object){ myType = STRING; strVal = read_string(c, fromstream); return; @@ -125,9 +133,7 @@ JSON::Value::Value(std::istream & fromstream){ case ',': if (!reading_object && !reading_array) return; c = fromstream.get(); - if (reading_object){ - reading_obj_name = true; - }else{ + if (reading_array){ append(JSON::Value(fromstream)); } break; @@ -141,18 +147,21 @@ JSON::Value::Value(std::istream & fromstream){ break; case 't': case 'T': + skipToEnd(fromstream); myType = BOOL; intVal = 1; return; break; case 'f': case 'F': + skipToEnd(fromstream); myType = BOOL; intVal = 0; return; break; case 'n': case 'N': + skipToEnd(fromstream); myType = EMPTY; return; break; @@ -427,10 +436,9 @@ JSON::Value JSON::fromString(std::string json){ /// Converts a file to a JSON::Value. JSON::Value JSON::fromFile(std::string filename){ - std::string Result; std::ifstream File; File.open(filename.c_str()); - while (File.good()){Result += File.get();} - File.close( ); - return fromString(Result); + JSON::Value ret(File); + File.close(); + return ret; } diff --git a/util/json.h b/util/json.h index 047178cd..f8d47aec 100644 --- a/util/json.h +++ b/util/json.h @@ -27,6 +27,7 @@ namespace JSON{ std::string read_string(int separator, std::istream & fromstream); std::string string_escape(std::string val); int c2hex(int c); + static void skipToEnd(std::istream & fromstream); public: //constructors Value();