summaryrefslogtreecommitdiff
path: root/modules/codec-json/c++/json.hpp
blob: b3ea19e7feaf8e3dd5fabcd9ca826438a94699b4 (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
#pragma once

#include <forstio/buffer.hpp>
#include <forstio/common.hpp>
#include <forstio/codec/data.hpp>

#include <algorithm>

namespace saw {
namespace encode {
struct Json {
	static constexpr string_literal name = "encode::Json";
};
}

template<typename Schema>
class data<Schema, encode::Json> {
private:
	own<buffer> buffer_;
public:
	data():buffer_{}{}
	data(own<buffer> buffer__):buffer_{std::move(buffer__)}{}

	data(std::size_t ring_size_):buffer_{heap<ring_buffer>(ring_size_)}{}

	data(const std::string_view& view__):
		buffer_{heap<ring_buffer>(view__.size())}
	{
		auto ptr = reinterpret_cast<const uint8_t*>(view__.data());
		if(!ptr){
			return;
		}
		buffer_->push(*ptr, view__.size());
	}

	buffer& get_buffer(){
		return *buffer_;
	}

	const buffer& get_buffer() const {
		return *buffer_;
	}

	error push(uint8_t val){
		return buffer_->push(val);
	}

	std::size_t get_size() const {
		return buffer_->read_composite_length();
	}

	uint8_t& at(std::size_t i){
		return buffer_->read(i);
	}
	
	const uint8_t& at(std::size_t i) const {
		return buffer_->read(i);
	}
};

template<typename Encoding>
struct codec_config;

template<>
struct codec_config<encode::Json> {
	uint64_t depth = 16u;
	uint64_t length = 4096u;
	bool pretty = false;

	bool is_within(uint64_t bytes, uint64_t d){
		return bytes <= length && d <= depth;
	}
};
}

#include "json.tmpl.hpp"

namespace saw {

/**
 * Codec class for json
 */
template<typename Schema>
class codec<Schema, encode::Json> {
private:
	codec_config<encode::Json> cfg_;
public:
	/**
	 * Default constructor
	 */
	codec(){}

	/**
	 * Constructor
	 */
	codec(const codec_config<encode::Json>& cfg__):cfg_{cfg__}{}

	SAW_FORBID_COPY(codec);
	SAW_DEFAULT_MOVE(codec);

	template <typename FromEncoding>
	error_or<void> encode(const data<Schema, FromEncoding>& from_encode, data<Schema, encode::Json>& to_encode){
		// To Be encoded
		buffer_view buff_v{to_encode.get_buffer()};
		auto eov = impl::json_encode<Schema, FromEncoding>::encode(from_encode, buff_v, 0, cfg_.pretty);
		if(eov.is_error()){
			return std::move(eov.get_error());
		}
		to_encode.get_buffer().write_advance(buff_v.write_offset());

		return void_t{};
	}

	template <typename ToEncoding>
	error_or<void> decode(data<Schema, encode::Json>& from_decode, data<Schema, ToEncoding>& to_decode){
		buffer_view buff_v{from_decode.get_buffer()};

		impl::json_helper::skip_whitespace(buff_v);
		auto eov = impl::json_decode<Schema, ToEncoding>::decode(buff_v, to_decode);
		if(eov.is_error()){
			return std::move(eov.get_error());
		}
		from_decode.get_buffer().read_advance(buff_v.read_offset());

		return void_t {};
	}
};
}