Skip to content

sbcgua/ajson

Repository files navigation

abaplint abap package version

abap json (ajson)

Yet another JSON parser/serializer for ABAP. It works with release 7.02 or higher.

Recent breaking changes

Since v1.2.0:

  • there are changes in mapper interface, see "Mapping (field renaming)" section in the docs. In essence, implement rename_node method if needed, to_json and to_abap will be deprecated. As well as create_field_mapping and create_camel_case mappers
  • potentially create_empty static method may be deprecated. It is considered to use new instead (and/or direct creation create object). Under consideration, post an issue if you have an opinion on this subject.
  • also create_from is potentially suboptimal, so prefer clone, filter and map instead.

Features

  • parse 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
    • 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)
  • allows conversion to fixed abap structures/tables (to_abap)
  • convenient interface to manipulate the data - set( value ), set( structure ), set( table ), set( another_instance_of_ajson ), also typed e.g. set_date
    • also setx for text-based value setting like setx( '/a/b:123' ) (useful e.g. for constants in APIs or in unit-tests)
  • seralization to string
  • freezing (read only) instance content
  • filtering. Create a JSON skipping empty values, predefined paths, or your custom filter.
  • mapping - rule-based changing node names (e.g. snake case to camel case, upper/lower case)
  • iterating through the array items or object members
  • utility to calculate difference between 2 JSONs

Installed using abapGit, see also Installation section of the documentation.

Example of usage

data r type ref to zif_ajson.
data fragment type ref to zif_ajson.

r = zcl_ajson=>parse( '{"success": 1, "error": "", "payload": {"text": "hello"}}' ).

r->get( '/success' ).              " returns "1"
r->get_integer( '/success' ).      " returns 1 (number)
r->get_boolean( '/success' ).      " returns "X" (abap_true - because not empty)
r->get( '/payload/text' ).         " returns "hello"

r->members( '/' ).                 " returns table of "success", "error", "payload"

fragment = r->slice( '/payload' ).
fragment->get( '/text' ).          " returns "hello" (the root has changed)

fragment->set(
  iv_path = '/text'
  iv_val  = 'new text' ).
fragment->stringify( ).            " {"text":"new text"}

See a lot more examples and usages in the Documentation.

References

About

Yet another json parser serializer for ABAP

Topics

Resources

License

Stars

Watchers

Forks

Contributors 13

Languages