LCOV - code coverage report
Current view: top level - spb - json.hpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 35 35
Test Date: 2026-10-31 08:41:17 Functions: 100.0 % 1613 1613

            Line data    Source code
       1              : /*
       2              :  * Protobuf serialization library - Public API for JSON.
       3              :  *
       4              :  * Copyright (c) 2024 Antonin Kriz <antonin.kriz@gmail.com>
       5              :  * Released under the MIT License. See LICENSE in the project root.
       6              :  */
       7              : 
       8              : #pragma once
       9              : 
      10              : #include "spb/io/io.hpp"
      11              : #include "spb/json/field.hpp"
      12              : #include "json/deserialize.hpp"
      13              : #include "json/field.hpp"
      14              : #include "json/options.h"
      15              : #include "json/serialize.hpp"
      16              : #include <cstdlib>
      17              : 
      18              : namespace spb::json
      19              : {
      20              : /**
      21              :  * @brief serialize message via writer
      22              :  *
      23              :  * @param[in] message to be serialized
      24              :  * @param[in] on_write function for handling the writes
      25              :  * @param[in] options
      26              :  * @return serialized size in bytes
      27              :  * @throws exceptions only from `on_write`
      28              :  */
      29          388 : size_t serialize(const auto &message, spb::io::writer on_write, const serialize_options &options = {})
      30              : {
      31          388 :     auto stream = detail::ostream_writer{options, on_write};
      32          388 :     detail::serialize<detail::field_attributes{}>(stream, message);
      33          776 :     return stream.end();
      34              : }
      35              : 
      36              : /**
      37              :  * @brief return JSON serialized size in bytes
      38              :  *
      39              :  * @param[in] message to be serialized
      40              :  * @param[in] options
      41              :  * @return serialized size in bytes
      42              :  */
      43          389 : [[nodiscard]] size_t serialize_size(const auto &message, const serialize_options &options = {})
      44              : {
      45          389 :     return detail::serialize_size(message, options);
      46              : }
      47              : 
      48          931 : size_t serialize(const auto &message, void *buffer, const serialize_options &options = {})
      49              : {
      50          931 :     const auto start = (uint8_t *)buffer;
      51          931 :     auto stream      = detail::ostream_buffer{options, (uint8_t *)buffer};
      52          931 :     detail::serialize<detail::field_attributes{}>(stream, message);
      53         1792 :     return stream.end(start);
      54              : }
      55              : 
      56              : /**
      57              :  * @brief serialize message into JSON
      58              :  *
      59              :  * @param[in] message to be serialized
      60              :  * @param[in] options
      61              :  * @param[out] result serialized JSON
      62              :  * @return serialized size in bytes
      63              :  * @throws std::runtime_error on error
      64              :  * @example `std::string json;`
      65              :  *          `spb::json::serialize(message, json);`
      66              :  */
      67              : template <spb::resizable_container Container>
      68          932 : size_t serialize(const auto &message, Container &result, const serialize_options &options = {})
      69              : {
      70              :     static_assert(sizeof(*result.data()) == sizeof(uint8_t));
      71              : 
      72          932 :     const auto size = detail::serialize_size(message, options);
      73          931 :     result.resize(size);
      74          931 :     return serialize(message, result.data(), options);
      75              : }
      76              : 
      77              : /**
      78              :  * @brief serialize message into JSON
      79              :  *
      80              :  * @param[in] message to be serialized
      81              :  * @param[in] options
      82              :  * @return serialized JSON
      83              :  * @throws std::runtime_error on error
      84              :  * @example `auto serialized_message = spb::json::serialize< std::vector< std::byte > >( message );`
      85              :  */
      86              : template <spb::resizable_container Container = std::string>
      87          544 : [[nodiscard]] Container serialize(const auto &message, const serialize_options &options = {})
      88              : {
      89          544 :     auto result = Container();
      90          544 :     serialize(message, result, options);
      91          510 :     return result;
      92           34 : }
      93              : 
      94         1385 : size_t deserialize(auto &message, const void *buffer, size_t size)
      95              : {
      96         1385 :     detail::istream_buffer stream((const uint8_t *)buffer, size);
      97         1385 :     deserialize<detail::field_attributes{}>(stream, message);
      98          985 :     return size - stream.size();
      99              : }
     100              : 
     101              : /**
     102              :  * @brief deserialize message from JSON
     103              :  *
     104              :  * @param[in] reader function for handling reads
     105              :  * @param[in] options
     106              :  * @param[out] message deserialized message
     107              :  * @throws std::runtime_error on error
     108              :  */
     109          388 : size_t deserialize(auto &message, spb::io::reader reader)
     110              : {
     111          388 :     detail::istream_reader stream{reader};
     112          388 :     deserialize<detail::field_attributes{}>(stream, message);
     113          776 :     return stream.consumed_size();
     114              : }
     115              : 
     116              : /**
     117              :  * @brief deserialize message from JSON
     118              :  *
     119              :  * @param[in] JSON string with JSON
     120              :  * @param[in] options
     121              :  * @param[out] message deserialized message
     122              :  * @throws std::runtime_error on error
     123              :  * @example `auto serialized = std::vector<std::byte>( ... );`
     124              :  *          `auto message = Message();`
     125              :  *          `spb::json::deserialize(message, serialized);`
     126              :  */
     127          408 : size_t deserialize(auto &message, const spb::size_container auto &json)
     128              : {
     129          408 :     return deserialize(message, json.data(), json.size());
     130              : }
     131              : 
     132              : /**
     133              :  * @brief deserialize message from JSON
     134              :  *
     135              :  * @param[in] JSON serialized JSON
     136              :  * @param[in] options
     137              :  * @return deserialized message
     138              :  * @throws std::runtime_error on error
     139              :  * @example `auto serialized = std::vector< std::byte >( ... );`
     140              :  *          `auto message = spb::json::deserialize< Message >( serialized );`
     141              :  */
     142          977 : template <typename Message> [[nodiscard]] Message deserialize(const spb::size_container auto &json)
     143              : {
     144          975 :     auto message = Message{};
     145          977 :     deserialize(message, json.data(), json.size());
     146          579 :     return message;
     147          355 : }
     148              : 
     149              : /**
     150              :  * @brief deserialize message from reader
     151              :  *
     152              :  * @param[in] reader function for handling reads
     153              :  * @param[in] options
     154              :  * @return deserialized message
     155              :  * @throws std::runtime_error on error
     156              :  */
     157              : template <typename Message> [[nodiscard]] Message deserialize(spb::io::reader reader)
     158              : {
     159              :     auto message = Message{};
     160              :     deserialize(message, reader);
     161              :     return message;
     162              : }
     163              : } // namespace spb::json
        

Generated by: LCOV version 2.0-1