diff --git a/phpunit.xml b/phpunit.xml
index 3ac414c..14b4e57 100644
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -4,7 +4,7 @@
- ./webfiori
+ ./webfiori/json
@@ -12,7 +12,7 @@
- ./tests
+ ./tests/webfiori/tests/json
\ No newline at end of file
diff --git a/tests/JsonConverterTest.php b/tests/webfiori/tests/json/JsonConverterTest.php
similarity index 99%
rename from tests/JsonConverterTest.php
rename to tests/webfiori/tests/json/JsonConverterTest.php
index 1c3d536..3713cc1 100644
--- a/tests/JsonConverterTest.php
+++ b/tests/webfiori/tests/json/JsonConverterTest.php
@@ -1,6 +1,6 @@
assertFalse($j->add(' ',null));
$this->assertTrue($j->add('null-value',null));
$this->assertEquals('{"null-value":null}',$j->toJSONString());
+ $this->expectException(\InvalidArgumentException::class);
+ $this->assertFalse($j->add(' ',null));
+
}
/**
* @test
@@ -814,6 +817,43 @@ public function testAdd09() {
$j->addNumber('one', 1);
$this->assertEquals(1, $j->get('one'));
}
+ /**
+ * @test
+ */
+ public function testAdd10() {
+ $j = new Json();
+ $j->add('one', 'one');
+ $this->assertEquals(1, count($j->getPropsNames()));
+ $j->add('one', null);
+ $this->assertEquals(1, count($j->getPropsNames()));
+ $this->assertNull($j->get('one'));
+ }
+ /**
+ * @test
+ */
+ public function testAddNull00() {
+ $j = new Json();
+ $this->assertTrue($j->addNull('null'));
+ $this->assertNull($j->get('null'));
+ }
+ /**
+ * @test
+ */
+ public function testAddNull01() {
+ $j = new Json();
+ $this->expectException(\InvalidArgumentException::class);
+ $this->assertTrue($j->addNull(' '));
+ }
+ /**
+ * @test
+ */
+ public function testAddNull02() {
+ $j = new Json();
+ $j->addString('not-null', 'Hello');
+ $this->assertEquals('Hello', $j->get('not-null'));
+ $this->assertTrue($j->addNull('not-null'));
+ $this->assertNull($j->get('not-null'));
+ }
/**
* @test
*/
@@ -976,7 +1016,19 @@ public function testAddObj02() {
/**
* @test
*/
- public function testAddStringTest00() {
+ public function testAddObj03() {
+ $j = new Json();
+ $obj = new Obj1('Hello',0,true,null,'he');
+ $j->addString('object','An Obj');
+ $this->assertEquals('An Obj', $j->get('object'));
+ $j->addObject('object', $obj);
+ $this->assertEquals('{"object":{"property-00":"Hello","property-01":0,"property-02":true}}',$j.'');
+ }
+ /**
+ * @test
+ */
+ public function testAddString00() {
+ $this->expectException(\InvalidArgumentException::class);
$j = new Json();
$this->assertFalse($j->addString('','Hello World!'));
$this->assertFalse($j->addString(' ','Hello World!'));
@@ -986,18 +1038,30 @@ public function testAddStringTest00() {
/**
* @test
*/
- public function testAddStringTest01() {
+ public function testAddString01() {
$j = new Json();
$this->assertTrue($j->addString('hello','Hello World!'));
$this->assertEquals('{"hello":"Hello World!"}',$j.'');
+ $this->assertFalse($j->addString('a-number', 33));
}
/**
* @test
*/
- public function testAddStringTest02() {
+ public function testAddString02() {
$j = new Json();
$this->assertFalse($j->addBoolean('invalid-boolean','falseX'));
}
+ /**
+ * @test
+ */
+ public function testAddString03() {
+ $j = new Json();
+ $j->addNumber('a-number', 33);
+ $this->assertSame(33, $j->get('a-number'));
+ $j->addString('a-number', '33');
+ $this->assertNotSame(33, $j->get('a-number'));
+ $this->assertEquals('33', $j->get('a-number'));
+ }
/**
* @test
*/
diff --git a/tests/PropertyTest.php b/tests/webfiori/tests/json/PropertyTest.php
similarity index 99%
rename from tests/PropertyTest.php
rename to tests/webfiori/tests/json/PropertyTest.php
index 5f658d1..d2c4120 100644
--- a/tests/PropertyTest.php
+++ b/tests/webfiori/tests/json/PropertyTest.php
@@ -1,5 +1,5 @@
toJSONString();
}
/**
- * Adds a new value to the JSON string.
+ * Adds a new value to JSON.
*
* This method can be used to add an integer, a double,
* a string, an array or an object. If null is given, the method will
* set the value at the given key to null. If the given value or key is
* invalid, the method will not add the value and will return false.
+ * This method also can be used to update the value of an existing property.
*
* @param string $key The value of the key.
*
@@ -193,31 +194,21 @@ public function __toString() {
* @since 1.1
*/
public function add(string $key, $value, $arrayAsObj = false) {
- if ($value !== null) {
- if (!$this->updateExisting($key, $value)) {
- return $this->addString($key, $value) ||
- $this->addArray($key, $value, $arrayAsObj) ||
- $this->addBoolean($key, $value) ||
- $this->addNumber($key, $value) ||
- $this->addObject($key, $value);
- }
- $this->getProperty($key)->setAsObject($arrayAsObj);
- return true;
- } else {
- $prop = $this->createProb($key, $value);
-
- if ($prop !== null) {
- $this->propsArr[] = $prop;
-
- return true;
- }
+ if (!$this->updateExisting($key, $value)) {
+ return $this->addString($key, $value) ||
+ $this->addArray($key, $value, $arrayAsObj) ||
+ $this->addBoolean($key, $value) ||
+ $this->addNumber($key, $value) ||
+ $this->addObject($key, $value) ||
+ $this->addNull($key);
}
-
- return false;
+ return true;
}
/**
* Adds an array to the JSON.
*
+ * This method also can be used to update the value of an existing property.
+ *
* @param string $key The name of the key.
*
* @param array $value The array that will be added.
@@ -234,7 +225,7 @@ public function addArray(string $key, $value, $asObject = false) {
$prop = $this->createProb($key, $value);
$propType = $prop->getType();
- if ($prop !== null && $propType == JsonTypes::ARR) {
+ if ($propType == JsonTypes::ARR) {
$prop->setAsObject($asObject);
$this->propsArr[] = $prop;
@@ -244,12 +235,15 @@ public function addArray(string $key, $value, $asObject = false) {
return false;
} else {
$this->getProperty($key)->setAsObject($asObject);
+
return true;
}
}
/**
* Adds a boolean value (true or false) to the JSON data.
*
+ * This method also can be used to update the value of an existing property.
+ *
* @param string $key The name of the key.
*
* @param boolean $val true or false. If not specified,
@@ -265,7 +259,7 @@ public function addBoolean($key, $val = true) {
if (!$this->updateExisting($key, $val)) {
$prop = $this->createProb($key, $val);
- if ($prop !== null && $prop->getType() == 'boolean') {
+ if ($prop->getType() == 'boolean') {
$this->propsArr[] = $prop;
return true;
@@ -273,6 +267,7 @@ public function addBoolean($key, $val = true) {
return false;
}
+
return true;
}
/**
@@ -290,12 +285,41 @@ public function addMultiple(array $arr) {
$this->add($key, $value);
}
}
+ /**
+ * Adds a 'null' value to JSON.
+ *
+ * This method also can be used to update the value of an existing property.
+ *
+ * @param string $key The name of value key.
+ *
+ * @return boolean The method will return true if the value is set.
+ * If the given value or key is invalid, the method will return false.
+ */
+ public function addNull(string $key) {
+ $nul = null;
+
+ if (!$this->updateExisting($key, $nul)) {
+ $prop = $this->createProb($key, $nul);
+ $propType = $prop->getType();
+
+ if ($propType == JsonTypes::NUL) {
+ $this->propsArr[] = $prop;
+
+ return true;
+ }
+
+ return false;
+ }
+
+ return true;
+ }
/**
* Adds a number to the JSON data.
*
* Note that if the given number is the constant INF or the constant
* NAN, The method will add them as a string. The 'INF' will be added
* as the string "Infinity" and the 'NAN' will be added as the string "Nan".
+ * This method also can be used to update the value of an existing property.
*
* @param string $key The name of the key.
*
@@ -313,7 +337,7 @@ public function addNumber(string $key, $value) {
$prop = $this->createProb($key, $value);
$propType = $prop->getType();
- if ($prop !== null && $propType == JsonTypes::INT || $propType == JsonTypes::DOUBLE) {
+ if ($propType == JsonTypes::INT || $propType == JsonTypes::DOUBLE) {
$this->propsArr[] = $prop;
return true;
@@ -321,6 +345,7 @@ public function addNumber(string $key, $value) {
return false;
}
+
return true;
}
/**
@@ -335,6 +360,7 @@ public function addNumber(string $key, $value) {
* getFirstProp() and getSecondProp().
* In that case, the generated JSON will be on the formate
* {"FirstProp":"prop-1","SecondProp":""}.
+ * This method also can be used to update the value of an existing property.
*
* @param string $key The key value.
*
@@ -345,12 +371,12 @@ public function addNumber(string $key, $value) {
*
* @since 1.0
*/
- public function addObject(string $key, $val) {
+ public function addObject(string $key, &$val) {
if (!$this->updateExisting($key, $val)) {
$prop = $this->createProb($key, $val);
$propType = $prop->getType();
- if ($prop !== null && $propType == JsonTypes::OBJ) {
+ if ($propType == JsonTypes::OBJ) {
$this->propsArr[] = $prop;
return true;
@@ -358,11 +384,14 @@ public function addObject(string $key, $val) {
return false;
}
+
return true;
}
/**
* Adds a new key to the JSON data with its value as string.
*
+ * This method also can be used to update the value of an existing property.
+ *
* @param string $key The name of the key. Must be non empty string.
*
* @param string $val The value of the string.
@@ -377,7 +406,7 @@ public function addString(string $key, $val) {
if (!$this->updateExisting($key, $val)) {
$prop = $this->createProb($key, $val);
- if ($prop !== null && $prop->getType() == JsonTypes::STRING) {
+ if ($prop->getType() == JsonTypes::STRING) {
$this->propsArr[] = $prop;
return true;
@@ -385,19 +414,9 @@ public function addString(string $key, $val) {
return false;
}
+
return true;
}
- private function updateExisting($key, $val) {
- $tempProp = $this->getProperty($key);
-
- if ($tempProp !== null) {
- $tempProp->setValue($val);
-
- return true;
- }
-
- return false;
- }
/**
* Converts a JSON-like string to JSON object.
@@ -764,8 +783,19 @@ private function _setIsFormattedArray(&$arr) {
private function createProb($name, $value) {
try {
return new Property($name, $value, $this->getPropStyle());
- } catch (\Exception $ex) {
- return null;
+ } catch (InvalidArgumentException $ex) {
+ throw new InvalidArgumentException($ex->getMessage(), $ex->getCode(), $ex);
+ }
+ }
+ private function updateExisting($key, &$val) {
+ $tempProp = $this->getProperty($key);
+
+ if ($tempProp !== null) {
+ $tempProp->setValue($val);
+
+ return true;
}
+
+ return false;
}
}
diff --git a/webfiori/json/Property.php b/webfiori/json/Property.php
index 5dc3798..6250512 100644
--- a/webfiori/json/Property.php
+++ b/webfiori/json/Property.php
@@ -86,17 +86,18 @@ class Property {
public function __construct(string $name, $value, $style = null) {
$this->name = '';
$this->setStyle('none');
+
if (!$this->setName($name)) {
throw new InvalidArgumentException('Invalid property name: "'.$name.'"');
}
-
+
$this->setAsObject(false);
if ($style !== null) {
$this->setStyle($style);
}
-
+
$this->setValue($value);
}
/**
@@ -121,7 +122,7 @@ public function &getValue() {
public function getJsonXTagName() : string {
$type = $this->getType();
$retVal = '';
-
+
if ($type == JsonTypes::ARR) {
if ($this->isAsObject()) {
$retVal = 'json:object';
@@ -143,7 +144,7 @@ public function getJsonXTagName() : string {
} else {
$retVal = 'json:null';
}
-
+
return $retVal;
}
/**