chromium/base/json/json_writer.h

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_JSON_JSON_WRITER_H_
#define BASE_JSON_JSON_WRITER_H_

#include <stddef.h>

#include <cstdint>
#include <optional>
#include <string>
#include <string_view>

#include "base/base_export.h"
#include "base/json/json_common.h"
#include "base/memory/raw_ptr.h"
#include "base/values.h"

namespace base {

enum JsonOptions {};

// Given a root node, generates and returns a JSON string.
//
// Returns `std::nullopt` if
//    * the nesting depth exceeds `max_depth`, or
//    * the JSON contains binary values.
BASE_EXPORT std::optional<std::string> WriteJson(
    ValueView node,
    size_t max_depth = internal::kAbsoluteMaxDepth);

// Given a root node, generates and returns a JSON string.
// The string is formatted according to `options` which is a bitmask of
// `JsonOptions`.
//
// Returns `std::nullopt` if
//    * the nesting depth exceeds `max_depth,` or
//    * the JSON contains binary values
//      (unless `JsonOptions::OPTIONS_OMIT_BINARY_VALUES` is passed).
BASE_EXPORT std::optional<std::string> WriteJsonWithOptions(
    ValueView node,
    uint32_t options,
    size_t max_depth = internal::kAbsoluteMaxDepth);

class BASE_EXPORT JSONWriter {};

}  // namespace base

#endif  // BASE_JSON_JSON_WRITER_H_