Line data Source code
1 : // Copyright 2019 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/torque/ls/json.h"
6 :
7 : #include <iostream>
8 : #include <sstream>
9 : #include "src/torque/utils.h"
10 :
11 : namespace v8 {
12 : namespace internal {
13 : namespace torque {
14 : namespace ls {
15 :
16 : namespace {
17 :
18 0 : void SerializeToString(std::stringstream& str, const JsonValue& value) {
19 0 : switch (value.tag) {
20 : case JsonValue::NUMBER:
21 0 : str << value.ToNumber();
22 : break;
23 : case JsonValue::STRING:
24 0 : str << StringLiteralQuote(value.ToString());
25 0 : break;
26 : case JsonValue::IS_NULL:
27 0 : str << "null";
28 0 : break;
29 : case JsonValue::BOOL:
30 0 : str << (value.ToBool() ? "true" : "false");
31 0 : break;
32 : case JsonValue::OBJECT: {
33 0 : str << "{";
34 : size_t i = 0;
35 0 : for (const auto& pair : value.ToObject()) {
36 0 : str << "\"" << pair.first << "\":";
37 0 : SerializeToString(str, pair.second);
38 0 : if (++i < value.ToObject().size()) str << ",";
39 : }
40 0 : str << "}";
41 0 : break;
42 : }
43 : case JsonValue::ARRAY: {
44 0 : str << "[";
45 : size_t i = 0;
46 0 : for (const auto& element : value.ToArray()) {
47 0 : SerializeToString(str, element);
48 0 : if (++i < value.ToArray().size()) str << ",";
49 : }
50 0 : str << "]";
51 0 : break;
52 : }
53 : default:
54 : break;
55 : }
56 0 : }
57 :
58 : } // namespace
59 :
60 0 : std::string SerializeToString(const JsonValue& value) {
61 0 : std::stringstream result;
62 0 : SerializeToString(result, value);
63 0 : return result.str();
64 : }
65 :
66 : } // namespace ls
67 : } // namespace torque
68 : } // namespace internal
69 59454 : } // namespace v8
|