ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

json.h

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 /*
00024  * Files JSON.h and JSONValue.h part of the SimpleJSON Library - https://github.com/MJPA/SimpleJSON
00025  *
00026  * Copyright (C) 2010 Mike Anchor
00027  *
00028  * Permission is hereby granted, free of charge, to any person obtaining a copy
00029  * of this software and associated documentation files (the "Software"), to deal
00030  * in the Software without restriction, including without limitation the rights
00031  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00032  * copies of the Software, and to permit persons to whom the Software is
00033  * furnished to do so, subject to the following conditions:
00034  *
00035  * The above copyright notice and this permission notice shall be included in
00036  * all copies or substantial portions of the Software.
00037  *
00038  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00039  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00040  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00041  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00042  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00043  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00044  * THE SOFTWARE.
00045  */
00046 
00047 #ifndef COMMON_JSON_H
00048 #define COMMON_JSON_H
00049 
00050 #include "common/array.h"
00051 #include "common/hashmap.h"
00052 #include "common/hash-str.h"
00053 #include "common/str.h"
00054 
00055 // Win32 incompatibilities
00056 #if defined(WIN32) && !defined(__GNUC__)
00057 static inline bool isnan(double x) {
00058     return x != x;
00059 }
00060 
00061 static inline bool isinf(double x) {
00062     return !isnan(x) && isnan(x - x);
00063 }
00064 #endif
00065 
00066 // Simple function to check a string 's' has at least 'n' characters
00067 static inline bool simplejson_wcsnlen(const char *s, size_t n) {
00068     if (s == nullptr)
00069         return false;
00070 
00071     const char *save = s;
00072     while (n-- > 0) {
00073         if (*(save++) == 0) return false;
00074     }
00075 
00076     return true;
00077 }
00078 
00079 namespace Common {
00080 
00081 // Custom types
00082 class JSONValue;
00083 typedef Array<JSONValue*> JSONArray;
00084 typedef HashMap<String, JSONValue*> JSONObject;
00085 
00086 class JSON;
00087 
00088 enum JSONType { JSONType_Null, JSONType_String, JSONType_Bool, JSONType_Number, JSONType_IntegerNumber, JSONType_Array, JSONType_Object };
00089 
00090 class JSONValue {
00091     friend class JSON;
00092 
00093 public:
00094     JSONValue(/*NULL*/);
00095     JSONValue(const char *charValue);
00096     JSONValue(const String &stringValue);
00097     JSONValue(bool boolValue);
00098     JSONValue(double numberValue);
00099     JSONValue(long long int numberValue);
00100     JSONValue(const JSONArray &arrayValue);
00101     JSONValue(const JSONObject &objectValue);
00102     JSONValue(const JSONValue &source);
00103     ~JSONValue();
00104 
00105     bool isNull() const;
00106     bool isString() const;
00107     bool isBool() const;
00108     bool isNumber() const;
00109     bool isIntegerNumber() const;
00110     bool isArray() const;
00111     bool isObject() const;
00112 
00113     const String &asString() const;
00114     bool asBool() const;
00115     double asNumber() const;
00116     long long int asIntegerNumber() const;
00117     const JSONArray &asArray() const;
00118     const JSONObject &asObject() const;
00119 
00120     size_t countChildren() const;
00121     bool hasChild(size_t index) const;
00122     JSONValue *child(size_t index);
00123     bool hasChild(const char *name) const;
00124     JSONValue *child(const char *name);
00125     Array<String> objectKeys() const;
00126 
00127     String stringify(bool const prettyprint = false) const;
00128 protected:
00129     static JSONValue *parse(const char **data);
00130 
00131 private:
00132     static String stringifyString(const String &str);
00133     String stringifyImpl(size_t const indentDepth) const;
00134     static String indent(size_t depth);
00135 
00136     JSONType _type;
00137 
00138     union {
00139         bool _boolValue;
00140         double _numberValue;
00141         long long int _integerValue;
00142         String *_stringValue;
00143         JSONArray *_arrayValue;
00144         JSONObject *_objectValue;
00145     };
00146 
00147 };
00148 
00149 class JSON {
00150     friend class JSONValue;
00151 
00152 public:
00153     static JSONValue *parse(const char *data);
00154     static String stringify(const JSONValue *value);
00155 protected:
00156     static bool skipWhitespace(const char **data);
00157     static bool extractString(const char **data, String &str);
00158     static double parseInt(const char **data);
00159     static double parseDecimal(const char **data);
00160 private:
00161     JSON();
00162 };
00163 
00164 } // End of namespace Common
00165 
00166 #endif


Generated on Sat Mar 16 2019 05:01:40 for ResidualVM by doxygen 1.7.1
curved edge   curved edge