Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/aws-cpp-sdk-core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,15 @@ check_cxx_source_compiles("
return 0;
}" AWS_HAS_ALIGNED_ALLOC)

add_library(${PROJECT_NAME} ${AWS_NATIVE_SDK_SRC})
add_library(${PROJECT_NAME} ${AWS_NATIVE_SDK_SRC}
include/smithy/client/schema/ShapeSerializer.h
include/smithy/client/schema/CborShapeSerializer.h
include/smithy/client/schema/Schema.h
include/smithy/client/schema/JsonShapeSerializer.h
include/smithy/client/schema/QueryShapeSerializer.h
include/smithy/client/schema/XmlShapeSerializer.h
source/smithy/client/schema/JsonShapeSerializer.cpp
)
add_library(AWS::${PROJECT_NAME} ALIAS ${PROJECT_NAME})

target_compile_definitions(${PROJECT_NAME} PUBLIC "AWS_SDK_VERSION_MAJOR=${AWSSDK_VERSION_MAJOR}")
Expand Down
2 changes: 2 additions & 0 deletions src/aws-cpp-sdk-core/include/smithy/client/schema/Schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ enum class ShapeType : uint8_t {
class Schema {
public:
Schema() = default;
Schema(const char* memberName, ShapeType type)
: m_type(type), m_memberName(memberName) {}

ShapeType GetType() const { return m_type; }
const char* GetId() const { return m_id; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/core/utils/HashingUtils.h>
#include <smithy/client/schema/JsonShapeSerializer.h>

using namespace smithy::schema;
using namespace Aws::Utils;

static const int MAX_DEPTH = 64;

struct JsonShapeSerializer::Impl {
Aws::String m_buf;
int m_depth = 0;
bool m_needsComma[MAX_DEPTH] = {};
bool m_isMap[MAX_DEPTH] = {};
bool m_isList[MAX_DEPTH] = {};
Aws::String m_currentMapKey;

void WriteCommaIfNeeded() {
if (m_needsComma[m_depth]) {
m_buf += ',';
} else {
m_needsComma[m_depth] = true;
}
}

void WriteKey(const char* key) {
m_buf += '"';
m_buf += key;
m_buf += "\":";
}

void WriteFieldName(const Schema& schema) {
WriteCommaIfNeeded();
if (m_isList[m_depth]) {
return;
}
if (m_depth > 0 && m_isMap[m_depth]) {
WriteKey(m_currentMapKey.c_str());
} else {
WriteKey(schema.GetMemberName());
}
}

void BeginStructure(const Schema&) {
m_buf += '{';
m_depth++;
m_needsComma[m_depth] = false;
m_isMap[m_depth] = false;
m_isList[m_depth] = false;
}

void EndStructure() {
m_depth--;
m_buf += '}';
}

void WriteBoolean(const Schema& schema, bool value) {
WriteFieldName(schema);
m_buf += value ? "true" : "false";
}

void WriteInteger(const Schema& schema, int value) {
WriteFieldName(schema);
m_buf += std::to_string(value);
}

void WriteLong(const Schema& schema, int64_t value) {
WriteFieldName(schema);
m_buf += std::to_string(value);
}

void WriteDouble(const Schema& schema, double value) {
WriteFieldName(schema);
char tmp[32];
snprintf(tmp, sizeof(tmp), "%g", value);
m_buf += tmp;
}

void WriteString(const Schema& schema, const Aws::String& value) {
WriteFieldName(schema);
m_buf += '"';
m_buf += value;
m_buf += '"';
}

void WriteTimestamp(const Schema& schema, const DateTime& value) {
WriteFieldName(schema);
char tmp[32];
snprintf(tmp, sizeof(tmp), "%g", value.SecondsWithMSPrecision());
m_buf += tmp;
}

void WriteBlob(const Schema& schema, const ByteBuffer& value) {
WriteFieldName(schema);
m_buf += '"';
m_buf += HashingUtils::Base64Encode(value);
m_buf += '"';
}

void WriteEnum(const Schema& schema, int value) { WriteInteger(schema, value); }

void WriteNull(const Schema& schema) {
WriteFieldName(schema);
m_buf += "null";
}

void BeginList(const Schema& schema, size_t) {
WriteFieldName(schema);
m_buf += '[';
m_depth++;
m_needsComma[m_depth] = false;
m_isMap[m_depth] = false;
m_isList[m_depth] = true;
}

void EndList() {
m_depth--;
m_buf += ']';
}

void BeginMap(const Schema& schema, size_t) {
WriteFieldName(schema);
m_buf += '{';
m_depth++;
m_needsComma[m_depth] = false;
m_isMap[m_depth] = true;
m_isList[m_depth] = false;
}

void WriteMapKey(const Aws::String& key) { m_currentMapKey = key; }

void EndMap() {
m_depth--;
m_buf += '}';
}

void BeginNestedStructure(const Schema& schema) {
WriteFieldName(schema);
m_buf += '{';
m_depth++;
m_needsComma[m_depth] = false;
m_isMap[m_depth] = false;
m_isList[m_depth] = false;
}

void EndNestedStructure() {
m_depth--;
m_buf += '}';
}

Aws::String GetPayload() const { return m_buf; }
};

JsonShapeSerializer::JsonShapeSerializer() : m_impl(new Impl) { m_impl->m_buf.reserve(256); }
JsonShapeSerializer::~JsonShapeSerializer() = default;

void JsonShapeSerializer::BeginStructure(const Schema& schema) { m_impl->BeginStructure(schema); }
void JsonShapeSerializer::EndStructure() { m_impl->EndStructure(); }
void JsonShapeSerializer::WriteBoolean(const Schema& schema, bool value) { m_impl->WriteBoolean(schema, value); }
void JsonShapeSerializer::WriteInteger(const Schema& schema, int value) { m_impl->WriteInteger(schema, value); }
void JsonShapeSerializer::WriteLong(const Schema& schema, int64_t value) { m_impl->WriteLong(schema, value); }
void JsonShapeSerializer::WriteDouble(const Schema& schema, double value) { m_impl->WriteDouble(schema, value); }
void JsonShapeSerializer::WriteString(const Schema& schema, const Aws::String& value) { m_impl->WriteString(schema, value); }
void JsonShapeSerializer::WriteTimestamp(const Schema& schema, const DateTime& value) { m_impl->WriteTimestamp(schema, value); }
void JsonShapeSerializer::WriteBlob(const Schema& schema, const ByteBuffer& value) { m_impl->WriteBlob(schema, value); }
void JsonShapeSerializer::WriteEnum(const Schema& schema, int value) { m_impl->WriteEnum(schema, value); }
void JsonShapeSerializer::WriteNull(const Schema& schema) { m_impl->WriteNull(schema); }
void JsonShapeSerializer::BeginList(const Schema& schema, size_t count) { m_impl->BeginList(schema, count); }
void JsonShapeSerializer::EndList() { m_impl->EndList(); }
void JsonShapeSerializer::BeginMap(const Schema& schema, size_t count) { m_impl->BeginMap(schema, count); }
void JsonShapeSerializer::WriteMapKey(const Aws::String& key) { m_impl->WriteMapKey(key); }
void JsonShapeSerializer::EndMap() { m_impl->EndMap(); }
void JsonShapeSerializer::BeginNestedStructure(const Schema& schema) { m_impl->BeginNestedStructure(schema); }
void JsonShapeSerializer::EndNestedStructure() { m_impl->EndNestedStructure(); }
Aws::String JsonShapeSerializer::GetPayload() const { return m_impl->GetPayload(); }
1 change: 1 addition & 0 deletions tests/aws-cpp-sdk-core-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ file(GLOB UTILS_COMPONENT_REGISTRY_SRC "${CMAKE_CURRENT_SOURCE_DIR}/utils/compon
file(GLOB MONITORING_SRC "${CMAKE_CURRENT_SOURCE_DIR}/monitoring/*.cpp")
file(GLOB SMITHY_TRACING_SRC "${CMAKE_CURRENT_SOURCE_DIR}/smithy/tracing/*.cpp")
file(GLOB SMITHY_CLIENT_SRC "${CMAKE_CURRENT_SOURCE_DIR}/smithy/client/*.cpp")
file(GLOB SMITHY_CLIENT_SRC "${CMAKE_CURRENT_SOURCE_DIR}/smithy/client/schema/*.cpp")
file(GLOB SMITHY_CLIENT_SERIALIZER_SRC "${CMAKE_CURRENT_SOURCE_DIR}/smithy/client/serializer/*.cpp")
file(GLOB SMITHY_CLIENT_FEATURE_SRC "${CMAKE_CURRENT_SOURCE_DIR}/smithy/client/feature/*.cpp")
file(GLOB ENDPOINT_SRC "${CMAKE_CURRENT_SOURCE_DIR}/endpoint/*.cpp")
Expand Down
Loading
Loading