Advanced Chunk Processing Library 0.2.0
A comprehensive C++ library for advanced data chunking strategies and processing operations
Loading...
Searching...
No Matches
chunk_serialization.hpp
Go to the documentation of this file.
1/**
2 * @file chunk_serialization.hpp
3 * @brief Serialization utilities for chunk data
4 * @author Jonathan Reich
5 * @date 2024-12-07
6 */
7
8#pragma once
9#include "chunk_common.hpp"
10#include <stdexcept>
11#include <string>
12#include <vector>
13
15
16/**
17 * @brief Class for serializing chunks to various formats
18 * @tparam T The data type of the chunks
19 */
20template <typename T>
22public:
23 /**
24 * @brief Serialize chunks to JSON format
25 * @param chunks Vector of chunk data
26 * @return JSON string representation
27 * @throws std::runtime_error if serialization fails
28 */
29 std::string to_json(const std::vector<std::vector<T>>& chunks) {
30 validate_chunks(chunks);
31
32 // Basic JSON array serialization
33 std::string result = "[";
34 for (size_t i = 0; i < chunks.size(); ++i) {
35 result += "[";
36 for (size_t j = 0; j < chunks[i].size(); ++j) {
37 result += std::to_string(chunks[i][j]);
38 if (j < chunks[i].size() - 1) {
39 result += ",";
40 }
41 }
42 result += "]";
43 if (i < chunks.size() - 1) {
44 result += ",";
45 }
46 }
47 result += "]";
48 return result;
49 }
50
51 /**
52 * @brief Serialize chunks to Protocol Buffers format
53 * @param chunks Vector of chunk data
54 * @return Protobuf binary string
55 * @throws std::runtime_error if Protobuf support not available
56 */
57 std::string to_protobuf(const std::vector<std::vector<T>>& chunks) {
58 validate_chunks(chunks);
59 throw std::runtime_error("Protocol Buffers serialization not implemented");
60 }
61
62 /**
63 * @brief Serialize chunks to MessagePack format
64 * @param chunks Vector of chunk data
65 * @return MessagePack binary string
66 * @throws std::runtime_error if MessagePack support not available
67 */
68 std::string to_msgpack(const std::vector<std::vector<T>>& chunks) {
69 validate_chunks(chunks);
70
71 // Basic implementation without MessagePack dependency
72 std::string result = "[";
73 for (size_t i = 0; i < chunks.size(); ++i) {
74 result += "[";
75 for (size_t j = 0; j < chunks[i].size(); ++j) {
76 result += std::to_string(chunks[i][j]);
77 if (j < chunks[i].size() - 1) {
78 result += ",";
79 }
80 }
81 result += "]";
82 if (i < chunks.size() - 1) {
83 result += ",";
84 }
85 }
86 result += "]";
87 return result;
88 }
89
90private:
91 /**
92 * @brief Validate chunk data before serialization
93 * @param chunks Vector of chunk data to validate
94 * @throws std::invalid_argument if validation fails
95 */
96 void validate_chunks(const std::vector<std::vector<T>>& chunks) {
97 if (chunks.empty()) {
98 throw std::invalid_argument("Cannot serialize empty chunks");
99 }
100
101 for (const auto& chunk : chunks) {
102 if (chunk.empty()) {
103 throw std::invalid_argument("Cannot serialize chunks containing empty vectors");
104 }
105 }
106 }
107};
108
109} // namespace chunk_serialization
#define CHUNK_EXPORT
Class for serializing chunks to various formats.
std::string to_msgpack(const std::vector< std::vector< T > > &chunks)
Serialize chunks to MessagePack format.
void validate_chunks(const std::vector< std::vector< T > > &chunks)
Validate chunk data before serialization.
std::string to_protobuf(const std::vector< std::vector< T > > &chunks)
Serialize chunks to Protocol Buffers format.
std::string to_json(const std::vector< std::vector< T > > &chunks)
Serialize chunks to JSON format.