forked from SAP/abap-to-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathz_ui2_json.clas.abap
2672 lines (2325 loc) · 98.9 KB
/
z_ui2_json.clas.abap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
*----------------------------------------------------------------------*
* CLASS Z_UI2_JSON DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
class Z_UI2_JSON definition
public
create public .
public section.
type-pools ABAP .
class CL_ABAP_TSTMP definition load .
class CX_SY_CONVERSION_ERROR definition load .
types JSON type STRING .
types:
BEGIN OF name_mapping,
abap TYPE abap_compname,
json TYPE string,
END OF name_mapping .
types:
name_mappings TYPE HASHED TABLE OF name_mapping WITH UNIQUE KEY abap .
types:
ref_tab TYPE STANDARD TABLE OF REF TO data WITH DEFAULT KEY .
types BOOL type CHAR1 .
types TRIBOOL type CHAR1 .
types PRETTY_NAME_MODE type CHAR1 .
constants:
BEGIN OF pretty_mode,
none TYPE char1 VALUE ``,
low_case TYPE char1 VALUE 'L',
camel_case TYPE char1 VALUE 'X',
pascal_case TYPE char1 VALUE 'P',
extended TYPE char1 VALUE 'Y',
user TYPE char1 VALUE 'U',
user_low_case TYPE char1 VALUE 'C',
END OF pretty_mode .
constants:
BEGIN OF c_bool,
true TYPE bool VALUE 'X',
false TYPE bool VALUE '',
END OF c_bool .
constants:
BEGIN OF c_tribool,
true TYPE tribool VALUE c_bool-true,
false TYPE tribool VALUE '-',
undefined TYPE tribool VALUE ``,
END OF c_tribool .
class-data SV_WHITE_SPACE type STRING read-only .
constants MC_KEY_SEPARATOR type STRING value `-` ##NO_TEXT.
class-data MC_BOOL_TYPES type STRING read-only value `\TYPE-POOL=ABAP\TYPE=ABAP_BOOL\TYPE=BOOLEAN\TYPE=BOOLE_D\TYPE=XFELD\TYPE=XSDBOOLEAN\TYPE=WDY_BOOLEAN` ##NO_TEXT.
class-data MC_BOOL_3STATE type STRING read-only value `\TYPE=BOOLEAN` ##NO_TEXT.
constants VERSION type I value 20 ##NO_TEXT.
class-data MC_JSON_TYPE type STRING read-only .
class-methods CLASS_CONSTRUCTOR .
class-methods STRING_TO_XSTRING
importing
!IN type STRING
changing
value(OUT) type ANY .
class-methods XSTRING_TO_STRING
importing
!IN type ANY
returning
value(OUT) type STRING .
class-methods RAW_TO_STRING
importing
!IV_XSTRING type XSTRING
!IV_ENCODING type ABAP_ENCODING optional
returning
value(RV_STRING) type STRING .
class-methods STRING_TO_RAW
importing
!IV_STRING type STRING
!IV_ENCODING type ABAP_ENCODING optional
returning
value(RV_XSTRING) type XSTRING .
class-methods DUMP
importing
!DATA type DATA
!COMPRESS type BOOL default C_BOOL-FALSE
!TYPE_DESCR type ref to CL_ABAP_TYPEDESCR optional
!PRETTY_NAME type PRETTY_NAME_MODE default PRETTY_MODE-NONE
!ASSOC_ARRAYS type BOOL default C_BOOL-FALSE
!TS_AS_ISO8601 type BOOL default C_BOOL-FALSE
returning
value(R_JSON) type JSON .
class-methods DESERIALIZE
importing
!JSON type JSON optional
!JSONX type XSTRING optional
!PRETTY_NAME type PRETTY_NAME_MODE default PRETTY_MODE-NONE
!ASSOC_ARRAYS type BOOL default C_BOOL-FALSE
!ASSOC_ARRAYS_OPT type BOOL default C_BOOL-FALSE
!NAME_MAPPINGS type NAME_MAPPINGS optional
!CONVERSION_EXITS type BOOL default C_BOOL-FALSE
!HEX_AS_BASE64 type BOOL default C_BOOL-TRUE
!GEN_OPTIMIZE type BOOL default C_BOOL-FALSE
changing
!DATA type DATA .
class-methods SERIALIZE
importing
!DATA type DATA
!COMPRESS type BOOL default C_BOOL-FALSE
!NAME type STRING optional
!PRETTY_NAME type PRETTY_NAME_MODE default PRETTY_MODE-NONE
!TYPE_DESCR type ref to CL_ABAP_TYPEDESCR optional
!ASSOC_ARRAYS type BOOL default C_BOOL-FALSE
!TS_AS_ISO8601 type BOOL default C_BOOL-FALSE
!EXPAND_INCLUDES type BOOL default C_BOOL-TRUE
!ASSOC_ARRAYS_OPT type BOOL default C_BOOL-FALSE
!NUMC_AS_STRING type BOOL default C_BOOL-FALSE
!NAME_MAPPINGS type NAME_MAPPINGS optional
!CONVERSION_EXITS type BOOL default C_BOOL-FALSE
!FORMAT_OUTPUT type BOOL default C_BOOL-FALSE
!HEX_AS_BASE64 type BOOL default C_BOOL-TRUE
returning
value(R_JSON) type JSON .
methods DESERIALIZE_INT
importing
!JSON type JSON optional
!JSONX type XSTRING optional
changing
!DATA type DATA
raising
CX_SY_MOVE_CAST_ERROR .
class-methods GENERATE
importing
!JSON type JSON optional
!PRETTY_NAME type PRETTY_NAME_MODE default PRETTY_MODE-NONE
!OPTIMIZE type BOOL default C_BOOL-FALSE
!NAME_MAPPINGS type NAME_MAPPINGS optional
!JSONX type XSTRING optional
preferred parameter JSON
returning
value(RR_DATA) type ref to DATA .
methods SERIALIZE_INT
importing
!DATA type DATA
!NAME type STRING optional
!TYPE_DESCR type ref to CL_ABAP_TYPEDESCR optional
returning
value(R_JSON) type JSON .
methods GENERATE_INT
importing
!JSON type JSON
value(LENGTH) type I optional
changing
!DATA type ref to DATA
!TYPE type ref to CL_ABAP_DATADESCR optional
!OFFSET type I default 0
raising
CX_SY_MOVE_CAST_ERROR .
methods CONSTRUCTOR
importing
!COMPRESS type BOOL default C_BOOL-FALSE
!PRETTY_NAME type PRETTY_NAME_MODE default PRETTY_MODE-NONE
!ASSOC_ARRAYS type BOOL default C_BOOL-FALSE
!TS_AS_ISO8601 type BOOL default C_BOOL-FALSE
!EXPAND_INCLUDES type BOOL default C_BOOL-TRUE
!ASSOC_ARRAYS_OPT type BOOL default C_BOOL-FALSE
!STRICT_MODE type BOOL default C_BOOL-FALSE
!NUMC_AS_STRING type BOOL default C_BOOL-FALSE
!NAME_MAPPINGS type NAME_MAPPINGS optional
!CONVERSION_EXITS type BOOL default C_BOOL-FALSE
!FORMAT_OUTPUT type BOOL default C_BOOL-FALSE
!HEX_AS_BASE64 type BOOL default C_BOOL-TRUE
!GEN_OPTIMIZE type BOOL default C_BOOL-FALSE
!BOOL_TYPES type STRING default MC_BOOL_TYPES
!BOOL_3STATE type STRING default MC_BOOL_3STATE
!INITIAL_TS type STRING default `""`
!INITIAL_DATE type STRING default `""`
!INITIAL_TIME type STRING default `""`
!TIME_ZONE like SY-ZONLO default 'UTC' .
class-methods BOOL_TO_TRIBOOL
importing
!IV_BOOL type BOOL
returning
value(RV_TRIBOOL) type TRIBOOL .
class-methods TRIBOOL_TO_BOOL
importing
!IV_TRIBOOL type TRIBOOL
returning
value(RV_BOOL) type BOOL .
protected section.
types:
BEGIN OF t_s_field_cache,
name TYPE string,
type TYPE REF TO cl_abap_datadescr,
elem_type TYPE REF TO cl_abap_elemdescr,
typekind TYPE abap_typekind,
convexit_out TYPE string,
convexit_in TYPE string,
value TYPE REF TO data,
END OF t_s_field_cache .
types:
BEGIN OF t_s_symbol,
header TYPE string,
compressable TYPE abap_bool,
read_only TYPE abap_bool.
INCLUDE TYPE t_s_field_cache.
TYPES: END OF t_s_symbol .
types:
t_t_symbol TYPE STANDARD TABLE OF t_s_symbol WITH DEFAULT KEY .
types:
t_t_field_cache TYPE HASHED TABLE OF t_s_field_cache WITH UNIQUE KEY name .
types:
name_mappings_ex TYPE HASHED TABLE OF name_mapping WITH UNIQUE KEY json .
types:
BEGIN OF t_s_name_json,
name TYPE string,
value TYPE json,
END OF t_s_name_json .
types:
t_t_name_json TYPE SORTED TABLE OF t_s_name_json WITH UNIQUE KEY name .
types:
BEGIN OF t_s_name_value,
name TYPE string,
value TYPE json,
data TYPE REF TO data,
type TYPE REF TO cl_abap_datadescr,
END OF t_s_name_value .
types:
t_t_name_value TYPE SORTED TABLE OF t_s_name_value WITH UNIQUE KEY name .
types:
t_t_json TYPE STANDARD TABLE OF json WITH DEFAULT KEY .
types:
BEGIN OF t_s_struct_type,
keys TYPE string,
type TYPE REF TO cl_abap_datadescr,
END OF t_s_struct_type .
types:
t_t_struct_type TYPE SORTED TABLE OF t_s_struct_type WITH UNIQUE KEY keys .
types:
BEGIN OF t_s_struct_cache_res,
data TYPE REF TO data,
symbols TYPE t_t_symbol,
END OF t_s_struct_cache_res .
types:
BEGIN OF t_s_struct_cache,
type_descr TYPE REF TO cl_abap_structdescr,
include_aliases TYPE abap_bool,
level TYPE i,
result TYPE t_s_struct_cache_res,
END OF t_s_struct_cache .
types:
t_t_struct_cache TYPE HASHED TABLE OF t_s_struct_cache WITH UNIQUE KEY type_descr include_aliases level .
data MV_BOOL_TYPES type STRING .
data MV_BOOL_3STATE type STRING .
data MV_INITIAL_TS type STRING value `""` ##NO_TEXT.
data MV_INITIAL_DATE type STRING value `""` ##NO_TEXT.
data MV_INITIAL_TIME type STRING value `""` ##NO_TEXT.
data MV_TIME_ZONE type TIMEZONE value `UTC` ##NO_TEXT.
data MV_COMPRESS type BOOL .
data MV_PRETTY_NAME type PRETTY_NAME_MODE .
data MV_ASSOC_ARRAYS type BOOL .
data MV_TS_AS_ISO8601 type BOOL .
data MV_EXPAND_INCLUDES type BOOL .
data MV_ASSOC_ARRAYS_OPT type BOOL .
data MV_STRICT_MODE type BOOL .
data MV_NUMC_AS_STRING type BOOL .
data MV_FORMAT_OUTPUT type BOOL .
data MV_CONVERSION_EXITS type BOOL .
data MV_HEX_AS_BASE64 type BOOL .
data MV_GEN_OPTIMIZE type BOOL .
data MT_NAME_MAPPINGS type NAME_MAPPINGS .
data MT_NAME_MAPPINGS_EX type NAME_MAPPINGS_EX .
data MT_STRUCT_TYPE type T_T_STRUCT_TYPE .
data MT_STRUCT_CACHE type T_T_STRUCT_CACHE .
data:
mt_ref_dump_idx TYPE SORTED TABLE OF REF TO data WITH UNIQUE DEFAULT KEY .
data:
mt_obj_dump_idx TYPE SORTED TABLE OF REF TO object WITH UNIQUE DEFAULT KEY .
class-data MC_NAME_SYMBOLS_MAP type STRING value ` _/_\_:_;_~_._,_-_+_=_>_<_|_(_)_[_]_{_}_@_+_*_?_!_&_$_#_%_^_'_` ##NO_TEXT.
constants MC_DEFAULT_INDENT type STRING value ` ` ##NO_TEXT.
constants MC_TYPEKIND_UTCLONG type ABAP_TYPEKIND value 'p' ##NO_TEXT. " CL_ABAP_TYPEDESCR=>TYPEKIND_UTCLONG -> 'p' only from 7.54
constants MC_TYPEKIND_INT8 type ABAP_TYPEKIND value '8' ##NO_TEXT. " TYPEKIND_INT8 -> '8' only from 7.40
class-data SO_TYPE_S type ref to CL_ABAP_ELEMDESCR .
class-data SO_TYPE_F type ref to CL_ABAP_ELEMDESCR .
class-data SO_TYPE_P type ref to CL_ABAP_ELEMDESCR .
class-data SO_TYPE_I type ref to CL_ABAP_ELEMDESCR .
class-data SO_TYPE_B type ref to CL_ABAP_ELEMDESCR .
class-data SO_TYPE_D type ref to CL_ABAP_ELEMDESCR .
class-data SO_TYPE_T type ref to CL_ABAP_ELEMDESCR .
class-data SO_TYPE_TS type ref to CL_ABAP_ELEMDESCR .
class-data SO_TYPE_TSL type ref to CL_ABAP_ELEMDESCR .
class-data SO_TYPE_T_JSON type ref to CL_ABAP_TABLEDESCR .
class-data SO_TYPE_T_NAME_VALUE type ref to CL_ABAP_TABLEDESCR .
class-data SO_REGEX_DATE type ref to CL_ABAP_REGEX .
class-data SO_REGEX_TIME type ref to CL_ABAP_REGEX .
class-data SO_REGEX_GUID type ref to CL_ABAP_REGEX .
class-data SO_REGEX_EDM_DATE_TIME type ref to CL_ABAP_REGEX .
class-data SO_REGEX_EDM_TIME type ref to CL_ABAP_REGEX .
class-data SO_REGEX_GENERATE_NORMALIZE type ref to CL_ABAP_REGEX .
class-data SO_REGEX_GENERATE_CAMEL_CASE type ref to CL_ABAP_REGEX .
class-data SO_REGEX_GENERATE_TYPE_DETECT type ref to CL_ABAP_REGEX .
class-data SO_REGEX_UNESCAPE_SPEC_CHAR type ref to CL_ABAP_REGEX .
constants:
BEGIN OF e_typekind,
" new extended pseudo typekind, hack and can clash with standard if new enums come...
" always check for duplicates !!!
convexit TYPE abap_typekind VALUE '1' ##NO_TEXT,
ts_iso8601 TYPE abap_typekind VALUE '2' ##NO_TEXT,
tsl_iso8601 TYPE abap_typekind VALUE '3' ##NO_TEXT,
numc_string TYPE abap_typekind VALUE '4' ##NO_TEXT,
json TYPE abap_typekind VALUE '5' ##NO_TEXT,
bool TYPE abap_typekind VALUE '6' ##NO_TEXT,
tribool TYPE abap_typekind VALUE '7' ##NO_TEXT,
" redefine for existing typekeinds for lower releases
utclong TYPE abap_typekind VALUE 'p' ##NO_TEXT, " CL_ABAP_TYPEDESCR=>TYPEKIND_UTCLONG -> 'p' only from 7.54
int8 TYPE abap_typekind VALUE '8' ##NO_TEXT, " CL_ABAP_TYPEDESCR=>TYPEKIND_INT8 -> '8' only from 7.40
enum TYPE abap_typekind VALUE 'k' ##NO_TEXT, " CL_ABAP_TYPEDESCR=>TYPEKIND_ENUM -> 'k'
" just aliasing
float TYPE abap_typekind VALUE cl_abap_typedescr=>typekind_float,
int TYPE abap_typekind VALUE cl_abap_typedescr=>typekind_int,
int1 TYPE abap_typekind VALUE cl_abap_typedescr=>typekind_int1,
int2 TYPE abap_typekind VALUE cl_abap_typedescr=>typekind_int2,
packed TYPE abap_typekind VALUE cl_abap_typedescr=>typekind_packed,
num TYPE abap_typekind VALUE cl_abap_typedescr=>typekind_num,
string TYPE abap_typekind VALUE cl_abap_typedescr=>typekind_string,
csequence TYPE abap_typekind VALUE cl_abap_typedescr=>typekind_csequence,
clike TYPE abap_typekind VALUE cl_abap_typedescr=>typekind_clike,
char TYPE abap_typekind VALUE cl_abap_typedescr=>typekind_char,
date TYPE abap_typekind VALUE cl_abap_typedescr=>typekind_date,
time TYPE abap_typekind VALUE cl_abap_typedescr=>typekind_time,
xstring TYPE abap_typekind VALUE cl_abap_typedescr=>typekind_xstring,
hex TYPE abap_typekind VALUE cl_abap_typedescr=>typekind_hex,
END OF e_typekind .
class-methods UNESCAPE
importing
!OFFSET type I default 0
!ESCAPED type STRING
returning
value(UNESCAPED) type STRING .
class-methods GET_CONVEXIT_FUNC
importing
!ELEM_DESCR type ref to CL_ABAP_ELEMDESCR
!INPUT type ABAP_BOOL optional
returning
value(RV_FUNC) type STRING .
methods DUMP_SYMBOLS
final
importing
!IT_SYMBOLS type T_T_SYMBOL
!OPT_ARRAY type BOOL optional
!FORMAT_SCOPE type BOOL default ABAP_TRUE
!LEVEL type I
returning
value(R_JSON) type JSON .
methods GET_SYMBOLS_STRUCT
final
importing
!TYPE_DESCR type ref to CL_ABAP_STRUCTDESCR
!INCLUDE_ALIASES type ABAP_BOOL default ABAP_FALSE
!DATA type ref to DATA optional
!LEVEL type I default 0
returning
value(RESULT) type T_S_STRUCT_CACHE_RES .
methods GET_SYMBOLS_CLASS
final
importing
!TYPE_DESCR type ref to CL_ABAP_CLASSDESCR
!OBJECT type ref to OBJECT optional
returning
value(RESULT) type T_T_SYMBOL .
methods GET_SYMBOLS
final
importing
!TYPE_DESCR type ref to CL_ABAP_TYPEDESCR
!DATA type ref to DATA optional
!OBJECT type ref to OBJECT optional
!INCLUDE_ALIASES type ABAP_BOOL default ABAP_FALSE
returning
value(RESULT) type T_T_SYMBOL .
methods GET_FIELDS
final
importing
!TYPE_DESCR type ref to CL_ABAP_TYPEDESCR
!DATA type ref to DATA optional
!OBJECT type ref to OBJECT optional
returning
value(RT_FIELDS) type T_T_FIELD_CACHE .
methods DUMP_INT
importing
!DATA type DATA
!TYPE_DESCR type ref to CL_ABAP_TYPEDESCR optional
!CONVEXIT type STRING optional
!LEVEL type I default 0
returning
value(R_JSON) type JSON .
methods IS_COMPRESSABLE
importing
!TYPE_DESCR type ref to CL_ABAP_TYPEDESCR ##NEEDED
!NAME type CSEQUENCE ##NEEDED
returning
value(RV_COMPRESS) type ABAP_BOOL .
methods RESTORE
importing
!JSON type JSON
!LENGTH type I
value(TYPE_DESCR) type ref to CL_ABAP_TYPEDESCR optional
!FIELD_CACHE type T_T_FIELD_CACHE optional
changing
!DATA type DATA optional
!OFFSET type I default 0
raising
CX_SY_MOVE_CAST_ERROR .
methods RESTORE_TYPE
importing
!JSON type JSON
!LENGTH type I
value(TYPE_DESCR) type ref to CL_ABAP_TYPEDESCR optional
!FIELD_CACHE type T_T_FIELD_CACHE optional
!CONVEXIT type STRING optional
changing
!DATA type DATA optional
!OFFSET type I default 0
raising
CX_SY_MOVE_CAST_ERROR .
methods DUMP_TYPE
importing
!DATA type DATA
!TYPE_DESCR type ref to CL_ABAP_ELEMDESCR
!CONVEXIT type STRING
!TYPEKIND type ABAP_TYPEKIND optional
returning
value(R_JSON) type JSON .
methods DETECT_TYPEKIND
final
importing
!TYPE_DESCR type ref to CL_ABAP_ELEMDESCR
!CONVEXIT type STRING
returning
value(RV_TYPE) type ABAP_TYPEKIND .
methods DUMP_TYPE_EX
importing
!DATA type DATA
returning
value(R_JSON) type JSON .
methods PRETTY_NAME_EX
importing
!IN type CSEQUENCE
returning
value(OUT) type STRING .
methods GENERATE_INT_EX
final
importing
!JSON type JSON
!LENGTH type I
changing
!DATA type DATA
!OFFSET type I .
methods PRETTY_NAME
importing
!IN type CSEQUENCE
!PASCAL_CASE type BOOL default C_BOOL-FALSE
returning
value(OUT) type STRING .
class-methods EDM_DATETIME_TO_TS
importing
!TICKS type STRING
!OFFSET type STRING optional
!TYPEKIND type ABAP_TYPEKIND
returning
value(R_DATA) type STRING .
class-methods GET_INDENT
importing
!LEVEL type I default 0
returning
value(INDENT) type STRING .
methods GENERATE_STRUCT
changing
!FIELDS type T_T_NAME_VALUE
!DATA type ref to DATA
!TYPE type ref to CL_ABAP_DATADESCR optional .
class-methods ESCAPE
importing
!IN type ANY
returning
value(OUT) type STRING .
PRIVATE SECTION.
DATA mv_extended TYPE bool .
CLASS-DATA mc_cov_error TYPE c .
CLASS-DATA mc_me_type TYPE string .
CLASS-DATA so_type_reftab TYPE REF TO cl_abap_tabledescr .
*"* private components of class Z_UI2_JSON
*"* do not include other source files here!!!
ENDCLASS.
CLASS Z_UI2_JSON IMPLEMENTATION.
METHOD bool_to_tribool.
IF iv_bool EQ c_bool-true.
rv_tribool = c_tribool-true.
ELSEIF iv_bool EQ abap_undefined. " fall back for abap _bool
rv_tribool = c_tribool-undefined.
ELSE.
rv_tribool = c_tribool-false.
ENDIF.
ENDMETHOD. "bool_to_tribool
METHOD class_constructor.
DATA: lo_bool_type_descr TYPE REF TO cl_abap_typedescr,
lo_tribool_type_descr TYPE REF TO cl_abap_typedescr,
lo_json_type_descr TYPE REF TO cl_abap_typedescr,
lo_type TYPE REF TO cl_abap_datadescr,
lv_pos LIKE sy-fdpos,
lv_nbsp TYPE c LENGTH 1,
lv_json_string TYPE json.
lo_bool_type_descr = cl_abap_typedescr=>describe_by_data( c_bool-true ).
lo_tribool_type_descr = cl_abap_typedescr=>describe_by_data( c_tribool-true ).
lo_json_type_descr = cl_abap_typedescr=>describe_by_data( lv_json_string ).
CONCATENATE mc_bool_types lo_bool_type_descr->absolute_name lo_tribool_type_descr->absolute_name INTO mc_bool_types.
CONCATENATE mc_bool_3state lo_tribool_type_descr->absolute_name INTO mc_bool_3state.
CONCATENATE mc_json_type lo_json_type_descr->absolute_name INTO mc_json_type.
FIND FIRST OCCURRENCE OF '\TYPE=' IN lo_json_type_descr->absolute_name MATCH OFFSET lv_pos.
IF sy-subrc IS INITIAL.
mc_me_type = lo_json_type_descr->absolute_name(lv_pos).
ENDIF.
sv_white_space = cl_abap_char_utilities=>get_simple_spaces_for_cur_cp( ).
lv_nbsp = cl_abap_conv_in_ce=>uccp( '00A0' ).
FIND FIRST OCCURRENCE OF lv_nbsp IN sv_white_space.
IF sy-subrc IS NOT INITIAL. " add non-breakable space
CONCATENATE sv_white_space lv_nbsp INTO sv_white_space.
ENDIF.
mc_cov_error = cl_abap_conv_in_ce=>uccp( '0000' ).
so_type_s = cl_abap_elemdescr=>get_string( ).
so_type_f = cl_abap_elemdescr=>get_f( ).
so_type_p = cl_abap_elemdescr=>get_p( p_length = 16 p_decimals = 0 ).
so_type_i = cl_abap_elemdescr=>get_i( ).
so_type_d = cl_abap_elemdescr=>get_d( ).
so_type_t = cl_abap_elemdescr=>get_t( ).
so_type_ts ?= cl_abap_typedescr=>describe_by_name( 'TIMESTAMP' ).
so_type_tsl ?= cl_abap_typedescr=>describe_by_name( 'TIMESTAMPL' ).
so_type_b ?= cl_abap_typedescr=>describe_by_name( 'ABAP_BOOL' ).
so_type_t_json ?= cl_abap_typedescr=>describe_by_name( 'T_T_JSON' ).
so_type_t_name_value ?= cl_abap_typedescr=>describe_by_name( 'T_T_NAME_JSON' ).
lo_type = cl_abap_refdescr=>get_ref_to_data( ).
so_type_reftab = cl_abap_tabledescr=>get( p_line_type = lo_type ).
create_regexp so_regex_date '^(\d{4})-(\d{2})-(\d{2})(?:[^T]|$)'.
create_regexp so_regex_time '^(\d{2}):(\d{2}):(\d{2})'.
create_regexp so_regex_guid '^([0-9A-Fa-f]{8})-([0-9A-Fa-f]{4})-([0-9A-Fa-f]{4})-([0-9A-Fa-f]{4})-([0-9A-Fa-f]{12})\s*$'. " support for Edm.Guid
" support for Edm.DateTime => http://www.odata.org/documentation/odata-version-2-0/json-format/
create_regexp so_regex_edm_date_time '^\/[Dd][Aa][Tt][Ee]\((-?\d+)([+-]\d{1,4})?\)\/\s*$'.
" support for Edm.Time => https://www.w3.org/TR/xmlschema11-2/#nt-durationRep
create_regexp so_regex_edm_time '^-?P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)(?:\.(\d+))?S)?)?\s*$'.
create_regexp so_regex_generate_normalize '[^0-9a-zA-Z_]{1,}'.
create_regexp so_regex_generate_camel_case '([a-z])([A-Z])'.
create_regexp so_regex_generate_type_detect '"(?:(?:\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[\.,]\d{0,9})?(?:Z|(?:[+-]\d{2}:\d{2})))|(?:\d{4}-\d{2}-\d{2})|(?:\d{2}:\d{2}:\d{2}))"'.
create_regexp so_regex_unescape_spec_char '\\[rntfbu\\]'.
ENDMETHOD. "class_constructor
METHOD constructor.
DATA: rtti TYPE REF TO cl_abap_classdescr,
pair LIKE LINE OF name_mappings.
mv_compress = compress.
mv_pretty_name = pretty_name.
mv_assoc_arrays = assoc_arrays.
mv_ts_as_iso8601 = ts_as_iso8601.
mv_expand_includes = expand_includes.
mv_assoc_arrays_opt = assoc_arrays_opt.
mv_strict_mode = strict_mode.
mv_numc_as_string = numc_as_string.
mv_conversion_exits = conversion_exits.
mv_format_output = format_output.
mv_hex_as_base64 = hex_as_base64.
mv_gen_optimize = gen_optimize.
mv_bool_types = bool_types.
mv_bool_3state = bool_3state.
mv_initial_ts = initial_ts.
mv_initial_date = initial_date.
mv_initial_time = initial_time.
mv_time_zone = time_zone.
LOOP AT name_mappings INTO pair.
TRANSLATE pair-abap TO UPPER CASE.
INSERT pair INTO TABLE mt_name_mappings.
ENDLOOP.
IF mt_name_mappings IS NOT INITIAL.
" if it dumps here, you have passed ambiguous mapping to the API
" Please check your code for duplicates, pairs ABAP - JSON shall be unique
" and there shall be no ambiguity for looking up only by JSON name (deserialize, generate)
" and no ambiguity for looking up only by ABAP name (serialize)
" per default, serializer/deserializer tries to have lossless conversion from JSON to ABAP and vice versa
INSERT LINES OF mt_name_mappings INTO TABLE mt_name_mappings_ex.
IF mv_pretty_name EQ pretty_mode-none.
mv_pretty_name = pretty_mode-user.
ELSEIF pretty_name EQ pretty_mode-low_case.
mv_pretty_name = pretty_mode-user_low_case.
ENDIF.
ENDIF.
rtti ?= cl_abap_classdescr=>describe_by_object_ref( me ).
IF rtti->absolute_name NE mc_me_type.
mv_extended = c_bool-true.
ENDIF.
ENDMETHOD.
METHOD deserialize.
CONSTANTS: lc_method TYPE string VALUE `DESERIALIZE_INT`.
DATA: lo_json TYPE REF TO object.
" **********************************************************************
" Usage examples and documentation can be found on GitHub:
" https://github.com/SAP/abap-to-json
" ********************************************************************** "
IF json IS NOT INITIAL OR jsonx IS NOT INITIAL.
CREATE OBJECT lo_json TYPE (mc_me_type)
EXPORTING
pretty_name = pretty_name
name_mappings = name_mappings
assoc_arrays = assoc_arrays
conversion_exits = conversion_exits
hex_as_base64 = hex_as_base64
gen_optimize = gen_optimize
assoc_arrays_opt = assoc_arrays_opt.
TRY .
CALL METHOD lo_json->(lc_method)
EXPORTING
json = json
jsonx = jsonx
CHANGING
data = data.
CATCH cx_sy_move_cast_error. "#EC NO_HANDLER
ENDTRY.
ENDIF.
ENDMETHOD. "deserialize
METHOD deserialize_int.
DATA: length TYPE i,
offset TYPE i,
lx_move TYPE REF TO cx_sy_move_cast_error,
lv_json LIKE json.
" **********************************************************************
" Usage examples and documentation can be found on GitHub:
" https://github.com/SAP/abap-to-json
" ********************************************************************** "
CHECK json IS NOT INITIAL OR jsonx IS NOT INITIAL.
IF jsonx IS NOT INITIAL.
lv_json = cl_abap_codepage=>convert_from( jsonx ).
ELSE.
lv_json = json.
ENDIF.
" skip leading BOM signs till string, array, object, boolean or number
length = strlen( lv_json ).
while_offset_not_cs '"{[aeflnrstu0123456789+-eE.' lv_json offset.
TRY.
restore_type( EXPORTING json = lv_json length = length CHANGING data = data offset = offset ).
CATCH cx_sy_move_cast_error INTO lx_move.
RAISE EXCEPTION TYPE cx_sy_move_cast_error
EXPORTING
previous = lx_move
source_typename = `$.` && lx_move->source_typename
target_typename = lx_move->target_typename.
ENDTRY.
ENDMETHOD. "deserialize
METHOD dump.
CONSTANTS: lc_method TYPE string VALUE `DUMP_INT`.
DATA: lo_json TYPE REF TO object.
CREATE OBJECT lo_json TYPE (mc_me_type)
EXPORTING
compress = compress
pretty_name = pretty_name
assoc_arrays = assoc_arrays
ts_as_iso8601 = ts_as_iso8601.
CALL METHOD lo_json->(lc_method)
EXPORTING
data = data
type_descr = type_descr
RECEIVING
r_json = r_json.
ENDMETHOD. "dump
METHOD dump_int.
DATA: lo_typedesc TYPE REF TO cl_abap_typedescr,
lo_elem_descr TYPE REF TO cl_abap_elemdescr,
lo_classdesc TYPE REF TO cl_abap_classdescr,
lo_structdesc TYPE REF TO cl_abap_structdescr,
lo_tabledescr TYPE REF TO cl_abap_tabledescr,
ls_struct_sym TYPE t_s_struct_cache_res,
lt_symbols TYPE t_t_symbol,
lt_keys TYPE STANDARD TABLE OF REF TO data WITH DEFAULT KEY,
lt_properties TYPE STANDARD TABLE OF string,
lo_obj_ref TYPE REF TO object,
lo_data_ref TYPE REF TO data,
ls_skip_key TYPE LINE OF abap_keydescr_tab,
lv_array_opt TYPE abap_bool,
indent TYPE string,
lv_indent LIKE indent,
lv_level LIKE level,
lv_lb TYPE string,
lv_prop_name TYPE string,
lv_keyval TYPE string,
lv_typekind TYPE abap_typekind,
lv_itemval TYPE string,
lv_property TYPE string.
FIELD-SYMBOLS: <line> TYPE any,
<value> TYPE any,
<data> TYPE data,
<key> TYPE LINE OF abap_keydescr_tab,
<symbol> TYPE t_s_symbol,
<table> TYPE ANY TABLE.
" increase hierarchy level
lv_level = level + 1.
" we need here macro instead of method calls because of the performance reasons.
" based on SAT measurements.
CASE type_descr->kind.
WHEN cl_abap_typedescr=>kind_ref.
IF data IS INITIAL.
r_json = `null`. "#EC NOTEXT
ELSEIF type_descr->type_kind EQ cl_abap_typedescr=>typekind_dref.
lo_data_ref ?= data.
INSERT lo_data_ref INTO TABLE mt_ref_dump_idx.
IF sy-subrc IS INITIAL. "not dumped yet
lo_typedesc = cl_abap_typedescr=>describe_by_data_ref( lo_data_ref ).
ASSIGN lo_data_ref->* TO <data>.
r_json = dump_int( data = <data> type_descr = lo_typedesc level = level ).
DELETE TABLE mt_ref_dump_idx WITH TABLE KEY table_line = lo_data_ref.
ELSE.
" it is a cycle reference, we can not proceed, so terminate and write empty object instead
r_json = `{}`. "#EC NOTEXT
ENDIF.
ELSE.
lo_obj_ref ?= data.
INSERT lo_obj_ref INTO TABLE mt_obj_dump_idx.
IF sy-subrc IS INITIAL. "not dumped yet
lo_classdesc ?= cl_abap_typedescr=>describe_by_object_ref( lo_obj_ref ).
lt_symbols = get_symbols_class( type_descr = lo_classdesc object = lo_obj_ref ).
r_json = dump_symbols( it_symbols = lt_symbols level = level ).
DELETE TABLE mt_obj_dump_idx WITH TABLE KEY table_line = lo_obj_ref.
ELSE.
" it is a cycle reference, we can not proceed, so terminate and write empty object instead
r_json = `{}`. "#EC NOTEXT
ENDIF.
ENDIF.
WHEN cl_abap_typedescr=>kind_elem.
lo_elem_descr ?= type_descr.
lv_typekind = detect_typekind( type_descr = lo_elem_descr convexit = convexit ).
dump_type data lo_elem_descr lv_typekind r_json convexit.
WHEN cl_abap_typedescr=>kind_struct.
lo_structdesc ?= type_descr.
ls_struct_sym = get_symbols_struct( type_descr = lo_structdesc level = level ).
ASSIGN ls_struct_sym-data->* TO <data>.
<data> = data.
r_json = dump_symbols( it_symbols = ls_struct_sym-symbols level = level ).
WHEN cl_abap_typedescr=>kind_table.
lo_tabledescr ?= type_descr.
lo_typedesc = lo_tabledescr->get_table_line_type( ).
ASSIGN data TO <table>.
IF mv_format_output EQ abap_true.
indent = get_indent( level ).
lv_indent = get_indent( lv_level ).
ENDIF.
" optimization for structured tables
IF lo_typedesc->kind EQ cl_abap_typedescr=>kind_struct.
lo_structdesc ?= lo_typedesc.
ls_struct_sym = get_symbols_struct( type_descr = lo_structdesc level = level ).
ASSIGN ls_struct_sym-data->* TO <line>.
" here we have differentiation of output of simple table to JSON array
" and sorted or hashed table with unique key into JSON associative array
IF lo_tabledescr->has_unique_key IS NOT INITIAL AND mv_assoc_arrays IS NOT INITIAL.
IF lo_tabledescr->key_defkind EQ lo_tabledescr->keydefkind_user.
LOOP AT lo_tabledescr->key ASSIGNING <key>.
READ TABLE ls_struct_sym-symbols WITH KEY name = <key>-name ASSIGNING <symbol>.
APPEND <symbol>-value TO lt_keys.
ENDLOOP.
ENDIF.
IF lines( lo_tabledescr->key ) EQ 1.
READ TABLE lo_tabledescr->key INDEX 1 INTO ls_skip_key.
DELETE ls_struct_sym-symbols WHERE name EQ ls_skip_key-name.
" remove object wrapping for simple name-value tables
IF mv_assoc_arrays_opt EQ abap_true AND lines( ls_struct_sym-symbols ) EQ 1.
lv_array_opt = abap_true.
ENDIF.
ENDIF.
LOOP AT <table> INTO <line>.
CLEAR: lv_prop_name.
" construct key attribute name
IF lo_tabledescr->key_defkind EQ lo_tabledescr->keydefkind_user.
LOOP AT lt_keys INTO lo_data_ref.
ASSIGN lo_data_ref->* TO <value>.
lv_keyval = <value>.
CONDENSE lv_keyval.
IF lv_prop_name IS NOT INITIAL.
CONCATENATE lv_prop_name mc_key_separator lv_keyval INTO lv_prop_name.
ELSE.
lv_prop_name = lv_keyval.
ENDIF.
ENDLOOP.
ELSE.
LOOP AT ls_struct_sym-symbols ASSIGNING <symbol>.
ASSIGN <symbol>-value->* TO <value>.
lv_keyval = <value>.
CONDENSE lv_keyval.
IF lv_prop_name IS NOT INITIAL.
CONCATENATE lv_prop_name mc_key_separator lv_keyval INTO lv_prop_name.
ELSE.
lv_prop_name = lv_keyval.
ENDIF.
ENDLOOP.
ENDIF.
lv_itemval = dump_symbols( it_symbols = ls_struct_sym-symbols opt_array = lv_array_opt format_scope = abap_false level = lv_level ).
IF lv_array_opt EQ abap_true.
IF mv_format_output EQ abap_true AND lv_itemval IS NOT INITIAL.
CONCATENATE '"' lv_prop_name '": ' lv_itemval INTO lv_property RESPECTING BLANKS.
ELSE.
CONCATENATE '"' lv_prop_name '":' lv_itemval INTO lv_property.
ENDIF.
ELSE.
IF mv_format_output EQ abap_true AND lv_itemval IS NOT INITIAL.
CONCATENATE '"' lv_prop_name '": {' lv_itemval lv_indent '}' INTO lv_property.
ELSE.
CONCATENATE '"' lv_prop_name '":{' lv_itemval '}' INTO lv_property.
ENDIF.
ENDIF.
APPEND lv_property TO lt_properties.
ENDLOOP.
format_list_output '{' lt_properties '}' r_json.
ELSE.
LOOP AT <table> INTO <line>.
lv_itemval = dump_symbols( it_symbols = ls_struct_sym-symbols level = lv_level ).
APPEND lv_itemval TO lt_properties.
ENDLOOP.
format_list_output '[' lt_properties ']' r_json.
ENDIF.
ELSE.
LOOP AT <table> ASSIGNING <value>.
lv_itemval = dump_int( data = <value> type_descr = lo_typedesc level = lv_level ).
APPEND lv_itemval TO lt_properties.
ENDLOOP.
format_list_output '[' lt_properties ']' r_json.
ENDIF.
ENDCASE.
ENDMETHOD. "dump
METHOD dump_symbols.
DATA: lt_fields TYPE STANDARD TABLE OF string INITIAL SIZE 10000,
lv_indent TYPE string,
lv_level LIKE level,
lv_itemval TYPE string,
lv_field TYPE string.
FIELD-SYMBOLS: <value> TYPE any,
<symbol> LIKE LINE OF it_symbols.
" increase hierarchy level
lv_level = level + 1.
IF mv_format_output EQ abap_true AND opt_array EQ abap_false.
lv_indent = get_indent( lv_level ).
ENDIF.
LOOP AT it_symbols ASSIGNING <symbol>.
ASSIGN <symbol>-value->* TO <value>.
CHECK <symbol>-compressable EQ abap_false OR <value> IS NOT INITIAL OR opt_array EQ abap_true.
IF <symbol>-elem_type IS NOT INITIAL.
dump_type <value> <symbol>-elem_type <symbol>-typekind lv_itemval <symbol>-convexit_out.
ELSE.
lv_itemval = dump_int( data = <value> type_descr = <symbol>-type convexit = <symbol>-convexit_out level = lv_level ).
ENDIF.
IF opt_array EQ abap_false.
IF mv_format_output EQ abap_true.
CONCATENATE lv_indent <symbol>-header lv_itemval INTO lv_field.
ELSE.
CONCATENATE <symbol>-header lv_itemval INTO lv_field.
ENDIF.
ELSE.
lv_field = lv_itemval.
ENDIF.
APPEND lv_field TO lt_fields.
ENDLOOP.
CONCATENATE LINES OF lt_fields INTO r_json SEPARATED BY ','.
IF format_scope EQ abap_true.
IF r_json IS INITIAL.
r_json = `{}`.
ELSEIF mv_format_output EQ abap_true.
lv_indent = get_indent( level ).
CONCATENATE '{' r_json lv_indent '}' INTO r_json.
ELSE.
CONCATENATE '{' r_json '}' INTO r_json.
ENDIF.
ENDIF.
ENDMETHOD.
METHOD dump_type.
DATA: lv_typekind LIKE typekind.
""!!! the fallback code for missing typekind is only for the method but not used in macro
IF typekind IS INITIAL.
lv_typekind = detect_typekind( type_descr = type_descr convexit = convexit ).
ELSE.
lv_typekind = typekind.
ENDIF.
""!!!!
CASE lv_typekind.
WHEN e_typekind-convexit.
IF data IS INITIAL.
r_json = `""`.
ELSE.
TRY.
CALL FUNCTION convexit
EXPORTING
input = data
IMPORTING