summaryrefslogtreecommitdiff
path: root/src/codec-json/json.h
blob: 58b64aa7cae1b9d69ef40cc2c78a5f275e37d195 (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
#pragma once

#include <forstio/core/buffer.h>
#include <forstio/core/common.h>
#include <forstio/codec/data.h>

#include <algorithm>

namespace saw {
namespace encode {
struct Json {};
}

template<typename Schema>
class data<Schema, encode::Json> {
private:
	ring_buffer buffer_;
public:
	data():buffer_{}{}

	data(std::size_t ring_size_):buffer_{ring_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);
	}
};
}

#include "json.tmpl.h"

namespace saw {

/**
 * Codec class for json
 */
template<typename Schema>
class codec<Schema, encode::Json> {
public:
	struct config {
		size_t depth = 16;
		size_t length = 1024;
	};
private:
	config cfg_;
public:
	/**
	 * Default constructor
	 */
	codec(){}

	/**
	 * Constructor
	 */
	codec(config cfg__):cfg_{std::move(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, Schema, FromEncoding>::encode(from_encode, buff_v);
		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()};

		return void_t {};
	}
};
}