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 0 : static inline u8_error_t json_writer_write_plain ( json_writer_t *this_, utf8string_t *text )
9 : {
10 0 : assert ( UTF8STRING_NULL != text );
11 : u8_error_t write_err;
12 :
13 0 : const size_t text_len = utf8string_get_length(text);
14 0 : write_err = universal_output_stream_write ( (*this_).output, text, text_len );
15 :
16 0 : 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 0 : static inline u8_error_t json_writer_write_string_enc ( json_writer_t *this_, utf8string_t *text )
31 : {
32 0 : assert ( UTF8STRING_NULL != text );
33 : u8_error_t write_err;
34 :
35 0 : const size_t text_len = utf8string_get_length(text);
36 0 : universal_escaping_output_stream_change_rules( &((*this_).esc_output), (*this_).json_string_encode_table );
37 0 : write_err = universal_escaping_output_stream_write( &((*this_).esc_output), text, text_len );
38 0 : universal_escaping_output_stream_flush( &((*this_).esc_output) );
39 :
40 0 : 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 0 : 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 0 : assert( indent <= JSON_WRITER_MAX_INDENT );
90 : u8_error_t write_err;
91 :
92 0 : write_err = json_writer_write_plain( this_, &(JSON_CONSTANTS_INDENT_QUOTE[2*(JSON_WRITER_MAX_INDENT-indent)]) );
93 0 : write_err |= json_writer_write_plain( this_, enc_name );
94 0 : write_err |= json_writer_write_plain( this_,
95 : JSON_CONSTANTS_QUOTE
96 : JSON_CONSTANTS_DEF
97 : );
98 0 : write_err |= json_writer_write_int( this_, number_value );
99 0 : write_err |= json_writer_write_plain( this_,
100 : next_follows
101 : ? JSON_CONSTANTS_NEXT_NL
102 : : JSON_CONSTANTS_NL
103 : );
104 :
105 0 : return write_err;
106 : }
107 :
108 0 : 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 0 : assert( indent <= JSON_WRITER_MAX_INDENT );
116 : u8_error_t write_err;
117 :
118 0 : write_err = json_writer_write_plain( this_, &(JSON_CONSTANTS_INDENT_QUOTE[2*(JSON_WRITER_MAX_INDENT-indent)]) );
119 0 : write_err |= json_writer_write_plain( this_, enc_name );
120 0 : write_err |= json_writer_write_plain( this_,
121 : JSON_CONSTANTS_QUOTE
122 : JSON_CONSTANTS_DEF
123 : JSON_CONSTANTS_QUOTE
124 : );
125 0 : write_err |= json_writer_write_string_enc( this_, unenc_value );
126 0 : 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 0 : 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-2024 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 : */
|