Browse Source

Add int/class support for `JArray`

pull/8/head
Anton Tarasenko 4 years ago
parent
commit
a823972cf6
  1. 92
      sources/Data/JSON/JArray.uc
  2. 8
      sources/Data/JSON/JObject.uc
  3. 8
      sources/Data/JSON/JSON.uc
  4. 124
      sources/Data/JSON/Tests/TEST_JSON.uc

92
sources/Data/JSON/JArray.uc

@ -79,6 +79,15 @@ public final function float GetNumber(int index, optional float defaultValue)
return data[index].numberValue; return data[index].numberValue;
} }
public final function float GetInteger(int index, optional float defaultValue)
{
if (index < 0) return defaultValue;
if (index >= data.length) return defaultValue;
if (data[index].type != JSON_Number) return defaultValue;
return data[index].numberValueAsInt;
}
public final function string GetString(int index, optional string defaultValue) public final function string GetString(int index, optional string defaultValue)
{ {
if (index < 0) return defaultValue; if (index < 0) return defaultValue;
@ -88,6 +97,21 @@ public final function string GetString(int index, optional string defaultValue)
return data[index].stringValue; return data[index].stringValue;
} }
public final function class<Object> GetClass(
int index,
optional class<Object> defaultValue)
{
if (index < 0) return defaultValue;
if (index >= data.length) return defaultValue;
if (data[index].type != JSON_String) return defaultValue;
TryLoadingStringAsClass(data[index]);
if (data[index].stringValueAsClass != none) {
return data[index].stringValueAsClass;
}
return defaultValue;
}
public final function bool GetBoolean(int index, optional bool defaultValue) public final function bool GetBoolean(int index, optional bool defaultValue)
{ {
if (index < 0) return defaultValue; if (index < 0) return defaultValue;
@ -132,8 +156,7 @@ public final function JObject GetObject(int index)
// `false` (nothing will be done in this case). // `false` (nothing will be done in this case).
// They return object itself, allowing user to chain calls like this: // They return object itself, allowing user to chain calls like this:
// `array.SetNumber("num1", 1).SetNumber("num2", 2);`. // `array.SetNumber("num1", 1).SetNumber("num2", 2);`.
public final function JArray SetNumber public final function JArray SetNumber(
(
int index, int index,
float value, float value,
optional bool preventExpansion optional bool preventExpansion
@ -155,6 +178,34 @@ public final function JArray SetNumber
} }
newStorageValue.type = JSON_Number; newStorageValue.type = JSON_Number;
newStorageValue.numberValue = value; newStorageValue.numberValue = value;
newStorageValue.numberValueAsInt = int(value);
data[index] = newStorageValue;
return self;
}
public final function JArray SetInteger(
int index,
int value,
optional bool preventExpansion
)
{
local JStorageAtom newStorageValue;
if (index < 0) return self;
if (index >= data.length)
{
if (preventExpansion)
{
return self;
}
else
{
SetLength(index + 1);
}
}
newStorageValue.type = JSON_Number;
newStorageValue.numberValue = float(value);
newStorageValue.numberValueAsInt = value;
data[index] = newStorageValue; data[index] = newStorageValue;
return self; return self;
} }
@ -186,6 +237,33 @@ public final function JArray SetString
return self; return self;
} }
public final function JArray SetClass(
int index,
class<Object> value,
optional bool preventExpansion
)
{
local JStorageAtom newStorageValue;
if (index < 0) return self;
if (index >= data.length)
{
if (preventExpansion)
{
return self;
}
else
{
SetLength(index + 1);
}
}
newStorageValue.type = JSON_String;
newStorageValue.stringValue = string(value);
newStorageValue.stringValueAsClass = value;
data[index] = newStorageValue;
return self;
}
public final function JArray SetBoolean public final function JArray SetBoolean
( (
int index, int index,
@ -306,11 +384,21 @@ public final function JArray AddNumber(float value)
return SetNumber(data.length, value); return SetNumber(data.length, value);
} }
public final function JArray AddInteger(int value)
{
return SetInteger(data.length, value);
}
public final function JArray AddString(string value) public final function JArray AddString(string value)
{ {
return SetString(data.length, value); return SetString(data.length, value);
} }
public final function JArray AddClass(class<Object> value)
{
return SetClass(data.length, value);
}
public final function JArray AddBoolean(bool value) public final function JArray AddBoolean(bool value)
{ {
return SetBoolean(data.length, value); return SetBoolean(data.length, value);

8
sources/Data/JSON/JObject.uc

@ -272,13 +272,7 @@ public final function class<Object> GetClass(
if (property.value.type != JSON_String) { if (property.value.type != JSON_String) {
return defaultValue; return defaultValue;
} }
if (!property.value.classLoadingWasAttempted) TryLoadingStringAsClass(property.value);
{
property.value.classLoadingWasAttempted = true;
property.value.stringValueAsClass =
class<Object>(DynamicLoadObject( property.value.stringValue,
class'Class', true));
}
if (property.value.stringValueAsClass != none) { if (property.value.stringValueAsClass != none) {
return property.value.stringValueAsClass; return property.value.stringValueAsClass;
} }

8
sources/Data/JSON/JSON.uc

@ -76,6 +76,14 @@ struct JStorageAtom
var protected bool classLoadingWasAttempted; var protected bool classLoadingWasAttempted;
}; };
protected final function TryLoadingStringAsClass(out JStorageAtom atom)
{
if (atom.classLoadingWasAttempted) return;
atom.classLoadingWasAttempted = true;
atom.stringValueAsClass =
class<Object>(DynamicLoadObject(atom.stringValue, class'Class', true));
}
defaultproperties defaultproperties
{ {
} }

124
sources/Data/JSON/Tests/TEST_JSON.uc

@ -50,8 +50,12 @@ protected static function Test_ArrayGetSetRemove()
Context("Testing get/set/remove functions for JSON arrays"); Context("Testing get/set/remove functions for JSON arrays");
SubTest_ArrayUndefined(); SubTest_ArrayUndefined();
SubTest_ArrayStringGetSetRemove(); SubTest_ArrayStringGetSetRemove();
SubTest_ArrayClassGetSetRemove();
SubTest_ArrayStringAsClass();
SubTest_ArrayBooleanGetSetRemove(); SubTest_ArrayBooleanGetSetRemove();
SubTest_ArrayNumberGetSetRemove(); SubTest_ArrayNumberGetSetRemove();
SubTest_ArrayIntegerGetSetRemove();
SubTest_ArrayFloatAndInteger();
SubTest_ArrayNullGetSetRemove(); SubTest_ArrayNullGetSetRemove();
SubTest_ArrayMultipleVariablesStorage(); SubTest_ArrayMultipleVariablesStorage();
SubTest_ArrayMultipleVariablesRemoval(); SubTest_ArrayMultipleVariablesRemoval();
@ -287,7 +291,6 @@ protected static function SubTest_FloatAndInteger()
local JObject testJSON; local JObject testJSON;
testJSON = _().json.newObject(); testJSON = _().json.newObject();
testJSON.SetNumber("SetNumber", 6.70087); testJSON.SetNumber("SetNumber", 6.70087);
testJSON.SetInteger("SetInteger", 62478623874);
Context("Testing how `JObject` treats mixed float and" Context("Testing how `JObject` treats mixed float and"
@ "integer setters/getters."); @ "integer setters/getters.");
@ -547,6 +550,68 @@ protected static function SubTest_ArrayStringGetSetRemove()
TEST_ExpectTrue(testJSON.GetString(0, "other") == "other"); TEST_ExpectTrue(testJSON.GetString(0, "other") == "other");
} }
protected static function SubTest_ArrayClassGetSetRemove()
{
local JArray testJSON;
testJSON = _().json.newArray();
testJSON.SetClass(0, class'Actor');
Context("Testing `JArray`'s get/set/remove functions for" @
"class variables");
Issue("Class type isn't properly set by `SetClass`");
TEST_ExpectTrue(testJSON.GetTypeOf(0) == JSON_String);
Issue("Value is incorrectly assigned by `SetClass`");
TEST_ExpectTrue(testJSON.GetClass(0) == class'Actor');
Issue( "Providing default variable value makes `GetClass`" @
"return incorrect value");
TEST_ExpectTrue(testJSON.GetClass(0, class'GameInfo') == class'Actor');
Issue("Variable value isn't correctly reassigned by `SetClass`");
testJSON.SetClass(0, class'Info');
TEST_ExpectTrue(testJSON.GetClass(0) == class'Info');
Issue( "Getting class variable as a wrong type" @
"doesn't yield default value");
TEST_ExpectTrue(testJSON.GetBoolean(0, true) == true);
Issue("Boolean variable isn't being properly removed");
testJSON.RemoveValue(0);
TEST_ExpectTrue(testJSON.GetTypeOf(0) == JSON_Undefined);
Issue( "Getters don't return default value for missing key that" @
"previously stored string value, but got removed");
TEST_ExpectTrue(testJSON.GetClass(0, class'Mutator') == class'Mutator');
}
protected static function SubTest_ArrayStringAsClass()
{
local JArray testJSON;
testJSON = _().json.NewArray();
testJSON.SetString(0, "Engine.Actor");
testJSON.AddString("blahblahblah");
testJSON.AddClass(class'Info');
testJSON.SetClass(3, none);
Context("Testing how `JArray` treats mixed string and"
@ "class setters/getters.");
Issue("Incorrect result of `SetClass().GetString()` sequence.");
TEST_ExpectTrue(testJSON.GetString(2) == "Engine.Info");
TEST_ExpectTrue(testJSON.GetString(3) == "None");
TEST_ExpectTrue(testJSON.GetString(3, "alternative") == "None");
Issue("Incorrect result of `SetString().GetClass()` sequence for"
@ "correct value in `SetString()`.");
TEST_ExpectTrue(testJSON.GetClass(0) == class'Actor');
TEST_ExpectTrue(testJSON.GetClass(0, class'Object') == class'Actor');
Issue("Incorrect result of `SetString().GetClass()` sequence for"
@ "incorrect value in `SetString()`.");
TEST_ExpectTrue(testJSON.GetClass(1) == none);
TEST_ExpectTrue(testJSON.GetClass(1, class'Object') == class'Object');
}
protected static function SubTest_ArrayNumberGetSetRemove() protected static function SubTest_ArrayNumberGetSetRemove()
{ {
local JArray testJSON; local JArray testJSON;
@ -582,6 +647,63 @@ protected static function SubTest_ArrayNumberGetSetRemove()
TEST_ExpectTrue(testJSON.GetNumber(0, 13) == 13); TEST_ExpectTrue(testJSON.GetNumber(0, 13) == 13);
} }
protected static function SubTest_ArrayIntegerGetSetRemove()
{
local JArray testJSON;
testJSON = _().json.newArray();
testJSON.SetInteger(0, 19);
Context("Testing `JArray`'s get/set/remove functions for" @
"integer variables");
Issue("Integer type isn't properly set by `SetInteger`");
TEST_ExpectTrue(testJSON.GetTypeOf(0) == JSON_Number);
Issue("Value is incorrectly assigned by `SetInteger`");
TEST_ExpectTrue(testJSON.GetInteger(0) == 19);
Issue( "Providing default variable value makes `GetInteger`" @
"return incorrect value");
TEST_ExpectTrue(testJSON.GetInteger(0, 5) == 19);
Issue("Variable value isn't correctly reassigned by `SetInteger`");
testJSON.SetInteger(0, MaxInt);
TEST_ExpectTrue(testJSON.GetInteger(0) == MaxInt);
Issue( "Getting integer variable as a wrong type" @
"doesn't yield default value");
TEST_ExpectTrue(testJSON.GetString(0, "default") == "default");
Issue("Integer type isn't being properly removed");
testJSON.RemoveValue(0);
TEST_ExpectTrue(testJSON.GetTypeOf(0) == JSON_Undefined);
Issue( "Getters don't return default value for missing key that" @
"previously stored integer value, but got removed");
TEST_ExpectTrue(testJSON.GetInteger(0, 13) == 13);
}
protected static function SubTest_ArrayFloatAndInteger()
{
local JArray testJSON;
testJSON = _().json.NewArray();
testJSON.SetNumber(0, 6.70087);
Context("Testing how `JArray` treats mixed float and"
@ "integer setters/getters.");
Issue("Incorrect result of `SetNumber().GetInteger()` sequence.");
TEST_ExpectTrue(testJSON.GetInteger(0) == 6);
testJSON.SetInteger(0, 11);
testJSON.SetNumber(1, 0.43);
Issue("SetNumber().SetInteger() for same variable name does not overwrite"
@ "initial number value.");
TEST_ExpectTrue(testJSON.GetNumber(0) == 11);
Issue("SetInteger().SetNumber() for same variable name does not overwrite"
@ "initial integer value.");
TEST_ExpectTrue(testJSON.GetInteger(1) == 0);
}
protected static function SubTest_ArrayNullGetSetRemove() protected static function SubTest_ArrayNullGetSetRemove()
{ {
local JArray testJSON; local JArray testJSON;

Loading…
Cancel
Save