]> git.saurik.com Git - apple/dyld.git/blame - dyld3/JSONWriter.h
dyld-851.27.tar.gz
[apple/dyld.git] / dyld3 / JSONWriter.h
CommitLineData
6cae9b63
A
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
cf998323
A
24#ifndef __JSON_WRITER_H__
25#define __JSON_WRITER_H__
6cae9b63 26
cf998323
A
27#include <iostream>
28
29#include "JSON.h"
6cae9b63
A
30
31namespace dyld3 {
32namespace json {
33
6cae9b63
A
34static inline std::string hex(uint64_t value) {
35 char buff[64];
36 sprintf(buff, "0x%llX", value);
37 return buff;
38}
39
40static inline std::string hex4(uint64_t value) {
41 char buff[64];
42 sprintf(buff, "0x%04llX", value);
43 return buff;
44}
45
46static inline std::string hex8(uint64_t value) {
47 char buff[64];
48 sprintf(buff, "0x%08llX", value);
49 return buff;
50}
51
52static inline std::string decimal(uint64_t value) {
53 char buff[64];
54 sprintf(buff, "%llu", value);
55 return buff;
56}
57
cf998323
A
58static inline void indentBy(uint32_t spaces, std::ostream& out) {
59 for (uint32_t i=0; i < spaces; ++i) {
60 out << " ";
6cae9b63
A
61 }
62}
63
bc3b7c8c 64static inline void printJSON(const Node& node, uint32_t indent = 0, std::ostream& out = std::cout)
6cae9b63
A
65{
66 if ( !node.map.empty() ) {
cf998323 67 out << "{";
6cae9b63
A
68 bool needComma = false;
69 for (const auto& entry : node.map) {
70 if ( needComma )
cf998323
A
71 out << ",";
72 out << "\n";
6cae9b63 73 indentBy(indent+2, out);
cf998323 74 out << "\"" << entry.first << "\": ";
6cae9b63
A
75 printJSON(entry.second, indent+2, out);
76 needComma = true;
77 }
cf998323 78 out << "\n";
6cae9b63 79 indentBy(indent, out);
cf998323 80 out << "}";
6cae9b63
A
81 }
82 else if ( !node.array.empty() ) {
cf998323 83 out << "[";
6cae9b63
A
84 bool needComma = false;
85 for (const auto& entry : node.array) {
86 if ( needComma )
cf998323
A
87 out << ",";
88 out << "\n";
6cae9b63
A
89 indentBy(indent+2, out);
90 printJSON(entry, indent+2, out);
91 needComma = true;
92 }
cf998323 93 out << "\n";
6cae9b63 94 indentBy(indent, out);
cf998323 95 out << "]";
6cae9b63
A
96 }
97 else {
bc3b7c8c
A
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;
cf998323 118 }
6cae9b63
A
119 }
120 if ( indent == 0 )
cf998323 121 out << "\n";
6cae9b63
A
122}
123
bc3b7c8c
A
124static inline void streamArrayBegin(bool &needsComma, std::ostream& out = std::cout)
125{
126 out << "[";
127 needsComma = false;
128}
129
130static 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
140static inline void streamArrayEnd(bool &needsComma, std::ostream& out = std::cout)
141{
142 if (needsComma)
143 out << "\n";
144 out << "]\n";
145}
6cae9b63
A
146
147} // namespace json
148} // namespace dyld3
cf998323
A
149
150
151#endif // __JSON_WRITER_H__