Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "algorithm.h"
- #include "byte-array.h"
- #include "common.h"
- #include "containers.h"
- #include "input-stream.h"
- #include "optional.h"
- #include "output-stream.h"
- #include "string.h"
- #include "value.h"
- namespace fn {
- FN_UNINDENT
- namespace mpack {
- //
- //
- //
- class ValueSerializer
- {
- public:
- // -1 means unsupported object (will be serialized as NIL);
- // Negative types are reserved for future use in MessagePack Specification
- virtual Int8 extTypeForValue(const Value & value) = 0;
- virtual void serializeValue(const Value & value, OutputStream & stream) = 0;
- virtual SizeType serializedLengthForValue(const Value & value)
- {
- CountingOutputStream stream;
- serializeValue(value, stream);
- return as<SizeType>(stream.currentOffset());
- }
- virtual ~ValueSerializer() {}
- };
- class FN_EXPORT Serializer : private ValueSerializer
- {
- public:
- Serializer(OutputStream & stream)
- : Serializer(stream, *this)
- {
- }
- Serializer(OutputStream & stream, ValueSerializer & valueSerializer)
- : _writer(stream)
- , _valueSerializer(valueSerializer)
- {
- }
- void serialize(const Value & value);
- virtual ~Serializer() override {}
- protected:
- Int8 extTypeForValue(const Value &) override { return -1; }
- void serializeValue(const Value &, OutputStream &) override {}
- private:
- void writeInt(Int64 i);
- void writeUInt(UInt64 i);
- void writeDouble(double f);
- void writeString(StringView s);
- void writeByteArray(const ByteArray & b);
- void writeBoxed(const Value & value);
- void writeArray(const Value::Vector & array);
- void writeMap(const Value::Map & map);
- void writeCustom(const Value & value);
- StreamWriter _writer;
- ValueSerializer & _valueSerializer;
- };
- //
- //
- //
- class ValueDeserializer
- {
- public:
- virtual Value deserializeValue(int objectType, SizeType size, InputStream & stream) = 0;
- virtual ~ValueDeserializer() {}
- };
- class FN_EXPORT Deserializer : private ValueDeserializer
- {
- public:
- Deserializer(InputStream & stream, ValueDeserializer & valueDeserializer)
- : _reader(stream)
- , _valueDeserializer(valueDeserializer)
- {
- }
- Deserializer(InputStream & stream)
- : Deserializer(stream, *this)
- {
- }
- Optional<Value> deserialize();
- protected:
- Value deserializeValue(int, SizeType, InputStream &) override { return Value(); }
- private:
- ByteArray readByteArray(SizeType size);
- Value readArray(SizeType size);
- Value readMap(SizeType size);
- Value readObject(SizeType size);
- StreamReader _reader;
- ValueDeserializer & _valueDeserializer;
- };
- //
- // Inlines
- //
- } // namespace mpack
- } // namespace fn
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement