@@ -131,55 +131,77 @@ export const getFileExtension = (fileName: string) => {
131
131
} ;
132
132
133
133
// eslint-disable-next-line complexity, @typescript-eslint/no-explicit-any
134
- export const convertToText = ( obj : any ) : string => {
135
- //create an array that will later be joined into a string.
136
- const string = [ ] ;
134
+ export type GetterJSONReponse =
135
+ | string
136
+ | number
137
+ | boolean
138
+ | null
139
+ | GetterJSONReponse [ ]
140
+ | { [ key : string ] : GetterJSONReponse } ;
137
141
142
+ /**
143
+ * Converts various types of objects, including custom types, arrays, dictionaries, and primitives, into a JSON-compatible format.
144
+ *
145
+ * This function is particularly useful when dealing with complex data structures that need to be serialized into a JSON format.
146
+ * It handles different types, including custom objects like `Address`, `Slice`, `Cell`, and `Dictionary`, converting them into strings.
147
+ * Arrays and objects are recursively converted, ensuring that all nested structures are correctly transformed.
148
+ *
149
+ * @param obj - The object or value to be converted to a JSON-compatible format. It can be of any type.
150
+ * @returns A JSON-compatible value (`string`, `number`, `boolean`, `null`, array, or object), or `null` if the input is `undefined` or `null`.
151
+ */
152
+ export const serializeToJSONFormat = (
153
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
154
+ obj : any ,
155
+ ) : GetterJSONReponse | GetterJSONReponse [ ] | null => {
138
156
//is object
139
157
// Both arrays and objects seem to return "object"
140
158
// when typeof(obj) is applied to them. So instead
141
159
// I am checking to see if they have the property
142
160
// join, which normal objects don't have but
143
161
// arrays do.
144
- if ( obj == undefined ) {
145
- return String ( obj ) ;
146
- } else if ( typeof obj == 'object' && obj . join == undefined ) {
162
+ if ( obj === undefined || obj === null ) {
163
+ return null ;
164
+ }
165
+
166
+ if ( typeof obj === 'object' && obj . join === undefined ) {
147
167
if ( obj instanceof Address ) return obj . toString ( ) ;
148
168
if ( obj instanceof Slice ) return obj . toString ( ) ;
149
169
if ( obj instanceof Cell ) return obj . toString ( ) ;
150
170
if ( obj instanceof Dictionary ) {
151
- const items = [ ] ;
152
- for ( const key of obj . keys ( ) )
153
- items . push ( `${ convertToText ( key ) } : ${ convertToText ( obj . get ( key ) ) } ` ) ;
154
- const itemsStr = items . join ( ', ' ) ;
155
- return itemsStr ? `{ ${ itemsStr } }` : `{}` ;
171
+ const resultDict : Record < string , GetterJSONReponse > = { } ;
172
+ for ( const key of obj . keys ( ) ) {
173
+ const jsonKey = serializeToJSONFormat ( key ) ;
174
+ if ( typeof jsonKey === 'string' ) {
175
+ resultDict [ jsonKey ] = serializeToJSONFormat ( obj . get ( key ) ) ;
176
+ }
177
+ }
178
+ return resultDict ;
156
179
}
157
180
158
- for ( const prop in obj ) {
159
- if ( Object . prototype . hasOwnProperty . call ( obj , prop ) )
160
- string . push ( prop + ': ' + convertToText ( obj [ prop ] ) ) ;
181
+ if ( Array . isArray ( obj ) ) {
182
+ return obj . map ( ( item ) => serializeToJSONFormat ( item ) ) ;
161
183
}
162
- return '{' + string . join ( ', ' ) + '}' ;
163
184
164
- //is array
165
- } else if ( typeof obj == 'object' && ! ( obj . join == undefined ) ) {
185
+ const resultObj : Record < string , GetterJSONReponse > = { } ;
166
186
for ( const prop in obj ) {
167
- string . push ( convertToText ( obj [ prop ] ) ) ;
187
+ if ( Object . prototype . hasOwnProperty . call ( obj , prop ) ) {
188
+ resultObj [ prop ] = serializeToJSONFormat (
189
+ ( obj as Record < string , unknown > ) [ prop ] ,
190
+ ) ;
191
+ }
168
192
}
169
- return '[' + string . join ( ',' ) + ']' ;
193
+ return resultObj ;
194
+ }
170
195
171
- //is function
172
- } else if ( typeof obj == 'function' ) {
173
- string . push ( obj . toString ( ) ) ;
196
+ if ( typeof obj === ' function' ) {
197
+ return obj . toString ( ) ;
198
+ }
174
199
175
- //all other values can be done with JSON.stringify
176
- } else {
177
- if ( typeof obj == 'string' ) string . push ( JSON . stringify ( obj ) ) ;
178
- else if ( typeof obj == 'bigint' ) string . push ( obj . toString ( ) ) ;
179
- else string . push ( obj . toString ( ) ) ;
200
+ if ( typeof obj === 'string' || typeof obj === 'bigint' ) {
201
+ return obj . toString ( ) ;
180
202
}
181
203
182
- return string . join ( ',' ) ;
204
+ return obj as GetterJSONReponse ;
183
205
} ;
184
206
185
207
export const tonHttpEndpoint = ( { network } : Config ) => {
0 commit comments