Skip to content

Commit 39b0c51

Browse files
committed
break docs into pieces
1 parent d79bde2 commit 39b0c51

File tree

16 files changed

+2140
-59
lines changed

16 files changed

+2140
-59
lines changed

docsite/blog/2022-07-23 - json alternative to maintenance views in abap/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Example of json config
3434

3535
As you can see the content is freely editable JSON text.
3636

37-
Finally, accessing the config parameters is done with a help of [ajson package](https://github.com/sbcgua/ajson). I wrote about it before - [AJSON – yet another abap json parser and serializer](/posts/2020-08-14---ajson---yet-another-abap-json-parser-and-serializer/). Since the original blog post, the library has matured and we use it in several productive tools which exchange the data with external APIs and web services. The tool is designed to be convenient for a developer, so the accessing code looks, for example, as follows:
37+
Finally, accessing the config parameters is done with a help of [ajson package](https://github.com/sbcgua/ajson). I wrote about it before - [AJSON – yet another abap json parser and serializer](/blog/ajson-yet-another-abap-json-parser-and-serializer/). Since the original blog post, the library has matured and we use it in several productive tools which exchange the data with external APIs and web services. The tool is designed to be convenient for a developer, so the accessing code looks, for example, as follows:
3838

3939
```abap
4040
data lo_json_settings type ref to zif_ajson.

docsite/docs/01-intro.md

Lines changed: 0 additions & 44 deletions
This file was deleted.

docsite/docs/10-intro.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
sidebar_position: 10
3+
---
4+
5+
# Introduction
6+
7+
AJson is a json parser/serializer for ABAP designed with the convenience for developer in mind. It works with abap release 7.02 or higher.
8+
9+
## Features
10+
11+
- Parse json content into a flexible form, not fixed to any predefined data structure, allowing to modify the parsed data, selectively access its parts and slice subsections of it
12+
- slicing can be particularly useful for REST header separation e.g. `{ "success": 1, "error": "", "payload": {...} }` where 1st level attrs are processed in one layer of your application and payload in another (and can differ from request to request)
13+
- Allows conversion to fixed abap structures/tables (`to_abap`)
14+
- Convenient interface to manipulate the data - `set( value )`, `set( structure )`, `set( table )`, `set( another_instance_of_ajson )`, also typed e.g. `set_date`
15+
- also `setx` for text-based value setting like `setx( '/a/b:123' )` (useful e.g. for constants in APIs or in unit-tests)
16+
- Seralization to string
17+
- Freezing (read only) instance content
18+
- Filtering - create a json skipping empty values, predefined paths, or your custom filter.
19+
- Mapping - rule-based changing node names (e.g. snake case to camel case, upper/lower case)
20+
- Utility to calculate difference between 2 jsons
21+
- Supports: timestamps, reference initialization
22+
23+
## Example of usage
24+
25+
```abap
26+
data r type ref to zif_ajson.
27+
data fragment type ref to zif_ajson.
28+
29+
r = zcl_ajson=>parse( '{"success": 1, "error": "", "payload": {"text": "hello"}}' ).
30+
31+
r->get( '/success' ). " returns "1"
32+
r->get_integer( '/success' ). " returns 1 (number)
33+
r->get_boolean( '/success' ). " returns "X" (abap_true - because not empty)
34+
r->get( '/payload/text' ). " returns "hello"
35+
36+
r->members( '/' ). " returns table of "success", "error", "payload"
37+
38+
fragment = r->slice( '/payload' ).
39+
fragment->get( '/text' ). " returns "hello" (the root has changed)
40+
41+
fragment->set(
42+
iv_path = '/text'
43+
iv_val = 'new text' ).
44+
fragment->stringify( ). " {"text":"new text"}
45+
```
46+
47+
See more examples and usages in the further docs.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 2
2+
sidebar_position: 20
33
---
44

55
# Installation

docsite/docs/30-basics.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
sidebar_position: 30
3+
---
4+
5+
# Instantiating and basics
6+
7+
- To **parse** existing json data - call `zcl_ajson=>parse( lv_json_string )`
8+
- To create a new **empty** json instance (to set values and serialize) - call `zcl_ajson=>new( )` or `new zcl_ajson( )` in newer abap syntax
9+
- You can **clone** an ajson instance to get a new independent copy with `lo_orig_json->clone( )`
10+
11+
## Basics
12+
13+
- All functional methods and types are defined in `zif_ajson` interface.
14+
- Methods have aliases in the `zcl_ajson` class, however please restrain from using them directly as they may be *deprecated* in future.
15+
- Json attributes are addressed by path in form `/obj1/obj2/value` of e.g. `->get( '/a/b/c' )` addresses `{ "a": { "b": { "c": "this value !" } } }`
16+
- Array items addressed with index starting from 1: `/tab/2/val` -> `{ "tab": [ {...}, { "val": "this value !" } ] }`
17+
18+
## Support for '/' in attribute names
19+
20+
*TBD*
21+
22+
## Chaining
23+
24+
- `Set` (and some other) methods also return `me` to support chaining: `li_json->set(...)->set(...)->touch_array(...)->push(...)`.
25+
26+
## Mapping and filtering libraries
27+
28+
- Mapping and formatting are enabled by interface `zif_ajson_mapping`. Predefined patterns for field mapping (ABAP ⇆ JSON), Camel Case, UPPER/lower case can be found in class `zcl_ajson_mapping`
29+
- Predefined filters are available in `zcl_ajson_filter_lib` class
30+
31+
## Creating from (deprecated?)
32+
33+
```abap
34+
lo_new_json = zcl_ajson=>create_from(
35+
ii_source_json = lo_orig_json ).
36+
```

docsite/docs/40-reading.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
sidebar_position: 40
3+
---
4+
5+
# Reading JSON values
6+
7+
The methods of interface allows accessing attributes and converting to abap structure.
8+
9+
Examples below assume original json was:
10+
11+
```json
12+
{
13+
"success": 1,
14+
"error": "",
15+
"payload": {
16+
"text": "hello",
17+
"num": 123,
18+
"bool": true,
19+
"false": false,
20+
"null": null,
21+
"date": "2020-07-28",
22+
"table": [
23+
"abc",
24+
"def"
25+
]
26+
}
27+
}
28+
```
29+
30+
#### Individual value reading
31+
32+
```abap
33+
data r type ref to zif_ajson.
34+
r = zcl_ajson=>parse( lv_json_string_from_above ).
35+
36+
r->is_empty( ). " returns abap_false
37+
r->exists( '/success' ). " returns abap_true
38+
39+
r->get( '/success' ). " returns "1"
40+
r->get_integer( '/success' ). " returns 1 (number)
41+
r->get_boolean( '/success' ). " returns "X" (abap_true - because not empty)
42+
43+
r->get( '/payload/bool' ). " returns "true"
44+
r->get_boolean( '/payload/bool' ). " returns "X" (abap_true)
45+
46+
r->get( '/payload/false' ). " returns "false"
47+
r->get_boolean( '/payload/false' ). " returns "" (abap_false)
48+
49+
r->get( '/payload/null' ). " returns "null"
50+
r->get_string( '/payload/null' ). " returns "" (empty string)
51+
52+
r->get( '/payload/date' ). " returns "2020-07-28"
53+
r->get_date( '/payload/date' ). " returns "20200728" (type d)
54+
55+
r->members( '/' ). " returns table of "success", "error", "payload"
56+
```
57+
58+
#### Segment slicing
59+
60+
```abap
61+
" Slice returns zif_ajson instance but "payload" becomes root
62+
" Useful to process API responses with unified wrappers
63+
data payload type ref to zif_ajson.
64+
payload = r->slice( '/payload' ).
65+
```
66+
67+
#### Getting node type
68+
69+
In some case you might want to know node type prior to accessing it first. Type can be 'str', 'num', 'null', 'bool', 'object', 'array'.
70+
71+
```abap
72+
r->get_node_type( '/payload/false' ). " returns "bool"
73+
r->get_node_type( '/payload/text' ). " returns "str"
74+
```
75+
76+
#### Converting to abap structure
77+
78+
```abap
79+
data:
80+
begin of ls_payload,
81+
text type string,
82+
num type i,
83+
bool type abap_bool,
84+
false type abap_bool,
85+
null type string,
86+
table type string_table, " Array !
87+
end of ls_payload.
88+
89+
payload->to_abap( importing ev_container = ls_payload ).
90+
```
91+
92+
`to_abap` supports transferring "corresponding only" fields.
93+
94+
```abap
95+
payload->to_abap(
96+
exporting iv_corresponding = abap_true
97+
importing ev_container = ls_payload ).
98+
99+
" Or via an instance flag (persists after setting!)
100+
payload->to_abap_corresponding_only( )->to_abap( importing ev_container = ls_payload ).
101+
```

0 commit comments

Comments
 (0)