├── LICENSE.txt ├── README.md ├── examples ├── sample_2_1.sh ├── sample_2_10.sh ├── sample_2_11.sh ├── sample_2_12.sh ├── sample_2_14.sh ├── sample_2_15.sh ├── sample_2_17.sh ├── sample_2_19.sh ├── sample_2_2.sh ├── sample_2_21.sh ├── sample_2_23.sh ├── sample_2_26.sh ├── sample_2_28.sh ├── sample_2_30.sh ├── sample_2_4.sh ├── sample_2_6.sh └── sample_2_8.sh └── include └── jshn.sh /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2016 Jeganath Thiagaraj 4 | 5 | Permission is hereby granted, free of charge, 6 | to any person obtaining a copy of this software and 7 | associated documentation files (the "Software"), to 8 | deal in the Software without restriction, including 9 | without limitation the rights to use, copy, modify, 10 | merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom 12 | the Software is furnished to do so, 13 | subject to the following conditions: 14 | 15 | The above copyright notice and this permission notice 16 | shall be included in all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 21 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 22 | ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # libjson-sh 2 | 3 | This is linux shell based json library, by which we can construct complete json object within the shell without the need for compilation. 4 | 5 | Since this is linux shell based library, it can be easily portable across all linux latforms. 6 | 7 | Provided we should include "./include/jshn.sh" file. 8 | 9 | The below are some of the well known library api: 10 | - json_init 11 | - json_add_object 12 | - json_close_object 13 | - json_add_string 14 | - json_add_array 15 | - json_close_array 16 | 17 | The below is the sample example, you can find lot more example in "examples" directory 18 | 19 | Here is the example: 20 | ==================== 21 | 22 | . ./include/jshn.sh 23 | 24 | json_init "object" 25 | 26 | json_add_array "AP_Autoconfig_config" 27 | 28 | json_add_object 29 | json_add_string "Radio_Id" "radioId" 30 | 31 | json_add_string "MAX_BSSID" "MAX_BSSID" 32 | 33 | json_add_string "radio" "radio_2G" 34 | json_add_string "supported_bandwidth" "40" 35 | 36 | json_add_string "regulatory_domain" "EU" 37 | 38 | json_add_string "channel_list" "1 2 3 4 5 6 7 8 9 10 11 12 13" 39 | 40 | json_close_object 41 | 42 | json_add_object 43 | json_add_string "Radio_Id" "radioId" 44 | 45 | json_add_string "MAX_BSSID" "MAX_BSSID" 46 | 47 | json_add_string "radio" "radio_5G" 48 | json_add_string "supported_bandwidth" "160" 49 | 50 | json_add_string "regulatory_domain" "EU" 51 | 52 | json_add_string "channel_list" "36 40 44 48 52 56 60 64 100 104 108 112 116 132 136 140" 53 | 54 | json_close_object 55 | json_close_array 56 | 57 | json_dump 58 | 59 | output: 60 | ======= 61 | 62 | { "AP_Autoconfig_config": [ { "Radio_Id": "radioId", "MAX_BSSID": "MAX_BSSID", "radio": "radio_2G", "supported_bandwidth": "40", "regulatory_domain": "EU", "channel_list": "1 2 3 5 6 7 8 9 10 11 12 13" }, { "Radio_Id": "radioId", "MAX_BSSID": "MAX_BSSID", "radio": "radio_5G", "supported_bandwidth": "160", "regulatory_domain": "EU", "channel_list": "36 40 44 48 52 56 60 64 100 104 108 112 116 132 136 140" } ] } 63 | 64 | -------------------------------------------------------------------------------- /examples/sample_2_1.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | . ../include/jshn.sh 4 | 5 | json_init "array" 6 | 7 | json_add_array 8 | json_add_int "bandwidth" 20 9 | json_add_double "efficiency" 12.5 10 | json_close_array 11 | 12 | json_add_array 13 | json_add_int "bandwidth" 40 14 | json_add_double "efficiency" 18.8 15 | json_close_array 16 | 17 | json_dump 18 | -------------------------------------------------------------------------------- /examples/sample_2_10.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | . ../include/jshn.sh 4 | 5 | json_init "array" 6 | 7 | json_add_object 8 | json_add_string "bandwidth" "20Mhz" 9 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10" 10 | json_add_double "radio_score" 17.39 11 | json_close_object 12 | 13 | json_add_object 14 | json_add_array "repeater1" 15 | json_add_string "bandwidth" "20Mhz" 16 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10" 17 | json_add_array 18 | json_add_string "bandwidth" "20Mhz" 19 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10" 20 | json_add_double "radio_score" 0.39 21 | json_close_array 22 | json_close_array 23 | json_close_object 24 | 25 | #export -p 26 | json_dump 27 | -------------------------------------------------------------------------------- /examples/sample_2_11.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | . ../include/jshn.sh 4 | 5 | json_init "array" 6 | 7 | json_add_object 8 | 9 | json_add_object "repeater2" 10 | json_add_string "bandwidth" "20Mhz" 11 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10" 12 | json_close_object 13 | json_add_string "bandwidth" "20Mhz" 14 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10" 15 | 16 | json_close_object 17 | 18 | json_add_object 19 | json_add_array "repeater1" 20 | json_add_string "bandwidth" "20Mhz" 21 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10" 22 | json_add_array 23 | json_add_string "bandwidth" "20Mhz" 24 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10" 25 | json_close_array 26 | json_close_array 27 | json_close_object 28 | 29 | #export -p 30 | json_dump 31 | -------------------------------------------------------------------------------- /examples/sample_2_12.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | . ../include/jshn.sh 4 | 5 | var="array object" 6 | while : 7 | do 8 | for i in $var 9 | do 10 | json_init "$i" 11 | 12 | if [ "$i" = "array" ] ; then 13 | 14 | json_add_object 15 | 16 | json_add_object "repeater2" 17 | json_add_string "bandwidth" "20Mhz" 18 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10" 19 | json_close_object 20 | json_add_string "bandwidth" "20Mhz" 21 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10" 22 | 23 | json_close_object 24 | 25 | json_add_object 26 | json_add_array "repeater1" 27 | json_add_string "bandwidth" "20Mhz" 28 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10" 29 | json_add_array 30 | json_add_string "bandwidth" "20Mhz" 31 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10" 32 | json_close_array 33 | json_close_array 34 | 35 | json_close_object 36 | else 37 | json_add_object "radio_2G" 38 | json_add_string "radio" "radio_2G" 39 | json_add_string "channel" "1 2 3 4" 40 | 41 | json_add_object "operating_class" 42 | json_add_int "class" 81 43 | 44 | json_add_object "supported_channels" 45 | json_add_string "channel_list" "1 2 3 4 5 6 7 8 9 10 11 12 13" 46 | 47 | json_add_object "eirp" 48 | json_add_string "power" "-29dBm" 49 | json_close_object 50 | 51 | json_close_object 52 | json_close_object 53 | json_close_object 54 | 55 | fi 56 | json_dump 57 | sleep 1 58 | done 59 | done 60 | -------------------------------------------------------------------------------- /examples/sample_2_14.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | . ../include/jshn.sh 4 | 5 | json_init "object" 6 | 7 | json_add_object 8 | json_add_string "radio" "radio_2G" 9 | json_add_string "channel" "1 2 3 4" 10 | json_close_object 11 | #json_add_object "radio_2G" 12 | #export -p 13 | json_dump 14 | -------------------------------------------------------------------------------- /examples/sample_2_15.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | . ../include/jshn.sh 4 | 5 | json_init "object" 6 | 7 | json_add_array "radio_2G" 8 | json_add_string "bandwidth" "20Mhz" 9 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10" 10 | json_close_array 11 | #export -p 12 | json_dump 13 | -------------------------------------------------------------------------------- /examples/sample_2_17.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | . ../include/jshn.sh 4 | 5 | json_init "object" 6 | 7 | json_add_object "radio_2G" 8 | json_add_string "radio" "radio_2G" 9 | json_add_string "channel" "1 2 3 4" 10 | json_close_object 11 | 12 | json_add_object "radio_5G" 13 | json_add_string "radio" "radio_5G" 14 | json_add_string "channel" "36 40 44 48" 15 | json_close_object 16 | 17 | json_add_array "operating_class" 18 | json_add_string "class" "81" 19 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10 11 12 13" 20 | json_close_array 21 | 22 | #json_add_object "radio_2G" 23 | #export -p 24 | json_dump 25 | -------------------------------------------------------------------------------- /examples/sample_2_19.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | . ../include/jshn.sh 4 | 5 | json_init "object" 6 | 7 | json_add_object "radio_2G" 8 | json_add_string "radio" "radio_2G" 9 | json_add_string "channel" "1 2 3 4" 10 | json_close_object 11 | 12 | json_add_object "radio_5G" 13 | json_add_string "radio" "radio_5G" 14 | json_add_string "channel" "36 40 44 48" 15 | json_close_object 16 | 17 | json_add_array "operating_class" 18 | json_add_string "class" "81" 19 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10 11 12 13" 20 | json_close_array 21 | 22 | json_add_array "operating_class" 23 | json_add_string "class" "82" 24 | json_add_string "channel" "14" 25 | json_close_array 26 | 27 | #json_add_object "radio_2G" 28 | #export -p 29 | json_dump 30 | -------------------------------------------------------------------------------- /examples/sample_2_2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | . ../include/jshn.sh 4 | 5 | json_init "array" 6 | 7 | json_add_array "radio_2G" 8 | json_add_string "bandwidth" "20Mhz" 9 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10" 10 | json_close_array 11 | #export -p 12 | json_dump 13 | -------------------------------------------------------------------------------- /examples/sample_2_21.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | . ../include/jshn.sh 4 | 5 | json_init "object" 6 | 7 | json_add_object "radio_2G" 8 | json_add_string "radio" "radio_2G" 9 | json_add_string "channel" "1 2 3 4" 10 | 11 | json_add_object "operating_class" 12 | json_add_string "class" "81" 13 | 14 | json_add_object "supported_channels" 15 | json_add_string "channel_list" "1 2 3 4 5 6 7 8 9 10 11 12 13" 16 | 17 | json_add_object "eirp" 18 | json_add_string "power" "-29dBm" 19 | json_close_object 20 | 21 | json_close_object 22 | json_close_object 23 | json_close_object 24 | 25 | json_add_object "radio_5G" 26 | json_add_string "radio" "radio_5G" 27 | json_add_string "channel" "36 40 44 48" 28 | json_close_object 29 | 30 | json_add_array "operating_class" 31 | json_add_string "class" "81" 32 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10 11 12 13" 33 | json_close_array 34 | 35 | json_add_array "operating_class1" 36 | json_add_string "class" "82" 37 | json_add_string "channel" "14" 38 | json_close_array 39 | 40 | #json_add_object "radio_2G" 41 | #export -p 42 | json_dump 43 | -------------------------------------------------------------------------------- /examples/sample_2_23.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | . ../include/jshn.sh 4 | 5 | json_init "object" 6 | 7 | json_add_object "radio_2G" 8 | json_add_string "radio" "radio_2G" 9 | json_add_string "channel" "1 2 3 4" 10 | 11 | json_add_object "operating_class" 12 | json_add_string "class" "81" 13 | 14 | json_add_object "supported_channels" 15 | json_add_string "channel_list" "1 2 3 4 5 6 7 8 9 10 11 12 13" 16 | 17 | json_add_object "eirp" 18 | json_add_string "power" "-29dBm" 19 | json_close_object 20 | 21 | json_close_object 22 | json_close_object 23 | json_close_object 24 | 25 | json_add_object "radio_5G" 26 | json_add_string "radio" "radio_5G" 27 | json_add_string "channel" "36 40 44 48" 28 | json_close_object 29 | 30 | json_add_array "operating_class" 31 | json_add_array 32 | json_add_string "class" "81" 33 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10 11 12 13" 34 | json_close_array 35 | 36 | json_add_array 37 | json_add_string "class" "82" 38 | json_add_string "channel" "14" 39 | json_close_array 40 | 41 | json_add_array 42 | json_add_string "class" "83" 43 | json_add_string "channel" "1 2 3 4 5 6 7 8" 44 | json_close_array 45 | 46 | json_close_array 47 | 48 | json_add_array "operating_class1" 49 | json_add_string "class" "82" 50 | json_add_string "channel" "14" 51 | json_close_array 52 | 53 | #json_add_object "radio_2G" 54 | #export -p 55 | json_dump 56 | -------------------------------------------------------------------------------- /examples/sample_2_26.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | . ../include/jshn.sh 4 | 5 | json_init "object" 6 | 7 | json_add_object "radio_2G" 8 | json_add_string "radio" "radio_2G" 9 | json_add_string "channel" "1 2 3 4" 10 | 11 | json_add_object "operating_class" 12 | json_add_string "class" "81" 13 | 14 | json_add_object "supported_channels" 15 | json_add_string "channel_list" "1 2 3 4 5 6 7 8 9 10 11 12 13" 16 | 17 | json_add_object "eirp" 18 | json_add_string "power" "-29dBm" 19 | json_close_object 20 | 21 | json_close_object 22 | json_close_object 23 | json_close_object 24 | 25 | json_add_object "radio_5G" 26 | json_add_string "radio" "radio_5G" 27 | json_add_string "channel" "36 40 44 48" 28 | json_close_object 29 | 30 | json_add_array "operating_class" 31 | json_add_array 32 | 33 | json_add_array 34 | json_add_string "class" "82" 35 | json_add_string "channel" "14" 36 | json_close_array 37 | 38 | json_add_string "class" "81" 39 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10 11 12 13" 40 | json_close_array 41 | 42 | json_add_array 43 | json_add_string "class" "82" 44 | json_add_string "channel" "14" 45 | json_close_array 46 | 47 | json_add_array 48 | json_add_string "class" "83" 49 | json_add_string "channel" "1 2 3 4 5 6 7 8" 50 | json_close_array 51 | 52 | json_close_array 53 | 54 | json_add_array "operating_class1" 55 | json_add_string "class" "82" 56 | json_add_string "channel" "14" 57 | json_close_array 58 | 59 | #json_add_object "radio_2G" 60 | #export -p 61 | json_dump 62 | -------------------------------------------------------------------------------- /examples/sample_2_28.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | . ../include/jshn.sh 4 | 5 | json_init "object" 6 | 7 | json_add_object "radio_2G" 8 | json_add_string "radio" "radio_2G" 9 | json_add_string "channel" "1 2 3 4" 10 | 11 | json_add_object "operating_class" 12 | json_add_string "class" "81" 13 | 14 | json_add_object "supported_channels" 15 | json_add_string "channel_list" "1 2 3 4 5 6 7 8 9 10 11 12 13" 16 | 17 | json_add_object "eirp" 18 | json_add_string "power" "-29dBm" 19 | 20 | json_add_array "operating_class" 21 | json_add_array 22 | 23 | json_add_array 24 | json_add_string "class" "82" 25 | json_add_string "channel" "14" 26 | json_close_array 27 | 28 | json_add_string "class" "81" 29 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10 11 12 13" 30 | json_close_array 31 | 32 | json_add_array 33 | json_add_string "class" "82" 34 | json_add_string "channel" "14" 35 | json_close_array 36 | 37 | json_add_array 38 | json_add_string "class" "83" 39 | json_add_string "channel" "1 2 3 4 5 6 7 8" 40 | json_close_array 41 | 42 | json_close_array 43 | 44 | json_add_array "operating_class1" 45 | json_add_string "class" "82" 46 | json_add_string "channel" "14" 47 | json_close_array 48 | 49 | json_close_object 50 | 51 | json_close_object 52 | json_close_object 53 | json_close_object 54 | 55 | json_add_object "radio_5G" 56 | json_add_string "radio" "radio_5G" 57 | json_add_string "channel" "36 40 44 48" 58 | json_close_object 59 | 60 | json_add_array "operating_class" 61 | json_add_array 62 | 63 | json_add_array 64 | json_add_string "class" "82" 65 | json_add_string "channel" "14" 66 | json_close_array 67 | 68 | json_add_string "class" "81" 69 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10 11 12 13" 70 | json_close_array 71 | 72 | json_add_array 73 | json_add_string "class" "82" 74 | json_add_string "channel" "14" 75 | json_close_array 76 | 77 | json_add_array 78 | json_add_string "class" "83" 79 | json_add_string "channel" "1 2 3 4 5 6 7 8" 80 | json_close_array 81 | 82 | json_close_array 83 | 84 | json_add_array "operating_class1" 85 | json_add_string "class" "82" 86 | json_add_string "channel" "14" 87 | json_close_array 88 | 89 | #json_add_object "radio_2G" 90 | #export -p 91 | json_dump 92 | -------------------------------------------------------------------------------- /examples/sample_2_30.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | . ../include/jshn.sh 4 | #. ./jshn.sh 5 | json_init "object" 6 | #json_init 7 | 8 | json_add_object "radio_2G" 9 | json_add_string "bandwidth" "20Mhz" 10 | json_add_string "country" "EU" 11 | json_close_object 12 | json_add_object "radio_5G" 13 | json_add_string "bandwidth" "40Mhz" 14 | json_add_string "country" "US" 15 | json_close_object 16 | json_add_object "operating_class" 17 | json_close_object 18 | json_dump 19 | -------------------------------------------------------------------------------- /examples/sample_2_4.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | . ../include/jshn.sh 4 | 5 | json_init "array" 6 | 7 | json_add_object "radio_2G" 8 | json_add_string "bandwidth" "20Mhz" 9 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10" 10 | json_close_object 11 | #export -p 12 | json_dump 13 | -------------------------------------------------------------------------------- /examples/sample_2_6.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | . ../include/jshn.sh 4 | 5 | json_init "array" 6 | 7 | json_add_array 8 | json_add_string "bandwidth" "20Mhz" 9 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10" 10 | json_close_array 11 | json_add_int "radio_id" 1 12 | json_add_array 13 | json_add_array 14 | json_add_string "bandwidth" "20Mhz" 15 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10" 16 | json_close_array 17 | json_add_string "bandwidth" "20Mhz" 18 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10" 19 | json_add_int "radio_id" 2 20 | json_close_array 21 | #export -p 22 | json_dump 23 | -------------------------------------------------------------------------------- /examples/sample_2_8.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | . ../include/jshn.sh 4 | 5 | json_init "array" 6 | 7 | json_add_object 8 | json_add_string "bandwidth" "20Mhz" 9 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10" 10 | json_close_object 11 | json_add_boolean "radio_state" 1 12 | json_add_object 13 | json_add_array "repeater1" 14 | json_add_string "bandwidth" "20Mhz" 15 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10" 16 | json_close_array 17 | json_add_boolean "radio_state" 0 18 | json_add_array "repeater2" 19 | json_add_string "bandwidth" "20Mhz" 20 | json_add_string "channel" "1 2 3 4 5 6 7 8 9 10" 21 | json_close_array 22 | json_close_object 23 | 24 | #export -p 25 | json_dump 26 | -------------------------------------------------------------------------------- /include/jshn.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | json_init() 4 | { 5 | json_cleanup 6 | export -- \ 7 | JSON_CURSOR="TOP_VAR" \ 8 | VALUE_TOP_VAR="JSON" \ 9 | TYPE_TOP_VAR="$1" \ 10 | KEY_TOP_VAR="TOP_VAR" \ 11 | KEYS_TOP_VAR= \ 12 | SEQ_NUM=0 \ 13 | JSON_PREV_TOP_VAR= \ 14 | JSON_ERROR= 15 | } 16 | 17 | json_cleanup() 18 | { 19 | unset \ 20 | KEYS_TOP_VAR \ 21 | VALUE_TOP_VAR \ 22 | TYPE_TOP_VAR \ 23 | KEY_TOP_VAR \ 24 | SEQ_NUM \ 25 | JSON_ERROR \ 26 | JSON_CURSOR \ 27 | JSON_PREV_TOP_VAR 28 | } 29 | 30 | json_get_type() 31 | { 32 | eval "$1=\"\$TYPE_$2\"" 33 | } 34 | 35 | json_append() 36 | { 37 | eval "$1=\"\${$1} \$2\"" 38 | } 39 | 40 | json_get_error() 41 | { 42 | eval "$1=\"\$JSON_ERROR\"" 43 | } 44 | 45 | json_get_keys() 46 | { 47 | eval "$1=\"\$KEYS_$2\"" 48 | } 49 | 50 | json_get_prev() 51 | { 52 | eval "$1=\"\$JSON_PREV_$2\"" 53 | } 54 | 55 | json_table_length() 56 | { 57 | local keys 58 | local i 59 | 60 | json_get_keys keys $2 61 | if [ ! -z "$keys" ]; then 62 | #printf "KEYS:$keys\n" 63 | for i in $keys 64 | do 65 | eval "$1=\"\$(($1 + 1))\"" 66 | done 67 | else 68 | eval "$1=0" 69 | fi 70 | } 71 | 72 | __json_close_generic() 73 | { 74 | local cursor_value 75 | local prev_cursor 76 | 77 | json_get_cursor cursor_value 78 | json_get_prev prev_cursor $cursor_value 79 | json_set_cursor $prev_cursor 80 | } 81 | 82 | json_close_array() 83 | { 84 | __json_close_generic 85 | } 86 | 87 | 88 | json_close_object() 89 | { 90 | __json_close_generic 91 | } 92 | 93 | __json_add_generic() 94 | { 95 | local key_name=$1 96 | local cursor_value 97 | local prev_table_type 98 | local value 99 | local seq_no 100 | local obj_type=$3 101 | local new_cursor 102 | 103 | json_get_error value 104 | if [ "$value" = "error" ]; then 105 | #Do not proceed if any error in JSON sequence 106 | return 107 | fi 108 | 109 | json_get_cursor cursor_value 110 | json_get_type prev_table_type $cursor_value 111 | 112 | #printf "add_string prev_table_type=$prev_table_type, cursor=$cursor_value\n" 113 | 114 | json_update_seq_number 115 | json_get_seq_number seq_no 116 | 117 | new_cursor="${key_name}_$seq_no" 118 | json_append "KEYS_$cursor_value" $new_cursor 119 | 120 | eval "export -- TYPE_$new_cursor=\"\$obj_type\" KEY_$new_cursor=\"\$key_name\" VALUE_$new_cursor=\"\$2\" JSON_PREV_$new_cursor=\"\$cursor_value\"" 121 | 122 | } 123 | 124 | json_add_string() 125 | { 126 | __json_add_generic $1 "$2" string 127 | 128 | } 129 | 130 | json_add_int() 131 | { 132 | __json_add_generic $1 $2 "integer" 133 | } 134 | 135 | 136 | json_add_boolean() 137 | { 138 | 139 | __json_add_generic $1 $2 "boolean" 140 | } 141 | 142 | 143 | json_add_double() 144 | { 145 | __json_add_generic $1 $2 "double" 146 | } 147 | 148 | 149 | json_add_array() 150 | { 151 | local key_name=$1 152 | local cursor_value 153 | local prev_table_type 154 | local value 155 | local seq_no 156 | local new_cursor 157 | 158 | json_get_cursor cursor_value 159 | json_get_type prev_table_type $cursor_value 160 | 161 | #printf "prev_table_type=$prev_table_type\n" 162 | 163 | json_get_error value 164 | if [ "$value" = "error" ]; then 165 | #Do not proceed if any error in JSON sequence 166 | return 167 | fi 168 | 169 | [ ! -z $key_name ] && [ $prev_table_type = "array" ] 170 | ## Array table at top will not have key named array and key named object within 171 | if [ $? -eq 0 ]; then 172 | printf "ERROR: Array table at top will not have key named array and key named object within\n" 173 | eval "JSON_ERROR=\"error\"" 174 | return 175 | fi 176 | 177 | [ -z $key_name ] && [ $prev_table_type = "object" ] 178 | ## Object table at top should have key named array and key named object within 179 | if [ $? -eq 0 ]; then 180 | printf "ERROR: Object table at top should have key named array and key named object within\n" 181 | eval "JSON_ERROR=\"error\"" 182 | return 183 | fi 184 | 185 | json_update_seq_number 186 | json_get_seq_number seq_no 187 | 188 | if [ -z $key_name ]; then 189 | ## Control comes here meaning that the previous table must be "array" 190 | new_cursor="${cursor_value}_$seq_no" 191 | else 192 | new_cursor="${key_name}_$seq_no" 193 | fi 194 | 195 | #printf "key_name=$key_name\n" 196 | 197 | #printf "new_cursor=$new_cursor\n" 198 | 199 | json_append "KEYS_$cursor_value" $new_cursor 200 | 201 | eval "export -- TYPE_$new_cursor=\"array\" KEY_$new_cursor=\"\$key_name\" KEYS_$new_cursor= VALUE_$new_cursor= JSON_PREV_$new_cursor=\"\$cursor_value\"" 202 | 203 | json_set_cursor $new_cursor 204 | } 205 | 206 | 207 | json_add_object() 208 | { 209 | local key_name=$1 210 | local cursor_value 211 | local prev_table_type 212 | local value 213 | local seq_no 214 | local new_cursor 215 | 216 | json_get_cursor cursor_value 217 | json_get_type prev_table_type $cursor_value 218 | 219 | #printf "prev_table_type=$prev_table_type\n" 220 | 221 | json_get_error value 222 | if [ "$value" = "error" ]; then 223 | #Do not proceed if any error in JSON sequence 224 | return 225 | fi 226 | 227 | [ ! -z "$key_name" ] && [ $prev_table_type = "array" ] 228 | ## Array table at top will not have key named array and key named object within 229 | if [ $? -eq 0 ]; then 230 | printf "ERROR: Array table at top will not have key named array and key named object within\n" 231 | eval "JSON_ERROR=\"error\"" 232 | return 233 | fi 234 | 235 | [ -z $key_name ] && [ $prev_table_type = "object" ] 236 | ## Object table at top should have key named array and key named object within 237 | if [ $? -eq 0 ]; then 238 | printf "ERROR: Object table at top should have key named array and key named object within\n" 239 | eval "JSON_ERROR=\"error\"" 240 | return 241 | fi 242 | 243 | json_update_seq_number 244 | json_get_seq_number seq_no 245 | 246 | if [ -z $key_name ]; then 247 | ## Control comes here meaning that the previous table must be "array" 248 | new_cursor="$cursor_value_$seq_no" 249 | else 250 | new_cursor="${key_name}_$seq_no" 251 | fi 252 | 253 | #printf "new_cursor=$new_cursor\n" 254 | #printf "key_name=$key_name\n" 255 | 256 | json_append "KEYS_$cursor_value" $new_cursor 257 | 258 | eval "export -- TYPE_$new_cursor=\"object\" KEY_$new_cursor=\"\$key_name\" KEYS_$new_cursor= VALUE_$new_cursor= JSON_PREV_$new_cursor=\"\$cursor_value\"" 259 | 260 | json_set_cursor $new_cursor 261 | 262 | } 263 | 264 | json_get_value() 265 | { 266 | eval "$1=\"\$VALUE_$2\"" 267 | } 268 | 269 | json_get_cursor() 270 | { 271 | eval "$1=$JSON_CURSOR" 272 | } 273 | 274 | json_set_cursor() 275 | { 276 | eval "export JSON_CURSOR=\"\$1\"" 277 | } 278 | 279 | json_get_curr_type() 280 | { 281 | json_get_cursor cursor_value 282 | #printf "cursor_value=$cursor_value\n" 283 | eval "$1=\"\$TYPE_$cursor_value\"" 284 | } 285 | 286 | json_update_seq_number() 287 | { 288 | eval "SEQ_NUM=\"\$(($SEQ_NUM + 1))\"" 289 | } 290 | 291 | json_get_seq_number() 292 | { 293 | eval "$1=\"\$SEQ_NUM\"" 294 | } 295 | 296 | json_get_orig_key() 297 | { 298 | eval "$1=\"\$KEY_$2\"" 299 | } 300 | 301 | json_dump_table() 302 | { 303 | local length=0 304 | local type 305 | local cursor 306 | local Keys 307 | local key 308 | local top_level_type 309 | local length 310 | 311 | json_get_cursor cursor 312 | json_get_keys Keys $cursor 313 | 314 | #printf "prev_type==$1\n" 315 | 316 | json_get_curr_type top_level_type 317 | 318 | #printf "top_level_type==$top_level_type\n" 319 | json_table_length length $cursor 320 | 321 | for i in $Keys 322 | do 323 | if [ $top_level_type = "object" ]; then 324 | ## if the previous table is "object", only then print "key" 325 | json_get_orig_key key $i 326 | printf "\"$key\": " 327 | fi 328 | json_get_type type $i 329 | if [ "$type" = "array" ]; then 330 | printf "[ " 331 | json_set_cursor $i 332 | json_dump_table 333 | json_set_cursor $cursor 334 | printf " ]" 335 | elif [ "$type" = "object" ]; then 336 | printf "{ " 337 | json_set_cursor $i 338 | json_dump_table 339 | json_set_cursor $cursor 340 | printf " }" 341 | elif [ "$type" = "string" ]; then 342 | json_get_value value $i 343 | printf "\"$value\"" 344 | elif [ "$type" = "integer" ]; then 345 | json_get_value value $i 346 | printf "$value" 347 | elif [ "$type" = "boolean" ]; then 348 | json_get_value value $i 349 | [ $value -eq 1 ] && printf "true" 350 | [ $value -eq 1 ] || printf "false" 351 | elif [ "$type" = "double" ]; then 352 | json_get_value value $i 353 | printf "$value" 354 | fi 355 | 356 | length=$(($length-1)) 357 | if [ $length -ne 0 ]; then 358 | printf ", " 359 | fi 360 | done 361 | } 362 | 363 | json_dump() 364 | { 365 | local top_value 366 | local type 367 | local current_cursor 368 | 369 | json_get_cursor current_cursor 370 | 371 | #Reset the JSON_CUR to default value which will be helpful for parsing 372 | json_set_cursor TOP_VAR 373 | 374 | #get current TYPE 375 | json_get_curr_type type 376 | 377 | #printf "type==$type\n" 378 | if [ "$type" = "array" ] ; then 379 | printf "[ " 380 | elif [ "$type" = "object" ]; then 381 | printf "{ " 382 | fi 383 | 384 | json_dump_table 385 | 386 | #Reset the curser value back 387 | json_set_cursor $current_cursor 388 | 389 | if [ "$type" = "array" ] ; then 390 | printf " ]" 391 | elif [ "$type" = "object" ]; then 392 | printf " }" 393 | fi 394 | printf "\n" 395 | ##export -p 396 | } 397 | --------------------------------------------------------------------------------