dyld-732.8.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, std::ostream& out)
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 std::string escapedString;
99 escapedString.reserve(node.value.size());
100 for (char c : node.value) {
101 if (c == '"')
102 escapedString += '\\';
103 escapedString += c;
104 }
105 out << "\"" << escapedString << "\"";
106 }
107 if ( indent == 0 )
108 out << "\n";
109 }
110
111
112 } // namespace json
113 } // namespace dyld3
114
115
116 #endif // __JSON_WRITER_H__