Ejemplo n.º 1
0
            protected override PhpValue CommonDeserialize(Context ctx, Stream data, RuntimeTypeHandle caller)
            {
                var jsonerror = ctx.GetStatic <JsonLastError>();

                var options = _decodeOptions ?? new DecodeOptions();
                var scanner = new Json.JsonScanner(new StreamReader(data), options);
                var parser  = new Json.Parser(options)
                {
                    Scanner = scanner
                };

                jsonerror.LastError = JsonSerialization.JSON_ERROR_NONE;

                if (!parser.Parse())
                {
                    var errorcode = JsonSerialization.JSON_ERROR_SYNTAX;

                    if ((options.Options & JsonDecodeOptions.JSON_THROW_ON_ERROR) == 0)
                    {
                        jsonerror.LastError = errorcode;
                        return(PhpValue.Null);
                    }
                    else
                    {
                        throw new JsonException(code: errorcode);
                    }
                }

                //
                return(parser.Result);
            }
Ejemplo n.º 2
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());
        }