Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sts=4 et sw=4 tw=99:
3 : * This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef vm_JSONPrinter_h
8 : #define vm_JSONPrinter_h
9 :
10 : #include "mozilla/TimeStamp.h"
11 :
12 : #include <stdio.h>
13 :
14 : #include "js/TypeDecls.h"
15 : #include "vm/Printer.h"
16 :
17 : struct DtoaState;
18 :
19 : namespace js {
20 :
21 : class JSONPrinter
22 : {
23 : protected:
24 : int indentLevel_;
25 : bool first_;
26 : GenericPrinter& out_;
27 : DtoaState* dtoaState_;
28 :
29 : void indent();
30 :
31 : public:
32 179 : explicit JSONPrinter(GenericPrinter& out)
33 179 : : indentLevel_(0),
34 : first_(true),
35 : out_(out),
36 179 : dtoaState_(nullptr)
37 : {
38 179 : }
39 :
40 : ~JSONPrinter();
41 :
42 : void beginObject();
43 : void beginObjectProperty(const char* name);
44 : void beginListProperty(const char* name);
45 :
46 : void value(const char* format, ...) MOZ_FORMAT_PRINTF(2, 3);
47 : void value(int value);
48 :
49 : void property(const char* name, const char* value);
50 : void property(const char* name, int32_t value);
51 : void property(const char* name, uint32_t value);
52 : void property(const char* name, int64_t value);
53 : void property(const char* name, uint64_t value);
54 : #if defined(XP_DARWIN) || defined(__OpenBSD__)
55 : // On OSX and OpenBSD, size_t is long unsigned, uint32_t is unsigned, and
56 : // uint64_t is long long unsigned. Everywhere else, size_t matches either
57 : // uint32_t or uint64_t.
58 : void property(const char* name, size_t value);
59 : #endif
60 :
61 : void formatProperty(const char* name, const char* format, ...) MOZ_FORMAT_PRINTF(3, 4);
62 :
63 : // JSON requires decimals to be separated by periods, but the LC_NUMERIC
64 : // setting may cause printf to use commas in some locales.
65 : enum TimePrecision { SECONDS, MILLISECONDS, MICROSECONDS };
66 : void property(const char* name, const mozilla::TimeDuration& dur, TimePrecision precision);
67 :
68 : void floatProperty(const char* name, double value, size_t precision);
69 :
70 : void beginStringProperty(const char* name);
71 : void endStringProperty();
72 :
73 : void endObject();
74 : void endList();
75 :
76 : // Notify the output that the caller has detected OOM and should transition
77 : // to its saw-OOM state.
78 : void outOfMemory() { out_.reportOutOfMemory(); }
79 :
80 : protected:
81 : void propertyName(const char* name);
82 : };
83 :
84 : } // namespace js
85 :
86 : #endif /* vm_JSONPrinter_h */
|