Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
larshp
Active Contributor
I have many unit tests which takes JSON as input, however editing JSON embedded in a ABAP string is a mess. And no, ecatt test data containers is not an option, never use it for anything in unit tests, but its a different story.

One option is to use string templates,
DATA(json) = |\{\n| &&
| "hello": 2\n| &&
|\}|.

It does require escaping the squiggly brackets, so I tend to do the following instead,
DATA(json) = `{` && |\n| &&
` "hello": 2` && |\n| &&
`}`.

Which is also a mess, but easy to add when using multi cursors.

After adding the JSON, I sometimes need to edit the JSON, and I typically mess it up, so the syntax is wrong or something else ๐Ÿ˜’

Lets add tooling to easily edit the JSON ๐Ÿ˜Ž


 

Introducing abap-json-editor, right click the start of the ABAP string to open an new editor. Changes in the JSON editor is automatically validated and reflected in the ABAP code,


Plus it allows for easy pasting of JSON from clipboard,


 

The extension will work with standalone ABAP files, files from ABAP fs by murbani, plus on web only

Immediately released to everyone ๐Ÿ˜… bug reports and fixes welcome on github.

 
9 Comments
matt
Active Contributor
Excellent!
Sandra_Rossi
Active Contributor
If I'm not wrong, there's nothing in Eclipse/ADT to help in this (so nicely), hopefully SAP developers have probably subscribe to your blog posts and will include such a nice feature soon...
fabianlupa
Contributor
Cool workaround! Hopefully ABAP will get multi line string literals at some point.
DATA(json) = ```
{
"hello": 1
}
```.

DATA(number) = 1.
DATA(json2) = |||
\{
"hello": { number }
\}
|||.

ADT has a feature to escape and line break contents when copy pasting it. But that doesn't help editing or reading anything later.

How to wrap long strings automatically in ADT | SAP Blogs

 
olegbash599
Active Participant
Your extension is good! Thank you!

 

However, for my opinion for unit tests more understandable would be use abap-structures like in the following
CLASS ltc DEFINITION FOR TESTING DURATION SHORT RISK LEVEL HARMLESS.

PUBLIC SECTION.
METHODS mega_feature_by_json_input FOR TESTING.

PRIVATE SECTION.
METHODS _get_json4mega RETURNING VALUE(rv_val) TYPE string.
ENDCLASS.

CLASS ltc IMPLEMENTATION.
METHOD mega_feature_by_json_input.
DATA(lv_json_input) = _get_json4mega( ).
ENDMETHOD.

METHOD _get_json4mega.
"RETURNING VALUE(rv_val) TYPE string.
TYPES: BEGIN OF ts_target_fields
, hello TYPE string
, world TYPE int1
, startdate TYPE sydatum
, END OF ts_target_fields
.
DATA ls_target_fields TYPE ts_target_fields.

ls_target_fields-hello = 'Data Dictionary More than just structure' .
ls_target_fields-world = '42'.
ls_target_fields-startdate = '19720401'.

rv_val =
/ui2/cl_json=>serialize( EXPORTING data = ls_target_fields
pretty_name = /ui2/cl_json=>pretty_mode-low_case ).
ENDMETHOD.
ENDCLASS.
larshp
Active Contributor
0 Kudos
yea, hopefully ๐Ÿ™‚
larshp
Active Contributor
yea, its just a different kind of test

if the team knows JSON, and is used to transfer JSON, its a different world to know the ABAP language. If the input to the logic is JSON then I recommend testing it as JSON
larshp
Active Contributor
well, to be fair, I did spend a few hours messing around with the vscode API
Chad_He
Participant
0 Kudos
Cool feature.

And some questions.

I'd like to know, did you often use VS Code to edit ABAP?

Or just some specific scenarios that like JSON-EDIT?

In your opinion, is VS Code better than ADT when edit abap except CDS?
larshp
Active Contributor
I'd say I spend 80->90% of my time developing ABAP in vscode, in a standalone setup editing just files -> pushing -> git -> then pulling into the system with abapGit.

The rest of the time I use SE24/SE80, I dont do very much CDS.