Skip to content

koemaeda/abap-json

Repository files navigation

ABAP-JSON

A JSON encoder/parser in ABAP.

Features

  • Pure ABAP (ECC6 compatible)
  • Support for deep ABAP structures and tables (unlimited levels)
  • Output compact or pretty printed JSON
  • Output lower case or upper case field names
  • Correctly outputs/parses dates, times and timestamps
  • Uses conversion exits for input and output based on dynamic typing

Installation

Install the ZCL_JSON class as a global class.

Usage

Methods

encode Import parameters:
CLIKE name (optional) - Object name
ANY value - ABAP value to be encoded

Return: STRING - JSON string.
Encodes an ABAP variable to JSON (recursively).

Simple variables will generate a JSON field (without enclosing brackets).
Structures will generate a JSON object.
Tables will generate a JSON array.
decode Import parameters:
CLIKE json - JSON string

Changing parameters:
ANY value - ABAP value to be set with the JSON content.
Parses a JSON string into an ABAP variable (recursively).

Simple JSON fields should be parsed into simple ABAP variables.
JSON objects should be parsed into ABAP structures.
JSON arrays should be parsed into ABAP tables.

JSON fields that are not found in the ABAP variable will be ignored.
JSON fields that are not compatible with the corresponding ABAP variable (eg. a JSON array matching an ABAP structure) will also be ignored.
Conversion errors are not supported.

Properties

include_empty_values When generating JSON, include ABAP fields that have no value.
The statement IS INITIAL is used to determine if an ABAP field is empty.
pretty_print Generates human-friendly JSON, organized in lines and indented.
This causes a serious performance impact, so it should only be used if the resulting JSON really needs to be read by a human.
lowercase_names When generating JSON, output field names in lower case. Field values are not affected by this.
This is not applicable to JSON parsing, as ABAP variable names are not case sensitive.
use_conversion_exits When generating ou parsing JSON, use corresponding conversion exits for ABAP variables that have it defined in the ABAP dictionary.
ABAP runtime type services (RTTS) is used to read Dictionary information for ABAP fields.
ABAP variables must be correctly typed for this feature to work. Generically typed fields will not be converted.

Code Example

data: s_vendor type vmds_ei_extern,
      o_json   type ref to zcl_json,
      v_json   type string.

cl_erp_vendor_api=>read_vendor(
  exporting iv_lifnr = '0004000000'
  importing es_vendor = s_vendor
).

create object o_json.
o_json->lowercase_names = abap_true.
o_json->include_empty_values = abap_false.
o_json->pretty_print = abap_true.

v_json = o_json->encode( s_vendor ).

Output:

{
  "header": {
    "object_instance": {
      "lifnr": "4000000"
    },
    "object_task": "C"
  },
  "central_data": {
    "central": {
      "data": {
        "ktokk": "VV04",
        "adrnr": "71207"
      }
    },
    "address": {
      "postal": {
        "data": {
          "from_date": "0001-01-01",
          "to_date": "9999-12-31",
          "name": "BLACK HAT EVENTS REGISTRATION DEPT.",
          "city": "SAN FRAN",
          "district": "Suite 900 South Tower",
          "postl_cod1": "99999-9999",
          "street": "303 2ND STREET",
          "house_no": "SN",
          "country": "US",
          "countryiso": "US",
          "langu": "EN",
          "langu_iso": "EN",
          "region": "CA",
          "sort1": "BLACK HAT",
          "time_zone": "PST",
          "langu_cr": "PT",
          "langucriso": "PT"
        }
      },
      "remark": {
        "current_state": "X"
      },
      "communication": {
        "phone": {
          "current_state": "X"
        },
        "fax": {
          "current_state": "X"
        },
        "ttx": {
          "current_state": "X"
        },
        "tlx": {
          "current_state": "X"
        },
        "smtp": {
          "current_state": "X",
          "smtp": [
            {
              "contact": {
                "data": {
                  "std_no": "X",
                  "e_mail": "EMAIL@EMAIL",
                  "email_srch": "EMAIL@EMAIL",
                  "home_flag": "X",
                  "consnumber": "001"
                }
              },
              "remark": {
                "current_state": "X"
              }
            }
          ]
        },
        "rml": {
          "current_state": "X"
        },
        "x400": {
          "current_state": "X"
        },
        "rfc": {
          "current_state": "X"
        },
        "prt": {
          "current_state": "X"
        },
        "ssf": {
          "current_state": "X"
        },
        "uri": {
          "current_state": "X"
        },
        "pager": {
          "current_state": "X"
        }
      },
      "version": {
        "current_state": "X"
      }
    },
    "text": {
      "current_state": "X"
    },
    "vat_number": {
      "current_state": "X"
    },
    "tax_grouping": {
      "current_state": "X"
    },
    "bankdetail": {
      "current_state": "X"
    },
    "subrange": {
      "current_state": "X"
    }
  },
  "company_data": {
    "current_state": "X",
    "company": [
      {
        "data_key": {
          "bukrs": "21"
        },
        "data": {
          "akont": "21011001",
          "zwels": "BCEGMOPRTU",
          "zterm": "D007",
          "fdgrv": "V3",
          "reprf": "X"
        },
        "dunning": {
          "current_state": "X"
        },
        "wtax_type": {
          "current_state": "X"
        },
        "texts": {
          "current_state": "X"
        }
      },
      {
        "data_key": {
          "bukrs": "29"
        },
        "data": {
          "akont": "21011001",
          "zwels": "BCEGMOPRTU",
          "zterm": "D007",
          "fdgrv": "V3",
          "reprf": "X"
        },
        "dunning": {
          "current_state": "X"
        },
        "wtax_type": {
          "current_state": "X"
        },
        "texts": {
          "current_state": "X"
        }
      }
    ]
  },
  "purchasing_data": {
    "current_state": "X",
    "purchasing": [
      {
        "data_key": {
          "ekorg": "VV01"
        },
        "data": {
          "waers": "USD",
          "zterm": "D007"
        },
        "functions": {
          "current_state": "X"
        },
        "texts": {
          "current_state": "X"
        },
        "purchasing2": {
          "current_state": "X"
        }
      }
    ]
  }
}

Contributors

License

This code is distributed under the MIT License, meaning you can freely and unrestrictedly use it, change it, share it, distribute it and package it with your own programs as long as you keep the copyright notice, license and disclaimer.

Releases

No releases published

Packages

No packages published

Languages