Skip to content

Commit a56e92a

Browse files
committed
API works now.
1 parent 747bb16 commit a56e92a

File tree

2 files changed

+80
-79
lines changed

2 files changed

+80
-79
lines changed

include/simdjson/parsedjson.h

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ struct ParsedJson {
228228
return false;
229229
}
230230
for (; tapeidx < howmany; tapeidx++) {
231+
printf("%zu : ", tapeidx);
231232
tape_val = tape[tapeidx];
232233
u64 payload = tape_val & JSONVALUEMASK;
233234
type = (tape_val >> 56);
@@ -243,39 +244,39 @@ struct ParsedJson {
243244
if (tapeidx + 1 >= howmany)
244245
return false;
245246
printf("integer: ");
246-
printf("%" PRId64, (int64_t)tape[++tapeidx]);
247+
printf("%" PRId64"\n", (int64_t)tape[++tapeidx]);
247248
break;
248249
case 'd': // we have a double
249250
printf("float: ");
250251
if (tapeidx + 1 >= howmany)
251252
return false;
252253
double answer;
253254
memcpy(&answer, &tape[++tapeidx], sizeof(answer));
254-
printf("%f", answer);
255+
printf("%f\n", answer);
255256
break;
256257
case 'n': // we have a null
257-
printf("null");
258+
printf("null\n");
258259
break;
259260
case 't': // we have a true
260-
printf("true");
261+
printf("true\n");
261262
break;
262263
case 'f': // we have a false
263-
printf("false");
264+
printf("false\n");
264265
break;
265266
case '{': // we have an object
266-
printf("{");
267+
printf("{\n");
267268
break;
268269
case '}': // we end an object
269-
printf("}");
270+
printf("}\n");
270271
break;
271272
case '[': // we start an array
272-
printf("[");
273+
printf("[\n");
273274
break;
274275
case ']': // we end an array
275-
printf("]");
276+
printf("]\n");
276277
break;
277278
case 'r': // we start and end with the root node
278-
printf("end of root");
279+
printf("end of root\n");
279280
return false;
280281
default:
281282
return false;
@@ -336,12 +337,11 @@ struct ParsedJson {
336337
current_type = (current_val >> 56);
337338
if (current_type == 'r') {
338339
tape_length = current_val & JSONVALUEMASK;
339-
if(location + 1 < tape_length) {
340+
if(location < tape_length) {
340341
current_val = pj.tape[location];
341342
current_type = (current_val >> 56);
342343
depth++;
343344
depthindex[depth] = location;
344-
location++;
345345
}
346346
}
347347
}
@@ -373,7 +373,13 @@ struct ParsedJson {
373373
return location < tape_length;
374374
}
375375

376+
size_t get_tape_location() const {
377+
return location;
378+
}
376379

380+
size_t get_tape_lenght() const {
381+
return tape_length;
382+
}
377383

378384
// return true if we can do the navigation, false
379385
// otherwise
@@ -382,7 +388,7 @@ struct ParsedJson {
382388
really_inline bool next() {
383389
if ((current_type == '[') || (current_type == '{')){
384390
// we need to jump
385-
size_t npos = ( current_val & JSONVALUEMASK) + 1; // +1 to skip of end
391+
size_t npos = ( current_val & JSONVALUEMASK);
386392
if(npos >= tape_length) {
387393
return false; // shoud never happen unless at the root
388394
}

tools/json2json.cpp

Lines changed: 61 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,83 @@
1+
#include <iostream>
12
#include <unistd.h>
23

3-
#include "simdjson/jsonparser.h"
44
#include "simdjson/jsonioutil.h"
5+
#include "simdjson/jsonparser.h"
56

67
using namespace std;
78

8-
void compute_dump(ParsedJson::iterator & pjh) {
9-
bool inobject = (pjh.get_type() == '{');
10-
bool inarray = (pjh.get_type() == '{');
11-
printf("got this: \n");
12-
pjh.print(std::cout);
13-
printf(" \n");
14-
if((!inobject) && (!inarray)) {
15-
pjh.print(std::cout);// just print the lone value
16-
pjh.up() && pjh.next();
17-
return; // we are done
9+
void compute_dump(ParsedJson::iterator &pjh) {
10+
bool inobject = (pjh.get_type() == '{');
11+
bool inarray = (pjh.get_type() == '[');
12+
if ((!inobject) && (!inarray)) {
13+
pjh.print(std::cout); // just print the lone value
14+
return; // we are done
15+
}
16+
// we have either an array or an object
17+
pjh.down();
18+
if (inobject) {
19+
std::cout << "{";
20+
assert(pjh.get_type() == '"');
21+
pjh.print(std::cout); // must be a string
22+
std::cout << ":";
23+
assert(pjh.next());
24+
compute_dump(pjh); // let us recurse
25+
while (pjh.next()) {
26+
std::cout << ",";
27+
assert(pjh.get_type() == '"');
28+
pjh.print(std::cout);
29+
std::cout << ":";
30+
assert(pjh.next());
31+
compute_dump(pjh); // let us recurse
1832
}
19-
printf("going to object/array\n");
20-
21-
// we have either an array or an object
22-
pjh.down();
23-
if(inobject) {
24-
std::cout <<"{";
25-
pjh.print(std::cout); // must be a string
26-
std::cout <<":";
27-
pjh.next();
28-
compute_dump(pjh);// let us recurse
29-
while (pjh.next()) {
30-
std::cout <<",";
31-
pjh.print(std::cout);
32-
std::cout <<":";
33-
pjh.next();
34-
compute_dump(pjh);// let us recurse
35-
}
36-
std::cout <<"}";
37-
} else {
38-
std::cout <<"[";
39-
compute_dump(pjh);// let us recurse
40-
while(pjh.next()) {
41-
std::cout <<",";
42-
compute_dump(pjh);// let us recurse
43-
}
44-
std::cout <<"]";
33+
std::cout << "}";
34+
} else {
35+
std::cout << "[";
36+
compute_dump(pjh); // let us recurse
37+
while (pjh.next()) {
38+
std::cout << ",";
39+
compute_dump(pjh); // let us recurse
4540
}
46-
pjh.up() && pjh.next();
41+
std::cout << "]";
42+
}
43+
pjh.up();
4744
}
48-
4945

5046
int main(int argc, char *argv[]) {
5147
int c;
5248
bool rawdump = false;
5349
bool apidump = false;
5450

55-
while ((c = getopt (argc, argv, "da")) != -1)
56-
switch (c)
57-
{
58-
case 'd':
59-
rawdump = true;
60-
break;
61-
case 'a':
62-
apidump = true;
63-
break;
64-
default:
65-
abort ();
66-
}
51+
while ((c = getopt(argc, argv, "da")) != -1)
52+
switch (c) {
53+
case 'd':
54+
rawdump = true;
55+
break;
56+
case 'a':
57+
apidump = true;
58+
break;
59+
default:
60+
abort();
61+
}
6762
if (optind >= argc) {
68-
cerr << "Reads json in, out the result of the parsing. " << endl;
63+
cerr << "Reads json in, out the result of the parsing. " << endl;
6964
cerr << "Usage: " << argv[0] << " <jsonfile>" << endl;
7065
exit(1);
7166
}
72-
const char * filename = argv[optind];
73-
if(optind + 1 < argc) {
74-
cerr << "warning: ignoring everything after " << argv[optind + 1] << endl;
67+
const char *filename = argv[optind];
68+
if (optind + 1 < argc) {
69+
cerr << "warning: ignoring everything after " << argv[optind + 1] << endl;
7570
}
7671
std::string_view p;
7772
try {
7873
p = get_corpus(filename);
79-
} catch (const std::exception& e) { // caught by reference to base
74+
} catch (const std::exception &e) { // caught by reference to base
8075
std::cout << "Could not load the file " << filename << std::endl;
8176
return EXIT_FAILURE;
8277
}
8378
ParsedJson pj;
8479
bool allocok = pj.allocateCapacity(p.size(), 1024);
85-
if(!allocok) {
80+
if (!allocok) {
8681
std::cerr << "failed to allocate memory" << std::endl;
8782
return EXIT_FAILURE;
8883
}
@@ -91,18 +86,18 @@ int main(int argc, char *argv[]) {
9186
std::cerr << " Parsing failed. " << std::endl;
9287
return EXIT_FAILURE;
9388
}
94-
if(apidump) {
95-
ParsedJson::iterator pjh(pj);
96-
if(!pjh.isOk()) {
97-
std::cerr << " Could not iterate parsed result. " << std::endl;
98-
return EXIT_FAILURE;
99-
}
100-
compute_dump(pjh);
89+
if (apidump) {
90+
ParsedJson::iterator pjh(pj);
91+
if (!pjh.isOk()) {
92+
std::cerr << " Could not iterate parsed result. " << std::endl;
93+
return EXIT_FAILURE;
94+
}
95+
compute_dump(pjh);
10196
} else {
10297
is_ok = rawdump ? pj.dump_raw_tape() : pj.printjson();
103-
if(!is_ok) {
98+
if (!is_ok) {
10499
std::cerr << " Could not print out parsed result. " << std::endl;
105-
return EXIT_FAILURE;
100+
return EXIT_FAILURE;
106101
}
107102
}
108103
return EXIT_SUCCESS;

0 commit comments

Comments
 (0)