Permalink
Browse files

Completed the json.stringify initial implementation with unit tests,

currently does not support nested complex objects.
  • Loading branch information...
tmpfs committed Jan 1, 2013
1 parent 34e202d commit 744cce1e834aa90c38b24f975f6593087e9e3e44
Showing with 79 additions and 58 deletions.
  1. +19 −0 lib/modules/array
  2. +39 −32 lib/modules/json
  3. +1 −10 lib/modules/variable
  4. +20 −16 test/json.test
View
@@ -12,6 +12,25 @@ function array.join {
_result=$(printf "${delimiter}%s" "${elements[@]}");
}
+
+function array.isarray {
+ local name="$1";
+ set +o errexit;
+ declare -p "$name" 2> /dev/null | grep -q 'declare \-a';
+ local exists="$?";
+ set -o errexit;
+ _result="$exists";
+}
+
+function array.isassoc {
+ local name="$1";
+ set +o errexit;
+ declare -p "$name" 2> /dev/null | grep -q 'declare \-A';
+ local exists="$?";
+ set -o errexit;
+ _result="$exists";
+}
+
# TODO: rename to array.last as it cannot pop
function array.pop {
#local arr=( "$@" );
View
@@ -105,43 +105,33 @@ function json.parse {
function json.stringify {
local json_data="${1:-}";
- local json="";
+ local json="{}";
json_string="";
if [ ! -z "$json_data" ]; then
- echo "got json.stringify data : $json_data";
-
- local key keys length value;
-
- # for key in ${!doc[@]}
- # do
- # echo "got doc key: $key";
- # done
-
- # dynamically reference keys of the referenced data
- eval keys="\${!$json_data[@]}";
- eval length="\${#$json_data[@]}";
- eval value="\${$json_data[__value__]}";
-
- if [ $length -eq 0 ]; then
- json="{}";
- # encoding a single value
- elif [ $length -eq 1 ] && [ ! -z "$value" ]; then
- __json_stringify_string "$value";
+ local keys length value;
+ array.isarray "$json_data";
+ local isarray="$_result";
+ if [ $isarray -eq 0 ]; then
+ __json_stringify_array "$json_data";
json="$_result";
- # echo "got plain string value to encode : $json";
- # got multiple keys, treat as an object
else
- __json_stringify_object "$json_data";
- echo "encode as object!!!!!!!!!!!!! $_result";
- json="$_result";
+ array.isassoc "$json_data";
+ local isassoc="$_result";
+ if [ $isassoc -eq 0 ]; then
+ eval length="\${#$json_data[@]}";
+ if [ $length -gt 0 ]; then
+ __json_stringify_object "$json_data";
+ json="$_result";
+ fi
+ else
+ variable.get "$json_data";
+ value="$_result";
+ if [ ! -z "$value" ]; then
+ __json_stringify_string "$value";
+ json="$_result";
+ fi
+ fi
fi
-
- echo "got array length : $length";
-
- for key in $keys
- do
- echo "got doc key: $key";
- done
fi
if [ ! -z "$json" ]; then
json_string="$json";
@@ -172,6 +162,23 @@ function json.clean {
#
######################################################################
+function __json_stringify_array {
+ local obj="[";
+ local json_data="$1";
+ local key value;
+ eval keys="\${!$json_data[@]}";
+ for key in $keys
+ do
+ eval value="\${$json_data[$key]}";
+ # encode the value
+ __json_stringify_string "$value";
+ obj="${obj}${_result},";
+ done
+ obj="${obj%,}";
+ obj="${obj}]";
+ _result="$obj";
+}
+
function __json_stringify_object {
local obj="{";
local json_data="$1";
View
@@ -1,7 +1,7 @@
function variable.get {
local name="$1";
set +o nounset;
- variable_is_array "$name";
+ array.isarray "$name";
local isarray="$_result";
if [ $isarray -eq 0 ]; then
eval value=\${"$name"[@]};
@@ -38,15 +38,6 @@ function variable_declare {
#echo "variable_declare: $name";
}
-function variable_is_array {
- local name="$1";
- set +o errexit;
- declare -p "$name" 2> /dev/null | grep -q 'declare \-a';
- local exists="$?";
- set -o errexit;
- _result="$exists";
-}
-
# TODO: implement
function variable_unset {
local prefix="$1";
View
@@ -25,55 +25,50 @@ require 'json';
# test for encoding an empty array
function test.json.stringify.empty {
- declare -A doc;
+ local doc;
json.stringify "doc";
local received="$json_string";
assert.equal "{}" "$received";
}
# test for encoding a string value
function test.json.stringify.string {
- declare -A doc;
- doc[__value__]="this is a string value";
+ local doc="this is a string value";
json.stringify "doc";
local received="$json_string";
- assert.equal "\"${doc[__value__]}\"" "$received";
+ assert.equal "\"this is a string value\"" "$received";
}
# test for encoding a boolean true
function test.json.stringify.boolean.true {
- declare -A doc;
- doc[__value__]="true";
+ local doc="true";
json.stringify "doc";
local received="$json_string";
- assert.equal "${doc[__value__]}" "$received";
+ assert.equal "true" "$received";
}
# test for encoding a boolean false
function test.json.stringify.boolean.false {
- declare -A doc;
- doc[__value__]="false";
+ local doc="false";
json.stringify "doc";
local received="$json_string";
- assert.equal "${doc[__value__]}" "$received";
+ assert.equal "false" "$received";
}
# test for encoding a null
function test.json.stringify.null {
- declare -A doc;
- doc[__value__]="null";
+ local doc="null";
json.stringify "doc";
local received="$json_string";
- assert.equal "${doc[__value__]}" "$received";
+ assert.equal "null" "$received";
}
# test for encoding an integer
function test.json.stringify.integer {
- declare -A doc;
- doc[__value__]="127";
+ local doc="127";
json.stringify "doc";
local received="$json_string";
- assert.equal "${doc[__value__]}" "$received";
+ assert.equal "127" "$received";
}
# test for encoding an object with a single key
@@ -95,4 +90,13 @@ function test.json.stringify.object.keys {
# key iterartion order appears to always be in reverse order
# this test will fail if that behaviour changes
assert.equal '{"key2":"the second value""key1":"the first value"}' "$received";
+}
+
+# test for encoding an array
+function test.json.stringify.array {
+ declare -a doc;
+ doc=( 127 null true false "a string value" );
+ json.stringify "doc";
+ local received="$json_string";
+ assert.equal '[127,null,true,false,"a string value"]' "$received";
}

0 comments on commit 744cce1

Please sign in to comment.