We know at least following ways to serialize/deserialize JSON in ABAP:
-
sXML library for manual parsing/rendering
-
/ui2/cl_json or open-source Z_UI2_JSON version of it
-
Full list you can find in a separate benchmark:
Method | Features |
---|---|
Identity Transformation (CALL TRANSFORMATION JSON ) |
✅ Standard SAP approach ✅ Very fast ✅ Supports deep structures ❌ Limited functionality |
sXML Library (cl_sxml_string_writer , cl_sxml_parser ) |
✅ Fine-grained control over parsing/rendering ✅ Handles large JSON streams efficiently ✅ Supports custom serialization logic ❌ Requires manual parsing and handling of JSON nodes ❌ More complex to implement compared to other methods |
/UI2/CL_JSON (or Z_UI2_JSON ) |
✅ Easy to use, with automatic conversion ✅ Supports deep and complex structures ✅ Open-source alternative available ( Z_UI2_JSON ) ✅ So far fastest JSON parser and rendered after identity transformation |
XCO_CP_JSON (xco_cp_json ) |
✅ Modern, officially supported API for Cloud ✅ Ability to apply own transformations ❌ Not available in older on-premise systems ❌ Enormously slow ❌ Dumps on data refs |
Here you can find a very good article about some of these methods
Unfortunately I was not please with any of approaches and decided to create an own library implementing following features:
- Using standard identity transformation supporting all available types mapping automatically
- Automatic
camelCase
<->CAMEL_CASE
conversion for property names - Suppress initital components ( feature of identity transformation )
- Allowing to use own conversion rules
You can find more details and data in JSON benchmark project where we analyse different libs, their features and compare performance. Feel free to contribute!
" render binary
data(json_binary) = zcl_json=>render( your_any_variable )
" or stringify (same but as string)
data(json_string) = zcl_json=>stringify( your_any_variable )
" Parsing requires two steps
zcl_json=>parse( json_binary_or_string )->to( ref #( your_data_variable ) ).
It's possible to create JSON from data references
" render binary
data(json_binary) = zcl_json=>render( value payload_type( ref_to_data = new any_type( some_values = .. ) ) )
By default ABAP internal tables do not support polymorphism or union types. You need to normalize type and build type including all elements. However in a modern world JSON schemas can use such constructions as oneOf
, anyOf
and etc. As a result of data refs support we can also support table of ref to data
which allows us to build arrays where every line may be of a different type.
TYPES:
BEGIN OF abap_bool_ts,
true TYPE abap_bool,
END OF abap_bool_ts,
BEGIN OF xsdboolean_ts,
true TYPE xsdboolean,
END OF xsdboolean_ts,
BEGIN OF root_ts,
array_of_ref_to_data TYPE TABLE of REF TO data WITH EMPTY KEY,
END OF root_ts.
TRY.
out->write(
name = 'Polymorphic array'
data = zcl_json=>stringify(
VALUE root_ts(
array_of_ref_to_data = value #(
( new abap_bool_ts( true = abap_true ) )
( new xsdboolean_ts( true = abap_true ) )
) ) ) ).
CATCH cx_static_check.
"handle exception
ENDTRY.
will print
{"arrayOfRefToData":[{"true":"X"},{"true":true}]}