Welcome to mirror list, hosted at ThFree Co, Russian Federation.

eye_serdes.cpp « metrics - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2851f13b53546303856a258fa19096f6187b764b (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
#include "metrics/eye_serdes.hpp"

#include "coding/reader.hpp"
#include "coding/serdes_json.hpp"
#include "coding/write_to_sink.hpp"
#include "coding/writer.hpp"

#include "base/logging.hpp"

#include <string>
#include <type_traits>

namespace eye
{
// static
void Serdes::Serialize(Info const & info, std::vector<int8_t> & result)
{
  result.clear();
  using Sink = MemWriter<std::vector<int8_t>>;
  Sink writer(result);

  WriteToSink(writer, static_cast<int8_t>(Info::GetVersion()));

  coding::SerializerJson<Sink> ser(writer);
  ser(info);
}

// static
void Serdes::Deserialize(std::vector<int8_t> const & bytes, Info & result)
{
  MemReader reader(bytes.data(), bytes.size());
  NonOwningReaderSource source(reader);

  auto version = static_cast<int8_t>(Version::Unknown);
  ReadPrimitiveFromSource(source, version);
  if (version == static_cast<int8_t>(Version::V0))
  {
    try
    {
      // TODO: Use temporary object InfoV0 and implement method to convert it to Info,
      // TODO: when InfoV1 will be implemented.
      coding::DeserializerJson des(source);
      des(result);
    }
    catch (my::Json::Exception & ex)
    {
      LOG(LERROR, ("Cannot deserialize eye file. Exception:", ex.Msg(), "Version:", version,
                   "File content:", bytes));
    }
    return;
  }

  MYTHROW(UnknownVersion, ("Unknown data version:", static_cast<int>(version)));
}
}  // namespace eye