Exemple #1
0
        /// <summary>
        /// Decodes given json encoded string.
        /// </summary>
        public static PhpValue JsonDecode(string value)
        {
            var options = new PhpSerialization.JsonSerializer.DecodeOptions();
            var scanner = new Json.JsonScanner(new StringReader(value), options);
            var parser  = new Json.Parser(options)
            {
                Scanner = scanner,
            };

            return(parser.Parse() ? parser.Result : throw new FormatException());
        }
Exemple #2
0
        /// <summary>
        /// Takes a JSON encoded string and converts it into a PHP variable.
        /// </summary>
        /// <param name="ctx">Runtime context.</param>
        /// <param name="json"></param>
        /// <param name="assoc">When TRUE, returned object's will be converted into associative array s. </param>
        /// <param name="depth">User specified recursion depth. </param>
        /// <param name="options"></param>
        /// <returns>Returns the value encoded in json in appropriate PHP type. Values true, false and null are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.</returns>
        public static PhpValue json_decode(Context ctx, PhpString json, bool assoc = false, int depth = 512, JsonDecodeOptions options = JsonDecodeOptions.Default)
        {
            if (json.IsEmpty)
            {
                return(PhpValue.Null);
            }

            var decodeoptions = new PhpSerialization.JsonSerializer.DecodeOptions()
            {
                Assoc   = assoc,
                Depth   = depth,
                Options = options,
            };

            return(new PhpSerialization.JsonSerializer(decodeOptions: decodeoptions).Deserialize(ctx, json, default));
        }
Exemple #3
0
        /// <summary>
        /// Takes a JSON encoded string and converts it into a PHP variable.
        /// </summary>
        /// <param name="ctx">Runtime context.</param>
        /// <param name="json"></param>
        /// <param name="assoc">When TRUE, returned object's will be converted into associative array s. </param>
        /// <param name="depth">User specified recursion depth. </param>
        /// <param name="options"></param>
        /// <returns>Returns the value encoded in json in appropriate PHP type. Values true, false and null are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.</returns>
        public static PhpValue json_decode(Context ctx, PhpString json, bool assoc = false, int depth = 512, JsonDecodeOptions options = JsonDecodeOptions.Default)
        {
            if (json.IsEmpty)
            {
                return(PhpValue.Null);
            }

            var decodeoptions = new PhpSerialization.JsonSerializer.DecodeOptions()
            {
                Assoc          = assoc,
                Depth          = depth,
                BigIntAsString = (options & JsonDecodeOptions.JSON_BIGINT_AS_STRING) != 0
            };

            return(new PhpSerialization.JsonSerializer(decodeOptions: decodeoptions).Deserialize(ctx, json, default(RuntimeTypeHandle)));
        }