xref: /test/ostest/wukong/report/src/format_json.cpp (revision a69a01cd)
1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "format_json.h"
17#include <sstream>
18namespace OHOS {
19namespace WuKong {
20FormatJSON::FormatJSON() : Format()
21{
22}
23
24FormatJSON::~FormatJSON()
25{
26}
27
28void FormatJSON::FormatDetail(std::shared_ptr<Table> tablePtr, std::string &target)
29{
30    std::vector<std::string> header = tablePtr->GetHeader();
31    std::vector<std::vector<std::string>> record = tablePtr->GetRecord();
32    std::stringstream ss;
33    ss << "[" << std::endl;
34    for (uint32_t row = 0; row < record.size(); row++) {
35        ss << "{" << std::endl;
36        for (uint32_t col = 0; col < header.size(); col++) {
37            ss  << header[col] << ":\"" << record[row][col] << "\"";
38            if (col == (header.size() - 1)) {
39                ss << "},";
40                break;
41            }
42            ss << ",";
43        }
44        ss << std::endl;
45    }
46    ss << "]," << std::endl;
47    target = ss.str();
48}
49}  // namespace WuKong
50}  // namespace OHOS
51