Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
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, this
9 * 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 ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/JsonArray.h>
28#include <AK/JsonObject.h>
29#include <AK/JsonValue.h>
30#include <AK/StringBuilder.h>
31#include <LibCore/File.h>
32#include <stdio.h>
33#include <unistd.h>
34
35static void print(const JsonValue& value, int indent = 0);
36static void print_indent(int indent)
37{
38 for (int i = 0; i < indent; ++i)
39 printf(" ");
40}
41
42int main(int argc, char** argv)
43{
44 if (pledge("stdio rpath", nullptr) < 0) {
45 perror("pledge");
46 return 1;
47 }
48
49 if (argc != 2) {
50 fprintf(stderr, "usage: jp <file>\n");
51 return 0;
52 }
53 auto file = Core::File::construct(argv[1]);
54 if (!file->open(Core::IODevice::ReadOnly)) {
55 fprintf(stderr, "Couldn't open %s for reading: %s\n", argv[1], file->error_string());
56 return 1;
57 }
58
59 if (pledge("stdio", nullptr) < 0) {
60 perror("pledge");
61 return 1;
62 }
63
64 auto file_contents = file->read_all();
65 auto json = JsonValue::from_string(file_contents);
66
67 print(json);
68 printf("\n");
69
70 return 0;
71}
72
73void print(const JsonValue& value, int indent)
74{
75 if (value.is_object()) {
76 printf("{\n");
77 value.as_object().for_each_member([&](auto& member_name, auto& member_value) {
78 print_indent(indent + 1);
79 printf("\"\033[33;1m%s\033[0m\": ", member_name.characters());
80 print(member_value, indent + 1);
81 printf(",\n");
82 });
83 print_indent(indent);
84 printf("}");
85 return;
86 }
87 if (value.is_array()) {
88 printf("[\n");
89 value.as_array().for_each([&](auto& entry_value) {
90 print_indent(indent + 1);
91 print(entry_value, indent + 1);
92 printf(",\n");
93 });
94 print_indent(indent);
95 printf("]");
96 return;
97 }
98 if (value.is_string())
99 printf("\033[31;1m");
100 else if (value.is_number())
101 printf("\033[35;1m");
102 else if (value.is_bool())
103 printf("\033[32;1m");
104 else if (value.is_null() || value.is_undefined())
105 printf("\033[34;1m");
106 if (value.is_string())
107 putchar('"');
108 printf("%s", value.to_string().characters());
109 if (value.is_string())
110 putchar('"');
111 printf("\033[0m");
112}