Advertisement
rplantiko

Complete Code of ZCL_JSON_PARSER

Sep 7th, 2011
1,355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ABAP 21.55 KB | None | 0 0
  1. * A complete image of the class ZCL_JSON_PARSER
  2. * as generated with function code SHOW_CLIF in transaction SE24
  3.  
  4. *----------------------------------------------------------------------*
  5. *       CLASS ZCL_JSON_PARSER  DEFINITIO
  6. *----------------------------------------------------------------------*
  7. *
  8. *----------------------------------------------------------------------*
  9. class zcl_json_parser definition
  10.   public
  11.   create public .
  12.  
  13. *"* public components of class ZCL_JSON_PARSER
  14. *"* do not include other source files here!!!
  15.   public section.
  16.  
  17.     methods parse
  18.       importing
  19.         !iv_json type string
  20.       returning
  21.         value(es_data) type zut_data
  22.       raising
  23.         zcx_parse_error .
  24. *"* protected components of class ZCL_JSON_PARSER
  25. *"* do not include other source files here!!!
  26. protected section.
  27. *"* private components of class ZCL_JSON_PARSER
  28. *"* do not include other source files here!!!
  29. private section.
  30.  
  31.   methods MAP_CHAR
  32.     importing
  33.       !IV_CHAR type CSEQUENCE
  34.     returning
  35.       value(EV_MAPPED_CHAR) type CHAR01 .
  36.   methods GET_ANY
  37.     importing
  38.       !IV_JSON type STRING
  39.     exporting
  40.       !ES_DATA type ZUT_DATA
  41.       !EV_FOUND type FLAG
  42.     changing
  43.       !CV_POS type I .
  44.   methods GET_ELEMENT
  45.     importing
  46.       !IV_JSON type STRING
  47.     exporting
  48.       !ES_ELEMENT type ZUT_HASH_ELEMENT
  49.       !EV_FOUND type FLAG
  50.     changing
  51.       !CV_POS type I .
  52.   methods GET_ARRAY
  53.     importing
  54.       !IV_JSON type STRING
  55.     exporting
  56.       !ET_ARRAY type ZUT_ARRAY_TAB
  57.       !EV_FOUND type FLAG
  58.     changing
  59.       !CV_POS type I .
  60.   methods GET_HASH
  61.     importing
  62.       !IV_JSON type STRING
  63.     exporting
  64.       !ET_HASH type ZUT_HASH_TAB
  65.       !EV_FOUND type FLAG
  66.     changing
  67.       !CV_POS type I .
  68.   methods GET_KEY
  69.     importing
  70.       !IV_JSON type STRING
  71.     exporting
  72.       !EV_KEY type STRING
  73.       !EV_FOUND type FLAG
  74.     changing
  75.       !CV_POS type I .
  76.   methods GET_NAME
  77.     importing
  78.       !IV_JSON type STRING
  79.     exporting
  80.       !EV_NAME type STRING
  81.       !EV_FOUND type FLAG
  82.     changing
  83.       !CV_POS type I .
  84.   methods GET_NUMBER
  85.     importing
  86.       !IV_JSON type STRING
  87.     exporting
  88.       !EV_NUMBER type ref to DATA
  89.       !EV_FOUND type FLAG
  90.     changing
  91.       !CV_POS type I .
  92.   methods GET_BOOLEAN
  93.     importing
  94.       !IV_JSON type STRING
  95.     exporting
  96.       !EV_BOOLEAN type FLAG
  97.       !EV_FOUND type FLAG
  98.     changing
  99.       !CV_POS type I .
  100.   methods GET_STRING
  101.     importing
  102.       !IV_JSON type STRING
  103.       !IV_DELIMITER type CHAR01 optional
  104.     exporting
  105.       !EV_STRING type STRING
  106.       !EV_FOUND type FLAG
  107.     changing
  108.       !CV_POS type I .
  109.   methods GET_SYMBOL
  110.     importing
  111.       !IV_JSON type STRING
  112.       !IV_SYMBOL type CSEQUENCE
  113.     exporting
  114.       !EV_FOUND type FLAG
  115.     changing
  116.       !CV_POS type I .
  117.   methods SKIP_WHITESPACE
  118.     importing
  119.       !IV_JSON type STRING
  120.     changing
  121.       !CV_POS type I .
  122. ENDCLASS.
  123.  
  124.  
  125.  
  126. CLASS ZCL_JSON_PARSER IMPLEMENTATION.
  127.  
  128.  
  129. * <SIGNATURE>---------------------------------------------------------------------------------------+
  130. * | Instance Private Method ZCL_JSON_PARSER->GET_ANY
  131. * +-------------------------------------------------------------------------------------------------+
  132. * | [--->] IV_JSON                        TYPE        STRING
  133. * | [<---] ES_DATA                        TYPE        ZUT_DATA
  134. * | [<---] EV_FOUND                       TYPE        FLAG
  135. * | [<-->] CV_POS                         TYPE        I
  136. * +--------------------------------------------------------------------------------------</SIGNATURE>
  137. method get_any.
  138.  
  139.   data: ls_data      type zut_data,
  140.         lv_pos       type i.
  141.  
  142.   field-symbols: <lv_boolean> type flag,
  143.                  <lv_string>  type string,
  144.                  <lt_hash>    type zut_hash_tab,
  145.                  <lt_array>   type zut_array_tab.
  146.  
  147.   ev_found = space.
  148.  
  149.   lv_pos = cv_pos.
  150.  
  151.   skip_whitespace( exporting iv_json = iv_json
  152.                    changing  cv_pos  = lv_pos ).
  153.   check lv_pos < strlen( iv_json ).
  154.  
  155.   case iv_json+lv_pos(1).
  156.     when '"' or `'`.
  157.       create data ls_data-data type string.
  158.       assign ls_data-data->* to <lv_string>.
  159.       ls_data-type = 'S'.
  160.       call method get_string
  161.         exporting
  162.           iv_json   = iv_json
  163.         importing
  164.           ev_string = <lv_string>
  165.           ev_found  = ev_found
  166.         changing
  167.           cv_pos    = lv_pos.
  168.  
  169.     when '{'.
  170.       create data ls_data-data type zut_hash_tab.
  171.       assign ls_data-data->* to <lt_hash>.
  172.       ls_data-type = 'h'.
  173.       call method get_hash
  174.         exporting
  175.           iv_json  = iv_json
  176.         importing
  177.           et_hash  = <lt_hash>
  178.           ev_found = ev_found
  179.         changing
  180.           cv_pos   = lv_pos.
  181.  
  182.     when '['.
  183.       create data ls_data-data type zut_array_tab.
  184.       assign ls_data-data->* to <lt_array>.
  185.       ls_data-type = 'a'.
  186.       call method get_array
  187.         exporting
  188.           iv_json  = iv_json
  189.         importing
  190.           et_array = <lt_array>
  191.           ev_found = ev_found
  192.         changing
  193.           cv_pos   = lv_pos.
  194.  
  195.     when 't' or 'f'.
  196.       create data ls_data-data type flag.
  197.       assign ls_data-data->* to <lv_boolean>.
  198.       ls_data-type = 'B'.
  199.       call method get_boolean
  200.         exporting
  201.           iv_json    = iv_json
  202.         importing
  203.           ev_boolean = <lv_boolean>
  204.           ev_found   = ev_found
  205.         changing
  206.           cv_pos     = lv_pos.
  207.  
  208.     when others.
  209.       if iv_json+lv_pos(1) ca '0123456789-'.
  210.         ls_data-type = 'N'.
  211.         call method get_number
  212.           exporting
  213.             iv_json   = iv_json
  214.           importing
  215.             ev_number = ls_data-data
  216.             ev_found  = ev_found
  217.           changing
  218.             cv_pos    = lv_pos.
  219.       endif.
  220.   endcase.
  221.  
  222.   if ev_found = 'X'.
  223.     cv_pos  = lv_pos.
  224.     es_data = ls_data.
  225.   endif.
  226.  
  227. endmethod.
  228.  
  229.  
  230. * <SIGNATURE>---------------------------------------------------------------------------------------+
  231. * | Instance Private Method ZCL_JSON_PARSER->GET_ARRAY
  232. * +-------------------------------------------------------------------------------------------------+
  233. * | [--->] IV_JSON                        TYPE        STRING
  234. * | [<---] ET_ARRAY                       TYPE        ZUT_ARRAY_TAB
  235. * | [<---] EV_FOUND                       TYPE        FLAG
  236. * | [<-->] CV_POS                         TYPE        I
  237. * +--------------------------------------------------------------------------------------</SIGNATURE>
  238. method get_array.
  239.  
  240.   data: ls_element  type zut_data,
  241.         lv_pos      type i,
  242.         lv_found    type flag,
  243.         lv_key      type string.
  244.  
  245.   clear ev_found.
  246.   lv_pos = cv_pos.
  247.  
  248.  
  249.   get_symbol( exporting iv_json = iv_json
  250.                         iv_symbol = '['
  251.               importing ev_found = lv_found
  252.               changing  cv_pos  = lv_pos ).
  253.   check lv_found = 'X'.
  254.  
  255.   while lv_found eq 'X'.
  256.  
  257.     call method get_any
  258.       exporting
  259.         iv_json  = iv_json
  260.       importing
  261.         es_data  = ls_element
  262.         ev_found = lv_found
  263.       changing
  264.         cv_pos   = lv_pos.
  265.  
  266.     if lv_found = 'X'.
  267.  
  268.       insert ls_element into table et_array.
  269.  
  270.       call method get_symbol
  271.         exporting
  272.           iv_json   = iv_json
  273.           iv_symbol = ','
  274.         importing
  275.           ev_found  = lv_found
  276.         changing
  277.           cv_pos    = lv_pos.
  278.  
  279.     endif.
  280.  
  281.   endwhile.
  282.  
  283.  
  284.   get_symbol( exporting iv_json = iv_json
  285.                         iv_symbol = ']'
  286.               importing ev_found = lv_found
  287.               changing  cv_pos  = lv_pos ).
  288.   check lv_found = 'X'.
  289.  
  290.   ev_found = 'X'.
  291.   cv_pos = lv_pos.
  292.  
  293. endmethod.
  294.  
  295.  
  296. * <SIGNATURE>---------------------------------------------------------------------------------------+
  297. * | Instance Private Method ZCL_JSON_PARSER->GET_BOOLEAN
  298. * +-------------------------------------------------------------------------------------------------+
  299. * | [--->] IV_JSON                        TYPE        STRING
  300. * | [<---] EV_BOOLEAN                     TYPE        FLAG
  301. * | [<---] EV_FOUND                       TYPE        FLAG
  302. * | [<-->] CV_POS                         TYPE        I
  303. * +--------------------------------------------------------------------------------------</SIGNATURE>
  304. method get_boolean.
  305.  
  306.   data: lv_symbol type string,
  307.         lv_boolean type flag.
  308.  
  309.  
  310.   check cv_pos < strlen( iv_json ).
  311.  
  312.   case iv_json+cv_pos(1).
  313.     when 't'.
  314.       lv_symbol = 'true'.
  315.       lv_boolean = 'X'.
  316.     when 'f'.
  317.       lv_symbol = 'false'.
  318.       lv_boolean = space.
  319.   endcase.
  320.  
  321.   call method get_symbol
  322.     exporting
  323.       iv_json   = iv_json
  324.       iv_symbol = lv_symbol
  325.     importing
  326.       ev_found  = ev_found
  327.     changing
  328.       cv_pos    = cv_pos.
  329.  
  330.   if ev_found = 'X'.
  331.     ev_boolean = lv_boolean.
  332.   endif.
  333.  
  334. endmethod.
  335.  
  336.  
  337. * <SIGNATURE>---------------------------------------------------------------------------------------+
  338. * | Instance Private Method ZCL_JSON_PARSER->GET_ELEMENT
  339. * +-------------------------------------------------------------------------------------------------+
  340. * | [--->] IV_JSON                        TYPE        STRING
  341. * | [<---] ES_ELEMENT                     TYPE        ZUT_HASH_ELEMENT
  342. * | [<---] EV_FOUND                       TYPE        FLAG
  343. * | [<-->] CV_POS                         TYPE        I
  344. * +--------------------------------------------------------------------------------------</SIGNATURE>
  345. method get_element.
  346.  
  347.   data: lv_pos     type i,
  348.         lv_found   type flag,
  349.         ls_element type zut_hash_element.
  350.  
  351.   clear: ev_found, es_element.
  352.  
  353.   lv_pos = cv_pos.
  354.  
  355. * Schlüssel
  356.   call method get_key
  357.     exporting
  358.       iv_json  = iv_json
  359.     importing
  360.       ev_key   = ls_element-key
  361.       ev_found = lv_found
  362.     changing
  363.       cv_pos   = lv_pos.
  364.   check lv_found eq 'X'.
  365.  
  366. * Doppelpunkt
  367.   get_symbol( exporting iv_json = iv_json
  368.                         iv_symbol = ':'
  369.               importing ev_found = lv_found
  370.               changing  cv_pos  = lv_pos ).
  371.   check lv_found = 'X'.
  372.  
  373. * Wert
  374.   call method get_any
  375.     exporting
  376.       iv_json  = iv_json
  377.     importing
  378.       es_data  = ls_element-value
  379.       ev_found = lv_found
  380.     changing
  381.       cv_pos   = lv_pos.
  382.  
  383.   if lv_found = 'X'.
  384.     ev_found   = 'X'.
  385.     cv_pos     = lv_pos.
  386.     es_element = ls_element.
  387.   endif.
  388.  
  389. endmethod.
  390.  
  391.  
  392. * <SIGNATURE>---------------------------------------------------------------------------------------+
  393. * | Instance Private Method ZCL_JSON_PARSER->GET_HASH
  394. * +-------------------------------------------------------------------------------------------------+
  395. * | [--->] IV_JSON                        TYPE        STRING
  396. * | [<---] ET_HASH                        TYPE        ZUT_HASH_TAB
  397. * | [<---] EV_FOUND                       TYPE        FLAG
  398. * | [<-->] CV_POS                         TYPE        I
  399. * +--------------------------------------------------------------------------------------</SIGNATURE>
  400. method get_hash.
  401.  
  402.   data: ls_element  type zut_hash_element,
  403.         lv_pos      type i,
  404.         lv_found    type flag,
  405.         lv_key      type string.
  406.  
  407.   clear ev_found.
  408.   lv_pos = cv_pos.
  409.  
  410.  
  411.   get_symbol( exporting iv_json = iv_json
  412.                         iv_symbol = '{'
  413.               importing ev_found = lv_found
  414.               changing  cv_pos  = lv_pos ).
  415.   check lv_found = 'X'.
  416.  
  417.   while lv_found eq 'X'.
  418.  
  419.     call method get_element
  420.       exporting
  421.         iv_json    = iv_json
  422.       importing
  423.         es_element = ls_element
  424.         ev_found   = lv_found
  425.       changing
  426.         cv_pos     = lv_pos.
  427.  
  428.     if lv_found = 'X'.
  429.  
  430.       insert ls_element into table et_hash.
  431.  
  432.       call method get_symbol
  433.         exporting
  434.           iv_json   = iv_json
  435.           iv_symbol = ','
  436.         importing
  437.           ev_found  = lv_found
  438.         changing
  439.           cv_pos    = lv_pos.
  440.  
  441.     endif.
  442.  
  443.   endwhile.
  444.  
  445.  
  446.   get_symbol( exporting iv_json = iv_json
  447.                         iv_symbol = '}'
  448.               importing ev_found = lv_found
  449.               changing  cv_pos  = lv_pos ).
  450.   check lv_found = 'X'.
  451.  
  452.   ev_found = 'X'.
  453.   cv_pos = lv_pos.
  454.  
  455. endmethod.
  456.  
  457.  
  458. * <SIGNATURE>---------------------------------------------------------------------------------------+
  459. * | Instance Private Method ZCL_JSON_PARSER->GET_KEY
  460. * +-------------------------------------------------------------------------------------------------+
  461. * | [--->] IV_JSON                        TYPE        STRING
  462. * | [<---] EV_KEY                         TYPE        STRING
  463. * | [<---] EV_FOUND                       TYPE        FLAG
  464. * | [<-->] CV_POS                         TYPE        I
  465. * +--------------------------------------------------------------------------------------</SIGNATURE>
  466. method get_key.
  467.  
  468.   data: lv_pos          type i,
  469.         lv_found        type flag.
  470.  
  471.   ev_found = space.
  472.   check cv_pos < strlen( iv_json ).
  473.  
  474.   lv_pos = cv_pos.
  475.  
  476. * Ist der Key als String notiert?
  477.   call method get_string
  478.     exporting
  479.       iv_json   = iv_json
  480.     importing
  481.       ev_string = ev_key
  482.       ev_found  = lv_found
  483.     changing
  484.       cv_pos    = lv_pos.
  485.  
  486.   if lv_found = space.
  487. * Zweiter Versuch: Symbolischer Name
  488.     call method get_name
  489.       exporting
  490.         iv_json  = iv_json
  491.       importing
  492.         ev_name  = ev_key
  493.         ev_found = lv_found
  494.       changing
  495.         cv_pos   = lv_pos.
  496.   endif.
  497.  
  498.   if lv_found = 'X'.
  499.     ev_found = lv_found.
  500.     cv_pos   = lv_pos.
  501.   endif.
  502.  
  503. endmethod.
  504.  
  505.  
  506. * <SIGNATURE>---------------------------------------------------------------------------------------+
  507. * | Instance Private Method ZCL_JSON_PARSER->GET_NAME
  508. * +-------------------------------------------------------------------------------------------------+
  509. * | [--->] IV_JSON                        TYPE        STRING
  510. * | [<---] EV_NAME                        TYPE        STRING
  511. * | [<---] EV_FOUND                       TYPE        FLAG
  512. * | [<-->] CV_POS                         TYPE        I
  513. * +--------------------------------------------------------------------------------------</SIGNATURE>
  514. method GET_NAME.
  515.  
  516.   data: lv_name         type string,
  517.         lv_length       type i.
  518.  
  519.   ev_found = ev_name = space.
  520.   check cv_pos < strlen( iv_json ).
  521.  
  522.   find regex '^\s*([a-z_]\w*)' in section offset cv_pos of iv_json
  523.                            submatches lv_name
  524.                            match length lv_length.
  525.   if sy-subrc eq 0.
  526.     ev_found = 'X'.
  527.     cv_pos   = cv_pos + lv_length.
  528.     ev_name  = lv_name.
  529.   endif.
  530.  
  531.  
  532. endmethod.
  533.  
  534.  
  535. * <SIGNATURE>---------------------------------------------------------------------------------------+
  536. * | Instance Private Method ZCL_JSON_PARSER->GET_NUMBER
  537. * +-------------------------------------------------------------------------------------------------+
  538. * | [--->] IV_JSON                        TYPE        STRING
  539. * | [<---] EV_NUMBER                      TYPE REF TO DATA
  540. * | [<---] EV_FOUND                       TYPE        FLAG
  541. * | [<-->] CV_POS                         TYPE        I
  542. * +--------------------------------------------------------------------------------------</SIGNATURE>
  543. method get_number.
  544.  
  545.   data: lv_pos    type i,
  546.         lv_exp    type string,
  547.         lv_length type i.
  548.  
  549.   field-symbols: <lv_number> type numeric.
  550.  
  551.   clear ev_number.
  552.   ev_found = space.
  553.   lv_pos = cv_pos.
  554.  
  555.   find regex '^\s*([\d.-]+(e-?\d+)?)' in iv_json+lv_pos submatches lv_exp match length lv_length.
  556.   if sy-subrc eq 0.
  557.     add lv_length to lv_pos.
  558. * Ganze Zahl?
  559.     if lv_exp co '0123456789'.
  560.       create data ev_number type i.
  561.     else.
  562.       find regex '^\d*\.\d+|\d+\.\d*$' in lv_exp.
  563.       if sy-subrc eq 0.
  564.         create data ev_number type f.
  565.       endif.
  566.     endif.
  567.  
  568.     if ev_number is bound.
  569.       assign ev_number->* to <lv_number>.
  570. * Hier überlassen wir die Feinheiten des Parsings dem ABAP-Befehl MOVE:
  571.       <lv_number> = lv_exp.
  572.       ev_found = 'X'.
  573.     endif.
  574.  
  575.   endif.
  576.  
  577.  
  578.   if ev_found = 'X'.
  579.     cv_pos = lv_pos.
  580.   endif.
  581.  
  582. endmethod.
  583.  
  584.  
  585. * <SIGNATURE>---------------------------------------------------------------------------------------+
  586. * | Instance Private Method ZCL_JSON_PARSER->GET_STRING
  587. * +-------------------------------------------------------------------------------------------------+
  588. * | [--->] IV_JSON                        TYPE        STRING
  589. * | [--->] IV_DELIMITER                   TYPE        CHAR01(optional)
  590. * | [<---] EV_STRING                      TYPE        STRING
  591. * | [<---] EV_FOUND                       TYPE        FLAG
  592. * | [<-->] CV_POS                         TYPE        I
  593. * +--------------------------------------------------------------------------------------</SIGNATURE>
  594. method get_string.
  595.  
  596.   data: lv_pos             type i,
  597.         lv_delimiter(1)    type c,
  598.         lv_char(1)         type c,
  599.         lv_mapped_char(1)  type c.
  600.  
  601.   ev_found = space.
  602.  
  603.   lv_pos = cv_pos.
  604.   call method skip_whitespace
  605.     exporting
  606.       iv_json = iv_json
  607.     changing
  608.       cv_pos  = lv_pos.
  609.  
  610.   check lv_pos < strlen( iv_json ).
  611.  
  612.   if iv_delimiter is not initial.
  613.     lv_delimiter = iv_delimiter.
  614.     check iv_json+lv_pos(1) eq lv_delimiter.
  615.   else.
  616.     lv_delimiter = iv_json+lv_pos(1).
  617.     check lv_delimiter ca `'"`.
  618.   endif.
  619.  
  620.  
  621.   do.
  622.  
  623.     add 1 to lv_pos.
  624.  
  625.     if strlen( iv_json ) <= lv_pos.
  626.       exit.
  627.     endif.
  628.  
  629. * Escaped sequences finden und auflösen
  630.     find regex `^\\(['"/bfnrt\\])` in section offset lv_pos of iv_json submatches lv_char.
  631.     if sy-subrc eq 0.
  632.       if lv_char ca `bfnrt`.
  633.         lv_mapped_char = map_char( lv_char ).
  634.       else.
  635.         lv_mapped_char = lv_char.  " Else auch hier, wg. Performance
  636.       endif.
  637.       concatenate ev_string lv_mapped_char into ev_string.
  638.       add 1 to lv_pos.
  639.       continue.
  640.     endif.
  641.  
  642.     if iv_json+lv_pos(1) eq lv_delimiter.
  643.       ev_found = 'X'.
  644.       exit.
  645.     endif.
  646.  
  647.     concatenate ev_string iv_json+lv_pos(1) into ev_string.
  648.  
  649.   enddo.
  650.  
  651.   if ev_found = 'X'.
  652.     add 1 to lv_pos.
  653.     cv_pos = lv_pos.
  654.   endif.
  655.  
  656. endmethod.
  657.  
  658.  
  659. * <SIGNATURE>---------------------------------------------------------------------------------------+
  660. * | Instance Private Method ZCL_JSON_PARSER->GET_SYMBOL
  661. * +-------------------------------------------------------------------------------------------------+
  662. * | [--->] IV_JSON                        TYPE        STRING
  663. * | [--->] IV_SYMBOL                      TYPE        CSEQUENCE
  664. * | [<---] EV_FOUND                       TYPE        FLAG
  665. * | [<-->] CV_POS                         TYPE        I
  666. * +--------------------------------------------------------------------------------------</SIGNATURE>
  667. method get_symbol.
  668.  
  669.   clear ev_found.
  670.  
  671.   skip_whitespace(
  672.     exporting iv_json = iv_json
  673.     changing  cv_pos  = cv_pos ).
  674.  
  675.   check cv_pos < strlen( iv_json ).
  676.  
  677.   if iv_json+cv_pos cs iv_symbol and sy-fdpos = 0.
  678.     ev_found = 'X'.
  679.     cv_pos = cv_pos + strlen( iv_symbol ).
  680.   endif.
  681.  
  682. endmethod.
  683.  
  684.  
  685. * <SIGNATURE>---------------------------------------------------------------------------------------+
  686. * | Instance Private Method ZCL_JSON_PARSER->MAP_CHAR
  687. * +-------------------------------------------------------------------------------------------------+
  688. * | [--->] IV_CHAR                        TYPE        CSEQUENCE
  689. * | [<-()] EV_MAPPED_CHAR                 TYPE        CHAR01
  690. * +--------------------------------------------------------------------------------------</SIGNATURE>
  691. method MAP_CHAR.
  692.   case iv_char.
  693.     when 'b'.
  694.       ev_mapped_char = cl_abap_char_utilities=>backspace.
  695.     when 'f'.
  696.       ev_mapped_char = cl_abap_char_utilities=>form_feed.
  697.     when 'n'.
  698.       ev_mapped_char = cl_abap_char_utilities=>newline.
  699.     when 'r'.
  700.       ev_mapped_char = cl_abap_char_utilities=>Cr_lf(1).
  701.     when 't'.
  702.       ev_mapped_char = cl_abap_char_utilities=>horizontal_tab.
  703.     when others.
  704.       ev_mapped_char = iv_char.
  705.   endcase.
  706. endmethod.
  707.  
  708.  
  709. * <SIGNATURE>---------------------------------------------------------------------------------------+
  710. * | Instance Public Method ZCL_JSON_PARSER->PARSE
  711. * +-------------------------------------------------------------------------------------------------+
  712. * | [--->] IV_JSON                        TYPE        STRING
  713. * | [<-()] ES_DATA                        TYPE        ZUT_DATA
  714. * | [!CX!] ZCX_PARSE_ERROR
  715. * +--------------------------------------------------------------------------------------</SIGNATURE>
  716. method parse.
  717.  
  718.   data: lv_pos type i,
  719.         lv_found type flag.
  720.  
  721.   clear es_data.
  722.   check iv_json is not initial.
  723.  
  724. * Einen JSON-Ausdruck auswerten
  725.   get_any( exporting iv_json = iv_json
  726.            importing es_data = es_data
  727.                      ev_found = lv_found
  728.            changing  cv_pos  = lv_pos ).
  729.  
  730.   if lv_found eq space or
  731. * Hintendran darf nichts mehr kommen
  732.     lv_pos < strlen( iv_json ).
  733.     find regex '\S' in section offset lv_pos of iv_json.
  734.     if sy-subrc eq 0.
  735.       raise exception type zcx_parse_error.
  736.     endif.
  737.   endif.
  738.  
  739. endmethod.
  740.  
  741.  
  742. * <SIGNATURE>---------------------------------------------------------------------------------------+
  743. * | Instance Private Method ZCL_JSON_PARSER->SKIP_WHITESPACE
  744. * +-------------------------------------------------------------------------------------------------+
  745. * | [--->] IV_JSON                        TYPE        STRING
  746. * | [<-->] CV_POS                         TYPE        I
  747. * +--------------------------------------------------------------------------------------</SIGNATURE>
  748. method skip_whitespace.
  749.  
  750.   data: lv_pos type i.
  751.  
  752.   find regex '(\S|\Z)' in section offset cv_pos of iv_json match offset lv_pos.
  753.   if sy-subrc eq 0.
  754.     cv_pos = lv_pos.
  755.   endif.
  756.  
  757. endmethod.
  758. ENDCLASS.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement