@abapify/asjson-parser 0.0.4 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,101 @@
1
+ # ABAP asJSON Parser
2
+
3
+ [asJSON](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenabap_asjson.htm) - Canonical JSON Representation
4
+
5
+ General asJSON format is based on `%heap` object delivering values and type metadata
6
+
7
+ So when we have a code like this:
8
+
9
+ ```abap
10
+ REPORT ZTEST_HEAP.
11
+
12
+ start-of-selection.
13
+
14
+ types:
15
+ begin of payload_ts,
16
+ i type i,
17
+ i_ref type ref to i,
18
+ end of payload_ts.
19
+
20
+
21
+ data(payload) = value payload_ts(
22
+ i = 123
23
+ i_ref = new #( 123 )
24
+ ).
25
+
26
+ data(json) = cl_sxml_string_writer=>create( if_sxml=>co_xt_json ).
27
+
28
+ call transformation id
29
+ source data = payload
30
+ result xml json.
31
+
32
+ cl_demo_output=>display_json( json = json->get_output( ) ).
33
+ ```
34
+
35
+ the output it generates will be:
36
+
37
+ ```json
38
+ {
39
+ "DATA":
40
+ {
41
+ "I":123,
42
+ "I_REF":
43
+ {
44
+ "%ref":"#d1"
45
+ }
46
+ },
47
+ "%heap":
48
+ {
49
+ "d1":
50
+ {
51
+ "%type":"xsd:int",
52
+ "%val":123
53
+ }
54
+ }
55
+ ```
56
+
57
+ The following library allows us to parse this payload as if we would deal with a regular ABAP object without data references
58
+
59
+ The code below
60
+
61
+ ```ts
62
+ import { parse } from '@abapify/asjson-parser';
63
+ const parser;
64
+ console.log(parse(json));
65
+ ```
66
+
67
+ would print the object like this:
68
+
69
+ ```json
70
+ {
71
+ "DATA":
72
+ {
73
+ "I":123,
74
+ "I_REF": 123
75
+ }
76
+ ```
77
+
78
+ ## Usage
79
+
80
+ - as a parser
81
+
82
+ ```ts
83
+ import { parse } from '@abapify/asjson-parser';
84
+ console.log(parse(json));
85
+ ```
86
+
87
+ - as a transformer
88
+
89
+ ```ts
90
+ import { transform } from '@abapify/asjson-parser';
91
+ const object = JSON.parse(json);
92
+ console.log(transform(object));
93
+ ```
94
+
95
+ - as a proxy (returns a proxied object)
96
+
97
+ ```ts
98
+ import { proxy } from '@abapify/asjson-parser';
99
+ const object = JSON.parse(json);
100
+ console.log(proxy(object));
101
+ ```
package/index.cjs.js CHANGED
@@ -30,25 +30,25 @@ const AbapJsonProxyHandler = {
30
30
  value = value[VAL];
31
31
  }
32
32
  if (typeof value === "object" && value !== null) {
33
- value = fromAbapJsonProxy(Object.assign(value, {
33
+ value = proxy(Object.assign(value, {
34
34
  [HEAP]: heap
35
35
  }));
36
36
  }
37
37
  return value;
38
38
  }
39
39
  };
40
- function fromAbapJsonProxy(target) {
40
+ function proxy(target) {
41
41
  return new Proxy(target, AbapJsonProxyHandler);
42
42
  }
43
- function fromAbapJson(target) {
43
+ function transform(target) {
44
44
  const proxy = new Proxy(target, AbapJsonProxyHandler);
45
45
  return Object.fromEntries(Object.entries(proxy));
46
46
  }
47
47
  function parse(asjson) {
48
- return fromAbapJson(JSON.parse(asjson));
48
+ return transform(JSON.parse(asjson));
49
49
  }
50
50
 
51
51
  exports.AbapJsonProxyHandler = AbapJsonProxyHandler;
52
- exports.fromAbapJson = fromAbapJson;
53
- exports.fromAbapJsonProxy = fromAbapJsonProxy;
54
52
  exports.parse = parse;
53
+ exports.proxy = proxy;
54
+ exports.transform = transform;
package/index.esm.js CHANGED
@@ -28,22 +28,22 @@ const AbapJsonProxyHandler = {
28
28
  value = value[VAL];
29
29
  }
30
30
  if (typeof value === "object" && value !== null) {
31
- value = fromAbapJsonProxy(Object.assign(value, {
31
+ value = proxy(Object.assign(value, {
32
32
  [HEAP]: heap
33
33
  }));
34
34
  }
35
35
  return value;
36
36
  }
37
37
  };
38
- function fromAbapJsonProxy(target) {
38
+ function proxy(target) {
39
39
  return new Proxy(target, AbapJsonProxyHandler);
40
40
  }
41
- function fromAbapJson(target) {
41
+ function transform(target) {
42
42
  const proxy = new Proxy(target, AbapJsonProxyHandler);
43
43
  return Object.fromEntries(Object.entries(proxy));
44
44
  }
45
45
  function parse(asjson) {
46
- return fromAbapJson(JSON.parse(asjson));
46
+ return transform(JSON.parse(asjson));
47
47
  }
48
48
 
49
- export { AbapJsonProxyHandler, fromAbapJson, fromAbapJsonProxy, parse };
49
+ export { AbapJsonProxyHandler, parse, proxy, transform };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abapify/asjson-parser",
3
- "version": "0.0.4",
3
+ "version": "0.1.0",
4
4
  "dependencies": {},
5
5
  "main": "./index.cjs.js",
6
6
  "module": "./index.esm.js",
@@ -1,4 +1,4 @@
1
1
  export declare const AbapJsonProxyHandler: ProxyHandler<object>;
2
- export declare function fromAbapJsonProxy(target: object): object;
3
- export declare function fromAbapJson(target: object): object;
2
+ export declare function proxy(target: object): object;
3
+ export declare function transform(target: object): object;
4
4
  export declare function parse(asjson: string): object;
package/CHANGELOG.md DELETED
@@ -1,11 +0,0 @@
1
- ## 0.0.4 (2024-10-07)
2
-
3
- This was a version bump only, there were no code changes.
4
-
5
- ## 0.0.3 (2024-10-07)
6
-
7
- This was a version bump only, there were no code changes.
8
-
9
- ## 0.0.2 (2024-10-07)
10
-
11
- This was a version bump only, there were no code changes.