]> git.saurik.com Git - apple/dyld.git/blob - dyld3/JSONWriter.h
dyld-832.7.1.tar.gz
[apple/dyld.git] / dyld3 / JSONWriter.h
1 /*
2 * Copyright (c) 2017 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #ifndef __JSON_WRITER_H__
25 #define __JSON_WRITER_H__
26
27 #include <iostream>
28
29 #include "JSON.h"
30
31 namespace dyld3 {
32 namespace json {
33
34 static inline std::string hex(uint64_t value) {
35 char buff[64];
36 sprintf(buff, "0x%llX", value);
37 return buff;
38 }
39
40 static inline std::string hex4(uint64_t value) {
41 char buff[64];
42 sprintf(buff, "0x%04llX", value);
43 return buff;
44 }
45
46 static inline std::string hex8(uint64_t value) {
47 char buff[64];
48 sprintf(buff, "0x%08llX", value);
49 return buff;
50 }
51
52 static inline std::string decimal(uint64_t value) {
53 char buff[64];
54 sprintf(buff, "%llu", value);
55 return buff;
56 }
57
58 static inline void indentBy(uint32_t spaces, std::ostream& out) {
59 for (uint32_t i=0; i < spaces; ++i) {
60 out << " ";
61 }
62 }
63
64 static inline void printJSON(const Node& node, uint32_t indent = 0, std::ostream& out = std::cout)
65 {
66 if ( !node.map.empty() ) {
67 out << "{";
68 bool needComma = false;
69 for (const auto& entry : node.map) {
70 if ( needComma )
71 out << ",";
72 out << "\n";
73 indentBy(indent+2, out);
74 out << "\"" << entry.first << "\": ";
75 printJSON(entry.second, indent+2, out);
76 needComma = true;
77 }
78 out << "\n";
79 indentBy(indent, out);
80 out << "}";
81 }
82 else if ( !node.array.empty() ) {
83 out << "[";
84 bool needComma = false;
85 for (const auto& entry : node.array) {
86 if ( needComma )
87 out << ",";
88 out << "\n";
89 indentBy(indent+2, out);
90 printJSON(entry, indent+2, out);
91 needComma = true;
92 }
93 out << "\n";
94 indentBy(indent, out);
95 out << "]";
96 }
97 else {
98 auto &value = node.value;
99 switch (node.type) {
100 case NodeValueType::Default:
101 case NodeValueType::String:
102 if (value.find('"') == std::string::npos) {
103 out << "\"" << value << "\"";
104 } else {
105 std::string escapedString;
106 escapedString.reserve(value.size());
107 for (char c : value) {
108 if (c == '"')
109 escapedString += '\\';
110 escapedString += c;
111 }
112 out << "\"" << escapedString << "\"";
113 }
114 break;
115 case NodeValueType::RawValue:
116 out << value;
117 break;
118 }
119 }
120 if ( indent == 0 )
121 out << "\n";
122 }
123
124 static inline void streamArrayBegin(bool &needsComma, std::ostream& out = std::cout)
125 {
126 out << "[";
127 needsComma = false;
128 }
129
130 static inline void streamArrayNode(bool &needsComma, Node &node, std::ostream& out = std::cout)
131 {
132 if (needsComma)
133 out << ",";
134 out << "\n";
135 indentBy(2, out);
136 printJSON(node, 2, out);
137 needsComma = true;
138 }
139
140 static inline void streamArrayEnd(bool &needsComma, std::ostream& out = std::cout)
141 {
142 if (needsComma)
143 out << "\n";
144 out << "]\n";
145 }
146
147 } // namespace json
148 } // namespace dyld3
149
150
151 #endif // __JSON_WRITER_H__