Hash :
b3a8033d
Author :
Date :
2021-03-31T09:34:00
JsonSerializer: use stubs when building without rapidjson Bug: angleproject:5805 Change-Id: Ibf51b8b75c3feb6efdef969effb3f50e2474c6b3 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2795772 Commit-Queue: Gert Wollny <gert.wollny@collabora.com> Reviewed-by: Cody Northrop <cnorthrop@google.com> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Jamie Madill <jmadill@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
//
// Copyright 2021 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// JsonSerializer.cpp: Implementation of a JSON based serializer
// Note that for binary blob data only a checksum is stored so that
// a lossless deserialization is not supported.
#include "JsonSerializer.h"
#include "common/debug.h"
#include <anglebase/sha1.h>
#include <rapidjson/document.h>
#include <rapidjson/filewritestream.h>
#include <rapidjson/ostreamwrapper.h>
#include <rapidjson/prettywriter.h>
namespace angle
{
namespace js = rapidjson;
JsonSerializer::JsonSerializer() : mDoc(js::kObjectType), mAllocator(mDoc.GetAllocator()) {}
JsonSerializer::~JsonSerializer() {}
void JsonSerializer::startDocument(const std::string &name)
{
startGroup(name);
}
void JsonSerializer::startGroup(const std::string &name)
{
auto group = std::make_unique<js::Value>(js::kObjectType);
mGroupValueStack.push(std::move(group));
mGroupNameStack.push(name);
}
void JsonSerializer::endGroup()
{
ValuePointer group = std::move(mGroupValueStack.top());
std::string name = std::move(mGroupNameStack.top());
mGroupValueStack.pop();
mGroupNameStack.pop();
rapidjson::Value name_value(name.c_str(), mAllocator);
mGroupValueStack.top()->AddMember(name_value, *group, mAllocator);
}
void JsonSerializer::addBlob(const std::string &name, const uint8_t *blob, size_t length)
{
rapidjson::Value tag(name.c_str(), mAllocator);
unsigned char hash[angle::base::kSHA1Length];
angle::base::SHA1HashBytes(blob, length, hash);
std::ostringstream os;
// Since we don't want to de-serialize the data we just store a checksume
// of the blob
os << "SHA1:";
static constexpr char kASCII[] = "0123456789ABCDEF";
for (size_t i = 0; i < angle::base::kSHA1Length; ++i)
{
os << kASCII[hash[i] & 0xf] << kASCII[hash[i] >> 4];
}
addString(name, os.str());
}
void JsonSerializer::addCString(const std::string &name, const char *value)
{
rapidjson::Value tag(name.c_str(), mAllocator);
rapidjson::Value val(value, mAllocator);
mGroupValueStack.top()->AddMember(tag, val, mAllocator);
}
void JsonSerializer::addString(const std::string &name, const std::string &value)
{
addCString(name, value.c_str());
}
const char *JsonSerializer::data() const
{
return mResult.c_str();
}
std::vector<uint8_t> JsonSerializer::getData() const
{
return std::vector<uint8_t>(mResult.begin(), mResult.end());
}
void JsonSerializer::endDocument()
{
// finalize last group
ASSERT(!mGroupValueStack.empty());
ASSERT(!mGroupNameStack.empty());
rapidjson::Value name_value(mGroupNameStack.top().c_str(), mAllocator);
mDoc.AddMember(name_value, *mGroupValueStack.top(), mAllocator);
mGroupValueStack.pop();
mGroupNameStack.pop();
ASSERT(mGroupValueStack.empty());
ASSERT(mGroupNameStack.empty());
std::stringstream os;
js::OStreamWrapper osw(os);
js::PrettyWriter<js::OStreamWrapper> pretty_os(osw);
mDoc.Accept(pretty_os);
mResult = os.str();
}
size_t JsonSerializer::length() const
{
return mResult.length();
}
} // namespace angle