├── test_cases ├── fail9.json ├── fail13.json ├── fail10.json ├── fail11.json ├── fail20.json ├── pass4.json ├── fail19.json ├── fail23.json ├── pass5.json ├── fail14.json ├── fail5.json ├── fail16.json ├── fail2.json ├── fail22.json ├── fail6.json ├── fail7.json ├── fail8.json ├── fail15.json ├── fail17.json ├── fail4.json ├── fail1.json ├── fail12.json ├── fail3.json ├── pass2.json ├── fail18.json ├── fail21.json ├── pass3.json └── pass1.json ├── .gitignore ├── src ├── demo │ ├── functions.h │ ├── nix-main.cpp │ ├── win-main.cpp │ ├── testcases.cpp │ └── example.cpp ├── JSONValue.h ├── JSON.h ├── JSON.cpp └── JSONValue.cpp ├── vs2008 ├── common_controls.manifest ├── JSONDemo.sln ├── resource.h ├── gui.rc └── JSONDemo.vcproj ├── Makefile ├── LICENSE ├── README.md └── xcode5 └── xcode5.xcodeproj └── project.pbxproj /test_cases/fail9.json: -------------------------------------------------------------------------------- 1 | Unclosed array 2 | ["Unclosed array" 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *xcworkspace* 2 | *xcuserdata* 3 | JSONDemo* 4 | obj 5 | -------------------------------------------------------------------------------- /test_cases/fail13.json: -------------------------------------------------------------------------------- 1 | Brackets must be matching 2 | ["mismatch"} 3 | -------------------------------------------------------------------------------- /test_cases/fail10.json: -------------------------------------------------------------------------------- 1 | Numbers require exponent if 'e' is there 2 | [0e] 3 | -------------------------------------------------------------------------------- /test_cases/fail11.json: -------------------------------------------------------------------------------- 1 | Only 1 sign char can precede the value 2 | [0e+-1] 3 | -------------------------------------------------------------------------------- /test_cases/fail20.json: -------------------------------------------------------------------------------- 1 | Extra comma after object 2 | {"Extra comma": true,} 3 | -------------------------------------------------------------------------------- /test_cases/pass4.json: -------------------------------------------------------------------------------- 1 | Simple string value 2 | "just a simple string here" 3 | -------------------------------------------------------------------------------- /test_cases/fail19.json: -------------------------------------------------------------------------------- 1 | Each opening bracket must be closed 2 | ["Extra close"]] 3 | -------------------------------------------------------------------------------- /test_cases/fail23.json: -------------------------------------------------------------------------------- 1 | Decimal numbers need a digit before the dot 2 | .234 3 | -------------------------------------------------------------------------------- /test_cases/pass5.json: -------------------------------------------------------------------------------- 1 | Unicode character string 2 | "\u0074\u0065\u0073\u0074" 3 | -------------------------------------------------------------------------------- /test_cases/fail14.json: -------------------------------------------------------------------------------- 1 | Double quotes must be escaped 2 | ["an unespcaed " quote"] 3 | -------------------------------------------------------------------------------- /test_cases/fail5.json: -------------------------------------------------------------------------------- 1 | Truth is not a valid boolean value 2 | ["Bad value", truth] 3 | -------------------------------------------------------------------------------- /test_cases/fail16.json: -------------------------------------------------------------------------------- 1 | Arrays must not have comma after last value 2 | ["extra comma",] 3 | -------------------------------------------------------------------------------- /test_cases/fail2.json: -------------------------------------------------------------------------------- 1 | Objects require colon between name/value 2 | {"Missing colon" null} 3 | -------------------------------------------------------------------------------- /test_cases/fail22.json: -------------------------------------------------------------------------------- 1 | Numbers can't be hex encoded 2 | {"Numbers cannot be hex": 0x14} 3 | -------------------------------------------------------------------------------- /test_cases/fail6.json: -------------------------------------------------------------------------------- 1 | Strings need double quotes, not single quotes 2 | ['single quote'] 3 | -------------------------------------------------------------------------------- /test_cases/fail7.json: -------------------------------------------------------------------------------- 1 | Line break in a string value is not valid 2 | ["line 3 | break"] 4 | -------------------------------------------------------------------------------- /test_cases/fail8.json: -------------------------------------------------------------------------------- 1 | Escaped line break char is still not valid 2 | ["line\ 3 | break"] 4 | -------------------------------------------------------------------------------- /test_cases/fail15.json: -------------------------------------------------------------------------------- 1 | Key string must be quoted 2 | {unquoted_key: "keys must be quoted"} 3 | -------------------------------------------------------------------------------- /test_cases/fail17.json: -------------------------------------------------------------------------------- 1 | Arrays must have values between commas 2 | [ , "<-- missing value"] 3 | -------------------------------------------------------------------------------- /test_cases/fail4.json: -------------------------------------------------------------------------------- 1 | Arrays don't have colon separators 2 | ["Colon instead of comma": false] 3 | -------------------------------------------------------------------------------- /test_cases/fail1.json: -------------------------------------------------------------------------------- 1 | \x is not a valid escape character 2 | ["Illegal backslash escape: \x15"] 3 | -------------------------------------------------------------------------------- /test_cases/fail12.json: -------------------------------------------------------------------------------- 1 | Commas cannot close objects 2 | {"Comma instead if closing brace": true, 3 | -------------------------------------------------------------------------------- /test_cases/fail3.json: -------------------------------------------------------------------------------- 1 | Objects do not have comma separators 2 | {"Comma instead of colon", null} 3 | -------------------------------------------------------------------------------- /test_cases/pass2.json: -------------------------------------------------------------------------------- 1 | Heavily nested array 2 | [[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]] 3 | -------------------------------------------------------------------------------- /test_cases/fail18.json: -------------------------------------------------------------------------------- 1 | Nothing but whitespace can follow the root value 2 | ["Comma after the close"], 3 | -------------------------------------------------------------------------------- /test_cases/fail21.json: -------------------------------------------------------------------------------- 1 | Numbers cannot have leading 0s 2 | {"Numbers cannot have leading zeroes": 013} 3 | -------------------------------------------------------------------------------- /test_cases/pass3.json: -------------------------------------------------------------------------------- 1 | Nested object 2 | { 3 | "JSON Test Pattern pass3": { 4 | "The outermost value": "must be an object or array.", 5 | "In this test": "It is an object." 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/demo/functions.h: -------------------------------------------------------------------------------- 1 | // The functions available, 1 single header to make things easier 2 | 3 | // The print out function 4 | void print_out(const wchar_t *output); 5 | 6 | // Example functions 7 | void example1(); 8 | void example2(); 9 | void example3(); 10 | void example4(); 11 | 12 | // Test case runner 13 | void run_tests(); 14 | -------------------------------------------------------------------------------- /vs2008/common_controls.manifest: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Change to whatever your C++ compiler is 2 | CC=g++ 3 | 4 | # Compile settings 5 | CFLAGS=-c -Wall 6 | LFLAGS=-lm 7 | 8 | # Source files 9 | SOURCES=src/JSON.cpp src/JSONValue.cpp src/demo/nix-main.cpp src/demo/example.cpp src/demo/testcases.cpp 10 | HEADERS=src/JSON.h src/JSONValue.h 11 | OBJECTS=$(SOURCES:src/%.cpp=obj/%.o) 12 | 13 | # Output 14 | EXECUTABLE=JSONDemo 15 | 16 | all: $(SOURCES) $(EXECUTABLE) 17 | 18 | $(EXECUTABLE): $(OBJECTS) 19 | $(CC) $(LFLAGS) $(OBJECTS) -o $@ 20 | 21 | obj/%.o: src/%.cpp $(HEADERS) 22 | @test -d $(@D) || mkdir -p $(@D) 23 | $(CC) $(CFLAGS) $(@:obj/%.o=src/%.cpp) -o $@ 24 | 25 | clean: 26 | rm -f $(OBJECTS) $(EXECUTABLE) 27 | 28 | -------------------------------------------------------------------------------- /vs2008/JSONDemo.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 10.00 3 | # Visual Studio 2008 4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "JSONDemo", "JSONDemo.vcproj", "{06DE72C5-3E2A-4DFC-A60E-69B8471E8AC1}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Release|Win32 = Release|Win32 9 | EndGlobalSection 10 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 11 | {06DE72C5-3E2A-4DFC-A60E-69B8471E8AC1}.Release|Win32.ActiveCfg = Release|Win32 12 | {06DE72C5-3E2A-4DFC-A60E-69B8471E8AC1}.Release|Win32.Build.0 = Release|Win32 13 | EndGlobalSection 14 | GlobalSection(SolutionProperties) = preSolution 15 | HideSolutionNode = FALSE 16 | EndGlobalSection 17 | EndGlobal 18 | -------------------------------------------------------------------------------- /vs2008/resource.h: -------------------------------------------------------------------------------- 1 | //{{NO_DEPENDENCIES}} 2 | // Microsoft Visual C++ generated include file. 3 | // Used by gui.rc 4 | // 5 | #define IDD_JSONDEMO 101 6 | #define IDC_INPUT 1001 7 | #define IDC_OUTPUT 1002 8 | #define IDC_VERIFY 1003 9 | #define IDC_ECHO 1004 10 | #define IDC_EX1 1005 11 | #define IDC_EX2 1006 12 | #define IDC_EX3 1008 13 | #define IDC_EX4 1009 14 | #define IDC_TESTCASES 1007 15 | 16 | // Next default values for new objects 17 | // 18 | #ifdef APSTUDIO_INVOKED 19 | #ifndef APSTUDIO_READONLY_SYMBOLS 20 | #define _APS_NEXT_RESOURCE_VALUE 102 21 | #define _APS_NEXT_COMMAND_VALUE 40001 22 | #define _APS_NEXT_CONTROL_VALUE 1007 23 | #define _APS_NEXT_SYMED_VALUE 101 24 | #endif 25 | #endif 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | SimpleJSON Library, copyright (C) 2010 Mike Anchor 2 | 3 | PermaLink: http://mjpa.in/json 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | About 2 | ----- 3 | 4 | SimpleJSON is a simple JSON library written in C++. It is designed to be simple 5 | to use and be cross platform. 6 | 7 | The library consists of 2 classes - JSON and JSONValue. The JSON class acts 8 | similar to the Javascript JSON object and contains Parse() and Stringify() 9 | methods for dealing with decoding and encoding JSON text. The JSONValue class 10 | is the class that deals with representing the various JSON values in a C++ way. 11 | 12 | Included with the source code is a demo application to give a basic example of 13 | how the library works and run some tests on the library. Linux users should 14 | just need the basic development tools installed while Windows will need VS2008 15 | to build the project (or be able to handle VS2008 project files). 16 | 17 | The library requires no 3rd party libraries and so can be dropped into any 18 | existing project effortlessly. It has been tested on Linux and Windows 19 | (using VS2008), if you come across any issues with using it on any system, feel 20 | free to contact me by visiting my site at http://mjpa.co.uk 21 | 22 | Notes 23 | ----- 24 | 25 | If building for Android and using Visual Studio, make sure `Ignore All Default 26 | Libraries` is set to `No`. The setting can be changed by going `Settings -> 27 | Configuration Properties -> Linker -> Input`. 28 | 29 | If you look at the examples, you will see usage of `JSONArray` and `JSONObject` 30 | these are simply type definitions as outlined below: 31 | 32 | * JSONArray: std::vector 33 | * JSONObject: std::map 34 | -------------------------------------------------------------------------------- /test_cases/pass1.json: -------------------------------------------------------------------------------- 1 | General large array testing valid values 2 | [ 3 | "JSON Test Pattern pass1", 4 | {"object with 1 member":["array with 1 element"]}, 5 | {}, 6 | [], 7 | -42, 8 | true, 9 | false, 10 | null, 11 | { 12 | "integer": 1234567890, 13 | "real": -9876.543210, 14 | "e": 0.123456789e-12, 15 | "E": 1.234567890E+34, 16 | "": 23456789012E66, 17 | "zero": 0, 18 | "one": 1, 19 | "space": " ", 20 | "quote": "\"", 21 | "backslash": "\\", 22 | "controls": "\b\f\n\r\t", 23 | "slash": "/ & \/", 24 | "alpha": "abcdefghijklmnopqrstuvwyz", 25 | "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ", 26 | "digit": "0123456789", 27 | "0123456789": "digit", 28 | "special": "`1~!@#$%^&*()_+-={':[,]}|;.?", 29 | "hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A", 30 | "true": true, 31 | "false": false, 32 | "null": null, 33 | "array":[ ], 34 | "object":{ }, 35 | "address": "50 St. James Street", 36 | "url": "http://www.JSON.org/", 37 | "comment": "// /* */": " ", 39 | " s p a c e d " :[1,2 , 3 40 | 41 | , 42 | 43 | 4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7], 44 | "jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}", 45 | "quotes": "" \u0022 %22 0x22 034 "", 46 | "\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?" 47 | : "A key can be any string" 48 | }, 49 | 0.5 ,98.6 50 | , 51 | 99.44 52 | , 53 | 54 | 1066, 55 | 1e1, 56 | 0.1e1, 57 | 1e-1, 58 | 1e00,2e+00,2e-00 59 | ,"rosebud"] 60 | -------------------------------------------------------------------------------- /vs2008/gui.rc: -------------------------------------------------------------------------------- 1 | // Microsoft Visual C++ generated resource script. 2 | // 3 | #include "resource.h" 4 | 5 | #define APSTUDIO_READONLY_SYMBOLS 6 | ///////////////////////////////////////////////////////////////////////////// 7 | // 8 | // Generated from the TEXTINCLUDE 2 resource. 9 | // 10 | #include "afxres.h" 11 | 12 | ///////////////////////////////////////////////////////////////////////////// 13 | #undef APSTUDIO_READONLY_SYMBOLS 14 | 15 | ///////////////////////////////////////////////////////////////////////////// 16 | // English (U.K.) resources 17 | 18 | #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENG) 19 | #ifdef _WIN32 20 | LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_UK 21 | #pragma code_page(1252) 22 | #endif //_WIN32 23 | 24 | #ifdef APSTUDIO_INVOKED 25 | ///////////////////////////////////////////////////////////////////////////// 26 | // 27 | // TEXTINCLUDE 28 | // 29 | 30 | 1 TEXTINCLUDE 31 | BEGIN 32 | "resource.h\0" 33 | END 34 | 35 | 2 TEXTINCLUDE 36 | BEGIN 37 | "#include ""afxres.h""\r\n" 38 | "\0" 39 | END 40 | 41 | 3 TEXTINCLUDE 42 | BEGIN 43 | "\r\n" 44 | "\0" 45 | END 46 | 47 | #endif // APSTUDIO_INVOKED 48 | 49 | 50 | ///////////////////////////////////////////////////////////////////////////// 51 | // 52 | // Dialog 53 | // 54 | 55 | IDD_JSONDEMO DIALOGEX 0, 0, 500, 294 56 | STYLE DS_SETFONT | DS_3DLOOK | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU 57 | EXSTYLE WS_EX_APPWINDOW 58 | CAPTION "SimpleJSON library demo" 59 | FONT 8, "Courier New", 400, 0, 0x0 60 | BEGIN 61 | GROUPBOX "Input data: ",IDC_STATIC,7,7,486,129 62 | GROUPBOX "Output data: ",IDC_STATIC,7,161,486,126 63 | EDITTEXT IDC_INPUT,17,22,464,100,ES_MULTILINE | ES_WANTRETURN | WS_VSCROLL 64 | EDITTEXT IDC_OUTPUT,17,177,464,100,ES_MULTILINE | ES_READONLY | ES_WANTRETURN | WS_VSCROLL 65 | PUSHBUTTON "Verify",IDC_VERIFY,8,141,55,14 66 | PUSHBUTTON "Verify + Echo",IDC_ECHO,75,141,55,14 67 | PUSHBUTTON "Example 1",IDC_EX1,142,141,55,14 68 | PUSHBUTTON "Example 2",IDC_EX2,209,141,55,14 69 | PUSHBUTTON "Example 3",IDC_EX3,276,141,55,14 70 | PUSHBUTTON "Example 4",IDC_EX4,343,141,55,14 71 | PUSHBUTTON "Test Cases",IDC_TESTCASES,410,141,55,14 72 | END 73 | 74 | 75 | ///////////////////////////////////////////////////////////////////////////// 76 | // 77 | // DESIGNINFO 78 | // 79 | 80 | #ifdef APSTUDIO_INVOKED 81 | GUIDELINES DESIGNINFO 82 | BEGIN 83 | IDD_JSONDEMO, DIALOG 84 | BEGIN 85 | LEFTMARGIN, 7 86 | RIGHTMARGIN, 493 87 | TOPMARGIN, 7 88 | BOTTOMMARGIN, 287 89 | END 90 | END 91 | #endif // APSTUDIO_INVOKED 92 | 93 | #endif // English (U.K.) resources 94 | ///////////////////////////////////////////////////////////////////////////// 95 | 96 | 97 | 98 | #ifndef APSTUDIO_INVOKED 99 | ///////////////////////////////////////////////////////////////////////////// 100 | // 101 | // Generated from the TEXTINCLUDE 3 resource. 102 | // 103 | 104 | 105 | ///////////////////////////////////////////////////////////////////////////// 106 | #endif // not APSTUDIO_INVOKED 107 | 108 | -------------------------------------------------------------------------------- /src/JSONValue.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File JSONValue.h part of the SimpleJSON Library - http://mjpa.in/json 3 | * 4 | * Copyright (C) 2010 Mike Anchor 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | 25 | #ifndef _JSONVALUE_H_ 26 | #define _JSONVALUE_H_ 27 | 28 | #include 29 | #include 30 | 31 | #include "JSON.h" 32 | 33 | class JSON; 34 | 35 | enum JSONType { JSONType_Null, JSONType_String, JSONType_Bool, JSONType_Number, JSONType_Array, JSONType_Object }; 36 | 37 | class JSONValue 38 | { 39 | friend class JSON; 40 | 41 | public: 42 | JSONValue(/*NULL*/); 43 | JSONValue(const wchar_t *m_char_value); 44 | JSONValue(const std::wstring &m_string_value); 45 | JSONValue(bool m_bool_value); 46 | JSONValue(double m_number_value); 47 | JSONValue(int m_integer_value); 48 | JSONValue(const JSONArray &m_array_value); 49 | JSONValue(const JSONObject &m_object_value); 50 | JSONValue(const JSONValue &m_source); 51 | ~JSONValue(); 52 | 53 | bool IsNull() const; 54 | bool IsString() const; 55 | bool IsBool() const; 56 | bool IsNumber() const; 57 | bool IsArray() const; 58 | bool IsObject() const; 59 | 60 | const std::wstring &AsString() const; 61 | bool AsBool() const; 62 | double AsNumber() const; 63 | const JSONArray &AsArray() const; 64 | const JSONObject &AsObject() const; 65 | 66 | std::size_t CountChildren() const; 67 | bool HasChild(std::size_t index) const; 68 | JSONValue *Child(std::size_t index); 69 | bool HasChild(const wchar_t* name) const; 70 | JSONValue *Child(const wchar_t* name); 71 | std::vector ObjectKeys() const; 72 | 73 | std::wstring Stringify(bool const prettyprint = false) const; 74 | protected: 75 | static JSONValue *Parse(const wchar_t **data); 76 | 77 | private: 78 | static std::wstring StringifyString(const std::wstring &str); 79 | std::wstring StringifyImpl(size_t const indentDepth) const; 80 | static std::wstring Indent(size_t depth); 81 | 82 | JSONType type; 83 | 84 | union 85 | { 86 | bool bool_value; 87 | double number_value; 88 | std::wstring *string_value; 89 | JSONArray *array_value; 90 | JSONObject *object_value; 91 | }; 92 | 93 | }; 94 | 95 | #endif 96 | -------------------------------------------------------------------------------- /src/JSON.h: -------------------------------------------------------------------------------- 1 | /* 2 | * File JSON.h part of the SimpleJSON Library - http://mjpa.in/json 3 | * 4 | * Copyright (C) 2010 Mike Anchor 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | 25 | #ifndef _JSON_H_ 26 | #define _JSON_H_ 27 | 28 | // Win32 incompatibilities 29 | #if defined(WIN32) && !defined(__GNUC__) 30 | #define wcsncasecmp _wcsnicmp 31 | static inline bool isnan(double x) { return x != x; } 32 | static inline bool isinf(double x) { return !isnan(x) && isnan(x - x); } 33 | #endif 34 | 35 | #include 36 | #include 37 | #include 38 | 39 | // Linux compile fix - from quaker66 40 | #ifdef __GNUC__ 41 | #include 42 | #include 43 | #endif 44 | 45 | // Mac compile fixes - from quaker66, Lion fix by dabrahams 46 | #if defined(__APPLE__) && __DARWIN_C_LEVEL < 200809L || (defined(WIN32) && defined(__GNUC__)) || defined(ANDROID) 47 | #include 48 | #include 49 | 50 | static inline int wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n) 51 | { 52 | int lc1 = 0; 53 | int lc2 = 0; 54 | 55 | while (n--) 56 | { 57 | lc1 = towlower (*s1); 58 | lc2 = towlower (*s2); 59 | 60 | if (lc1 != lc2) 61 | return (lc1 - lc2); 62 | 63 | if (!lc1) 64 | return 0; 65 | 66 | ++s1; 67 | ++s2; 68 | } 69 | 70 | return 0; 71 | } 72 | #endif 73 | 74 | // Simple function to check a string 's' has at least 'n' characters 75 | static inline bool simplejson_wcsnlen(const wchar_t *s, size_t n) { 76 | if (s == 0) 77 | return false; 78 | 79 | const wchar_t *save = s; 80 | while (n-- > 0) 81 | { 82 | if (*(save++) == 0) return false; 83 | } 84 | 85 | return true; 86 | } 87 | 88 | // Custom types 89 | class JSONValue; 90 | typedef std::vector JSONArray; 91 | typedef std::map JSONObject; 92 | 93 | #include "JSONValue.h" 94 | 95 | class JSON 96 | { 97 | friend class JSONValue; 98 | 99 | public: 100 | static JSONValue* Parse(const char *data); 101 | static JSONValue* Parse(const wchar_t *data); 102 | static std::wstring Stringify(const JSONValue *value); 103 | protected: 104 | static bool SkipWhitespace(const wchar_t **data); 105 | static bool ExtractString(const wchar_t **data, std::wstring &str); 106 | static double ParseInt(const wchar_t **data); 107 | static double ParseDecimal(const wchar_t **data); 108 | private: 109 | JSON(); 110 | }; 111 | 112 | #endif 113 | -------------------------------------------------------------------------------- /vs2008/JSONDemo.vcproj: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 15 | 16 | 17 | 18 | 19 | 27 | 30 | 33 | 36 | 39 | 42 | 54 | 57 | 60 | 63 | 72 | 75 | 79 | 82 | 85 | 88 | 91 | 95 | 96 | 97 | 98 | 99 | 100 | 105 | 108 | 111 | 114 | 115 | 116 | 119 | 120 | 123 | 126 | 129 | 130 | 131 | 134 | 137 | 138 | 141 | 142 | 145 | 146 | 149 | 150 | 151 | 152 | 157 | 160 | 161 | 164 | 165 | 166 | 171 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | -------------------------------------------------------------------------------- /src/demo/nix-main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * File demo/nix-main.cpp part of the SimpleJSON Library Demo - http://mjpa.in/json 3 | * 4 | * Copyright (C) 2010 Mike Anchor 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | #include "../JSON.h" 30 | #include "functions.h" 31 | 32 | using namespace std; 33 | 34 | // Print out function 35 | void print_out(const wchar_t* output) 36 | { 37 | wcout << output; 38 | wcout.flush(); 39 | } 40 | 41 | // Linux entry point 42 | int main(int argc, char **argv) 43 | { 44 | // Required for utf8 chars 45 | setlocale(LC_CTYPE, ""); 46 | 47 | // The mode... 48 | string mode = argc != 2 ? "" : argv[1]; 49 | 50 | // Verifying? 51 | if (mode == "-v" || mode == "-f") 52 | { 53 | // Get the stdin data 54 | cin >> noskipws; 55 | istream_iterator it(cin); 56 | istream_iterator end; 57 | string results(it, end); 58 | 59 | // Parse it, print if the test is good/bad 60 | JSONValue *value = JSON::Parse(results.c_str()); 61 | if ((value != NULL && mode == "-v") || (value == NULL && mode == "-f")) 62 | wcout << L"PASS" << endl; 63 | else 64 | wcout << L"FAIL" << endl; 65 | 66 | if (value) delete value; 67 | } 68 | 69 | // Parse + echo? 70 | else if (mode == "-e" || mode == "-p") 71 | { 72 | // Get the stdin data 73 | cin >> noskipws; 74 | istream_iterator it(cin); 75 | istream_iterator end; 76 | string results(it, end); 77 | 78 | // Parse it & echo if it's valid 79 | JSONValue *value = NULL; 80 | if ((value = JSON::Parse(results.c_str())) == NULL) 81 | wcout << L"Code entered is *NOT* valid."; 82 | else 83 | { 84 | bool const prettyprint = (mode == "-p"); 85 | wcout << value->Stringify(prettyprint); 86 | } 87 | wcout << endl; 88 | 89 | if (value) delete value; 90 | } 91 | 92 | // Example ? 93 | else if (mode == "-ex1") 94 | { 95 | example1(); 96 | } 97 | else if (mode == "-ex2") 98 | { 99 | example2(); 100 | } 101 | else if (mode == "-ex3") 102 | { 103 | example3(); 104 | } 105 | else if (mode == "-ex4") 106 | { 107 | example4(); 108 | } 109 | 110 | // Test cases? 111 | else if (mode == "-t") 112 | { 113 | run_tests(); 114 | } 115 | 116 | // Help! 117 | else 118 | { 119 | wcout << L"Usage: " << argv[0] << L"