LCOV - code coverage report
Current view: top level - io/source/json - json_writer.c (source / functions) Hit Total Coverage
Test: crystal-facet-uml_v1.61.0_covts Lines: 0 35 0.0 %
Date: 2024-10-26 21:44:38 Functions: 0 4 0.0 %

          Line data    Source code
       1             : /* File: json_writer.c; Copyright and License: see below */
       2             : 
       3             : #include "json/json_writer.h"
       4             : #include "entity/data_id.h"
       5             : #include "u8/u8_trace.h"
       6             : #include "u8/u8_log.h"
       7             : #include <stdio.h>
       8             : #include <stdbool.h>
       9             : #include <assert.h>
      10             : 
      11             : static const char * const JSON_WRITER_PRIVATE_ENCODE_JSON_STRINGS[][2] = {
      12             :     { "\x09", "\\t" },  /* tab */
      13             :     { "\x0a", "\\n" },  /* newline */
      14             :     { "\x0d", "\\r" },  /* return */
      15             :     { "\x08", "\\b" },  /* backspace */
      16             :     { "\x0c", "\\f" },  /* form feed */
      17             :     { "\"", "\\\"" },  /* double quote */
      18             :     { "\\", "\\\\" },  /* backslash*/
      19             :     { NULL, NULL }  /* for JSON, see rfc7159 */
      20             : };
      21             : 
      22             : static const char * const JSON_WRITER_PRIVATE_ENCODE_JSON_STRING_ARRAYS[][2] = {
      23             :     { "\x09", "\\t" },  /* tab */
      24             :     { "\x0a", "\\n\",\n                \"" },  /* newline */
      25             :     { "\x0d", "\\r" },  /* return */
      26             :     { "\x08", "\\b" },  /* backspace */
      27             :     { "\x0c", "\\f" },  /* form feed */
      28             :     { "\"", "\\\"" },  /* double quote */
      29             :     { "\\", "\\\\" },  /* backslash*/
      30             :     { NULL, NULL }  /* for JSON, see rfc7159 */
      31             : };
      32             : 
      33             : const char JSON_CONSTANTS_INDENT[(2*JSON_WRITER_MAX_INDENT)+sizeof("")]
      34             :     = JSON_CONSTANTS_TAB JSON_CONSTANTS_TAB JSON_CONSTANTS_TAB JSON_CONSTANTS_TAB
      35             :       JSON_CONSTANTS_TAB JSON_CONSTANTS_TAB JSON_CONSTANTS_TAB;
      36             : 
      37             : const char JSON_CONSTANTS_INDENT_QUOTE[(2*JSON_WRITER_MAX_INDENT)+sizeof(JSON_CONSTANTS_QUOTE)]
      38             :     = JSON_CONSTANTS_TAB JSON_CONSTANTS_TAB JSON_CONSTANTS_TAB JSON_CONSTANTS_TAB
      39             :       JSON_CONSTANTS_TAB JSON_CONSTANTS_TAB JSON_CONSTANTS_TAB JSON_CONSTANTS_QUOTE;
      40             : 
      41           0 : void json_writer_init( json_writer_t *this_,
      42             :                        universal_output_stream_t *output )
      43             : {
      44           0 :     U8_TRACE_BEGIN();
      45           0 :     assert( NULL != output );
      46             : 
      47           0 :     (*this_).output = output;
      48           0 :     universal_escaping_output_stream_init( &((*this_).esc_output), &JSON_WRITER_PRIVATE_ENCODE_JSON_STRINGS, output );
      49             : 
      50           0 :     (*this_).json_string_encode_table = &JSON_WRITER_PRIVATE_ENCODE_JSON_STRINGS;
      51           0 :     (*this_).json_stringlist_encode_table = &JSON_WRITER_PRIVATE_ENCODE_JSON_STRING_ARRAYS;
      52             : 
      53           0 :     U8_TRACE_END();
      54           0 : }
      55             : 
      56           0 : void json_writer_destroy( json_writer_t *this_ )
      57             : {
      58           0 :     U8_TRACE_BEGIN();
      59             : 
      60           0 :     universal_escaping_output_stream_destroy( &((*this_).esc_output) );
      61           0 :     (*this_).output = NULL;
      62             : 
      63           0 :     U8_TRACE_END();
      64           0 : }
      65             : 
      66           0 : u8_error_t json_writer_write_plain_id ( json_writer_t *this_, data_id_t id )
      67             : {
      68           0 :     U8_TRACE_BEGIN();
      69           0 :     assert( DATA_TABLE_VOID != data_id_get_table(&id) );
      70           0 :     assert( DATA_ROW_ID_VOID != data_id_get_row_id(&id) );
      71           0 :     u8_error_t result = U8_ERROR_NONE;
      72             : 
      73             :     /* print id */
      74             :     {
      75             :         char id_buf[DATA_ID_MAX_UTF8STRING_SIZE];
      76           0 :         utf8stringbuf_t id_str = UTF8STRINGBUF( id_buf );
      77           0 :         utf8stringbuf_clear( id_str );
      78           0 :         data_id_to_utf8stringbuf( &id, id_str );
      79             : 
      80           0 :         const unsigned int len = utf8stringbuf_get_length(id_str);
      81           0 :         universal_escaping_output_stream_change_rules( &((*this_).esc_output), (*this_).json_string_encode_table );
      82           0 :         result = universal_escaping_output_stream_write( &((*this_).esc_output), utf8stringbuf_get_string(id_str), len );
      83             :     }
      84             : 
      85           0 :     U8_TRACE_END_ERR( result );
      86           0 :     return result;
      87             : }
      88             : 
      89           0 : u8_error_t json_writer_write_int ( json_writer_t *this_, int64_t number )
      90             : {
      91           0 :     U8_TRACE_BEGIN();
      92             :     char numberStr[21]; /* this is sufficient for signed 64 bit integers: -9223372036854775806 */
      93           0 :     u8_error_t result = U8_ERROR_NONE;
      94             : 
      95             :     /* Note: snprintf is not available on every OS */
      96           0 :     sprintf( numberStr, "%" PRIi64, number );
      97           0 :     result = json_writer_write_plain( this_, &(numberStr[0]) );
      98             : 
      99           0 :     U8_TRACE_END_ERR( result );
     100           0 :     return result;
     101             : }
     102             : 
     103             : 
     104             : /*
     105             : Copyright 2021-2024 Andreas Warnke
     106             : 
     107             : Licensed under the Apache License, Version 2.0 (the "License");
     108             : you may not use this file except in compliance with the License.
     109             : You may obtain a copy of the License at
     110             : 
     111             :     http://www.apache.org/licenses/LICENSE-2.0
     112             : 
     113             : Unless required by applicable law or agreed to in writing, software
     114             : distributed under the License is distributed on an "AS IS" BASIS,
     115             : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     116             : See the License for the specific language governing permissions and
     117             : limitations under the License.
     118             : */

Generated by: LCOV version 1.16