ruco-cpp 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Rakefile +17 -0
- data/bin/console +14 -0
- data/bin/ruco +30 -0
- data/bin/setup +7 -0
- data/data/ruco/Parser.frame +359 -0
- data/data/ruco/Scanner.frame +896 -0
- data/data/ruco/picojson/Changes +14 -0
- data/data/ruco/picojson/LICENSE +25 -0
- data/data/ruco/picojson/Makefile +8 -0
- data/data/ruco/picojson/README.mkdn +183 -0
- data/data/ruco/picojson/examples/github-issues.cc +110 -0
- data/data/ruco/picojson/examples/iostream.cc +70 -0
- data/data/ruco/picojson/examples/streaming.cc +76 -0
- data/data/ruco/picojson/picojson.h +1299 -0
- data/ext/cocor/Action.cpp +81 -0
- data/ext/cocor/Action.h +59 -0
- data/ext/cocor/ArrayList.cpp +79 -0
- data/ext/cocor/ArrayList.h +52 -0
- data/ext/cocor/BitArray.cpp +156 -0
- data/ext/cocor/BitArray.h +68 -0
- data/ext/cocor/CharClass.cpp +42 -0
- data/ext/cocor/CharClass.h +48 -0
- data/ext/cocor/CharSet.cpp +166 -0
- data/ext/cocor/CharSet.h +68 -0
- data/ext/cocor/Coco.atg +528 -0
- data/ext/cocor/Coco.cpp +173 -0
- data/ext/cocor/Comment.cpp +45 -0
- data/ext/cocor/Comment.h +51 -0
- data/ext/cocor/Copyright.frame +27 -0
- data/ext/cocor/DFA.cpp +865 -0
- data/ext/cocor/DFA.h +132 -0
- data/ext/cocor/Generator.cpp +182 -0
- data/ext/cocor/Generator.h +61 -0
- data/ext/cocor/Graph.h +59 -0
- data/ext/cocor/HashTable.cpp +115 -0
- data/ext/cocor/HashTable.h +84 -0
- data/ext/cocor/Makefile +11 -0
- data/ext/cocor/Melted.cpp +39 -0
- data/ext/cocor/Melted.h +51 -0
- data/ext/cocor/Node.cpp +69 -0
- data/ext/cocor/Node.h +86 -0
- data/ext/cocor/Parser.cpp +925 -0
- data/ext/cocor/Parser.frame +326 -0
- data/ext/cocor/Parser.h +153 -0
- data/ext/cocor/ParserGen.cpp +486 -0
- data/ext/cocor/ParserGen.h +99 -0
- data/ext/cocor/Position.cpp +37 -0
- data/ext/cocor/Position.h +46 -0
- data/ext/cocor/README.md +12 -0
- data/ext/cocor/Scanner.cpp +833 -0
- data/ext/cocor/Scanner.frame +897 -0
- data/ext/cocor/Scanner.h +291 -0
- data/ext/cocor/Sets.h +84 -0
- data/ext/cocor/SortedList.cpp +141 -0
- data/ext/cocor/SortedList.h +68 -0
- data/ext/cocor/State.cpp +77 -0
- data/ext/cocor/State.h +55 -0
- data/ext/cocor/StringBuilder.cpp +88 -0
- data/ext/cocor/StringBuilder.h +29 -0
- data/ext/cocor/Symbol.cpp +61 -0
- data/ext/cocor/Symbol.h +70 -0
- data/ext/cocor/Tab.cpp +1248 -0
- data/ext/cocor/Tab.h +245 -0
- data/ext/cocor/Target.cpp +41 -0
- data/ext/cocor/Target.h +48 -0
- data/ext/cocor/build.bat +3 -0
- data/ext/cocor/build.sh +4 -0
- data/ext/cocor/coc.bat +1 -0
- data/ext/cocor/coc.sh +2 -0
- data/ext/cocor/cocor_ruby_ext.cpp +124 -0
- data/ext/cocor/cygBuild.bat +1 -0
- data/ext/cocor/extconf.rb +5 -0
- data/ext/cocor/mingwbuild.bat +2 -0
- data/ext/cocor/mkmf.log +57 -0
- data/ext/cocor/zipsources.bat +1 -0
- data/lib/cocor.rb +14 -0
- data/lib/ruco/version.rb +3 -0
- data/lib/ruco.rb +728 -0
- metadata +195 -0
@@ -0,0 +1,14 @@
|
|
1
|
+
Revision history for picojson
|
2
|
+
|
3
|
+
1.1.1 2014-06-25 10:35:00+0900
|
4
|
+
- tweaks to suppress compiler errors / warning (#38 #39)
|
5
|
+
- clarify the licenses of the files in exmaple/ (#42)
|
6
|
+
|
7
|
+
1.1 2014-06-16 12:57:00+0900
|
8
|
+
- added experimental support for int64 type (#34)
|
9
|
+
- by default, throw std::runtime_error instead of using assert for runtime errors (#33)
|
10
|
+
- refine compatibility regarding the use of isinf/isnan (#29, #36)
|
11
|
+
- remove `.get<int>()` (#35)
|
12
|
+
|
13
|
+
1.0 2014-06-05 12:54:00+0900
|
14
|
+
- initial release with a version number
|
@@ -0,0 +1,25 @@
|
|
1
|
+
Copyright 2009-2010 Cybozu Labs, Inc.
|
2
|
+
Copyright 2011-2014 Kazuho Oku
|
3
|
+
All rights reserved.
|
4
|
+
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
7
|
+
|
8
|
+
1. Redistributions of source code must retain the above copyright notice,
|
9
|
+
this list of conditions and the following disclaimer.
|
10
|
+
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
13
|
+
and/or other materials provided with the distribution.
|
14
|
+
|
15
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
16
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
17
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
18
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
19
|
+
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
20
|
+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
21
|
+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
22
|
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
23
|
+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
24
|
+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
25
|
+
POSSIBILITY OF SUCH DAMAGE.
|
@@ -0,0 +1,183 @@
|
|
1
|
+
# PicoJSON - a C++ JSON parser / serializer
|
2
|
+
|
3
|
+
Copyright © 2009-2010 Cybozu Labs, Inc.
|
4
|
+
Copyright © 2011-2014 Kazuho Oku
|
5
|
+
|
6
|
+
Licensed under [2-clause BSD license](http://opensource.org/licenses/BSD-2-Clause)
|
7
|
+
|
8
|
+
## Version
|
9
|
+
|
10
|
+
1.1.2-dev [![Build Status](https://travis-ci.org/kazuho/picojson.svg?branch=master)](https://travis-ci.org/kazuho/picojson)
|
11
|
+
|
12
|
+
## Introduction
|
13
|
+
|
14
|
+
PicoJSON is a tiny JSON parser / serializer for C++ with following properties:
|
15
|
+
|
16
|
+
- header-file only
|
17
|
+
- no external dependencies (only uses standard C++ libraries)
|
18
|
+
- STL-frendly (arrays are represented by using std::vector, objects are std::map)
|
19
|
+
- provides both pull interface and streaming (event-based) interface
|
20
|
+
|
21
|
+
## Reading JSON using the pull interface
|
22
|
+
|
23
|
+
There are two ways to use the pull (DOM-like) interface of picojson. One is to use operator<<, and the other is by specifying a set of iterators specifying the range from where the JSON stream should be read.
|
24
|
+
|
25
|
+
<pre>
|
26
|
+
picojson::value v;
|
27
|
+
std::cin >> v;
|
28
|
+
std::string err = picojson::get_last_error();
|
29
|
+
if (! err.empty()) {
|
30
|
+
std::cerr << err << std::endl;
|
31
|
+
}
|
32
|
+
</pre>
|
33
|
+
|
34
|
+
<pre>
|
35
|
+
std::istream_iterator input(cin);
|
36
|
+
picojson::value v;
|
37
|
+
std::string err;
|
38
|
+
input = picojson::parse(v, input, std::istream_iterator(), &err);
|
39
|
+
if (! err.empty()) {
|
40
|
+
std::cerr << err << std::endl;
|
41
|
+
}
|
42
|
+
</pre>
|
43
|
+
|
44
|
+
<pre>
|
45
|
+
const char* json = "{\"a\":1}";
|
46
|
+
picojson::value v;
|
47
|
+
std::string err;
|
48
|
+
picojson::parse(v, json, json + strlen(json), &err);
|
49
|
+
if (! err.empty()) {
|
50
|
+
std::cerr << err << std::endl;
|
51
|
+
}
|
52
|
+
</pre>
|
53
|
+
|
54
|
+
## Accessing the values
|
55
|
+
|
56
|
+
Values of a JSON object is represented as instances of picojson::value class.
|
57
|
+
|
58
|
+
<pre>
|
59
|
+
namespace picojson {
|
60
|
+
|
61
|
+
class value {
|
62
|
+
...
|
63
|
+
|
64
|
+
public:
|
65
|
+
|
66
|
+
typedef std::vector<value> array;
|
67
|
+
typedef std::map<std::string, value> object;
|
68
|
+
|
69
|
+
value(); // create a null object
|
70
|
+
explicit value(bool b); // create a boolean object
|
71
|
+
explicit value(double n); // create a number object
|
72
|
+
explicit value(const std::string& s); // create a string object
|
73
|
+
explicit value(const array& a); // create an array object
|
74
|
+
explicit value(const object& o); // create an "object"
|
75
|
+
|
76
|
+
bool is<picojson::null>() const; // check if the object is "null"
|
77
|
+
|
78
|
+
bool is<bool>() const; // check if the object is a boolean
|
79
|
+
const bool& get<bool>() const; // const accessor (usable only if the object is a boolean)
|
80
|
+
bool& get<bool>(); // non-const accessor (usable only if the object is a boolean)
|
81
|
+
|
82
|
+
bool is<double>() const; // check if the object is a number
|
83
|
+
const double& get<double>() const; // const accessor (usable only if the object is a number)
|
84
|
+
double& get<double>(); // non-const accessor (usable only if the object is a number)
|
85
|
+
|
86
|
+
bool is<std::string>() const; // check if the object is a string
|
87
|
+
const std::string& get<std::string>() const;
|
88
|
+
// const accessor (usable only if the object is a string)
|
89
|
+
std::string& get<std::string>(); // non-const accessor (usable only if the object is a string)
|
90
|
+
|
91
|
+
bool is<array>() const; // check if the object is an array
|
92
|
+
const array& get<array>() const; // const accessor (usable only if the object is an array)
|
93
|
+
array& get<array>(); // non-const accessor (usable only if the object is an array)
|
94
|
+
|
95
|
+
bool is<object>() const; // check if the object is an "object"
|
96
|
+
const object& get<object>() const; // const accessor (usable only if the object is an object)
|
97
|
+
object& get<object>(); // non-const accessor (usable only if the object is an array)
|
98
|
+
|
99
|
+
bool evaluate_as_boolean() const; // evaluates the object as a boolean
|
100
|
+
|
101
|
+
std::string serialize() const; // returns the object in JSON representation
|
102
|
+
template<typename Iter> void serialize(Iter os) const;
|
103
|
+
// serializes the object in JSON representation through an output iterator
|
104
|
+
|
105
|
+
std::string to_str() const; // returns the object in string (for casual use)
|
106
|
+
|
107
|
+
};
|
108
|
+
|
109
|
+
}
|
110
|
+
</pre>
|
111
|
+
|
112
|
+
The code below parses a JSON string and prints the contents of the object.
|
113
|
+
|
114
|
+
<pre>
|
115
|
+
picojson::value v;
|
116
|
+
|
117
|
+
// parse the input
|
118
|
+
std::cin >> v;
|
119
|
+
std::string err = picojson::get_last_error();
|
120
|
+
if (! err.empty()) {
|
121
|
+
std::cerr << err << std::endl;
|
122
|
+
exit(1);
|
123
|
+
}
|
124
|
+
|
125
|
+
// check if the type of the value is "object"
|
126
|
+
if (! v.is<picojson::object>()) {
|
127
|
+
std::cerr << "JSON is not an object" << std::endl;
|
128
|
+
exit(2);
|
129
|
+
}
|
130
|
+
|
131
|
+
// obtain a const reference to the map, and print the contents
|
132
|
+
const picojson::value::object& obj = v.get<picojson::object>();
|
133
|
+
for (picojson::value::object::const_iterator i = obj.begin();
|
134
|
+
i != obj.end();
|
135
|
+
++i) {
|
136
|
+
std::cout << i->first << ': ' << i->second.to_str() << std::endl;
|
137
|
+
}
|
138
|
+
</pre>
|
139
|
+
|
140
|
+
Please note that the type check is mandatory; do not forget to check the type of the object by calling is<type>() before accessing the value by calling get<type>().
|
141
|
+
|
142
|
+
## Reading JSON using the streaming (event-driven) interface
|
143
|
+
|
144
|
+
Please refer to the implementation of picojson::default_parse_context and picojson::null_parse_context. There is also an example (examples/streaming.cc) .
|
145
|
+
|
146
|
+
## Serializing to JSON
|
147
|
+
|
148
|
+
Instances of the picojson::value class can be serialized in three ways, to ostream, to std::string, or to an output iterator.
|
149
|
+
|
150
|
+
<pre>
|
151
|
+
picojson::value v;
|
152
|
+
...
|
153
|
+
std::cout << v;
|
154
|
+
</pre>
|
155
|
+
|
156
|
+
<pre>
|
157
|
+
picojson::value v;
|
158
|
+
...
|
159
|
+
std::string json = v.serialize();
|
160
|
+
</pre>
|
161
|
+
|
162
|
+
<pre>
|
163
|
+
picojson::value v;
|
164
|
+
...
|
165
|
+
v.serialize(std::ostream_iterator(std::cout));
|
166
|
+
</pre>
|
167
|
+
|
168
|
+
## Experimental support for int64_t
|
169
|
+
|
170
|
+
Experimental suport for int64_t becomes available if the code is compiled with preprocessor macro `PICOJSON_USE_INT64`.
|
171
|
+
|
172
|
+
Turning on the feature will cause following changes to picojson:
|
173
|
+
- new constructor `picojson::value(int64_t)` is defined
|
174
|
+
- `is<int64_t>()` and `get<int64_t>()` become available
|
175
|
+
- numerics in JSON within the bounds of int64_t and not using `.` nor `e`/`E` are considered as int64 type
|
176
|
+
- the values are also avaliable as `double`s as well (i.e. all values which are `.is<int64_t>() == true` are also `.is<double>() == true`)
|
177
|
+
- int64 values are converted to double once `get<double>()` is called
|
178
|
+
|
179
|
+
Enabling the feature should not cause compatibility problem with code that do not use the feature.
|
180
|
+
|
181
|
+
## Further reading
|
182
|
+
|
183
|
+
Examples can be found in the <i>examples</i> directory, and on the [Wiki](https://github.com/kazuho/picojson/wiki). Please add your favorite examples to the Wiki.
|
@@ -0,0 +1,110 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright 2009-2010 Cybozu Labs, Inc.
|
3
|
+
* Copyright 2011-2014 Kazuho Oku
|
4
|
+
* All rights reserved.
|
5
|
+
*
|
6
|
+
* Redistribution and use in source and binary forms, with or without
|
7
|
+
* modification, are permitted provided that the following conditions are met:
|
8
|
+
*
|
9
|
+
* 1. Redistributions of source code must retain the above copyright notice,
|
10
|
+
* this list of conditions and the following disclaimer.
|
11
|
+
*
|
12
|
+
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
13
|
+
* this list of conditions and the following disclaimer in the documentation
|
14
|
+
* and/or other materials provided with the distribution.
|
15
|
+
*
|
16
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
17
|
+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
18
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
19
|
+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
20
|
+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
21
|
+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
22
|
+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
23
|
+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
24
|
+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
25
|
+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
26
|
+
* POSSIBILITY OF SUCH DAMAGE.
|
27
|
+
*/
|
28
|
+
#include <curl/curl.h>
|
29
|
+
#include "../picojson.h"
|
30
|
+
|
31
|
+
typedef struct {
|
32
|
+
char* data; // response data from server
|
33
|
+
size_t size; // response size of data
|
34
|
+
} MEMFILE;
|
35
|
+
|
36
|
+
MEMFILE*
|
37
|
+
memfopen() {
|
38
|
+
MEMFILE* mf = (MEMFILE*) malloc(sizeof(MEMFILE));
|
39
|
+
mf->data = NULL;
|
40
|
+
mf->size = 0;
|
41
|
+
return mf;
|
42
|
+
}
|
43
|
+
|
44
|
+
void
|
45
|
+
memfclose(MEMFILE* mf) {
|
46
|
+
if (mf->data) free(mf->data);
|
47
|
+
free(mf);
|
48
|
+
}
|
49
|
+
|
50
|
+
size_t
|
51
|
+
memfwrite(char* ptr, size_t size, size_t nmemb, void* stream) {
|
52
|
+
MEMFILE* mf = (MEMFILE*) stream;
|
53
|
+
int block = size * nmemb;
|
54
|
+
if (!mf->data)
|
55
|
+
mf->data = (char*) malloc(block);
|
56
|
+
else
|
57
|
+
mf->data = (char*) realloc(mf->data, mf->size + block);
|
58
|
+
if (mf->data) {
|
59
|
+
memcpy(mf->data + mf->size, ptr, block);
|
60
|
+
mf->size += block;
|
61
|
+
}
|
62
|
+
return block;
|
63
|
+
}
|
64
|
+
|
65
|
+
char*
|
66
|
+
memfstrdup(MEMFILE* mf) {
|
67
|
+
char* buf = (char*)malloc(mf->size + 1);
|
68
|
+
memcpy(buf, mf->data, mf->size);
|
69
|
+
buf[mf->size] = 0;
|
70
|
+
return buf;
|
71
|
+
}
|
72
|
+
|
73
|
+
using namespace std;
|
74
|
+
using namespace picojson;
|
75
|
+
|
76
|
+
int
|
77
|
+
main(int argc, char* argv[]) {
|
78
|
+
char error[256];
|
79
|
+
|
80
|
+
MEMFILE* mf = memfopen();
|
81
|
+
CURL* curl = curl_easy_init();
|
82
|
+
curl_easy_setopt(curl, CURLOPT_URL, "https://api.github.com/repos/kazuho/picojson/issues");
|
83
|
+
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
|
84
|
+
curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl");
|
85
|
+
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error);
|
86
|
+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, memfwrite);
|
87
|
+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, mf);
|
88
|
+
if (curl_easy_perform(curl) != CURLE_OK) {
|
89
|
+
cerr << error << endl;
|
90
|
+
} else {
|
91
|
+
value v;
|
92
|
+
string err;
|
93
|
+
parse(v, mf->data, mf->data + mf->size, &err);
|
94
|
+
if (err.empty()) {
|
95
|
+
array arr = v.get<array>();
|
96
|
+
array::iterator it;
|
97
|
+
for (it = arr.begin(); it != arr.end(); it++) {
|
98
|
+
object obj = it->get<object>();
|
99
|
+
cout << "#" << obj["number"].to_str() << ": " << obj["title"].to_str() << endl;
|
100
|
+
cout << " " << obj["html_url"].to_str() << endl << endl;
|
101
|
+
}
|
102
|
+
} else {
|
103
|
+
cerr << err << endl;
|
104
|
+
}
|
105
|
+
}
|
106
|
+
curl_easy_cleanup(curl);
|
107
|
+
memfclose(mf);
|
108
|
+
|
109
|
+
return 0;
|
110
|
+
}
|
@@ -0,0 +1,70 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright 2009-2010 Cybozu Labs, Inc.
|
3
|
+
* Copyright 2011-2014 Kazuho Oku
|
4
|
+
* All rights reserved.
|
5
|
+
*
|
6
|
+
* Redistribution and use in source and binary forms, with or without
|
7
|
+
* modification, are permitted provided that the following conditions are met:
|
8
|
+
*
|
9
|
+
* 1. Redistributions of source code must retain the above copyright notice,
|
10
|
+
* this list of conditions and the following disclaimer.
|
11
|
+
*
|
12
|
+
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
13
|
+
* this list of conditions and the following disclaimer in the documentation
|
14
|
+
* and/or other materials provided with the distribution.
|
15
|
+
*
|
16
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
17
|
+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
18
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
19
|
+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
20
|
+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
21
|
+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
22
|
+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
23
|
+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
24
|
+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
25
|
+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
26
|
+
* POSSIBILITY OF SUCH DAMAGE.
|
27
|
+
*/
|
28
|
+
#include "../picojson.h"
|
29
|
+
|
30
|
+
int main(void)
|
31
|
+
{
|
32
|
+
picojson::value v;
|
33
|
+
|
34
|
+
// read json value from stream
|
35
|
+
std::cin >> v;
|
36
|
+
if (std::cin.fail()) {
|
37
|
+
std::cerr << picojson::get_last_error() << std::endl;
|
38
|
+
return 1;
|
39
|
+
}
|
40
|
+
|
41
|
+
// dump json object
|
42
|
+
std::cout << "---- dump input ----" << std::endl;
|
43
|
+
std::cout << v << std::endl;
|
44
|
+
|
45
|
+
// accessors
|
46
|
+
std::cout << "---- analyzing input ----" << std::endl;
|
47
|
+
if (v.is<picojson::null>()) {
|
48
|
+
std::cout << "input is null" << std::endl;
|
49
|
+
} else if (v.is<bool>()) {
|
50
|
+
std::cout << "input is " << (v.get<bool>() ? "true" : "false") << std::endl;
|
51
|
+
} else if (v.is<double>()) {
|
52
|
+
std::cout << "input is " << v.get<double>() << std::endl;
|
53
|
+
} else if (v.is<std::string>()) {
|
54
|
+
std::cout << "input is " << v.get<std::string>() << std::endl;
|
55
|
+
} else if (v.is<picojson::array>()) {
|
56
|
+
std::cout << "input is an array" << std::endl;
|
57
|
+
const picojson::array& a = v.get<picojson::array>();
|
58
|
+
for (picojson::array::const_iterator i = a.begin(); i != a.end(); ++i) {
|
59
|
+
std::cout << " " << *i << std::endl;
|
60
|
+
}
|
61
|
+
} else if (v.is<picojson::object>()) {
|
62
|
+
std::cout << "input is an object" << std::endl;
|
63
|
+
const picojson::object& o = v.get<picojson::object>();
|
64
|
+
for (picojson::object::const_iterator i = o.begin(); i != o.end(); ++i) {
|
65
|
+
std::cout << i->first << " " << i->second << std::endl;
|
66
|
+
}
|
67
|
+
}
|
68
|
+
|
69
|
+
return 0;
|
70
|
+
}
|
@@ -0,0 +1,76 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright 2009-2010 Cybozu Labs, Inc.
|
3
|
+
* Copyright 2011-2014 Kazuho Oku
|
4
|
+
* All rights reserved.
|
5
|
+
*
|
6
|
+
* Redistribution and use in source and binary forms, with or without
|
7
|
+
* modification, are permitted provided that the following conditions are met:
|
8
|
+
*
|
9
|
+
* 1. Redistributions of source code must retain the above copyright notice,
|
10
|
+
* this list of conditions and the following disclaimer.
|
11
|
+
*
|
12
|
+
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
13
|
+
* this list of conditions and the following disclaimer in the documentation
|
14
|
+
* and/or other materials provided with the distribution.
|
15
|
+
*
|
16
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
17
|
+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
18
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
19
|
+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
20
|
+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
21
|
+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
22
|
+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
23
|
+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
24
|
+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
25
|
+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
26
|
+
* POSSIBILITY OF SUCH DAMAGE.
|
27
|
+
*/
|
28
|
+
#include <iostream>
|
29
|
+
#include <iterator>
|
30
|
+
#include "../picojson.h"
|
31
|
+
|
32
|
+
// this example reads a array of hashes (each item representing a 2D point),
|
33
|
+
// and prints the x and y values to stdout
|
34
|
+
|
35
|
+
namespace {
|
36
|
+
|
37
|
+
class root_context : public picojson::deny_parse_context {
|
38
|
+
public:
|
39
|
+
bool parse_array_start() {
|
40
|
+
return true; // only allow array as root
|
41
|
+
}
|
42
|
+
template <typename Iter> bool parse_array_item(picojson::input<Iter>& in, size_t) {
|
43
|
+
picojson::value item;
|
44
|
+
// parse the array item
|
45
|
+
picojson::default_parse_context ctx(&item);
|
46
|
+
if (! picojson::_parse(ctx, in)) {
|
47
|
+
return false;
|
48
|
+
}
|
49
|
+
// assert that the array item is a hash
|
50
|
+
if (! item.is<picojson::object>()) {
|
51
|
+
return false;
|
52
|
+
}
|
53
|
+
// print x and y
|
54
|
+
std::cout << item.get("x") << ',' << item.get("y").to_str()
|
55
|
+
<< std::endl;
|
56
|
+
return true;
|
57
|
+
}
|
58
|
+
};
|
59
|
+
|
60
|
+
}
|
61
|
+
|
62
|
+
int main(void)
|
63
|
+
{
|
64
|
+
root_context ctx;
|
65
|
+
std::string err;
|
66
|
+
|
67
|
+
picojson::_parse(ctx, std::istream_iterator<char>(std::cin),
|
68
|
+
std::istream_iterator<char>(), &err);
|
69
|
+
|
70
|
+
if (! err.empty()) {
|
71
|
+
std::cerr << err << std::endl;
|
72
|
+
return 1;
|
73
|
+
}
|
74
|
+
|
75
|
+
return 0;
|
76
|
+
}
|