Пример #1
0
        /// <summary>
        /// Returns the error string of the last <see cref="json_encode"/> or <see cref="json_decode"/> call.
        /// </summary>
        public static string json_last_error_msg(Context ctx)
        {
            var error = (JsonError)PhpSerialization.GetLastJsonError(ctx);

            // TODO: to resources

            switch (error)
            {
            case JsonError.None: return("No error");

            case JsonError.Depth: return("Maximum stack depth exceeded");

            case JsonError.StateMismatch: return("State mismatch (invalid or malformed JSON)");

            case JsonError.CtrlChar: return("Control character error, possibly incorrectly encoded");

            case JsonError.Syntax: return("Syntax error");

            case JsonError.Utf8: return("Malformed UTF-8 characters, possibly incorrectly encoded");

            case JsonError.Recursion:
            case JsonError.InfOrNan:
            case JsonError.UnsupportedType:
            case JsonError.InvalidPropertyName:
                return(error.ToString());

            default: throw new ArgumentOutOfRangeException();
            }
        }
Пример #2
0
        /// <summary>
        /// Returns the JSON representation of a value.
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="value">The value being encoded. Can be any type except a <see cref="PhpResource"/>.
        /// All string data must be UTF-8 encoded.</param>
        /// <param name="options"></param>
        /// <param name="depth">Set the maximum depth. Must be greater than zero.</param>
        public static string json_encode(Context ctx, PhpValue value, JsonEncodeOptions options = JsonEncodeOptions.Default, int depth = 512)
        {
            // TODO: depth

            PhpSerialization.SetLastJsonError(ctx, 0);

            //return new PhpSerialization.JsonSerializer(encodeOptions: options).Serialize(ctx, value, default);
            return(PhpSerialization.JsonSerializer.ObjectWriter.Serialize(ctx, value, options, default));
        }
Пример #3
0
        /// <summary>
        /// Returns the error string of the last <see cref="json_encode"/> or <see cref="json_decode"/> call.
        /// </summary>
        public static string json_last_error_msg(Context ctx)
        {
            switch (PhpSerialization.GetLastJsonError(ctx))
            {
            case JSON_ERROR_NONE: return("No error");

            case JSON_ERROR_DEPTH: return("Maximum stack depth exceeded");

            case JSON_ERROR_STATE_MISMATCH: return("State mismatch (invalid or malformed JSON)");

            case JSON_ERROR_CTRL_CHAR: return("Control character error, possibly incorrectly encoded");

            case JSON_ERROR_SYNTAX: return("Syntax error");

            case JSON_ERROR_UTF8: return("Malformed UTF-8 characters, possibly incorrectly encoded");

            default: throw new ArgumentOutOfRangeException();
            }
        }
Пример #4
0
 public static int json_last_error(Context ctx) => PhpSerialization.GetLastJsonError(ctx);