Update
This commit is contained in:
@@ -710,7 +710,7 @@ ref1 = NEW #( ). "Type derived from already declared ref1
|
||||
DATA(ref2) = NEW some_class( ). "Reference variable declared inline, explicit type
|
||||
"(class) specification
|
||||
|
||||
"Old syntax. Do not use.
|
||||
"Older syntax
|
||||
"CREATE OBJECT ref3. "Type derived from already declared ref3
|
||||
"CREATE OBJECT ref4 TYPE some_class. "Corresponds to the result of the expression above
|
||||
```
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
- [Declaring Data Reference Variables](#declaring-data-reference-variables)
|
||||
- [Assigning References to Existing Data Objects](#assigning-references-to-existing-data-objects)
|
||||
- [Creating New Data Objects at Runtime (Anonymous Data Objects)](#creating-new-data-objects-at-runtime-anonymous-data-objects)
|
||||
- [Excursion: Creating Objects as Instances of Classes and CREATE OBJECT Statements](#excursion-creating-objects-as-instances-of-classes-and-create-object-statements)
|
||||
- [Assignments Between Two Reference Variables (Static and Dynamic Type, Upcast and Downcast)](#assignments-between-two-reference-variables-static-and-dynamic-type-upcast-and-downcast)
|
||||
- [Addressing Data References](#addressing-data-references)
|
||||
- [Excursion: Generic Data References and Field Symbols](#excursion-generic-data-references-and-field-symbols)
|
||||
@@ -420,10 +421,6 @@ ASSIGN s-oref TO <object>.
|
||||
- are typed with the addition [`REF TO`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abaptypes_references.htm) followed by a [static type](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenstatic_type_glosry.htm). Note the [dynamic type](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abendynamic_type_glosry.htm) in this context: The dynamic type of such a variable is the data type to which it actually points. This concept is particularly relevant in the context of assignments (see the assignment rules [here](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenconversion_references.htm)).
|
||||
- can be typed with a complete or generic type. However, only `data` can be used as generic type.
|
||||
|
||||
> **💡 Note**<br>
|
||||
> [Object references](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenobject_reference_glosry.htm "Glossary Entry")
|
||||
and [object reference variables](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenobject_refer_variable_glosry.htm "Glossary Entry") are not part of this cheat sheet. To get more details, refer to the [ABAP Keyword Documentation](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenobject_reference_type.htm) or the cheat sheet [ABAP Object Orientation](04_ABAP_Object_Orientation.md).
|
||||
|
||||
<p align="right"><a href="#top">⬆️ back to top</a></p>
|
||||
|
||||
#### Declaring Data Reference Variables
|
||||
@@ -500,7 +497,6 @@ DATA(ref3) = REF some_type( ... ).
|
||||
|
||||
> **💡 Note**<br>
|
||||
> The following snippet covers statically defined types. Data objects can also be created with `CREATE DATA` dynamically using dynamic type definitions (the type name is specified within a pair of parentheses) and type description objects ([`TYPE HANDLE` addition](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abapcreate_data_handle.htm)) as shown further down.
|
||||
> Using [`CREATE OBJECT`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapcreate_object.htm) statements, you can create an object as an instance of a class and assign the reference to the object to an [object reference variable](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenobject_refer_variable_glosry.htm). Find more information in the [ABAP Keyword Documentation](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapcreate_object.htm).
|
||||
|
||||
```abap
|
||||
"CREATE DATA statements
|
||||
@@ -577,6 +573,39 @@ SELECT SINGLE *
|
||||
|
||||
<p align="right"><a href="#top">⬆️ back to top</a></p>
|
||||
|
||||
#### Excursion: Creating Objects as Instances of Classes and CREATE OBJECT Statements
|
||||
|
||||
- To create an object, a [reference variable](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenreference_variable_glosry.htm "Glossary Entry")
|
||||
must be declared.
|
||||
- Such an [object reference variable](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenobject_refer_variable_glosry.htm "Glossary Entry")
|
||||
is required to access objects and their components. That means objects are not directly accessed but only via references that point to those objects. This object reference variable contains the reference to the object - after assigning the reference to the object.
|
||||
- Using [`CREATE OBJECT`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapcreate_object.htm) statements, you can create an object as an instance of a class and assign the reference to the object to an [object reference variable](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenobject_refer_variable_glosry.htm).
|
||||
- The instance operator [`NEW`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenconstructor_expression_new.htm) basically replaces `CREATE OBJECT` statements.
|
||||
- However, `CREATE OBJECT` statements are still required and they are the only option for creating objects dynamically (`NEW` is not possible in that context) as shown further down.
|
||||
- Note the use of the built-in generic ABAP type `object` (`TYPE REF TO object`) that is used for the generic typing of object references. It is for any object type. `object` stands for the root class of the inheritance hierarchy. More information: [Generic ABAP Types](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenbuilt_in_types_generic.htm)
|
||||
- Find more information in the [ABAP Keyword Documentation](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapcreate_object.htm) and the [ABAP Object Orientation](04_ABAP_Object_Orientation.md) cheat sheet
|
||||
|
||||
```abap
|
||||
DATA oref_a TYPE REF TO zcl_some_class.
|
||||
oref_a = NEW #( ).
|
||||
"Using Inline declaration
|
||||
DATA(oref_b) = NEW zcl_some_class( ).
|
||||
"Generic type
|
||||
DATA oref_c TYPE REF TO object.
|
||||
oref_c = NEW zcl_some_class( ).
|
||||
|
||||
"This object creation with the NEW operator corresponds to the older
|
||||
"syntax using CREATE OBJECT, and replaces it. See more examples in the
|
||||
"context of dynamic object creation that require the CREATE OBJECT
|
||||
"syntax further down.
|
||||
DATA oref_d TYPE REF TO zcl_some_class.
|
||||
CREATE OBJECT oref_d.
|
||||
DATA oref_e TYPE REF TO object.
|
||||
CREATE OBJECT oref_e TYPE zcl_some_class.
|
||||
```
|
||||
|
||||
<p align="right"><a href="#top">⬆️ back to top</a></p>
|
||||
|
||||
#### Assignments Between Two Reference Variables (Static and Dynamic Type, Upcast and Downcast)
|
||||
|
||||
As mentioned above regarding the assignment, note that static types of both data
|
||||
|
||||
118
21_XML_JSON.md
118
21_XML_JSON.md
@@ -11,6 +11,7 @@
|
||||
- [CALL TRANSFORMATION Syntax](#call-transformation-syntax)
|
||||
- [Working with JSON](#working-with-json)
|
||||
- [Excursions](#excursions)
|
||||
- [Serializing and Deserializing Instances of Classes](#serializing-and-deserializing-instances-of-classes)
|
||||
- [Converting string \<-\> xstring](#converting-string---xstring)
|
||||
- [Compressing and Decompressing Binary Data](#compressing-and-decompressing-binary-data)
|
||||
- [More Information](#more-information)
|
||||
@@ -421,7 +422,7 @@ Possible transformations, some of which are covered in the example:
|
||||
> - Used as an intermediate format that defines a mapping between ABAP data and XML
|
||||
> - Therefore, if you want to deserialize XML data in ABAP, you must first transform it to the asXML format.
|
||||
> - Make sure that you use appropriate exception classes for potential errors in transformations. See the section [Catchable Exceptions](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapcall_transformation.htm) in the `CALL TRANSFORMATION` topic.
|
||||
> - For serializing instances of classes, find more information [here](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenasxml_class_instances.htm) in the ABAP Keyword Documentation. The classes must implement the `IF_SERIALIZABLE_OBJECT` interface (find a demo in the executable example).
|
||||
> - For serializing instances of classes, find more information [here](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenasxml_class_instances.htm) in the ABAP Keyword Documentation. The classes must implement the `IF_SERIALIZABLE_OBJECT` interface (find a demo in the executable example and further down).
|
||||
|
||||
<p align="right"><a href="#top">⬆️ back to top</a></p>
|
||||
|
||||
@@ -674,6 +675,121 @@ xco_cp_json=>data->from_string( json_created_xco )->apply( VALUE #(
|
||||
<p align="right"><a href="#top">⬆️ back to top</a></p>
|
||||
|
||||
## Excursions
|
||||
|
||||
### Serializing and Deserializing Instances of Classes
|
||||
|
||||
For serializing instances of classes, find more information [here](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenasxml_class_instances.htm) in the ABAP Keyword Documentation. The classes must implement the `IF_SERIALIZABLE_OBJECT` interface.
|
||||
|
||||
Expand the following collapsible section to view the code of an example. To try it out, create a demo class named `zcl_some_class` and paste the code into it. After activation, choose *F9* in ADT to execute the class. This example is set up to display the results in the console.
|
||||
|
||||
<details>
|
||||
<summary>Expand to view the details</summary>
|
||||
<!-- -->
|
||||
|
||||
When running the class, three instances of this class are created, and two instance attributes are assigned values for output purposes. The current UTC timestamp
|
||||
and a random number are retrieved and assigned. Then, the instances are serialized.
|
||||
The instances are deserialized again, and the instance attributes are accessed. Their values are stored in an internal table, which is then displayed.
|
||||
|
||||
|
||||
```abap
|
||||
CLASS zcl_some_class DEFINITION
|
||||
PUBLIC
|
||||
FINAL
|
||||
CREATE PUBLIC .
|
||||
|
||||
PUBLIC SECTION.
|
||||
INTERFACES: if_oo_adt_classrun,
|
||||
if_serializable_object.
|
||||
PROTECTED SECTION.
|
||||
PRIVATE SECTION.
|
||||
|
||||
DATA timestamp TYPE utclong.
|
||||
DATA random_number TYPE i.
|
||||
DATA serialized_obj_tab TYPE TABLE OF xstring WITH EMPTY KEY.
|
||||
|
||||
TYPES: BEGIN OF deserialized_obj_struc,
|
||||
timestamp TYPE utclong,
|
||||
random_number TYPE i,
|
||||
END OF deserialized_obj_struc.
|
||||
DATA deserialized_obj_tab TYPE TABLE OF deserialized_obj_struc WITH EMPTY KEY.
|
||||
|
||||
METHODS:
|
||||
"The following method can only have output parameters.
|
||||
"For each output parameter of the serialize_helper method, you must specify
|
||||
"an identically named input parameter of the deserialize_helper method
|
||||
"with the same type.
|
||||
"The example uses two instance attributes that are specified in the private
|
||||
"visibility section.
|
||||
serialize_helper EXPORTING timestamp TYPE utclong
|
||||
random_number TYPE i,
|
||||
"This method can only have input parameters.
|
||||
deserialize_helper IMPORTING timestamp TYPE utclong
|
||||
random_number TYPE i.
|
||||
ENDCLASS.
|
||||
|
||||
|
||||
|
||||
CLASS zcl_some_class IMPLEMENTATION.
|
||||
|
||||
METHOD if_oo_adt_classrun~main.
|
||||
|
||||
"For demonstration purposes, 3 instances of the class are created and serialized.
|
||||
"Two instance attributes are assigned values for output purposes (the current UTC timestamp
|
||||
"and a random number are retrieved and assigned). The assigned values are also serialized and
|
||||
"can be addressed after deserialization. The serialized instances of the class are added to an
|
||||
"internal table which is processed below.
|
||||
DO 3 TIMES.
|
||||
DATA(oref) = NEW zcl_some_class( ).
|
||||
oref->timestamp = utclong_current( ).
|
||||
oref->random_number = cl_abap_random_int=>create( seed = cl_abap_random=>seed( )
|
||||
min = 1
|
||||
max = 100 )->get_next( ).
|
||||
|
||||
DATA serialized_obj TYPE xstring.
|
||||
CALL TRANSFORMATION id SOURCE obj = oref
|
||||
RESULT XML serialized_obj.
|
||||
|
||||
APPEND serialized_obj TO serialized_obj_tab.
|
||||
ENDDO.
|
||||
|
||||
"Deserializing instances of classes from above
|
||||
"For output purposes, the values of the instance attributes are added to an
|
||||
"internal table.
|
||||
LOOP AT serialized_obj_tab INTO DATA(wa).
|
||||
DATA deserialized_oref TYPE REF TO zcl_some_class.
|
||||
CALL TRANSFORMATION id SOURCE XML wa
|
||||
RESULT obj = deserialized_oref.
|
||||
|
||||
"Addressing instance attributes after deserialization
|
||||
DATA(deserialized_timestamp) = deserialized_oref->timestamp.
|
||||
DATA(deserialized_random_number) = deserialized_oref->random_number.
|
||||
|
||||
APPEND VALUE #( timestamp = deserialized_timestamp
|
||||
random_number = deserialized_random_number
|
||||
) TO deserialized_obj_tab.
|
||||
ENDLOOP.
|
||||
|
||||
"The output is intended to visualize the deserialized instance of a class
|
||||
"and the values of its instance attributes that should differ from line to
|
||||
"line.
|
||||
out->write( deserialized_obj_tab ).
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD deserialize_helper.
|
||||
me->timestamp = timestamp.
|
||||
me->random_number = random_number.
|
||||
ENDMETHOD.
|
||||
|
||||
METHOD serialize_helper.
|
||||
timestamp = me->timestamp.
|
||||
random_number = me->random_number.
|
||||
ENDMETHOD.
|
||||
ENDCLASS.
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
|
||||
### Converting string <-> xstring
|
||||
In the code snippets above and in the executable example, many operations are performed using binary data.
|
||||
This excursion shows the conversion of string <-> xstring using a codepage. The examples use UTF-8.
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
- [Executable Example](#executable-example)
|
||||
|
||||
|
||||
This ABAP cheat sheet covers options of how to handle and process dates, times, and time stamps in ABAP. Note the different types when working with them in ABAP, such as for calculations, evaluations, or displaying on a user interface.
|
||||
This ABAP cheat sheet covers options of how to handle and process dates, times, and time stamps in ABAP. Note the different types when working with them in ABAP, such as for calculations, evaluations, or displaying on a user interface. As with most of the ABAP cheat sheets, the focus here is on [ABAP for Cloud Development](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenabap_for_cloud_dev_glosry.htm).
|
||||
|
||||
## Data Types for Date, Time and Time Stamp
|
||||
|
||||
|
||||
Reference in New Issue
Block a user