]>
git.saurik.com Git - apple/dyld.git/blob - dyld3/JSONWriter.h
2 * Copyright (c) 2017 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #ifndef __JSON_WRITER_H__
25 #define __JSON_WRITER_H__
34 static inline std::string
hex(uint64_t value
) {
36 sprintf(buff
, "0x%llX", value
);
40 static inline std::string
hex4(uint64_t value
) {
42 sprintf(buff
, "0x%04llX", value
);
46 static inline std::string
hex8(uint64_t value
) {
48 sprintf(buff
, "0x%08llX", value
);
52 static inline std::string
decimal(uint64_t value
) {
54 sprintf(buff
, "%llu", value
);
58 static inline void indentBy(uint32_t spaces
, std::ostream
& out
) {
59 for (uint32_t i
=0; i
< spaces
; ++i
) {
64 static inline void printJSON(const Node
& node
, uint32_t indent
, std::ostream
& out
)
66 if ( !node
.map
.empty() ) {
68 bool needComma
= false;
69 for (const auto& entry
: node
.map
) {
73 indentBy(indent
+2, out
);
74 out
<< "\"" << entry
.first
<< "\": ";
75 printJSON(entry
.second
, indent
+2, out
);
79 indentBy(indent
, out
);
82 else if ( !node
.array
.empty() ) {
84 bool needComma
= false;
85 for (const auto& entry
: node
.array
) {
89 indentBy(indent
+2, out
);
90 printJSON(entry
, indent
+2, out
);
94 indentBy(indent
, out
);
98 std::string escapedString
;
99 escapedString
.reserve(node
.value
.size());
100 for (char c
: node
.value
) {
102 escapedString
+= '\\';
105 out
<< "\"" << escapedString
<< "\"";
116 #endif // __JSON_WRITER_H__