File: boostjson.h

package info (click to toggle)
simdjson 4.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,936 kB
  • sloc: cpp: 171,612; ansic: 19,122; sh: 1,126; python: 842; makefile: 47; ruby: 25; javascript: 13
file content (104 lines) | stat: -rw-r--r-- 2,534 bytes parent folder | download | duplicates (2)
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
#pragma once

#ifdef SIMDJSON_COMPETITION_BOOSTJSON

#include "json2msgpack.h"

namespace json2msgpack {

struct boostjson2msgpack {
  inline std::string_view to_msgpack(const boost::json::value &root, uint8_t *buf) {
    buff = buf;
    recursive_processor(root);
    return std::string_view(reinterpret_cast<char *>(buf), size_t(buff - buf));
  }

private:
  uint8_t *buff{};

  inline void write_double(const double d) noexcept {
    *buff++ = 0xcb;
    ::memcpy(buff, &d, sizeof(d));
    buff += sizeof(d);
  }

  inline void write_byte(const uint8_t b) noexcept {
    *buff = b;
    buff++;
  }

  inline void write_uint32(const uint32_t w) noexcept {
    ::memcpy(buff, &w, sizeof(w));
    buff += sizeof(w);
  }

  inline void write_string(const std::string & str) {
    write_byte(0xdb);
    write_uint32(uint32_t(str.size()));
    ::memcpy(buff, str.data(), str.size());
    buff += str.size();
  }

  inline void recursive_processor(const boost::json::value &element) {
    switch(element.kind()) {
    case boost::json::kind::array: {
      write_byte(0xdd);
      const auto &array = element.as_array();
      write_uint32(static_cast<uint32_t>(array.size()));
      for (const auto &child : array) {
        recursive_processor(child);
      }
    } break;

    case boost::json::kind::object: {
      write_byte(0xdf);
      const auto &object = element.as_object();
      write_uint32(static_cast<uint32_t>(object.size()));
      for (const auto &child : object) {
        write_string(child.key_c_str());
        recursive_processor(child.value());
      }
    } break;

    case boost::json::kind::int64:
    case boost::json::kind::uint64:
    case boost::json::kind::double_:
      write_double(element.to_number<double>());
      break;

    case boost::json::kind::string:
      write_string(element.as_string().c_str());
      break;

    case boost::json::kind::bool_:
      write_byte(0xc2 + element.as_bool());
      break;

    case boost::json::kind::null:
      write_byte(0xc0);
      break;

    default:
      printf("unexpected\n");
      break;
    }
  }
};

struct boostjson {
  using StringType=std::string;

  boostjson2msgpack parser{};

  bool run(simdjson::padded_string &json, char *buffer, std::string_view &result) {
    auto root = boost::json::parse(json);
    result = parser.to_msgpack(root, reinterpret_cast<uint8_t *>(buffer));
    return true;
  }
};

BENCHMARK_TEMPLATE(json2msgpack, boostjson)->UseManualTime();

} // namespace json2msgpack

#endif // SIMDJSON_COMPETITION_BOOSTJSON