LCOV - code coverage report
Current view: top level - spb - json.hpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 32 32
Test Date: 2025-05-23 14:18:13 Functions: 100.0 % 1588 1588

            Line data    Source code
       1              : /***************************************************************************\
       2              : * Name        : Public API for JSON                                         *
       3              : * Description : all json serialize and deserialize functions                *
       4              : * Author      : antonin.kriz@gmail.com                                      *
       5              : * ------------------------------------------------------------------------- *
       6              : * This is free software; you can redistribute it and/or modify it under the *
       7              : * terms of the MIT license. A copy of the license can be found in the file  *
       8              : * "LICENSE" at the root of this distribution.                               *
       9              : \***************************************************************************/
      10              : #pragma once
      11              : 
      12              : #include "spb/io/io.hpp"
      13              : #include "json/deserialize.hpp"
      14              : #include "json/serialize.hpp"
      15              : #include <cstdlib>
      16              : 
      17              : namespace spb::json
      18              : {
      19              : 
      20              : /**
      21              :  * @brief serialize message via writer
      22              :  *
      23              :  * @param message to be serialized
      24              :  * @param on_write function for handling the writes
      25              :  * @return serialized size in bytes
      26              :  * @throws exceptions only from `on_write`
      27              :  */
      28         1832 : static inline auto serialize( const auto & message, spb::io::writer on_write ) -> size_t
      29              : {
      30         1832 :     return detail::serialize( message, on_write );
      31              : }
      32              : 
      33              : /**
      34              :  * @brief return json-string serialized size in bytes
      35              :  *
      36              :  * @param message to be serialized
      37              :  * @return serialized size in bytes
      38              :  */
      39         1084 : [[nodiscard]] static inline auto serialize_size( const auto & message ) -> size_t
      40              : {
      41         1084 :     return serialize( message, spb::io::writer( nullptr ) );
      42              : }
      43              : 
      44              : /**
      45              :  * @brief serialize message into json-string
      46              :  *
      47              :  * @param message to be serialized
      48              :  * @return serialized json
      49              :  * @throws std::runtime_error on error
      50              :  * @example `auto serialized = std::vector< std::byte >();`
      51              :  *          `spb::json::serialize( message, serialized );`
      52              :  */
      53              : template < typename Message, spb::resizable_container Container >
      54          749 : static inline auto serialize( const Message & message, Container & result ) -> size_t
      55              : {
      56          749 :     const auto size = serialize_size( message );
      57          748 :     result.resize( size );
      58         5732 :     auto writer = [ ptr = result.data( ) ]( const void * data, size_t size ) mutable
      59              :     {
      60         4984 :         memcpy( ptr, data, size );
      61         4984 :         ptr += size;
      62              :     };
      63              : 
      64          748 :     serialize( message, writer );
      65          748 :     return size;
      66              : }
      67              : 
      68              : /**
      69              :  * @brief serialize message into json
      70              :  *
      71              :  * @param[in] message to be serialized
      72              :  * @return serialized json
      73              :  * @throws std::runtime_error on error
      74              :  * @example `auto serialized_message = spb::json::serialize( message );`
      75              :  */
      76              : template < spb::resizable_container Container = std::string, typename Message >
      77          749 : [[nodiscard]] static inline auto serialize( const Message & message ) -> Container
      78              : {
      79          749 :     auto result = Container( );
      80          749 :     serialize< Message, Container >( message, result );
      81          748 :     return result;
      82            1 : }
      83              : 
      84              : /**
      85              :  * @brief deserialize json-string into variable
      86              :  *
      87              :  * @param on_read function for handling reads
      88              :  * @param result deserialized json
      89              :  * @throws std::runtime_error on error
      90              :  */
      91         1212 : static inline void deserialize( auto & result, spb::io::reader on_read )
      92              : {
      93         1212 :     return detail::deserialize( result, on_read );
      94              : }
      95              : 
      96              : /**
      97              :  * @brief deserialize json-string into variable
      98              :  *
      99              :  * @param json string with json
     100              :  * @param message deserialized json
     101              :  * @throws std::runtime_error on error
     102              :  * @example `auto serialized = std::string( ... );`
     103              :  *          `auto message = Message();`
     104              :  *          `spb::json::deserialize( message, serialized );`
     105              :  */
     106              : template < typename Message, spb::size_container Container >
     107         1212 : static inline void deserialize( Message & message, const Container & json )
     108              : {
     109         3621 :     auto reader = [ ptr = json.data( ), end = json.data( ) + json.size( ) ](
     110              :                       void * data, size_t size ) mutable -> size_t
     111              :     {
     112         2409 :         size_t bytes_left = end - ptr;
     113              : 
     114         2409 :         size = std::min( size, bytes_left );
     115         2409 :         memcpy( data, ptr, size );
     116         2409 :         ptr += size;
     117         2409 :         return size;
     118              :     };
     119         2056 :     return deserialize( message, reader );
     120              : }
     121              : 
     122              : /**
     123              :  * @brief deserialize json-string into variable
     124              :  *
     125              :  * @param json string with json
     126              :  * @return deserialized json or throw an exception
     127              :  * @example `auto serialized = std::string( ... );`
     128              :  *          `auto message = spb::json::deserialize< Message >( serialized );`
     129              :  */
     130              : template < typename Message, spb::size_container Container >
     131          858 : [[nodiscard]] static inline auto deserialize( const Container & json ) -> Message
     132              : {
     133          858 :     auto message = Message{ };
     134          858 :     deserialize( message, json );
     135          490 :     return message;
     136          322 : }
     137              : 
     138              : /**
     139              :  * @brief deserialize json-string into variable
     140              :  *
     141              :  * @param on_read function for handling reads
     142              :  * @return deserialized json
     143              :  * @throws std::runtime_error on error
     144              :  * @example `auto message = spb::json::deserialize< Message >( reader )`
     145              :  */
     146              : template < typename Message >
     147              : [[nodiscard]] static inline auto deserialize( spb::io::reader on_read ) -> Message
     148              : {
     149              :     auto message = Message{ };
     150              :     return deserialize( message, on_read );
     151              : }
     152              : 
     153              : }// namespace spb::json
        

Generated by: LCOV version 2.0-1