Skip to content

Commit 5f6bfd2

Browse files
authored
Merge pull request sbcgua#215 from mbtools/docs2
docs: ref to data
2 parents bb9776a + 212a419 commit 5f6bfd2

File tree

1 file changed

+74
-8
lines changed

1 file changed

+74
-8
lines changed

docsite/docs/40-reading.md

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,40 @@ r->get_node_type( '/payload/text' ). " returns "str"
7676

7777
## Converting to ABAP structure
7878

79+
Instead of reading individual values, you can use `to_abap` for converting the JSON data into a corresponding ABAP structure. The types are mapped as follows:
80+
81+
| JSON | ABAP | Expected JSON format |
82+
|----------|---------------------|----------------------|
83+
| str | date (d) | "YYYY-MM-DD" or "YYYY-MM-DDT" (year, month, day, optional fixed "T") |
84+
| str | time (t) | "HH:MM:SS" or "HH:MM:SST" (hour, minute, seconds, optional "T") |
85+
| str | timestamp (p 15,0) | "YYYY-MM-DDTHH:MM:SSZ" or "YYYY-MM-DDTHH:MM:SS+XX:YY" (with timezone) |
86+
| str | timestampl (p 21,7) | "YYYY-MM-DDTHH:MM:SS.FFFFFFFZ" or "YYYY-MM-DDTHH:MM:SS.FFFFFFF+XX:YY" (with timezone) |
87+
| str | utclong | "YYYY-MM-DDTHH:MM:SS.FFFFFFFZ" or "YYYY-MM-DDTHH:MM:SS.FFFFFFF+XX:YY" (with timezone) |
88+
| str | other | Standard ABAP value mapping |
89+
| num | any | Standard ABAP value mapping |
90+
| bool | abap_bool (c 1) | true, false |
91+
| null | - | null (ignored) |
92+
| object | structure | |
93+
| array | table | |
94+
95+
Except for the special date/time mappings, JSON data will be assigned to the corresponding ABAP fields using standard ABAP [data conversion rules](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenconversion_elementary.htm). Look out for conversion errors which throw `CX_SY_CONVERSION_ERROR` or unintentionally truncating values if a target field is shorter than the value.
96+
97+
7998
```abap
8099
data:
81100
begin of ls_payload,
82-
text type string,
83-
num type i,
84-
bool type abap_bool,
101+
text type string,
102+
num type i,
103+
bool type abap_bool,
85104
false type abap_bool,
86-
null type string,
105+
null type string,
87106
table type string_table, " Array !
88107
end of ls_payload.
89108
90109
payload->to_abap( importing ev_container = ls_payload ).
91110
```
92111

93-
`to_abap` supports transferring "corresponding only" fields.
112+
`to_abap` supports transferring "corresponding only" fields. This will ignore JSON attributes that do not have a matching ABAP field name (see [Mapping](70-mapping.md)).
94113

95114
```abap
96115
payload->to_abap(
@@ -101,8 +120,55 @@ payload->to_abap(
101120
payload->to_abap_corresponding_only( )->to_abap( importing ev_container = ls_payload ).
102121
```
103122

104-
## Support for data references
123+
`to_abap` supports creating data references. You can determine the type of a field in the ABAP structure at runtime. For this to work, define the field as `type ref to data` and initialize the reference using `zcl_ajson_ref_initializer_lib=>create_path_refs_init` for each path to a reference field.
124+
125+
Example:
126+
127+
```json
128+
// 1st JSON
129+
{ "tableName": "T002", "tableContent": { ...structure of table T002... } }
130+
// 2nd JSON
131+
{ "tableName": "T100", "tableContent": { ...structure of table T100... } }
132+
```
105133

106-
`to_abap` supports creating data references.
134+
```abap
135+
data:
136+
begin of ls_data,
137+
table_name type string,
138+
table_content type ref to data,
139+
end of ls_data.
107140
108-
*TBD*
141+
data:
142+
ls_t002 type t002,
143+
ls_t100 type t100,
144+
li_refs type ref to zif_ajson_ref_initializer,
145+
lt_refs type zif_ajson_ref_initializer=>tty_data_refs,
146+
ls_refs like line of lt_refs.
147+
148+
do 2 times.
149+
" prepare mapping of paths to data references
150+
clear: ls_refs, lt_refs.
151+
152+
case sy-index.
153+
when 1.
154+
" 1st JSON: reference T002 structure
155+
get reference of ls_t002 into ls_refs-dref.
156+
when 2.
157+
" 2nd JSON: reference T100 structure
158+
get reference of ls_t100 into ls_refs-dref.
159+
endcase.
160+
161+
ls_refs-path = '/'.
162+
ls_refs-name = 'table_content'.
163+
insert ls_refs into table lt_refs.
164+
165+
" create json instance with ref initializer
166+
li_refs = zcl_ajson_ref_initializer_lib=>create_path_refs_init( lt_refs ).
167+
168+
lo_json = new zcl_ajson=>new( ii_refs_initiator = li_refs ).
169+
lo_json->to_abap( importing ev_container = ls_data ).
170+
171+
" ...
172+
173+
enddo.
174+
```

0 commit comments

Comments
 (0)