Encodes to JSON if it's not empty or already JSON

@param mixed $value

@return string | null
184501
public function setInternalOrderInfo($value)
    {
        if ($value instanceof ItemInfo) {
            $value = $value->toJson();
        } elseif (is_array($value)) {
            $value = \Zend_Json_Encoder::encode($value);
        }

        return parent::setInternalOrderInfo($value);
    }
1147270
protected function _sanitize($value)
    {
        // If instance of AnConfig casting to string will make it encode itself to JSON
        if ($value instanceof AnConfig) {
            $result = (string) $value;
        } else {
            //Don't re-encode if the value is already in json format
            if (is_string($value) && (json_decode($value) !== null)) {
                $result = $value;
            } else {
                $result = json_encode($value);
            }
        }

        return $result;
    }
392525
private function phpEncodeValue($value)
    {
        if (is_array($value)) {
            return $this->phpEncodeArray($value);
        }

        if (is_string($value)) {
            return json_encode($value);
        }

        return var_export($value, true);
    }
15260
public function filter($value) {
    	    	
    	$value   = str_replace(["\r", "\r\n", "\n"], '', $value);
    	$decoded = json_decode($value,true);
    	if(is_null($decoded)){
    		return null;
    	}    	
    	
    	switch($this->options["format"]){
    	    case self::DECODED:
    	        return $decoded;
    	    case self::JSON:
    	        return $value;
    	    case self::SERIALIZED:
    	        return serialize($decoded);
    	    default:
    	        throw new FilterException("unknown format for JSON filter specified");
    	}
    }
206628
protected function formatValue($value)
    {
        if (PHP_VERSION_ID < 50400) {
            return str_replace('\/', '/', json_encode($value));
        }

        return json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
    }
535035
protected function castAttributeAsJson($value)
    {
        $value = $this->asJson($value);

        if ($value === false) {
            throw new RuntimeException('Error encoding model ['.get_class($this).'] to JSON: '.json_last_error_msg());
        }

        return $value;
    }
324275
private function getJsonCastValue($value)
    {
        return method_exists($this, 'fromJson') ? $this->fromJson($value) : json_decode($value, true);
    }
1055324
public static function serialize($value): string
    {
        $serialized = json_encode(
            self::encode($value, new SplObjectStorage()),
            JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
        );

        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new \InvalidArgumentException(json_last_error_msg());
        }

        return $serialized;
    }
1206284
public function encode($value)
    {
        switch (true) {
            case is_null($value):
                return $this->encodeString('', self::T_NULL);
            case is_bool($value):
                return $this->encodeString($value ? 'true' : 'false', self::T_BOOL);
            case is_int($value):
                return $this->encodeString($value, self::T_INT);
            case is_float($value):
                return $this->encodeString($value, self::T_FLOAT);
            case is_array($value):
                return $this->encodePHPArray($value);

            case is_resource($value):
                throw new InvalidArgumentException("You can't encode a PHP resource as a tagged netstring.");

            default:
                return $this->encodeString($value);
        }
    }
153109
public function encode($value)
    {

        $createdIv = $this->createIvSizeAndIvString();
        $createdKey = $this->createSpecialKey($createdIv);

        if (false !== $encrypted = @mcrypt_encrypt($this->algorithm, $createdKey, $value, $this->mode, $createdIv)) {

            return urlencode(base64_encode(
                serialize(
                    [
                        'iv'    => base64_encode($createdIv),
                        'key'   => base64_encode($createdKey),
                        'value' => trim(base64_encode($encrypted)),
                    ]
                )
            ));
        }

        return false;
    }
1020188
protected function encodeDate(DateTime $value = null) {
        return null === $value ? "" : $value->format(self::DATE_FORMAT);
    }
1222027
public static function decode($value)
    {
        if (empty($value)) {
            return null;
        }

        if (!is_string($value)) {
            throw new \InvalidArgumentException(sprintf(
                "Value type '%s' is not a PHP string so it cannot be unserialized.",
                gettype($value)
            ));
        }

        $data = unserialize($value);

        return $data;
    }
