コード例 #1
0
                static OrderedDictionary ReadObject(ref Utf8JsonReader reader, JsonDecodeOptions phpoptions)
                {
                    var props = new OrderedDictionary();

                    // read properties until EndObject
                    for (; ;)
                    {
                        if (reader.Read())
                        {
                            if (reader.TokenType == JsonTokenType.PropertyName)
                            {
                                props[reader.GetString()] = ReadValue(ref reader, phpoptions);
                                continue;
                            }
                            else if (reader.TokenType == JsonTokenType.EndObject)
                            {
                                break;
                            }
                        }

                        ThrowError(JSON_ERROR_SYNTAX);
                    }

                    //
                    return(props);
                }
コード例 #2
0
                static OrderedDictionary ReadArray(ref Utf8JsonReader reader, JsonDecodeOptions phpoptions)
                {
                    var props = new OrderedDictionary();

                    // read values until EndArray
                    for (; ;)
                    {
                        if (reader.Read())
                        {
                            if (reader.TokenType == JsonTokenType.EndArray)
                            {
                                break;
                            }
                            else
                            {
                                props.Add(GetValue(ref reader, phpoptions));
                                continue;
                            }
                        }

                        ThrowError(JSON_ERROR_SYNTAX);
                    }

                    //
                    return(props);
                }
コード例 #3
0
                static PhpValue ReadValue(ref Utf8JsonReader reader, JsonDecodeOptions phpoptions)
                {
                    if (reader.Read())
                    {
                        return(GetValue(ref reader, phpoptions));
                    }

                    // EOF
                    return(PhpValue.Null);
                }
コード例 #4
0
                static PhpValue GetValue(ref Utf8JsonReader reader, JsonDecodeOptions phpoptions)
                {
                    switch (reader.TokenType)
                    {
                    case JsonTokenType.StartObject:
                        var props = new PhpArray(ReadObject(ref reader, phpoptions));
                        return((phpoptions & JsonDecodeOptions.JSON_OBJECT_AS_ARRAY) != 0
                                ? PhpValue.Create(props)
                                : PhpValue.FromClass(props.AsStdClass()));

                    case JsonTokenType.StartArray:
                        return(new PhpArray(ReadArray(ref reader, phpoptions)));

                    case JsonTokenType.String:
                        return(reader.GetString());

                    case JsonTokenType.Number:
                        if (reader.TryGetInt64(out var l))
                        {
                            return(l);
                        }
                        if ((phpoptions & JsonDecodeOptions.JSON_BIGINT_AS_STRING) != 0 && IsIntegerType(reader.ValueSpan))
                        {
                            // big int encode as string
                            //return Encoding.ASCII.GetString(reader.ValueSpan); // NETSTANDARD2.1: ReadOnlySpan<byte>
                            return(JsonEncodedText.Encode(reader.ValueSpan).ToString());
                        }
                        if (reader.TryGetDouble(out var d))
                        {
                            return(d);
                        }
                        ThrowError(JSON_ERROR_INF_OR_NAN);
                        break;

                    case JsonTokenType.True:
                        return(PhpValue.True);

                    case JsonTokenType.False:
                        return(PhpValue.False);

                    case JsonTokenType.Null:
                        return(PhpValue.Null);
                    }

                    //
                    ThrowError(JSON_ERROR_SYNTAX);
                    return(PhpValue.Null);
                }
コード例 #5
0
ファイル: Serialization.Json.cs プロジェクト: Valks/peachpie
        /// <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));
        }
コード例 #6
0
ファイル: PhpJson.cs プロジェクト: jiahao42/weverca
        public static PhpReference Unserialize(PhpBytes json, bool assoc /* = false*/, int depth /* = 512*/, JsonDecodeOptions options /* = 0 */)
        {
            if (json == null)
            {
                return(null);
            }

            return(new PhpJsonSerializer(
                       new JsonFormatter.EncodeOptions(),
                       new JsonFormatter.DecodeOptions()
            {
                Assoc = assoc,
                Depth = depth,
                BigIntAsString = (options & JsonDecodeOptions.JSON_BIGINT_AS_STRING) != 0
            }
                       ).Deserialize(json, UnknownTypeDesc.Singleton));
        }
コード例 #7
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)));
        }
コード例 #8
0
                public static PhpValue Deserialize(ReadOnlySpan <byte> utf8bytes, JsonReaderOptions options, JsonDecodeOptions phpoptions)
                {
                    var reader = new Utf8JsonReader(utf8bytes, options);

                    return(ReadValue(ref reader, phpoptions));
                }
コード例 #9
0
                /// <summary>
                /// Deserializes the value, sets json_last_error or throws <see cref="JsonException"/> eventually.
                /// </summary>
                public static PhpValue DeserializeWithError(Context ctx, ReadOnlySpan <byte> utf8bytes, JsonReaderOptions options, JsonDecodeOptions phpoptions)
                {
                    try
                    {
                        var value = Deserialize(utf8bytes, options, phpoptions);

                        SetLastJsonError(ctx, JSON_ERROR_NONE);

                        return(value);
                    }
                    catch (JsonException jsonex)
                    {
                        if ((phpoptions & JsonDecodeOptions.JSON_THROW_ON_ERROR) == 0)
                        {
                            SetLastJsonError(ctx, jsonex.getCode());
                            return(PhpValue.Null);
                        }
                        else
                        {
                            throw;
                        }
                    }
                    catch (System.Exception ex)
                    {
                        // internal error,
                        // treat as syntax error

                        if ((phpoptions & JsonDecodeOptions.JSON_THROW_ON_ERROR) == 0)
                        {
                            SetLastJsonError(ctx, JSON_ERROR_SYNTAX);
                            return(PhpValue.Null);
                        }
                        else
                        {
                            throw new JsonException(ex.Message, JSON_ERROR_SYNTAX, ex as Throwable);
                        }
                    }
                }
コード例 #10
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 objects will be converted into associative arrays. </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);
            }

            if (assoc)
            {
                options |= JsonDecodeOptions.JSON_OBJECT_AS_ARRAY;
            }

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

            //return new PhpSerialization.JsonSerializer(decodeOptions: decodeoptions).Deserialize(ctx, new PhpString(json.ToBytes(Encoding.UTF8)), default);

            //
            return(PhpSerialization.JsonSerializer.ObjectReader.DeserializeWithError(
                       ctx,
                       json.ToBytes(Encoding.UTF8).AsSpan(),
                       new JsonReaderOptions
            {
                AllowTrailingCommas = true,
                CommentHandling = JsonCommentHandling.Skip,
                MaxDepth = depth,
            },
                       options));
        }