Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrected getBoolean and added getUntypedBoolean #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions JSONParse.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,21 +270,26 @@ class json {
return getNumber( indexOf(key) );
}

function getBoolean(index: int): boolean {
var v : json = getType(index, jsonType._boolean);
function getBoolean(index : int ) : boolean {
var v : json = getType(index, jsonType._boolean);
return v.boolean_value;
}

function getUntypedBoolean(index: int): boolean {
var v : json = values[index];
var b: boolean;

// truthiness!
switch (type) {
switch (v.type) {
case jsonType._string:
b = string_value !== "";
b = v.string_value !== "";
break;
case jsonType._undefined:
case jsonType._null:
b = false;
break;
case jsonType._boolean:
b = boolean_value;
b = v.boolean_value;
break;
case jsonType._array:
b = true;
Expand All @@ -293,7 +298,7 @@ class json {
b = true;
break;
case jsonType._number:
b = number_value != 0;
b = v.number_value != 0;
break;
}
return b;
Expand Down Expand Up @@ -345,6 +350,10 @@ class json {
return getBoolean( indexOf(key) );
}

function getUntypedBoolean(key : String) : boolean {
return getUntypedBoolean( indexOf(key) );
}

function getArray(index: int): json {
return getType( index, jsonType._array );
}
Expand Down Expand Up @@ -427,7 +436,7 @@ class json {
}

static function test(){
var s = "{ \"foo\": \"bar\", \"baz\" : [ 17, 18, 19, { \"fish\" : \"soup\" } ]}";
var s = "{ \"foo\": \"bar\", \"baz\" : [ 17, 18, 19, { \"fish\" : \"soup\" }, 0 ], \"NotTrue\" : false, \"NotFalse\" : true }";

Debug.Log( "JSONParse Unit Tests" );
var j:json = json.fromString(s);
Expand All @@ -437,6 +446,11 @@ class json {
Debug.Log( "obj.foo: " + j.getString("foo") );
Debug.Log( "obj.baz[2]: " + j.getArray("baz").getNumber(0) );
Debug.Log( "obj.baz[3].fish: " + j.getArray("baz").getObject(3).getString("fish") );
Debug.Log( "obj.NotTrue: " + j.getBoolean("NotTrue"));
Debug.Log( "obj.NotFalse: " + j.getBoolean("NotFalse"));
Debug.Log( "obj.foo (as boolean): " + j.getUntypedBoolean("foo"));
Debug.Log( "obj.baz[4] (as boolean): " + j.getArray("baz").getUntypedBoolean(4));
Debug.Log( "obj.baz[0] (as boolean): " + j.getArray("baz").getUntypedBoolean(0));

var json_obj:json = json._object(); // new empty object
json_obj._set("key", json._string("value"));
Expand Down