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 0 : 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 0 : const size_t length = utf8stringview_get_length( string_view );
48 0 : const char *const start = utf8stringview_get_start( string_view );
49 0 : universal_escaping_output_stream_change_rules( &((*this_).esc_output), (*this_).json_string_encode_table );
50 0 : write_err = universal_escaping_output_stream_write( &((*this_).esc_output), start, length );
51 0 : universal_escaping_output_stream_flush( &((*this_).esc_output) );
52 :
53 0 : return write_err;
54 : }
55 :
56 4 : static inline u8_error_t json_writer_write_member_int ( json_writer_t *this_,
57 : unsigned int indent,
58 : utf8string_t *enc_name,
59 : int64_t number_value,
60 : bool next_follows )
61 : {
62 : assert( 7 == JSON_WRITER_MAX_INDENT );
63 4 : assert( indent <= JSON_WRITER_MAX_INDENT );
64 : u8_error_t write_err;
65 :
66 4 : write_err = json_writer_write_plain( this_, &(JSON_CONSTANTS_INDENT_QUOTE[2*(JSON_WRITER_MAX_INDENT-indent)]) );
67 4 : write_err |= json_writer_write_plain( this_, enc_name );
68 4 : write_err |= json_writer_write_plain( this_,
69 : JSON_CONSTANTS_QUOTE
70 : JSON_CONSTANTS_DEF
71 : );
72 4 : write_err |= json_writer_write_int( this_, number_value );
73 4 : write_err |= json_writer_write_plain( this_,
74 : next_follows
75 : ? JSON_CONSTANTS_NEXT_NL
76 : : JSON_CONSTANTS_NL
77 : );
78 :
79 4 : return write_err;
80 : }
81 :
82 10 : static inline u8_error_t json_writer_write_member_string ( json_writer_t *this_,
83 : unsigned int indent,
84 : utf8string_t *enc_name,
85 : utf8string_t *unenc_value,
86 : bool next_follows )
87 : {
88 : assert( 7 == JSON_WRITER_MAX_INDENT );
89 10 : assert( indent <= JSON_WRITER_MAX_INDENT );
90 : u8_error_t write_err;
91 :
92 10 : write_err = json_writer_write_plain( this_, &(JSON_CONSTANTS_INDENT_QUOTE[2*(JSON_WRITER_MAX_INDENT-indent)]) );
93 10 : write_err |= json_writer_write_plain( this_, enc_name );
94 10 : write_err |= json_writer_write_plain( this_,
95 : JSON_CONSTANTS_QUOTE
96 : JSON_CONSTANTS_DEF
97 : JSON_CONSTANTS_QUOTE
98 : );
99 10 : write_err |= json_writer_write_string_enc( this_, unenc_value );
100 10 : write_err |= json_writer_write_plain( this_,
101 : next_follows
102 : ? JSON_CONSTANTS_QUOTE JSON_CONSTANTS_NEXT_NL
103 : : JSON_CONSTANTS_QUOTE JSON_CONSTANTS_NL
104 : );
105 :
106 10 : return write_err;
107 : }
108 :
109 0 : static inline u8_error_t json_writer_write_member_enum ( json_writer_t *this_,
110 : unsigned int indent,
111 : utf8string_t *enc_name_1,
112 : utf8string_t *unenc_value_1,
113 : utf8string_t *enc_name_2,
114 : int64_t number_value_2,
115 : bool next_follows )
116 : {
117 : assert( 7 == JSON_WRITER_MAX_INDENT );
118 0 : assert( indent <= JSON_WRITER_MAX_INDENT );
119 : u8_error_t write_err;
120 :
121 0 : write_err = json_writer_write_plain( this_, &(JSON_CONSTANTS_INDENT_QUOTE[2*(JSON_WRITER_MAX_INDENT-indent)]) );
122 0 : write_err |= json_writer_write_plain( this_, enc_name_1 );
123 0 : write_err |= json_writer_write_plain( this_,
124 : JSON_CONSTANTS_QUOTE
125 : JSON_CONSTANTS_DEF
126 : JSON_CONSTANTS_QUOTE
127 : );
128 0 : write_err |= json_writer_write_string_enc( this_, unenc_value_1 );
129 0 : write_err |= json_writer_write_plain( this_,
130 : JSON_CONSTANTS_QUOTE
131 : JSON_CONSTANTS_NEXT
132 : JSON_CONSTANTS_QUOTE
133 : );
134 0 : write_err |= json_writer_write_plain( this_, enc_name_2 );
135 0 : write_err |= json_writer_write_plain( this_,
136 : JSON_CONSTANTS_QUOTE
137 : JSON_CONSTANTS_DEF
138 : );
139 0 : write_err |= json_writer_write_int( this_, number_value_2 );
140 0 : write_err |= json_writer_write_plain( this_,
141 : next_follows
142 : ? JSON_CONSTANTS_NEXT_NL
143 : : JSON_CONSTANTS_NL
144 : );
145 :
146 0 : return write_err;
147 : }
148 :
149 :
150 : /*
151 : Copyright 2021-2025 Andreas Warnke
152 :
153 : Licensed under the Apache License, Version 2.0 (the "License");
154 : you may not use this file except in compliance with the License.
155 : You may obtain a copy of the License at
156 :
157 : http://www.apache.org/licenses/LICENSE-2.0
158 :
159 : Unless required by applicable law or agreed to in writing, software
160 : distributed under the License is distributed on an "AS IS" BASIS,
161 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
162 : See the License for the specific language governing permissions and
163 : limitations under the License.
164 : */
|