Repository: carbon-language/carbon-lang
File: testing/file_test/test_file.cpp (C++)

Code before:
                          << "`";
  }
  llvm::StringRef body = body_start.take_front(body_end);
  auto [method_or_id, extra_content] = body.split(":");

  llvm::json::Value parsed_extra_content = nullptr;
  if (!extra_content.empty()) {
    std::string bracketed_extra_content =
        llvm::formatv("{{{0}}", extra_content);
    auto parse_result = llvm::json::parse(bracketed_extra_content);
    if (auto err = parse_result.takeError()) {
      return ErrorBuilder() << "Error parsing extra content: " << err;
    }
    parsed_extra_content = std::move(*parse_result);
    CARBON_CHECK(parsed_extra_content.kind() == llvm::json::Value::Object);
    if (extra_content_label == "params" &&
        method_or_id == "textDocument/didOpen") {
      CARBON_RETURN_IF_ERROR(
          AutoFillDidOpenParams(parsed_extra_content.getAsObject(), splits));
    }
  }

  // Form the JSON.
  std::string buffer;
  llvm::raw_string_ostream buffer_os(buffer);
  llvm::json::OStream json(buffer_os);

  json.object([&] {
    json.attribute("jsonrpc", "2.0");
    json.attribute(method_or_id_label, method_or_id);

    if (use_call_id) {
      json.attribute("id", ++lsp_call_id);
    }
    if (parsed_extra_content != nullptr) {
      if (!extra_content_label.empty()) {
        json.attribute(extra_content_label, parsed_extra_content);
      } else {
        for (const auto& [key, value] : *parsed_extra_content.getAsObject()) {
          json.attribute(key, value);
        }
      }
    }
  });

  // Add the Content-Length header. The `2` accounts for extra newlines.
  auto json_with_header =
      llvm::formatv("Content-Length: {0}\n\n{1}\n", buffer.size() + 2, buffer)
          .str();
  int keyword_len =
      (body_start.data() + body_end + LspEnd.size()) - keyword.data();

Review comment (refactor):
We have a utility class for this, so maybe:

```suggestion
  RawStringOstream buffer;
  llvm::json::OStream json(buffer);
```

Code after:
  if (body_end == std::string::npos) {
    return ErrorBuilder() << "Missing `" << LspEnd << "` after `" << keyword
                          << "`";
  }
  llvm::StringRef body = body_start.take_front(body_end);
  auto [method_or_id, extra_content] = body.split(":");

  llvm::json::Value parsed_extra_content = nullptr;
  if (!extra_content.empty()) {
    std::string extra_content_as_object =
        llvm::formatv("{{{0}}", extra_content);
    auto parse_result = llvm::json::parse(extra_content_as_object);
    if (auto err = parse_result.takeError()) {
      return ErrorBuilder() << "Error parsing extra content: " << err;
    }
    parsed_extra_content = std::move(*parse_result);
    CARBON_CHECK(parsed_extra_content.kind() == llvm::json::Value::Object);
    if (extra_content_label == "params" &&
        method_or_id == "textDocument/didOpen") {
      CARBON_RETURN_IF_ERROR(
          AutoFillDidOpenParams(parsed_extra_content.getAsObject(), splits));
    }
  }

  // Form the JSON.
  RawStringOstream buffer;
  llvm::json::OStream json(buffer);

  json.object([&] {
    json.attribute("jsonrpc", "2.0");
    json.attribute(method_or_id_label, method_or_id);

    if (use_call_id) {
      json.attribute("id", ++lsp_call_id);
    }
    if (parsed_extra_content != nullptr) {
      if (!extra_content_label.empty()) {
        json.attribute(extra_content_label, parsed_extra_content);
      } else {
        for (const auto& [key, value] : *parsed_extra_content.getAsObject()) {
          json.attribute(key, value);
        }
      }
    }
  });

  // Add the Content-Length header. The `2` accounts for extra newlines.
  int content_length = buffer.size() + 2;
  auto json_with_header = llvm::formatv("Content-Length: {0}\n\n{1}\n",
                                        content_length, buffer.TakeStr())
                              .str();