File tree Expand file tree Collapse file tree 2 files changed +57
-1
lines changed Expand file tree Collapse file tree 2 files changed +57
-1
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,8 @@ use crate::core::{
25
25
use crate :: deprecated_contract_class:: ContractClass as DeprecatedContractClass ;
26
26
use crate :: hash:: { PoseidonHash , StarkHash } ;
27
27
use crate :: rpc_transaction:: EntryPointByType ;
28
+ #[ cfg( any( test, feature = "testing" ) ) ]
29
+ use crate :: test_utils:: py_json_dumps;
28
30
use crate :: { impl_from_through_intermediate, StarknetApiError , StarknetApiResult } ;
29
31
30
32
pub type DeclaredClasses = IndexMap < ClassHash , SierraContractClass > ;
@@ -311,7 +313,10 @@ impl From<cairo_lang_starknet_classes::contract_class::ContractClass> for Sierra
311
313
. collect ( ) ,
312
314
contract_class_version : cairo_lang_contract_class. contract_class_version ,
313
315
entry_points_by_type : cairo_lang_contract_class. entry_points_by_type . into ( ) ,
314
- abi : cairo_lang_contract_class. abi . map ( |abi| abi. json ( ) ) . unwrap_or_default ( ) ,
316
+ abi : cairo_lang_contract_class
317
+ . abi
318
+ . map ( |abi| py_json_dumps ( & abi) . expect ( "ABI is valid JSON" ) )
319
+ . unwrap_or_default ( ) ,
315
320
}
316
321
}
317
322
}
Original file line number Diff line number Diff line change @@ -212,3 +212,54 @@ impl ContractClass {
212
212
ContractClass :: V1 ( ( default_casm, SierraVersion :: default ( ) ) )
213
213
}
214
214
}
215
+
216
+ /// Formats a json object in the same way that python's json.dumps() formats.
217
+ pub ( crate ) struct PyJsonFormatter ;
218
+
219
+ impl PyJsonFormatter {
220
+ pub ( crate ) fn comma ( ) -> & ' static [ u8 ; 2 ] {
221
+ b", "
222
+ }
223
+
224
+ pub ( crate ) fn colon ( ) -> & ' static [ u8 ; 2 ] {
225
+ b": "
226
+ }
227
+ }
228
+
229
+ impl serde_json:: ser:: Formatter for PyJsonFormatter {
230
+ fn begin_array_value < W : ?Sized + std:: io:: Write > (
231
+ & mut self ,
232
+ writer : & mut W ,
233
+ first : bool ,
234
+ ) -> std:: io:: Result < ( ) > {
235
+ if !first {
236
+ writer. write_all ( Self :: comma ( ) ) ?;
237
+ }
238
+ Ok ( ( ) )
239
+ }
240
+
241
+ fn begin_object_key < W : ?Sized + std:: io:: Write > (
242
+ & mut self ,
243
+ writer : & mut W ,
244
+ first : bool ,
245
+ ) -> std:: io:: Result < ( ) > {
246
+ if !first {
247
+ writer. write_all ( Self :: comma ( ) ) ?;
248
+ }
249
+ Ok ( ( ) )
250
+ }
251
+
252
+ fn begin_object_value < W : ?Sized + std:: io:: Write > (
253
+ & mut self ,
254
+ writer : & mut W ,
255
+ ) -> std:: io:: Result < ( ) > {
256
+ writer. write_all ( Self :: colon ( ) )
257
+ }
258
+ }
259
+
260
+ pub ( crate ) fn py_json_dumps < T : ?Sized + Serialize > ( value : & T ) -> Result < String , serde_json:: Error > {
261
+ let mut string_buffer = vec ! [ ] ;
262
+ let mut ser = serde_json:: Serializer :: with_formatter ( & mut string_buffer, PyJsonFormatter ) ;
263
+ value. serialize ( & mut ser) ?;
264
+ Ok ( String :: from_utf8 ( string_buffer) . expect ( "serialized JSON should be valid UTF-8" ) )
265
+ }
You can’t perform that action at this time.
0 commit comments