968948
private static function jsonEncode($value, int $flags = null, int $maxDepth = 512): string
    {
        $flags = $flags ?? (JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_PRESERVE_ZERO_FRACTION);

        if (!\is_array($value) && !$value instanceof \JsonSerializable) {
            throw new InvalidArgumentException(sprintf('Option "json" must be array or JsonSerializable, %s given.', \is_object($value) ? \get_class($value) : \gettype($value)));
        }

        try {
            $value = json_encode($value, $flags | (\PHP_VERSION_ID >= 70300 ? JSON_THROW_ON_ERROR : 0), $maxDepth);
        } catch (\JsonException $e) {
            throw new InvalidArgumentException(sprintf('Invalid value for "json" option: %s.', $e->getMessage()));
        }

        if (\PHP_VERSION_ID < 70300 && JSON_ERROR_NONE !== json_last_error() && (false === $value || !($flags & JSON_PARTIAL_OUTPUT_ON_ERROR))) {
            throw new InvalidArgumentException(sprintf('Invalid value for "json" option: %s.', json_last_error_msg()));
        }

        return $value;
    }
1081508
protected function _encodeDatum(&$value)
    {
        $result = 'null';

        if (is_int($value) || is_float($value)) {
            $result = (string)$value;
        } elseif (is_string($value)) {
            $result = $this->_encodeString($value);
        } elseif (is_bool($value)) {
            $result = $value ? 'true' : 'false';
        }

        return $result;
    }
223948
protected function castValueAsJson($value)
    {
        $value = $this->asJson($value);

        if ($value === false) {
            throw new \RuntimeException(
                sprintf(
                    "Unable to encode value [%s] for element [%s] to JSON: %s.",
                    $this->getName(),
                    get_class($this),
                    json_last_error_msg()
                )
            );
        }

        return $value;
    }
50895
protected function encode($value)
    {
        $key = (string) $value;
        if (!isset(static::$cacheEncode[$key])) {
            $isAngularVar = false;
            if (\mb_substr($value, 0, 2) == '{{' && \mb_substr($value, -2) == '}}') {
                $isAngularVar = true;
                $value = \trim(\mb_substr($value, 2, -2));
            }
            static::$cacheEncode[$key] = \rawurlencode($value);
            static::$cacheEncode[$key] = \strtr(static::$cacheEncode[$key], static::$urlencodeCorrectionMap);
            if ($isAngularVar === true) {
                static::$cacheEncode[$key] = '{{ ' . static::$cacheEncode[$key] . ' }}';
            }
        }

        return static::$cacheEncode[$key];
    }
73319
public static function encodeJsVar( $value ) {
		if( is_bool( $value ) ) {
			$s = $value ? 'true' : 'false';
		} elseif( is_null( $value ) ) {
			$s = 'null';
		} elseif( is_int( $value ) ) {
			$s = $value;
		} elseif( is_array( $value ) && // Make sure it's not associative.
				  array_keys( $value ) === range( 0, count( $value ) - 1 ) ||
				  count( $value ) == 0
		) {
			$s = '[';
			foreach( $value as $elt ){
				if( $s != '[' ) {
					$s .= ', ';
				}
				$s .= self::encodeJsVar( $elt );
			}
			$s .= ']';
		} elseif( is_object( $value ) || is_array( $value ) ) {
			// Objects and associative arrays
			$s = '{';
			foreach( (array)$value as $name => $elt ){
				if( $s != '{' ) {
					$s .= ', ';
				}
				$s .= '"' . self::escapeJsString( $name ) . '": ' .
					  self::encodeJsVar( $elt );
			}
			$s .= '}';
		} else {
			$s = '"' . self::escapeJsString( $value ) . '"';
		}
		return $s;
	}