• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SerializeToString(std::stringstream & str,const JsonValue & value)18 void SerializeToString(std::stringstream& str, const JsonValue& value) {
19   switch (value.tag) {
20     case JsonValue::NUMBER:
21       str << value.ToNumber();
22       break;
23     case JsonValue::STRING:
24       str << StringLiteralQuote(value.ToString());
25       break;
26     case JsonValue::IS_NULL:
27       str << "null";
28       break;
29     case JsonValue::BOOL:
30       str << (value.ToBool() ? "true" : "false");
31       break;
32     case JsonValue::OBJECT: {
33       str << "{";
34       size_t i = 0;
35       for (const auto& pair : value.ToObject()) {
36         str << "\"" << pair.first << "\":";
37         SerializeToString(str, pair.second);
38         if (++i < value.ToObject().size()) str << ",";
39       }
40       str << "}";
41       break;
42     }
43     case JsonValue::ARRAY: {
44       str << "[";
45       size_t i = 0;
46       for (const auto& element : value.ToArray()) {
47         SerializeToString(str, element);
48         if (++i < value.ToArray().size()) str << ",";
49       }
50       str << "]";
51       break;
52     }
53     default:
54       break;
55   }
56 }
57 
58 }  // namespace
59 
SerializeToString(const JsonValue & value)60 std::string SerializeToString(const JsonValue& value) {
61   std::stringstream result;
62   SerializeToString(result, value);
63   return result.str();
64 }
65 
66 }  // namespace ls
67 }  // namespace torque
68 }  // namespace internal
69 }  // namespace v8
70