namespace Cubist\Util;
-class Json extends \Zend\Json\Json
+class Json
{
- /**
- *
- * @param mixed $v
- * @return boolean
- */
- public static function isJson($v)
- {
- if (!is_string($v)) {
- return false;
- }
-
- $v = trim($v);
-
- $firstchar = mb_substr($v, 0, 1);
- $lastchar = mb_substr($v, -1);
- return (($firstchar == '[' && $lastchar == ']') || ($firstchar == '{' && $lastchar == '}'));
- }
-
- public static function decode($encodedValue, $objectDecodeType = \Zend\Json\Json::TYPE_OBJECT)
- {
- if ((is_array($encodedValue) && $objectDecodeType == \Zend\Json\Json::TYPE_ARRAY) ||
- (is_object($encodedValue) && $objectDecodeType == \Zend\Json\Json::TYPE_OBJECT)
- ) {
- return $encodedValue;
- } else if (is_array($encodedValue) || is_object($encodedValue)) {
- $encodedValue = self::encode($encodedValue);
- }
- try {
- return parent::decode($encodedValue, $objectDecodeType);
- } catch (\Exception $e) {
- return null;
- }
- }
-
- public static function encode($valueToEncode, $cycleCheck = false, array $options = [])
- {
- return parent::encode($valueToEncode, $cycleCheck, $options);
- }
+ const TYPE_OBJECT = false;
+ const TYPE_ARRAY = true;
+
+ /**
+ *
+ * @param mixed $v
+ * @return boolean
+ */
+ public static function isJson($v)
+ {
+ if (!is_string($v)) {
+ return false;
+ }
+
+ $v = trim($v);
+
+ $firstchar = mb_substr($v, 0, 1);
+ $lastchar = mb_substr($v, -1);
+ return (($firstchar == '[' && $lastchar == ']') || ($firstchar == '{' && $lastchar == '}'));
+ }
+
+ public static function decode($encodedValue, $objectDecodeType = self::TYPE_OBJECT)
+ {
+ if ((is_array($encodedValue) && $objectDecodeType == self::TYPE_ARRAY) ||
+ (is_object($encodedValue) && $objectDecodeType == self::TYPE_OBJECT)
+ ) {
+ return $encodedValue;
+ } else if (is_array($encodedValue) || is_object($encodedValue)) {
+ $encodedValue = self::encode($encodedValue);
+ }
+ try {
+ return parent::decode($encodedValue, $objectDecodeType);
+ } catch (\Exception $e) {
+ return null;
+ }
+ }
+
+ public static function encode($valueToEncode)
+ {
+ return json_encode($valueToEncode);
+ }
+
+ public static function decodeRecursive($encodedValue, $objectDecodeType = self::TYPE_OBJECT)
+ {
+ $v = $encodedValue;
+
+ if (is_string($encodedValue)) {
+ $v = json_decode($encodedValue);
+ if (!$v) {
+ return $encodedValue;
+ }
+ }
+
+ $v = ArrayUtil::asArray($v);
+
+ array_walk_recursive($v, function (&$v, $k) {
+ $v_decoded = json_decode($v, true);
+ if ($v_decoded) {
+ $v = $v_decoded;
+ }
+ });
+
+ if ($objectDecodeType == self::TYPE_OBJECT) {
+ return ObjectUtil::asObject($v);
+ }
+ return $v;
+ }
}
\ No newline at end of file