LCOV - code coverage report
Current view: top level - io/include/json - json_writer.inl (source / functions) Coverage Total Hit
Test: crystal-facet-uml_v1.63.2_covts Lines: 62.2 % 45 28
Test Date: 2025-05-01 10:10:14 Functions: 66.7 % 6 4

            Line data    Source code
       1              : /* File: json_writer.inl; Copyright and License: see below */
       2              : 
       3              : #include <assert.h>
       4              : #include "utf8stringbuf/utf8error.h"
       5              : #include "utf8stringbuf/utf8codepoint.h"
       6              : #include "utf8stringbuf/utf8codepointiterator.h"
       7              : 
       8           90 : static inline u8_error_t json_writer_write_plain ( json_writer_t *this_, utf8string_t *text )
       9              : {
      10           90 :     assert ( UTF8STRING_NULL != text );
      11              :     u8_error_t write_err;
      12              : 
      13           90 :     const size_t text_len = utf8string_get_length(text);
      14           90 :     write_err = universal_output_stream_write ( (*this_).output, text, text_len );
      15              : 
      16           90 :     return ( write_err );
      17              : }
      18              : 
      19              : static inline u8_error_t json_writer_write_plain_view ( json_writer_t *this_, const utf8stringview_t *string_view )
      20              : {
      21              :     u8_error_t write_err;
      22              : 
      23              :     const size_t length = utf8stringview_get_length( string_view );
      24              :     const char *const start = utf8stringview_get_start( string_view );
      25              :     write_err = universal_output_stream_write( (*this_).output, start, length );
      26              : 
      27              :     return ( write_err );
      28              : }
      29              : 
      30           10 : static inline u8_error_t json_writer_write_string_enc ( json_writer_t *this_, utf8string_t *text )
      31              : {
      32           10 :     assert ( UTF8STRING_NULL != text );
      33              :     u8_error_t write_err;
      34              : 
      35           10 :     const size_t text_len = utf8string_get_length(text);
      36           10 :     universal_escaping_output_stream_change_rules( &((*this_).esc_output), (*this_).json_string_encode_table );
      37           10 :     write_err = universal_escaping_output_stream_write( &((*this_).esc_output), text, text_len );
      38           10 :     universal_escaping_output_stream_flush( &((*this_).esc_output) );
      39              : 
      40           10 :     return write_err;
      41              : }
      42              : 
      43              : static inline u8_error_t json_writer_write_string_view_enc ( json_writer_t *this_, const utf8stringview_t *string_view )
      44              : {
      45              :     u8_error_t write_err;
      46              : 
      47              :     const size_t length = utf8stringview_get_length( string_view );
      48              :     const char *const start = utf8stringview_get_start( string_view );
      49              :     universal_escaping_output_stream_change_rules( &((*this_).esc_output), (*this_).json_string_encode_table );
      50              :     write_err = universal_escaping_output_stream_write( &((*this_).esc_output), start, length );
      51              :     universal_escaping_output_stream_flush( &((*this_).esc_output) );
      52              : 
      53              :     return write_err;
      54              : }
      55              : 
      56            0 : static inline u8_error_t json_writer_write_stringlist_enc ( json_writer_t *this_, utf8string_t *text )
      57              : {
      58            0 :     assert ( UTF8STRING_NULL != text );
      59              :     u8_error_t write_err;
      60              : 
      61            0 :     const size_t text_len = utf8string_get_length(text);
      62            0 :     universal_escaping_output_stream_change_rules( &((*this_).esc_output), (*this_).json_stringlist_encode_table );
      63            0 :     write_err = universal_escaping_output_stream_write( &((*this_).esc_output), text, text_len );
      64            0 :     universal_escaping_output_stream_flush( &((*this_).esc_output) );
      65              : 
      66            0 :     return write_err;
      67              : }
      68              : 
      69              : static inline u8_error_t json_writer_write_stringlist_view_enc ( json_writer_t *this_, const utf8stringview_t *string_view )
      70              : {
      71              :     u8_error_t write_err;
      72              : 
      73              :     const size_t length = utf8stringview_get_length( string_view );
      74              :     const char *const start = utf8stringview_get_start( string_view );
      75              :     universal_escaping_output_stream_change_rules( &((*this_).esc_output), (*this_).json_stringlist_encode_table );
      76              :     write_err = universal_escaping_output_stream_write( &((*this_).esc_output), start, length );
      77              :     universal_escaping_output_stream_flush( &((*this_).esc_output) );
      78              : 
      79              :     return write_err;
      80              : }
      81              : 
      82            4 : static inline u8_error_t json_writer_write_member_int ( json_writer_t *this_,
      83              :                                                         unsigned int indent,
      84              :                                                         utf8string_t *enc_name,
      85              :                                                         int64_t number_value,
      86              :                                                         bool next_follows )
      87              : {
      88              :     assert( 7 == JSON_WRITER_MAX_INDENT );
      89            4 :     assert( indent <= JSON_WRITER_MAX_INDENT );
      90              :     u8_error_t write_err;
      91              : 
      92            4 :     write_err = json_writer_write_plain( this_, &(JSON_CONSTANTS_INDENT_QUOTE[2*(JSON_WRITER_MAX_INDENT-indent)]) );
      93            4 :     write_err |= json_writer_write_plain( this_, enc_name );
      94            4 :     write_err |= json_writer_write_plain( this_,
      95              :                                           JSON_CONSTANTS_QUOTE
      96              :                                           JSON_CONSTANTS_DEF
      97              :                                         );
      98            4 :     write_err |= json_writer_write_int( this_, number_value );
      99            4 :     write_err |= json_writer_write_plain( this_,
     100              :                                           next_follows
     101              :                                           ? JSON_CONSTANTS_NEXT_NL
     102              :                                           : JSON_CONSTANTS_NL
     103              :                                         );
     104              : 
     105            4 :     return write_err;
     106              : }
     107              : 
     108           10 : static inline u8_error_t json_writer_write_member_string ( json_writer_t *this_,
     109              :                                                            unsigned int indent,
     110              :                                                            utf8string_t *enc_name,
     111              :                                                            utf8string_t *unenc_value,
     112              :                                                            bool next_follows )
     113              : {
     114              :     assert( 7 == JSON_WRITER_MAX_INDENT );
     115           10 :     assert( indent <= JSON_WRITER_MAX_INDENT );
     116              :     u8_error_t write_err;
     117              : 
     118           10 :     write_err = json_writer_write_plain( this_, &(JSON_CONSTANTS_INDENT_QUOTE[2*(JSON_WRITER_MAX_INDENT-indent)]) );
     119           10 :     write_err |= json_writer_write_plain( this_, enc_name );
     120           10 :     write_err |= json_writer_write_plain( this_,
     121              :                                           JSON_CONSTANTS_QUOTE
     122              :                                           JSON_CONSTANTS_DEF
     123              :                                           JSON_CONSTANTS_QUOTE
     124              :                                         );
     125           10 :     write_err |= json_writer_write_string_enc( this_, unenc_value );
     126           10 :     write_err |= json_writer_write_plain( this_,
     127              :                                           next_follows
     128              :                                           ? JSON_CONSTANTS_QUOTE JSON_CONSTANTS_NEXT_NL
     129              :                                           : JSON_CONSTANTS_QUOTE JSON_CONSTANTS_NL
     130              :                                         );
     131              : 
     132           10 :     return write_err;
     133              : }
     134              : 
     135            0 : static inline u8_error_t json_writer_write_member_string_array ( json_writer_t *this_,
     136              :                                                                  unsigned int indent,
     137              :                                                                  utf8string_t *enc_name,
     138              :                                                                  utf8string_t *unenc_value,
     139              :                                                                  bool next_follows )
     140              : {
     141              :     assert( 7 == JSON_WRITER_MAX_INDENT );
     142            0 :     assert( indent <= JSON_WRITER_MAX_INDENT );
     143              :     u8_error_t write_err;
     144              : 
     145            0 :     write_err = json_writer_write_plain( this_, &(JSON_CONSTANTS_INDENT_QUOTE[2*(JSON_WRITER_MAX_INDENT-indent)]) );
     146            0 :     write_err |= json_writer_write_plain( this_, enc_name );
     147            0 :     write_err |= json_writer_write_plain( this_,
     148              :                                           JSON_CONSTANTS_QUOTE
     149              :                                           JSON_CONSTANTS_DEF
     150              :                                           JSON_CONSTANTS_BEGIN_ARRAY
     151              :                                           JSON_CONSTANTS_NL
     152              :                                           JSON_CONSTANTS_TAB
     153              :                                           JSON_CONSTANTS_TAB
     154              :                                           JSON_CONSTANTS_TAB
     155              :                                           JSON_CONSTANTS_TAB
     156              :                                           JSON_CONSTANTS_TAB
     157              :                                           JSON_CONSTANTS_TAB
     158              :                                           JSON_CONSTANTS_TAB
     159              :                                           JSON_CONSTANTS_TAB
     160              :                                           JSON_CONSTANTS_QUOTE
     161              :                                         );
     162            0 :     write_err |= json_writer_write_stringlist_enc( this_, unenc_value );
     163            0 :     write_err |= json_writer_write_plain( this_, JSON_CONSTANTS_QUOTE JSON_CONSTANTS_NL );
     164            0 :     write_err = json_writer_write_plain( this_, &(JSON_CONSTANTS_INDENT[2*(JSON_WRITER_MAX_INDENT-indent)]) );
     165            0 :     write_err |= json_writer_write_plain( this_,
     166              :                                           next_follows
     167              :                                           ? JSON_CONSTANTS_END_ARRAY JSON_CONSTANTS_NEXT_NL
     168              :                                           : JSON_CONSTANTS_END_ARRAY JSON_CONSTANTS_NL
     169              :                                         );
     170              : 
     171            0 :     return write_err;
     172              : }
     173              : 
     174              : 
     175              : /*
     176              : Copyright 2021-2025 Andreas Warnke
     177              : 
     178              : Licensed under the Apache License, Version 2.0 (the "License");
     179              : you may not use this file except in compliance with the License.
     180              : You may obtain a copy of the License at
     181              : 
     182              :     http://www.apache.org/licenses/LICENSE-2.0
     183              : 
     184              : Unless required by applicable law or agreed to in writing, software
     185              : distributed under the License is distributed on an "AS IS" BASIS,
     186              : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     187              : See the License for the specific language governing permissions and
     188              : limitations under the License.
     189              : */
        

Generated by: LCOV version 2.0-1