11cb0ef41Sopenharmony_ci// This file is generated by Values_cpp.template.
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Copyright 2016 The Chromium Authors. All rights reserved.
41cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
51cb0ef41Sopenharmony_ci// found in the LICENSE file.
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci//#include "Values.h"
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci{% if config.encoding_lib.header %}
101cb0ef41Sopenharmony_ci#include "{{config.encoding_lib.header}}"
111cb0ef41Sopenharmony_ci{% endif %}
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci{% for namespace in config.protocol.namespace %}
141cb0ef41Sopenharmony_cinamespace {{namespace}} {
151cb0ef41Sopenharmony_ci{% endfor %}
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cinamespace {
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciconst char* const nullValueString = "null";
201cb0ef41Sopenharmony_ciconst char* const trueValueString = "true";
211cb0ef41Sopenharmony_ciconst char* const falseValueString = "false";
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciinline bool escapeChar(uint16_t c, StringBuilder* dst)
241cb0ef41Sopenharmony_ci{
251cb0ef41Sopenharmony_ci    switch (c) {
261cb0ef41Sopenharmony_ci    case '\b': StringUtil::builderAppend(*dst, "\\b"); break;
271cb0ef41Sopenharmony_ci    case '\f': StringUtil::builderAppend(*dst, "\\f"); break;
281cb0ef41Sopenharmony_ci    case '\n': StringUtil::builderAppend(*dst, "\\n"); break;
291cb0ef41Sopenharmony_ci    case '\r': StringUtil::builderAppend(*dst, "\\r"); break;
301cb0ef41Sopenharmony_ci    case '\t': StringUtil::builderAppend(*dst, "\\t"); break;
311cb0ef41Sopenharmony_ci    case '\\': StringUtil::builderAppend(*dst, "\\\\"); break;
321cb0ef41Sopenharmony_ci    case '"': StringUtil::builderAppend(*dst, "\\\""); break;
331cb0ef41Sopenharmony_ci    default:
341cb0ef41Sopenharmony_ci        return false;
351cb0ef41Sopenharmony_ci    }
361cb0ef41Sopenharmony_ci    return true;
371cb0ef41Sopenharmony_ci}
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ciconst char hexDigits[17] = "0123456789ABCDEF";
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_civoid appendUnsignedAsHex(uint16_t number, StringBuilder* dst)
421cb0ef41Sopenharmony_ci{
431cb0ef41Sopenharmony_ci    StringUtil::builderAppend(*dst, "\\u");
441cb0ef41Sopenharmony_ci    for (size_t i = 0; i < 4; ++i) {
451cb0ef41Sopenharmony_ci        uint16_t c = hexDigits[(number & 0xF000) >> 12];
461cb0ef41Sopenharmony_ci        StringUtil::builderAppend(*dst, c);
471cb0ef41Sopenharmony_ci        number <<= 4;
481cb0ef41Sopenharmony_ci    }
491cb0ef41Sopenharmony_ci}
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_citemplate <typename Char>
521cb0ef41Sopenharmony_civoid escapeStringForJSONInternal(const Char* str, unsigned len,
531cb0ef41Sopenharmony_ci                                 StringBuilder* dst)
541cb0ef41Sopenharmony_ci{
551cb0ef41Sopenharmony_ci    for (unsigned i = 0; i < len; ++i) {
561cb0ef41Sopenharmony_ci        Char c = str[i];
571cb0ef41Sopenharmony_ci        if (escapeChar(c, dst))
581cb0ef41Sopenharmony_ci            continue;
591cb0ef41Sopenharmony_ci        if (c < 32 || c > 126) {
601cb0ef41Sopenharmony_ci            appendUnsignedAsHex(c, dst);
611cb0ef41Sopenharmony_ci        } else {
621cb0ef41Sopenharmony_ci            StringUtil::builderAppend(*dst, c);
631cb0ef41Sopenharmony_ci        }
641cb0ef41Sopenharmony_ci    }
651cb0ef41Sopenharmony_ci}
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci// When parsing CBOR, we limit recursion depth for objects and arrays
681cb0ef41Sopenharmony_ci// to this constant.
691cb0ef41Sopenharmony_cistatic constexpr int kStackLimitValues = 1000;
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci{% if config.encoding_lib.namespace %}
721cb0ef41Sopenharmony_ciusing {{"::".join(config.encoding_lib.namespace)}}::Error;
731cb0ef41Sopenharmony_ciusing {{"::".join(config.encoding_lib.namespace)}}::Status;
741cb0ef41Sopenharmony_ciusing {{"::".join(config.encoding_lib.namespace)}}::span;
751cb0ef41Sopenharmony_cinamespace cbor {
761cb0ef41Sopenharmony_ciusing {{"::".join(config.encoding_lib.namespace + ['cbor'])}}::CBORTokenTag;
771cb0ef41Sopenharmony_ciusing {{"::".join(config.encoding_lib.namespace + ['cbor'])}}::CBORTokenizer;
781cb0ef41Sopenharmony_ciusing {{"::".join(config.encoding_lib.namespace + ['cbor'])}}::EncodeBinary;
791cb0ef41Sopenharmony_ciusing {{"::".join(config.encoding_lib.namespace + ['cbor'])}}::EncodeDouble;
801cb0ef41Sopenharmony_ciusing {{"::".join(config.encoding_lib.namespace + ['cbor'])}}::EncodeFalse;
811cb0ef41Sopenharmony_ciusing {{"::".join(config.encoding_lib.namespace + ['cbor'])}}::EncodeFromLatin1;
821cb0ef41Sopenharmony_ciusing {{"::".join(config.encoding_lib.namespace + ['cbor'])}}::EncodeFromUTF16;
831cb0ef41Sopenharmony_ciusing {{"::".join(config.encoding_lib.namespace + ['cbor'])}}::EncodeIndefiniteLengthArrayStart;
841cb0ef41Sopenharmony_ciusing {{"::".join(config.encoding_lib.namespace + ['cbor'])}}::EncodeIndefiniteLengthMapStart;
851cb0ef41Sopenharmony_ciusing {{"::".join(config.encoding_lib.namespace + ['cbor'])}}::EncodeInt32;
861cb0ef41Sopenharmony_ciusing {{"::".join(config.encoding_lib.namespace + ['cbor'])}}::EncodeNull;
871cb0ef41Sopenharmony_ciusing {{"::".join(config.encoding_lib.namespace + ['cbor'])}}::EncodeStop;
881cb0ef41Sopenharmony_ciusing {{"::".join(config.encoding_lib.namespace + ['cbor'])}}::EncodeString8;
891cb0ef41Sopenharmony_ciusing {{"::".join(config.encoding_lib.namespace + ['cbor'])}}::EncodeTrue;
901cb0ef41Sopenharmony_ciusing {{"::".join(config.encoding_lib.namespace + ['cbor'])}}::EnvelopeEncoder;
911cb0ef41Sopenharmony_ciusing {{"::".join(config.encoding_lib.namespace + ['cbor'])}}::InitialByteForEnvelope;
921cb0ef41Sopenharmony_ci}  // namespace cbor
931cb0ef41Sopenharmony_ci{% endif %}
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci// Below are three parsing routines for CBOR, which cover enough
961cb0ef41Sopenharmony_ci// to roundtrip JSON messages.
971cb0ef41Sopenharmony_cistd::unique_ptr<DictionaryValue> parseMap(int32_t stack_depth, cbor::CBORTokenizer* tokenizer);
981cb0ef41Sopenharmony_cistd::unique_ptr<ListValue> parseArray(int32_t stack_depth, cbor::CBORTokenizer* tokenizer);
991cb0ef41Sopenharmony_cistd::unique_ptr<Value> parseValue(int32_t stack_depth, cbor::CBORTokenizer* tokenizer);
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci// |bytes| must start with the indefinite length array byte, so basically,
1021cb0ef41Sopenharmony_ci// ParseArray may only be called after an indefinite length array has been
1031cb0ef41Sopenharmony_ci// detected.
1041cb0ef41Sopenharmony_cistd::unique_ptr<ListValue> parseArray(int32_t stack_depth, cbor::CBORTokenizer* tokenizer) {
1051cb0ef41Sopenharmony_ci  DCHECK(tokenizer->TokenTag() == cbor::CBORTokenTag::ARRAY_START);
1061cb0ef41Sopenharmony_ci  tokenizer->Next();
1071cb0ef41Sopenharmony_ci  auto list = ListValue::create();
1081cb0ef41Sopenharmony_ci  while (tokenizer->TokenTag() != cbor::CBORTokenTag::STOP) {
1091cb0ef41Sopenharmony_ci    // Error::CBOR_UNEXPECTED_EOF_IN_ARRAY
1101cb0ef41Sopenharmony_ci    if (tokenizer->TokenTag() == cbor::CBORTokenTag::DONE) return nullptr;
1111cb0ef41Sopenharmony_ci    if (tokenizer->TokenTag() == cbor::CBORTokenTag::ERROR_VALUE) return nullptr;
1121cb0ef41Sopenharmony_ci    // Parse value.
1131cb0ef41Sopenharmony_ci    auto value = parseValue(stack_depth, tokenizer);
1141cb0ef41Sopenharmony_ci    if (!value) return nullptr;
1151cb0ef41Sopenharmony_ci    list->pushValue(std::move(value));
1161cb0ef41Sopenharmony_ci  }
1171cb0ef41Sopenharmony_ci  tokenizer->Next();
1181cb0ef41Sopenharmony_ci  return list;
1191cb0ef41Sopenharmony_ci}
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_cistd::unique_ptr<Value> parseValue(
1221cb0ef41Sopenharmony_ci    int32_t stack_depth, cbor::CBORTokenizer* tokenizer) {
1231cb0ef41Sopenharmony_ci  // Error::CBOR_STACK_LIMIT_EXCEEDED
1241cb0ef41Sopenharmony_ci  if (stack_depth > kStackLimitValues) return nullptr;
1251cb0ef41Sopenharmony_ci  // Skip past the envelope to get to what's inside.
1261cb0ef41Sopenharmony_ci  if (tokenizer->TokenTag() == cbor::CBORTokenTag::ENVELOPE)
1271cb0ef41Sopenharmony_ci    tokenizer->EnterEnvelope();
1281cb0ef41Sopenharmony_ci  switch (tokenizer->TokenTag()) {
1291cb0ef41Sopenharmony_ci    case cbor::CBORTokenTag::ERROR_VALUE:
1301cb0ef41Sopenharmony_ci      return nullptr;
1311cb0ef41Sopenharmony_ci    case cbor::CBORTokenTag::DONE:
1321cb0ef41Sopenharmony_ci      // Error::CBOR_UNEXPECTED_EOF_EXPECTED_VALUE
1331cb0ef41Sopenharmony_ci      return nullptr;
1341cb0ef41Sopenharmony_ci    case cbor::CBORTokenTag::TRUE_VALUE: {
1351cb0ef41Sopenharmony_ci      std::unique_ptr<Value> value = FundamentalValue::create(true);
1361cb0ef41Sopenharmony_ci      tokenizer->Next();
1371cb0ef41Sopenharmony_ci      return value;
1381cb0ef41Sopenharmony_ci    }
1391cb0ef41Sopenharmony_ci    case cbor::CBORTokenTag::FALSE_VALUE: {
1401cb0ef41Sopenharmony_ci      std::unique_ptr<Value> value = FundamentalValue::create(false);
1411cb0ef41Sopenharmony_ci      tokenizer->Next();
1421cb0ef41Sopenharmony_ci      return value;
1431cb0ef41Sopenharmony_ci    }
1441cb0ef41Sopenharmony_ci    case cbor::CBORTokenTag::NULL_VALUE: {
1451cb0ef41Sopenharmony_ci      std::unique_ptr<Value> value = FundamentalValue::null();
1461cb0ef41Sopenharmony_ci      tokenizer->Next();
1471cb0ef41Sopenharmony_ci      return value;
1481cb0ef41Sopenharmony_ci    }
1491cb0ef41Sopenharmony_ci    case cbor::CBORTokenTag::INT32: {
1501cb0ef41Sopenharmony_ci      std::unique_ptr<Value> value = FundamentalValue::create(tokenizer->GetInt32());
1511cb0ef41Sopenharmony_ci      tokenizer->Next();
1521cb0ef41Sopenharmony_ci      return value;
1531cb0ef41Sopenharmony_ci    }
1541cb0ef41Sopenharmony_ci    case cbor::CBORTokenTag::DOUBLE: {
1551cb0ef41Sopenharmony_ci      std::unique_ptr<Value> value = FundamentalValue::create(tokenizer->GetDouble());
1561cb0ef41Sopenharmony_ci      tokenizer->Next();
1571cb0ef41Sopenharmony_ci      return value;
1581cb0ef41Sopenharmony_ci    }
1591cb0ef41Sopenharmony_ci    case cbor::CBORTokenTag::STRING8: {
1601cb0ef41Sopenharmony_ci      span<uint8_t> str = tokenizer->GetString8();
1611cb0ef41Sopenharmony_ci      std::unique_ptr<Value> value =
1621cb0ef41Sopenharmony_ci          StringValue::create(StringUtil::fromUTF8(str.data(), str.size()));
1631cb0ef41Sopenharmony_ci      tokenizer->Next();
1641cb0ef41Sopenharmony_ci      return value;
1651cb0ef41Sopenharmony_ci    }
1661cb0ef41Sopenharmony_ci    case cbor::CBORTokenTag::STRING16: {
1671cb0ef41Sopenharmony_ci      span<uint8_t> wire = tokenizer->GetString16WireRep();
1681cb0ef41Sopenharmony_ci      DCHECK_EQ(wire.size() & 1, 0u);
1691cb0ef41Sopenharmony_ci      std::unique_ptr<Value> value = StringValue::create(StringUtil::fromUTF16(
1701cb0ef41Sopenharmony_ci          reinterpret_cast<const uint16_t*>(wire.data()), wire.size() / 2));
1711cb0ef41Sopenharmony_ci      tokenizer->Next();
1721cb0ef41Sopenharmony_ci      return value;
1731cb0ef41Sopenharmony_ci    }
1741cb0ef41Sopenharmony_ci    case cbor::CBORTokenTag::BINARY: {
1751cb0ef41Sopenharmony_ci      span<uint8_t> payload = tokenizer->GetBinary();
1761cb0ef41Sopenharmony_ci      tokenizer->Next();
1771cb0ef41Sopenharmony_ci      return BinaryValue::create(Binary::fromSpan(payload.data(), payload.size()));
1781cb0ef41Sopenharmony_ci    }
1791cb0ef41Sopenharmony_ci    case cbor::CBORTokenTag::MAP_START:
1801cb0ef41Sopenharmony_ci      return parseMap(stack_depth + 1, tokenizer);
1811cb0ef41Sopenharmony_ci    case cbor::CBORTokenTag::ARRAY_START:
1821cb0ef41Sopenharmony_ci      return parseArray(stack_depth + 1, tokenizer);
1831cb0ef41Sopenharmony_ci    default:
1841cb0ef41Sopenharmony_ci      // Error::CBOR_UNSUPPORTED_VALUE
1851cb0ef41Sopenharmony_ci      return nullptr;
1861cb0ef41Sopenharmony_ci  }
1871cb0ef41Sopenharmony_ci}
1881cb0ef41Sopenharmony_ci
1891cb0ef41Sopenharmony_ci// |bytes| must start with the indefinite length array byte, so basically,
1901cb0ef41Sopenharmony_ci// ParseArray may only be called after an indefinite length array has been
1911cb0ef41Sopenharmony_ci// detected.
1921cb0ef41Sopenharmony_cistd::unique_ptr<DictionaryValue> parseMap(
1931cb0ef41Sopenharmony_ci    int32_t stack_depth, cbor::CBORTokenizer* tokenizer) {
1941cb0ef41Sopenharmony_ci  auto dict = DictionaryValue::create();
1951cb0ef41Sopenharmony_ci  tokenizer->Next();
1961cb0ef41Sopenharmony_ci  while (tokenizer->TokenTag() != cbor::CBORTokenTag::STOP) {
1971cb0ef41Sopenharmony_ci    if (tokenizer->TokenTag() == cbor::CBORTokenTag::DONE) {
1981cb0ef41Sopenharmony_ci      // Error::CBOR_UNEXPECTED_EOF_IN_MAP
1991cb0ef41Sopenharmony_ci      return nullptr;
2001cb0ef41Sopenharmony_ci    }
2011cb0ef41Sopenharmony_ci    if (tokenizer->TokenTag() == cbor::CBORTokenTag::ERROR_VALUE) return nullptr;
2021cb0ef41Sopenharmony_ci    // Parse key.
2031cb0ef41Sopenharmony_ci    String key;
2041cb0ef41Sopenharmony_ci    if (tokenizer->TokenTag() == cbor::CBORTokenTag::STRING8) {
2051cb0ef41Sopenharmony_ci      span<uint8_t> key_span = tokenizer->GetString8();
2061cb0ef41Sopenharmony_ci      key = StringUtil::fromUTF8(key_span.data(), key_span.size());
2071cb0ef41Sopenharmony_ci      tokenizer->Next();
2081cb0ef41Sopenharmony_ci    } else if (tokenizer->TokenTag() == cbor::CBORTokenTag::STRING16) {
2091cb0ef41Sopenharmony_ci      span<uint8_t> key_span = tokenizer->GetString16WireRep();
2101cb0ef41Sopenharmony_ci      if (key_span.size() & 1) return nullptr;  // UTF16 is 2 byte multiple.
2111cb0ef41Sopenharmony_ci      key = StringUtil::fromUTF16(
2121cb0ef41Sopenharmony_ci          reinterpret_cast<const uint16_t*>(key_span.data()),
2131cb0ef41Sopenharmony_ci          key_span.size() / 2);
2141cb0ef41Sopenharmony_ci      tokenizer->Next();
2151cb0ef41Sopenharmony_ci    } else {
2161cb0ef41Sopenharmony_ci      // Error::CBOR_INVALID_MAP_KEY
2171cb0ef41Sopenharmony_ci      return nullptr;
2181cb0ef41Sopenharmony_ci    }
2191cb0ef41Sopenharmony_ci    // Parse value.
2201cb0ef41Sopenharmony_ci    auto value = parseValue(stack_depth, tokenizer);
2211cb0ef41Sopenharmony_ci    if (!value) return nullptr;
2221cb0ef41Sopenharmony_ci    dict->setValue(key, std::move(value));
2231cb0ef41Sopenharmony_ci  }
2241cb0ef41Sopenharmony_ci  tokenizer->Next();
2251cb0ef41Sopenharmony_ci  return dict;
2261cb0ef41Sopenharmony_ci}
2271cb0ef41Sopenharmony_ci
2281cb0ef41Sopenharmony_ci} // anonymous namespace
2291cb0ef41Sopenharmony_ci
2301cb0ef41Sopenharmony_ci// static
2311cb0ef41Sopenharmony_cistd::unique_ptr<Value> Value::parseBinary(const uint8_t* data, size_t size) {
2321cb0ef41Sopenharmony_ci  span<uint8_t> bytes(data, size);
2331cb0ef41Sopenharmony_ci
2341cb0ef41Sopenharmony_ci  // Error::CBOR_NO_INPUT
2351cb0ef41Sopenharmony_ci  if (bytes.empty()) return nullptr;
2361cb0ef41Sopenharmony_ci
2371cb0ef41Sopenharmony_ci  // Error::CBOR_INVALID_START_BYTE
2381cb0ef41Sopenharmony_ci  if (bytes[0] != cbor::InitialByteForEnvelope()) return nullptr;
2391cb0ef41Sopenharmony_ci
2401cb0ef41Sopenharmony_ci  cbor::CBORTokenizer tokenizer(bytes);
2411cb0ef41Sopenharmony_ci  if (tokenizer.TokenTag() == cbor::CBORTokenTag::ERROR_VALUE) return nullptr;
2421cb0ef41Sopenharmony_ci
2431cb0ef41Sopenharmony_ci  // We checked for the envelope start byte above, so the tokenizer
2441cb0ef41Sopenharmony_ci  // must agree here, since it's not an error.
2451cb0ef41Sopenharmony_ci  DCHECK(tokenizer.TokenTag() == cbor::CBORTokenTag::ENVELOPE);
2461cb0ef41Sopenharmony_ci  tokenizer.EnterEnvelope();
2471cb0ef41Sopenharmony_ci  // Error::MAP_START_EXPECTED
2481cb0ef41Sopenharmony_ci  if (tokenizer.TokenTag() != cbor::CBORTokenTag::MAP_START) return nullptr;
2491cb0ef41Sopenharmony_ci  std::unique_ptr<Value> result = parseMap(/*stack_depth=*/1, &tokenizer);
2501cb0ef41Sopenharmony_ci  if (!result) return nullptr;
2511cb0ef41Sopenharmony_ci  if (tokenizer.TokenTag() == cbor::CBORTokenTag::DONE) return result;
2521cb0ef41Sopenharmony_ci  if (tokenizer.TokenTag() == cbor::CBORTokenTag::ERROR_VALUE) return nullptr;
2531cb0ef41Sopenharmony_ci  // Error::CBOR_TRAILING_JUNK
2541cb0ef41Sopenharmony_ci  return nullptr;
2551cb0ef41Sopenharmony_ci}
2561cb0ef41Sopenharmony_ci
2571cb0ef41Sopenharmony_cibool Value::asBoolean(bool*) const
2581cb0ef41Sopenharmony_ci{
2591cb0ef41Sopenharmony_ci    return false;
2601cb0ef41Sopenharmony_ci}
2611cb0ef41Sopenharmony_ci
2621cb0ef41Sopenharmony_cibool Value::asDouble(double*) const
2631cb0ef41Sopenharmony_ci{
2641cb0ef41Sopenharmony_ci    return false;
2651cb0ef41Sopenharmony_ci}
2661cb0ef41Sopenharmony_ci
2671cb0ef41Sopenharmony_cibool Value::asInteger(int*) const
2681cb0ef41Sopenharmony_ci{
2691cb0ef41Sopenharmony_ci    return false;
2701cb0ef41Sopenharmony_ci}
2711cb0ef41Sopenharmony_ci
2721cb0ef41Sopenharmony_cibool Value::asString(String*) const
2731cb0ef41Sopenharmony_ci{
2741cb0ef41Sopenharmony_ci    return false;
2751cb0ef41Sopenharmony_ci}
2761cb0ef41Sopenharmony_ci
2771cb0ef41Sopenharmony_cibool Value::asBinary(Binary*) const
2781cb0ef41Sopenharmony_ci{
2791cb0ef41Sopenharmony_ci    return false;
2801cb0ef41Sopenharmony_ci}
2811cb0ef41Sopenharmony_ci
2821cb0ef41Sopenharmony_civoid Value::writeJSON(StringBuilder* output) const
2831cb0ef41Sopenharmony_ci{
2841cb0ef41Sopenharmony_ci    DCHECK(m_type == TypeNull);
2851cb0ef41Sopenharmony_ci    StringUtil::builderAppend(*output, nullValueString, 4);
2861cb0ef41Sopenharmony_ci}
2871cb0ef41Sopenharmony_ci
2881cb0ef41Sopenharmony_civoid Value::writeBinary(std::vector<uint8_t>* bytes) const {
2891cb0ef41Sopenharmony_ci    DCHECK(m_type == TypeNull);
2901cb0ef41Sopenharmony_ci    bytes->push_back(cbor::EncodeNull());
2911cb0ef41Sopenharmony_ci}
2921cb0ef41Sopenharmony_ci
2931cb0ef41Sopenharmony_cistd::unique_ptr<Value> Value::clone() const
2941cb0ef41Sopenharmony_ci{
2951cb0ef41Sopenharmony_ci    return Value::null();
2961cb0ef41Sopenharmony_ci}
2971cb0ef41Sopenharmony_ci
2981cb0ef41Sopenharmony_ciString Value::toJSONString() const
2991cb0ef41Sopenharmony_ci{
3001cb0ef41Sopenharmony_ci    StringBuilder result;
3011cb0ef41Sopenharmony_ci    StringUtil::builderReserve(result, 512);
3021cb0ef41Sopenharmony_ci    writeJSON(&result);
3031cb0ef41Sopenharmony_ci    return StringUtil::builderToString(result);
3041cb0ef41Sopenharmony_ci}
3051cb0ef41Sopenharmony_ci
3061cb0ef41Sopenharmony_ciString Value::serializeToJSON() {
3071cb0ef41Sopenharmony_ci    return toJSONString();
3081cb0ef41Sopenharmony_ci}
3091cb0ef41Sopenharmony_ci
3101cb0ef41Sopenharmony_cistd::vector<uint8_t> Value::serializeToBinary() {
3111cb0ef41Sopenharmony_ci    std::vector<uint8_t> bytes;
3121cb0ef41Sopenharmony_ci    writeBinary(&bytes);
3131cb0ef41Sopenharmony_ci    return bytes;
3141cb0ef41Sopenharmony_ci}
3151cb0ef41Sopenharmony_ci
3161cb0ef41Sopenharmony_cibool FundamentalValue::asBoolean(bool* output) const
3171cb0ef41Sopenharmony_ci{
3181cb0ef41Sopenharmony_ci    if (type() != TypeBoolean)
3191cb0ef41Sopenharmony_ci        return false;
3201cb0ef41Sopenharmony_ci    *output = m_boolValue;
3211cb0ef41Sopenharmony_ci    return true;
3221cb0ef41Sopenharmony_ci}
3231cb0ef41Sopenharmony_ci
3241cb0ef41Sopenharmony_cibool FundamentalValue::asDouble(double* output) const
3251cb0ef41Sopenharmony_ci{
3261cb0ef41Sopenharmony_ci    if (type() == TypeDouble) {
3271cb0ef41Sopenharmony_ci        *output = m_doubleValue;
3281cb0ef41Sopenharmony_ci        return true;
3291cb0ef41Sopenharmony_ci    }
3301cb0ef41Sopenharmony_ci    if (type() == TypeInteger) {
3311cb0ef41Sopenharmony_ci        *output = m_integerValue;
3321cb0ef41Sopenharmony_ci        return true;
3331cb0ef41Sopenharmony_ci    }
3341cb0ef41Sopenharmony_ci    return false;
3351cb0ef41Sopenharmony_ci}
3361cb0ef41Sopenharmony_ci
3371cb0ef41Sopenharmony_cibool FundamentalValue::asInteger(int* output) const
3381cb0ef41Sopenharmony_ci{
3391cb0ef41Sopenharmony_ci    if (type() != TypeInteger)
3401cb0ef41Sopenharmony_ci        return false;
3411cb0ef41Sopenharmony_ci    *output = m_integerValue;
3421cb0ef41Sopenharmony_ci    return true;
3431cb0ef41Sopenharmony_ci}
3441cb0ef41Sopenharmony_ci
3451cb0ef41Sopenharmony_civoid FundamentalValue::writeJSON(StringBuilder* output) const
3461cb0ef41Sopenharmony_ci{
3471cb0ef41Sopenharmony_ci    DCHECK(type() == TypeBoolean || type() == TypeInteger || type() == TypeDouble);
3481cb0ef41Sopenharmony_ci    if (type() == TypeBoolean) {
3491cb0ef41Sopenharmony_ci        if (m_boolValue)
3501cb0ef41Sopenharmony_ci            StringUtil::builderAppend(*output, trueValueString, 4);
3511cb0ef41Sopenharmony_ci        else
3521cb0ef41Sopenharmony_ci            StringUtil::builderAppend(*output, falseValueString, 5);
3531cb0ef41Sopenharmony_ci    } else if (type() == TypeDouble) {
3541cb0ef41Sopenharmony_ci        if (!std::isfinite(m_doubleValue)) {
3551cb0ef41Sopenharmony_ci            StringUtil::builderAppend(*output, nullValueString, 4);
3561cb0ef41Sopenharmony_ci            return;
3571cb0ef41Sopenharmony_ci        }
3581cb0ef41Sopenharmony_ci        StringUtil::builderAppend(*output, StringUtil::fromDouble(m_doubleValue));
3591cb0ef41Sopenharmony_ci    } else if (type() == TypeInteger) {
3601cb0ef41Sopenharmony_ci        StringUtil::builderAppend(*output, StringUtil::fromInteger(m_integerValue));
3611cb0ef41Sopenharmony_ci    }
3621cb0ef41Sopenharmony_ci}
3631cb0ef41Sopenharmony_ci
3641cb0ef41Sopenharmony_civoid FundamentalValue::writeBinary(std::vector<uint8_t>* bytes) const {
3651cb0ef41Sopenharmony_ci    switch (type()) {
3661cb0ef41Sopenharmony_ci    case TypeDouble:
3671cb0ef41Sopenharmony_ci        cbor::EncodeDouble(m_doubleValue, bytes);
3681cb0ef41Sopenharmony_ci        return;
3691cb0ef41Sopenharmony_ci    case TypeInteger:
3701cb0ef41Sopenharmony_ci        cbor::EncodeInt32(m_integerValue, bytes);
3711cb0ef41Sopenharmony_ci        return;
3721cb0ef41Sopenharmony_ci    case TypeBoolean:
3731cb0ef41Sopenharmony_ci        bytes->push_back(m_boolValue ? cbor::EncodeTrue() : cbor::EncodeFalse());
3741cb0ef41Sopenharmony_ci        return;
3751cb0ef41Sopenharmony_ci    default:
3761cb0ef41Sopenharmony_ci        DCHECK(false);
3771cb0ef41Sopenharmony_ci    }
3781cb0ef41Sopenharmony_ci}
3791cb0ef41Sopenharmony_ci
3801cb0ef41Sopenharmony_cistd::unique_ptr<Value> FundamentalValue::clone() const
3811cb0ef41Sopenharmony_ci{
3821cb0ef41Sopenharmony_ci    switch (type()) {
3831cb0ef41Sopenharmony_ci    case TypeDouble: return FundamentalValue::create(m_doubleValue);
3841cb0ef41Sopenharmony_ci    case TypeInteger: return FundamentalValue::create(m_integerValue);
3851cb0ef41Sopenharmony_ci    case TypeBoolean: return FundamentalValue::create(m_boolValue);
3861cb0ef41Sopenharmony_ci    default:
3871cb0ef41Sopenharmony_ci        DCHECK(false);
3881cb0ef41Sopenharmony_ci    }
3891cb0ef41Sopenharmony_ci    return nullptr;
3901cb0ef41Sopenharmony_ci}
3911cb0ef41Sopenharmony_ci
3921cb0ef41Sopenharmony_cibool StringValue::asString(String* output) const
3931cb0ef41Sopenharmony_ci{
3941cb0ef41Sopenharmony_ci    *output = m_stringValue;
3951cb0ef41Sopenharmony_ci    return true;
3961cb0ef41Sopenharmony_ci}
3971cb0ef41Sopenharmony_ci
3981cb0ef41Sopenharmony_civoid StringValue::writeJSON(StringBuilder* output) const
3991cb0ef41Sopenharmony_ci{
4001cb0ef41Sopenharmony_ci    DCHECK(type() == TypeString);
4011cb0ef41Sopenharmony_ci    StringUtil::builderAppendQuotedString(*output, m_stringValue);
4021cb0ef41Sopenharmony_ci}
4031cb0ef41Sopenharmony_ci
4041cb0ef41Sopenharmony_cinamespace {
4051cb0ef41Sopenharmony_ci// This routine distinguishes between the current encoding for a given
4061cb0ef41Sopenharmony_ci// string |s|, and calls encoding routines that will
4071cb0ef41Sopenharmony_ci// - Ensure that all ASCII strings end up being encoded as UTF8 in
4081cb0ef41Sopenharmony_ci//   the wire format - e.g., EncodeFromUTF16 will detect ASCII and
4091cb0ef41Sopenharmony_ci//   do the (trivial) transcode to STRING8 on the wire, but if it's
4101cb0ef41Sopenharmony_ci//   not ASCII it'll do STRING16.
4111cb0ef41Sopenharmony_ci// - Select a format that's cheap to convert to. E.g., we don't
4121cb0ef41Sopenharmony_ci//   have LATIN1 on the wire, so we call EncodeFromLatin1 which
4131cb0ef41Sopenharmony_ci//   transcodes to UTF8 if needed.
4141cb0ef41Sopenharmony_civoid EncodeString(const String& s, std::vector<uint8_t>* out) {
4151cb0ef41Sopenharmony_ci  if (StringUtil::CharacterCount(s) == 0) {
4161cb0ef41Sopenharmony_ci    cbor::EncodeString8(span<uint8_t>(nullptr, 0), out);  // Empty string.
4171cb0ef41Sopenharmony_ci  } else if (StringUtil::CharactersLatin1(s)) {
4181cb0ef41Sopenharmony_ci    cbor::EncodeFromLatin1(span<uint8_t>(StringUtil::CharactersLatin1(s),
4191cb0ef41Sopenharmony_ci		                         StringUtil::CharacterCount(s)),
4201cb0ef41Sopenharmony_ci                           out);
4211cb0ef41Sopenharmony_ci  } else if (StringUtil::CharactersUTF16(s)) {
4221cb0ef41Sopenharmony_ci    cbor::EncodeFromUTF16(span<uint16_t>(StringUtil::CharactersUTF16(s),
4231cb0ef41Sopenharmony_ci                                         StringUtil::CharacterCount(s)),
4241cb0ef41Sopenharmony_ci                          out);
4251cb0ef41Sopenharmony_ci  } else if (StringUtil::CharactersUTF8(s)) {
4261cb0ef41Sopenharmony_ci    cbor::EncodeString8(span<uint8_t>(StringUtil::CharactersUTF8(s),
4271cb0ef41Sopenharmony_ci                                      StringUtil::CharacterCount(s)),
4281cb0ef41Sopenharmony_ci                        out);
4291cb0ef41Sopenharmony_ci  }
4301cb0ef41Sopenharmony_ci}
4311cb0ef41Sopenharmony_ci}  // namespace
4321cb0ef41Sopenharmony_ci
4331cb0ef41Sopenharmony_civoid StringValue::writeBinary(std::vector<uint8_t>* bytes) const {
4341cb0ef41Sopenharmony_ci  EncodeString(m_stringValue, bytes);
4351cb0ef41Sopenharmony_ci}
4361cb0ef41Sopenharmony_ci
4371cb0ef41Sopenharmony_cistd::unique_ptr<Value> StringValue::clone() const
4381cb0ef41Sopenharmony_ci{
4391cb0ef41Sopenharmony_ci    return StringValue::create(m_stringValue);
4401cb0ef41Sopenharmony_ci}
4411cb0ef41Sopenharmony_ci
4421cb0ef41Sopenharmony_cibool BinaryValue::asBinary(Binary* output) const
4431cb0ef41Sopenharmony_ci{
4441cb0ef41Sopenharmony_ci    *output = m_binaryValue;
4451cb0ef41Sopenharmony_ci    return true;
4461cb0ef41Sopenharmony_ci}
4471cb0ef41Sopenharmony_ci
4481cb0ef41Sopenharmony_civoid BinaryValue::writeJSON(StringBuilder* output) const
4491cb0ef41Sopenharmony_ci{
4501cb0ef41Sopenharmony_ci    DCHECK(type() == TypeBinary);
4511cb0ef41Sopenharmony_ci    StringUtil::builderAppendQuotedString(*output, m_binaryValue.toBase64());
4521cb0ef41Sopenharmony_ci}
4531cb0ef41Sopenharmony_ci
4541cb0ef41Sopenharmony_civoid BinaryValue::writeBinary(std::vector<uint8_t>* bytes) const {
4551cb0ef41Sopenharmony_ci    cbor::EncodeBinary(span<uint8_t>(m_binaryValue.data(),
4561cb0ef41Sopenharmony_ci                                     m_binaryValue.size()), bytes);
4571cb0ef41Sopenharmony_ci}
4581cb0ef41Sopenharmony_ci
4591cb0ef41Sopenharmony_cistd::unique_ptr<Value> BinaryValue::clone() const
4601cb0ef41Sopenharmony_ci{
4611cb0ef41Sopenharmony_ci    return BinaryValue::create(m_binaryValue);
4621cb0ef41Sopenharmony_ci}
4631cb0ef41Sopenharmony_ci
4641cb0ef41Sopenharmony_civoid SerializedValue::writeJSON(StringBuilder* output) const
4651cb0ef41Sopenharmony_ci{
4661cb0ef41Sopenharmony_ci    DCHECK(type() == TypeSerialized);
4671cb0ef41Sopenharmony_ci    StringUtil::builderAppend(*output, m_serializedJSON);
4681cb0ef41Sopenharmony_ci}
4691cb0ef41Sopenharmony_ci
4701cb0ef41Sopenharmony_civoid SerializedValue::writeBinary(std::vector<uint8_t>* output) const
4711cb0ef41Sopenharmony_ci{
4721cb0ef41Sopenharmony_ci    DCHECK(type() == TypeSerialized);
4731cb0ef41Sopenharmony_ci    output->insert(output->end(), m_serializedBinary.begin(), m_serializedBinary.end());
4741cb0ef41Sopenharmony_ci}
4751cb0ef41Sopenharmony_ci
4761cb0ef41Sopenharmony_cistd::unique_ptr<Value> SerializedValue::clone() const
4771cb0ef41Sopenharmony_ci{
4781cb0ef41Sopenharmony_ci    return std::unique_ptr<SerializedValue>(new SerializedValue(m_serializedJSON, m_serializedBinary));
4791cb0ef41Sopenharmony_ci}
4801cb0ef41Sopenharmony_ci
4811cb0ef41Sopenharmony_ciDictionaryValue::~DictionaryValue()
4821cb0ef41Sopenharmony_ci{
4831cb0ef41Sopenharmony_ci}
4841cb0ef41Sopenharmony_ci
4851cb0ef41Sopenharmony_civoid DictionaryValue::setBoolean(const String& name, bool value)
4861cb0ef41Sopenharmony_ci{
4871cb0ef41Sopenharmony_ci    setValue(name, FundamentalValue::create(value));
4881cb0ef41Sopenharmony_ci}
4891cb0ef41Sopenharmony_ci
4901cb0ef41Sopenharmony_civoid DictionaryValue::setInteger(const String& name, int value)
4911cb0ef41Sopenharmony_ci{
4921cb0ef41Sopenharmony_ci    setValue(name, FundamentalValue::create(value));
4931cb0ef41Sopenharmony_ci}
4941cb0ef41Sopenharmony_ci
4951cb0ef41Sopenharmony_civoid DictionaryValue::setDouble(const String& name, double value)
4961cb0ef41Sopenharmony_ci{
4971cb0ef41Sopenharmony_ci    setValue(name, FundamentalValue::create(value));
4981cb0ef41Sopenharmony_ci}
4991cb0ef41Sopenharmony_ci
5001cb0ef41Sopenharmony_civoid DictionaryValue::setString(const String& name, const String& value)
5011cb0ef41Sopenharmony_ci{
5021cb0ef41Sopenharmony_ci    setValue(name, StringValue::create(value));
5031cb0ef41Sopenharmony_ci}
5041cb0ef41Sopenharmony_ci
5051cb0ef41Sopenharmony_civoid DictionaryValue::setValue(const String& name, std::unique_ptr<Value> value)
5061cb0ef41Sopenharmony_ci{
5071cb0ef41Sopenharmony_ci    set(name, value);
5081cb0ef41Sopenharmony_ci}
5091cb0ef41Sopenharmony_ci
5101cb0ef41Sopenharmony_civoid DictionaryValue::setObject(const String& name, std::unique_ptr<DictionaryValue> value)
5111cb0ef41Sopenharmony_ci{
5121cb0ef41Sopenharmony_ci    set(name, value);
5131cb0ef41Sopenharmony_ci}
5141cb0ef41Sopenharmony_ci
5151cb0ef41Sopenharmony_civoid DictionaryValue::setArray(const String& name, std::unique_ptr<ListValue> value)
5161cb0ef41Sopenharmony_ci{
5171cb0ef41Sopenharmony_ci    set(name, value);
5181cb0ef41Sopenharmony_ci}
5191cb0ef41Sopenharmony_ci
5201cb0ef41Sopenharmony_cibool DictionaryValue::getBoolean(const String& name, bool* output) const
5211cb0ef41Sopenharmony_ci{
5221cb0ef41Sopenharmony_ci    protocol::Value* value = get(name);
5231cb0ef41Sopenharmony_ci    if (!value)
5241cb0ef41Sopenharmony_ci        return false;
5251cb0ef41Sopenharmony_ci    return value->asBoolean(output);
5261cb0ef41Sopenharmony_ci}
5271cb0ef41Sopenharmony_ci
5281cb0ef41Sopenharmony_cibool DictionaryValue::getInteger(const String& name, int* output) const
5291cb0ef41Sopenharmony_ci{
5301cb0ef41Sopenharmony_ci    Value* value = get(name);
5311cb0ef41Sopenharmony_ci    if (!value)
5321cb0ef41Sopenharmony_ci        return false;
5331cb0ef41Sopenharmony_ci    return value->asInteger(output);
5341cb0ef41Sopenharmony_ci}
5351cb0ef41Sopenharmony_ci
5361cb0ef41Sopenharmony_cibool DictionaryValue::getDouble(const String& name, double* output) const
5371cb0ef41Sopenharmony_ci{
5381cb0ef41Sopenharmony_ci    Value* value = get(name);
5391cb0ef41Sopenharmony_ci    if (!value)
5401cb0ef41Sopenharmony_ci        return false;
5411cb0ef41Sopenharmony_ci    return value->asDouble(output);
5421cb0ef41Sopenharmony_ci}
5431cb0ef41Sopenharmony_ci
5441cb0ef41Sopenharmony_cibool DictionaryValue::getString(const String& name, String* output) const
5451cb0ef41Sopenharmony_ci{
5461cb0ef41Sopenharmony_ci    protocol::Value* value = get(name);
5471cb0ef41Sopenharmony_ci    if (!value)
5481cb0ef41Sopenharmony_ci        return false;
5491cb0ef41Sopenharmony_ci    return value->asString(output);
5501cb0ef41Sopenharmony_ci}
5511cb0ef41Sopenharmony_ci
5521cb0ef41Sopenharmony_ciDictionaryValue* DictionaryValue::getObject(const String& name) const
5531cb0ef41Sopenharmony_ci{
5541cb0ef41Sopenharmony_ci    return DictionaryValue::cast(get(name));
5551cb0ef41Sopenharmony_ci}
5561cb0ef41Sopenharmony_ci
5571cb0ef41Sopenharmony_ciprotocol::ListValue* DictionaryValue::getArray(const String& name) const
5581cb0ef41Sopenharmony_ci{
5591cb0ef41Sopenharmony_ci    return ListValue::cast(get(name));
5601cb0ef41Sopenharmony_ci}
5611cb0ef41Sopenharmony_ci
5621cb0ef41Sopenharmony_ciprotocol::Value* DictionaryValue::get(const String& name) const
5631cb0ef41Sopenharmony_ci{
5641cb0ef41Sopenharmony_ci    Dictionary::const_iterator it = m_data.find(name);
5651cb0ef41Sopenharmony_ci    if (it == m_data.end())
5661cb0ef41Sopenharmony_ci        return nullptr;
5671cb0ef41Sopenharmony_ci    return it->second.get();
5681cb0ef41Sopenharmony_ci}
5691cb0ef41Sopenharmony_ci
5701cb0ef41Sopenharmony_ciDictionaryValue::Entry DictionaryValue::at(size_t index) const
5711cb0ef41Sopenharmony_ci{
5721cb0ef41Sopenharmony_ci    const String key = m_order[index];
5731cb0ef41Sopenharmony_ci    return std::make_pair(key, m_data.find(key)->second.get());
5741cb0ef41Sopenharmony_ci}
5751cb0ef41Sopenharmony_ci
5761cb0ef41Sopenharmony_cibool DictionaryValue::booleanProperty(const String& name, bool defaultValue) const
5771cb0ef41Sopenharmony_ci{
5781cb0ef41Sopenharmony_ci    bool result = defaultValue;
5791cb0ef41Sopenharmony_ci    getBoolean(name, &result);
5801cb0ef41Sopenharmony_ci    return result;
5811cb0ef41Sopenharmony_ci}
5821cb0ef41Sopenharmony_ci
5831cb0ef41Sopenharmony_ciint DictionaryValue::integerProperty(const String& name, int defaultValue) const
5841cb0ef41Sopenharmony_ci{
5851cb0ef41Sopenharmony_ci    int result = defaultValue;
5861cb0ef41Sopenharmony_ci    getInteger(name, &result);
5871cb0ef41Sopenharmony_ci    return result;
5881cb0ef41Sopenharmony_ci}
5891cb0ef41Sopenharmony_ci
5901cb0ef41Sopenharmony_cidouble DictionaryValue::doubleProperty(const String& name, double defaultValue) const
5911cb0ef41Sopenharmony_ci{
5921cb0ef41Sopenharmony_ci    double result = defaultValue;
5931cb0ef41Sopenharmony_ci    getDouble(name, &result);
5941cb0ef41Sopenharmony_ci    return result;
5951cb0ef41Sopenharmony_ci}
5961cb0ef41Sopenharmony_ci
5971cb0ef41Sopenharmony_civoid DictionaryValue::remove(const String& name)
5981cb0ef41Sopenharmony_ci{
5991cb0ef41Sopenharmony_ci    m_data.erase(name);
6001cb0ef41Sopenharmony_ci    m_order.erase(std::remove(m_order.begin(), m_order.end(), name), m_order.end());
6011cb0ef41Sopenharmony_ci}
6021cb0ef41Sopenharmony_ci
6031cb0ef41Sopenharmony_civoid DictionaryValue::writeJSON(StringBuilder* output) const
6041cb0ef41Sopenharmony_ci{
6051cb0ef41Sopenharmony_ci    StringUtil::builderAppend(*output, '{');
6061cb0ef41Sopenharmony_ci    for (size_t i = 0; i < m_order.size(); ++i) {
6071cb0ef41Sopenharmony_ci        Dictionary::const_iterator it = m_data.find(m_order[i]);
6081cb0ef41Sopenharmony_ci        CHECK(it != m_data.end());
6091cb0ef41Sopenharmony_ci        if (i)
6101cb0ef41Sopenharmony_ci            StringUtil::builderAppend(*output, ',');
6111cb0ef41Sopenharmony_ci        StringUtil::builderAppendQuotedString(*output, it->first);
6121cb0ef41Sopenharmony_ci        StringUtil::builderAppend(*output, ':');
6131cb0ef41Sopenharmony_ci        it->second->writeJSON(output);
6141cb0ef41Sopenharmony_ci    }
6151cb0ef41Sopenharmony_ci    StringUtil::builderAppend(*output, '}');
6161cb0ef41Sopenharmony_ci}
6171cb0ef41Sopenharmony_ci
6181cb0ef41Sopenharmony_civoid DictionaryValue::writeBinary(std::vector<uint8_t>* bytes) const {
6191cb0ef41Sopenharmony_ci    cbor::EnvelopeEncoder encoder;
6201cb0ef41Sopenharmony_ci    encoder.EncodeStart(bytes);
6211cb0ef41Sopenharmony_ci    bytes->push_back(cbor::EncodeIndefiniteLengthMapStart());
6221cb0ef41Sopenharmony_ci    for (size_t i = 0; i < m_order.size(); ++i) {
6231cb0ef41Sopenharmony_ci        const String& key = m_order[i];
6241cb0ef41Sopenharmony_ci        Dictionary::const_iterator value = m_data.find(key);
6251cb0ef41Sopenharmony_ci        DCHECK(value != m_data.cend() && value->second);
6261cb0ef41Sopenharmony_ci        EncodeString(key, bytes);
6271cb0ef41Sopenharmony_ci        value->second->writeBinary(bytes);
6281cb0ef41Sopenharmony_ci    }
6291cb0ef41Sopenharmony_ci    bytes->push_back(cbor::EncodeStop());
6301cb0ef41Sopenharmony_ci    encoder.EncodeStop(bytes);
6311cb0ef41Sopenharmony_ci}
6321cb0ef41Sopenharmony_ci
6331cb0ef41Sopenharmony_cistd::unique_ptr<Value> DictionaryValue::clone() const
6341cb0ef41Sopenharmony_ci{
6351cb0ef41Sopenharmony_ci    std::unique_ptr<DictionaryValue> result = DictionaryValue::create();
6361cb0ef41Sopenharmony_ci    for (size_t i = 0; i < m_order.size(); ++i) {
6371cb0ef41Sopenharmony_ci        String key = m_order[i];
6381cb0ef41Sopenharmony_ci        Dictionary::const_iterator value = m_data.find(key);
6391cb0ef41Sopenharmony_ci        DCHECK(value != m_data.cend() && value->second);
6401cb0ef41Sopenharmony_ci        result->setValue(key, value->second->clone());
6411cb0ef41Sopenharmony_ci    }
6421cb0ef41Sopenharmony_ci    return result;
6431cb0ef41Sopenharmony_ci}
6441cb0ef41Sopenharmony_ci
6451cb0ef41Sopenharmony_ciDictionaryValue::DictionaryValue()
6461cb0ef41Sopenharmony_ci    : Value(TypeObject)
6471cb0ef41Sopenharmony_ci{
6481cb0ef41Sopenharmony_ci}
6491cb0ef41Sopenharmony_ci
6501cb0ef41Sopenharmony_ciListValue::~ListValue()
6511cb0ef41Sopenharmony_ci{
6521cb0ef41Sopenharmony_ci}
6531cb0ef41Sopenharmony_ci
6541cb0ef41Sopenharmony_civoid ListValue::writeJSON(StringBuilder* output) const
6551cb0ef41Sopenharmony_ci{
6561cb0ef41Sopenharmony_ci    StringUtil::builderAppend(*output, '[');
6571cb0ef41Sopenharmony_ci    bool first = true;
6581cb0ef41Sopenharmony_ci    for (const std::unique_ptr<protocol::Value>& value : m_data) {
6591cb0ef41Sopenharmony_ci        if (!first)
6601cb0ef41Sopenharmony_ci            StringUtil::builderAppend(*output, ',');
6611cb0ef41Sopenharmony_ci        value->writeJSON(output);
6621cb0ef41Sopenharmony_ci        first = false;
6631cb0ef41Sopenharmony_ci    }
6641cb0ef41Sopenharmony_ci    StringUtil::builderAppend(*output, ']');
6651cb0ef41Sopenharmony_ci}
6661cb0ef41Sopenharmony_ci
6671cb0ef41Sopenharmony_civoid ListValue::writeBinary(std::vector<uint8_t>* bytes) const {
6681cb0ef41Sopenharmony_ci    cbor::EnvelopeEncoder encoder;
6691cb0ef41Sopenharmony_ci    encoder.EncodeStart(bytes);
6701cb0ef41Sopenharmony_ci    bytes->push_back(cbor::EncodeIndefiniteLengthArrayStart());
6711cb0ef41Sopenharmony_ci    for (size_t i = 0; i < m_data.size(); ++i) {
6721cb0ef41Sopenharmony_ci        m_data[i]->writeBinary(bytes);
6731cb0ef41Sopenharmony_ci    }
6741cb0ef41Sopenharmony_ci    bytes->push_back(cbor::EncodeStop());
6751cb0ef41Sopenharmony_ci    encoder.EncodeStop(bytes);
6761cb0ef41Sopenharmony_ci}
6771cb0ef41Sopenharmony_ci
6781cb0ef41Sopenharmony_cistd::unique_ptr<Value> ListValue::clone() const
6791cb0ef41Sopenharmony_ci{
6801cb0ef41Sopenharmony_ci    std::unique_ptr<ListValue> result = ListValue::create();
6811cb0ef41Sopenharmony_ci    for (const std::unique_ptr<protocol::Value>& value : m_data)
6821cb0ef41Sopenharmony_ci        result->pushValue(value->clone());
6831cb0ef41Sopenharmony_ci    return result;
6841cb0ef41Sopenharmony_ci}
6851cb0ef41Sopenharmony_ci
6861cb0ef41Sopenharmony_ciListValue::ListValue()
6871cb0ef41Sopenharmony_ci    : Value(TypeArray)
6881cb0ef41Sopenharmony_ci{
6891cb0ef41Sopenharmony_ci}
6901cb0ef41Sopenharmony_ci
6911cb0ef41Sopenharmony_civoid ListValue::pushValue(std::unique_ptr<protocol::Value> value)
6921cb0ef41Sopenharmony_ci{
6931cb0ef41Sopenharmony_ci    DCHECK(value);
6941cb0ef41Sopenharmony_ci    m_data.push_back(std::move(value));
6951cb0ef41Sopenharmony_ci}
6961cb0ef41Sopenharmony_ci
6971cb0ef41Sopenharmony_ciprotocol::Value* ListValue::at(size_t index)
6981cb0ef41Sopenharmony_ci{
6991cb0ef41Sopenharmony_ci    DCHECK_LT(index, m_data.size());
7001cb0ef41Sopenharmony_ci    return m_data[index].get();
7011cb0ef41Sopenharmony_ci}
7021cb0ef41Sopenharmony_ci
7031cb0ef41Sopenharmony_civoid escapeLatinStringForJSON(const uint8_t* str, unsigned len, StringBuilder* dst)
7041cb0ef41Sopenharmony_ci{
7051cb0ef41Sopenharmony_ci    escapeStringForJSONInternal<uint8_t>(str, len, dst);
7061cb0ef41Sopenharmony_ci}
7071cb0ef41Sopenharmony_ci
7081cb0ef41Sopenharmony_civoid escapeWideStringForJSON(const uint16_t* str, unsigned len, StringBuilder* dst)
7091cb0ef41Sopenharmony_ci{
7101cb0ef41Sopenharmony_ci    escapeStringForJSONInternal<uint16_t>(str, len, dst);
7111cb0ef41Sopenharmony_ci}
7121cb0ef41Sopenharmony_ci
7131cb0ef41Sopenharmony_ci{% for namespace in config.protocol.namespace %}
7141cb0ef41Sopenharmony_ci} // namespace {{namespace}}
7151cb0ef41Sopenharmony_ci{% endfor %}
716