问题
If I want to develop a serializer for a language for which one doesnt exist already (e.g. ABAP), what could be the efforts involved in it? Does it involve merely writing "text equivalent" of ABAP serialzer - how would I tackle complex objects. What would be the best starting point for this?
回答1:
Update: Starting with Releases 7.02 and 7.03/7.31 (kernel patch 116), JSON is supported natively in ABAP - check this blog by Horst Keller.
You should search for ABAP and JSON using site:sap.com first and then choose one of the existing projects to learn from and contribute. However, if you're still out for the NIH bonus points, make sure you know how to use generic types, field symbols, RTTI and recursion inside out, then practice walking complex data structure recursively using RTTI. Once you've done that, assembling any kind of output string is easy.
回答2:
There is a class that does exactly that : CL_TREX_JSON_SERIALIZER. The only thing it does not ( and SAP told me thru customer message that they will not fix it, they don't support this code ) is put the attribute in double quotes.
This is easily fixed by adding on line 52 in CL_TREX_JSON_SERIALIZER-RECURSE:
CONCATENATE '"' <abapcomp>-name '"' c_colon INTO l_value .
An example program would be:
"We are going to serialize an error
DATA: wa_error TYPE bapireturn.
"Reference to the serializer
DATA: cl_serializer TYPE REF TO zcl_trex_json_serializer.
"Final output
DATA: l_json_string TYPE string.
wa_error-type = 'E'.
wa_error-code = 'BC' .
wa_error-message = 'This will serialize correctly.'.
CREATE OBJECT cl_serializer
EXPORTING
DATA = wa_error.
cl_serializer->serialize( ) .
l_json_string = cl_serializer->get_data( ) .
WRITE l_json_string.
I use zcl_trex_json_serializer which is a clone of cl_trex_json_serializer with the beforementioned fix. This code will return :
{"type": "E", "code": "BC", "message": "This will serialize correctly.", "log_no": "", "log_msg_no": "000000", "message_v1": "", "message_v2": "", "message_v3": "", "message_v4": ""}
I have used this code on structures that contain tables etcetera; the code seems to be able to handle it all.
回答3:
In addition to vwegert's response check out SAP to Json transforms
来源:https://stackoverflow.com/questions/11216032/writing-json-serializer