来源资料待手动核验
项目简介
JSON API
JSON API
We know at least following ways to serialize/deserialize JSON in ABAP:
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 (clsxmlstringwriter, clsxmlparser) | ✅ 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/CLJSON (or ZUI2JSON) | ✅ Easy to use, with automatic conversion ✅ Supports deep and complex structures ✅ Open-source alternative available (ZUI2JSON) ✅ So far fastest JSON parser and rendered after identity transformation | | XCOCPJSON (xcocpjson) | ✅ 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:
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}]}