Repository: ijl/orjson Branch: master Commit: ec2b066cae79 Files: 570 Total size: 1.3 MB Directory structure: gitextract_wnu8x3ig/ ├── .gitattributes ├── .github/ │ ├── actions/ │ │ └── manylinux/ │ │ └── action.yaml │ └── workflows/ │ ├── artifact.yaml │ ├── lint.yaml │ └── unusual.yaml ├── .gitignore ├── CHANGELOG.md ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── LICENSE-MPL-2.0 ├── README.md ├── bench/ │ ├── __init__.py │ ├── benchmark_dumps.py │ ├── benchmark_loads.py │ ├── data.py │ ├── requirements.txt │ ├── run_func │ └── util.py ├── build.rs ├── ci/ │ ├── config.toml │ └── sdist.toml ├── data/ │ ├── blns.txt.xz │ ├── canada.json.xz │ ├── citm_catalog.json.xz │ ├── github.json.xz │ ├── issue331_1.json.xz │ ├── issue331_2.json.xz │ ├── jsonchecker/ │ │ ├── fail01.json │ │ ├── fail02.json │ │ ├── fail03.json │ │ ├── fail04.json │ │ ├── fail05.json │ │ ├── fail06.json │ │ ├── fail07.json │ │ ├── fail08.json │ │ ├── fail09.json │ │ ├── fail10.json │ │ ├── fail11.json │ │ ├── fail12.json │ │ ├── fail13.json │ │ ├── fail14.json │ │ ├── fail15.json │ │ ├── fail16.json │ │ ├── fail17.json │ │ ├── fail18.json │ │ ├── fail19.json │ │ ├── fail20.json │ │ ├── fail21.json │ │ ├── fail22.json │ │ ├── fail23.json │ │ ├── fail24.json │ │ ├── fail25.json │ │ ├── fail26.json │ │ ├── fail27.json │ │ ├── fail28.json │ │ ├── fail29.json │ │ ├── fail30.json │ │ ├── fail31.json │ │ ├── fail32.json │ │ ├── fail33.json │ │ ├── pass01.json │ │ ├── pass02.json │ │ └── pass03.json │ ├── parsing/ │ │ ├── i_number_double_huge_neg_exp.json │ │ ├── i_number_huge_exp.json │ │ ├── i_number_neg_int_huge_exp.json │ │ ├── i_number_pos_double_huge_exp.json │ │ ├── i_number_real_neg_overflow.json │ │ ├── i_number_real_pos_overflow.json │ │ ├── i_number_real_underflow.json │ │ ├── i_number_too_big_neg_int.json │ │ ├── i_number_too_big_pos_int.json │ │ ├── i_number_very_big_negative_int.json │ │ ├── i_object_key_lone_2nd_surrogate.json │ │ ├── i_string_1st_surrogate_but_2nd_missing.json │ │ ├── i_string_1st_valid_surrogate_2nd_invalid.json │ │ ├── i_string_UTF-16LE_with_BOM.json │ │ ├── i_string_UTF-8_invalid_sequence.json │ │ ├── i_string_UTF8_surrogate_U+D800.json │ │ ├── i_string_incomplete_surrogate_and_escape_valid.json │ │ ├── i_string_incomplete_surrogate_pair.json │ │ ├── i_string_incomplete_surrogates_escape_valid.json │ │ ├── i_string_invalid_lonely_surrogate.json │ │ ├── i_string_invalid_surrogate.json │ │ ├── i_string_invalid_utf-8.json │ │ ├── i_string_inverted_surrogates_U+1D11E.json │ │ ├── i_string_iso_latin_1.json │ │ ├── i_string_lone_second_surrogate.json │ │ ├── i_string_lone_utf8_continuation_byte.json │ │ ├── i_string_not_in_unicode_range.json │ │ ├── i_string_overlong_sequence_2_bytes.json │ │ ├── i_string_overlong_sequence_6_bytes.json │ │ ├── i_string_overlong_sequence_6_bytes_null.json │ │ ├── i_string_truncated-utf-8.json │ │ ├── i_string_utf16BE_no_BOM.json │ │ ├── i_string_utf16LE_no_BOM.json │ │ ├── i_structure_500_nested_arrays.json.xz │ │ ├── i_structure_UTF-8_BOM_empty_object.json │ │ ├── n_array_1_true_without_comma.json │ │ ├── n_array_a_invalid_utf8.json │ │ ├── n_array_colon_instead_of_comma.json │ │ ├── n_array_comma_after_close.json │ │ ├── n_array_comma_and_number.json │ │ ├── n_array_double_comma.json │ │ ├── n_array_double_extra_comma.json │ │ ├── n_array_extra_close.json │ │ ├── n_array_extra_comma.json │ │ ├── n_array_incomplete.json │ │ ├── n_array_incomplete_invalid_value.json │ │ ├── n_array_inner_array_no_comma.json │ │ ├── n_array_invalid_utf8.json │ │ ├── n_array_items_separated_by_semicolon.json │ │ ├── n_array_just_comma.json │ │ ├── n_array_just_minus.json │ │ ├── n_array_missing_value.json │ │ ├── n_array_newlines_unclosed.json │ │ ├── n_array_number_and_comma.json │ │ ├── n_array_number_and_several_commas.json │ │ ├── n_array_spaces_vertical_tab_formfeed.json │ │ ├── n_array_star_inside.json │ │ ├── n_array_unclosed.json │ │ ├── n_array_unclosed_trailing_comma.json │ │ ├── n_array_unclosed_with_new_lines.json │ │ ├── n_array_unclosed_with_object_inside.json │ │ ├── n_incomplete_false.json │ │ ├── n_incomplete_null.json │ │ ├── n_incomplete_true.json │ │ ├── n_multidigit_number_then_00.json │ │ ├── n_number_++.json │ │ ├── n_number_+1.json │ │ ├── n_number_+Inf.json │ │ ├── n_number_-01.json │ │ ├── n_number_-1.0..json │ │ ├── n_number_-2..json │ │ ├── n_number_-NaN.json │ │ ├── n_number_.-1.json │ │ ├── n_number_.2e-3.json │ │ ├── n_number_0.1.2.json │ │ ├── n_number_0.3e+.json │ │ ├── n_number_0.3e.json │ │ ├── n_number_0.e1.json │ │ ├── n_number_0_capital_E+.json │ │ ├── n_number_0_capital_E.json │ │ ├── n_number_0e+.json │ │ ├── n_number_0e.json │ │ ├── n_number_1.0e+.json │ │ ├── n_number_1.0e-.json │ │ ├── n_number_1.0e.json │ │ ├── n_number_1_000.json │ │ ├── n_number_1eE2.json │ │ ├── n_number_2.e+3.json │ │ ├── n_number_2.e-3.json │ │ ├── n_number_2.e3.json │ │ ├── n_number_9.e+.json │ │ ├── n_number_Inf.json │ │ ├── n_number_NaN.json │ │ ├── n_number_U+FF11_fullwidth_digit_one.json │ │ ├── n_number_expression.json │ │ ├── n_number_hex_1_digit.json │ │ ├── n_number_hex_2_digits.json │ │ ├── n_number_infinity.json │ │ ├── n_number_invalid+-.json │ │ ├── n_number_invalid-negative-real.json │ │ ├── n_number_invalid-utf-8-in-bigger-int.json │ │ ├── n_number_invalid-utf-8-in-exponent.json │ │ ├── n_number_invalid-utf-8-in-int.json │ │ ├── n_number_minus_infinity.json │ │ ├── n_number_minus_sign_with_trailing_garbage.json │ │ ├── n_number_minus_space_1.json │ │ ├── n_number_neg_int_starting_with_zero.json │ │ ├── n_number_neg_real_without_int_part.json │ │ ├── n_number_neg_with_garbage_at_end.json │ │ ├── n_number_real_garbage_after_e.json │ │ ├── n_number_real_with_invalid_utf8_after_e.json │ │ ├── n_number_real_without_fractional_part.json │ │ ├── n_number_starting_with_dot.json │ │ ├── n_number_with_alpha.json │ │ ├── n_number_with_alpha_char.json │ │ ├── n_number_with_leading_zero.json │ │ ├── n_object_bad_value.json │ │ ├── n_object_bracket_key.json │ │ ├── n_object_comma_instead_of_colon.json │ │ ├── n_object_double_colon.json │ │ ├── n_object_emoji.json │ │ ├── n_object_garbage_at_end.json │ │ ├── n_object_key_with_single_quotes.json │ │ ├── n_object_lone_continuation_byte_in_key_and_trailing_comma.json │ │ ├── n_object_missing_colon.json │ │ ├── n_object_missing_key.json │ │ ├── n_object_missing_semicolon.json │ │ ├── n_object_missing_value.json │ │ ├── n_object_no-colon.json │ │ ├── n_object_non_string_key.json │ │ ├── n_object_non_string_key_but_huge_number_instead.json │ │ ├── n_object_repeated_null_null.json │ │ ├── n_object_several_trailing_commas.json │ │ ├── n_object_single_quote.json │ │ ├── n_object_trailing_comma.json │ │ ├── n_object_trailing_comment.json │ │ ├── n_object_trailing_comment_open.json │ │ ├── n_object_trailing_comment_slash_open.json │ │ ├── n_object_trailing_comment_slash_open_incomplete.json │ │ ├── n_object_two_commas_in_a_row.json │ │ ├── n_object_unquoted_key.json │ │ ├── n_object_unterminated-value.json │ │ ├── n_object_with_single_string.json │ │ ├── n_object_with_trailing_garbage.json │ │ ├── n_single_space.json │ │ ├── n_string_1_surrogate_then_escape.json │ │ ├── n_string_1_surrogate_then_escape_u.json │ │ ├── n_string_1_surrogate_then_escape_u1.json │ │ ├── n_string_1_surrogate_then_escape_u1x.json │ │ ├── n_string_accentuated_char_no_quotes.json │ │ ├── n_string_backslash_00.json │ │ ├── n_string_escape_x.json │ │ ├── n_string_escaped_backslash_bad.json │ │ ├── n_string_escaped_ctrl_char_tab.json │ │ ├── n_string_escaped_emoji.json │ │ ├── n_string_incomplete_escape.json │ │ ├── n_string_incomplete_escaped_character.json │ │ ├── n_string_incomplete_surrogate.json │ │ ├── n_string_incomplete_surrogate_escape_invalid.json │ │ ├── n_string_invalid-utf-8-in-escape.json │ │ ├── n_string_invalid_backslash_esc.json │ │ ├── n_string_invalid_unicode_escape.json │ │ ├── n_string_invalid_utf8_after_escape.json │ │ ├── n_string_leading_uescaped_thinspace.json │ │ ├── n_string_no_quotes_with_bad_escape.json │ │ ├── n_string_single_doublequote.json │ │ ├── n_string_single_quote.json │ │ ├── n_string_single_string_no_double_quotes.json │ │ ├── n_string_start_escape_unclosed.json │ │ ├── n_string_unescaped_crtl_char.json │ │ ├── n_string_unescaped_newline.json │ │ ├── n_string_unescaped_tab.json │ │ ├── n_string_unicode_CapitalU.json │ │ ├── n_string_with_trailing_garbage.json │ │ ├── n_structure_100000_opening_arrays.json.xz │ │ ├── n_structure_U+2060_word_joined.json │ │ ├── n_structure_UTF8_BOM_no_data.json │ │ ├── n_structure_angle_bracket_..json │ │ ├── n_structure_angle_bracket_null.json │ │ ├── n_structure_array_trailing_garbage.json │ │ ├── n_structure_array_with_extra_array_close.json │ │ ├── n_structure_array_with_unclosed_string.json │ │ ├── n_structure_ascii-unicode-identifier.json │ │ ├── n_structure_capitalized_True.json │ │ ├── n_structure_close_unopened_array.json │ │ ├── n_structure_comma_instead_of_closing_brace.json │ │ ├── n_structure_double_array.json │ │ ├── n_structure_end_array.json │ │ ├── n_structure_incomplete_UTF8_BOM.json │ │ ├── n_structure_lone-invalid-utf-8.json │ │ ├── n_structure_lone-open-bracket.json │ │ ├── n_structure_no_data.json │ │ ├── n_structure_null-byte-outside-string.json │ │ ├── n_structure_number_with_trailing_garbage.json │ │ ├── n_structure_object_followed_by_closing_object.json │ │ ├── n_structure_object_unclosed_no_value.json │ │ ├── n_structure_object_with_comment.json │ │ ├── n_structure_object_with_trailing_garbage.json │ │ ├── n_structure_open_array_apostrophe.json │ │ ├── n_structure_open_array_comma.json │ │ ├── n_structure_open_array_object.json.xz │ │ ├── n_structure_open_array_open_object.json │ │ ├── n_structure_open_array_open_string.json │ │ ├── n_structure_open_array_string.json │ │ ├── n_structure_open_object.json │ │ ├── n_structure_open_object_close_array.json │ │ ├── n_structure_open_object_comma.json │ │ ├── n_structure_open_object_open_array.json │ │ ├── n_structure_open_object_open_string.json │ │ ├── n_structure_open_object_string_with_apostrophes.json │ │ ├── n_structure_open_open.json │ │ ├── n_structure_single_eacute.json │ │ ├── n_structure_single_star.json │ │ ├── n_structure_trailing_#.json │ │ ├── n_structure_uescaped_LF_before_string.json │ │ ├── n_structure_unclosed_array.json │ │ ├── n_structure_unclosed_array_partial_null.json │ │ ├── n_structure_unclosed_array_unfinished_false.json │ │ ├── n_structure_unclosed_array_unfinished_true.json │ │ ├── n_structure_unclosed_object.json │ │ ├── n_structure_unicode-identifier.json │ │ ├── n_structure_whitespace_U+2060_word_joiner.json │ │ ├── n_structure_whitespace_formfeed.json │ │ ├── y_array_arraysWithSpaces.json │ │ ├── y_array_empty-string.json │ │ ├── y_array_empty.json │ │ ├── y_array_ending_with_newline.json │ │ ├── y_array_false.json │ │ ├── y_array_heterogeneous.json │ │ ├── y_array_null.json │ │ ├── y_array_with_1_and_newline.json │ │ ├── y_array_with_leading_space.json │ │ ├── y_array_with_several_null.json │ │ ├── y_array_with_trailing_space.json │ │ ├── y_number.json │ │ ├── y_number_0e+1.json │ │ ├── y_number_0e1.json │ │ ├── y_number_after_space.json │ │ ├── y_number_double_close_to_zero.json │ │ ├── y_number_int_with_exp.json │ │ ├── y_number_minus_zero.json │ │ ├── y_number_negative_int.json │ │ ├── y_number_negative_one.json │ │ ├── y_number_negative_zero.json │ │ ├── y_number_real_capital_e.json │ │ ├── y_number_real_capital_e_neg_exp.json │ │ ├── y_number_real_capital_e_pos_exp.json │ │ ├── y_number_real_exponent.json │ │ ├── y_number_real_fraction_exponent.json │ │ ├── y_number_real_neg_exp.json │ │ ├── y_number_real_pos_exponent.json │ │ ├── y_number_simple_int.json │ │ ├── y_number_simple_real.json │ │ ├── y_object.json │ │ ├── y_object_basic.json │ │ ├── y_object_duplicated_key.json │ │ ├── y_object_duplicated_key_and_value.json │ │ ├── y_object_empty.json │ │ ├── y_object_empty_key.json │ │ ├── y_object_escaped_null_in_key.json │ │ ├── y_object_extreme_numbers.json │ │ ├── y_object_long_strings.json │ │ ├── y_object_simple.json │ │ ├── y_object_string_unicode.json │ │ ├── y_object_with_newlines.json │ │ ├── y_string_1_2_3_bytes_UTF-8_sequences.json │ │ ├── y_string_accepted_surrogate_pair.json │ │ ├── y_string_accepted_surrogate_pairs.json │ │ ├── y_string_allowed_escapes.json │ │ ├── y_string_backslash_and_u_escaped_zero.json │ │ ├── y_string_backslash_doublequotes.json │ │ ├── y_string_comments.json │ │ ├── y_string_double_escape_a.json │ │ ├── y_string_double_escape_n.json │ │ ├── y_string_escaped_control_character.json │ │ ├── y_string_escaped_noncharacter.json │ │ ├── y_string_in_array.json │ │ ├── y_string_in_array_with_leading_space.json │ │ ├── y_string_last_surrogates_1_and_2.json │ │ ├── y_string_nbsp_uescaped.json │ │ ├── y_string_nonCharacterInUTF-8_U+10FFFF.json │ │ ├── y_string_nonCharacterInUTF-8_U+FFFF.json │ │ ├── y_string_null_escape.json │ │ ├── y_string_one-byte-utf-8.json │ │ ├── y_string_pi.json │ │ ├── y_string_reservedCharacterInUTF-8_U+1BFFF.json │ │ ├── y_string_simple_ascii.json │ │ ├── y_string_space.json │ │ ├── y_string_surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF.json │ │ ├── y_string_three-byte-utf-8.json │ │ ├── y_string_two-byte-utf-8.json │ │ ├── y_string_u+2028_line_sep.json │ │ ├── y_string_u+2029_par_sep.json │ │ ├── y_string_uEscape.json │ │ ├── y_string_uescaped_newline.json │ │ ├── y_string_unescaped_char_delete.json │ │ ├── y_string_unicode.json │ │ ├── y_string_unicodeEscapedBackslash.json │ │ ├── y_string_unicode_2.json │ │ ├── y_string_unicode_U+10FFFE_nonchar.json │ │ ├── y_string_unicode_U+1FFFE_nonchar.json │ │ ├── y_string_unicode_U+200B_ZERO_WIDTH_SPACE.json │ │ ├── y_string_unicode_U+2064_invisible_plus.json │ │ ├── y_string_unicode_U+FDD0_nonchar.json │ │ ├── y_string_unicode_U+FFFE_nonchar.json │ │ ├── y_string_unicode_escaped_double_quote.json │ │ ├── y_string_utf8.json │ │ ├── y_string_with_del_character.json │ │ ├── y_structure_lonely_false.json │ │ ├── y_structure_lonely_int.json │ │ ├── y_structure_lonely_negative_real.json │ │ ├── y_structure_lonely_null.json │ │ ├── y_structure_lonely_string.json │ │ ├── y_structure_lonely_true.json │ │ ├── y_structure_string_empty.json │ │ ├── y_structure_trailing_newline.json │ │ ├── y_structure_true_in_array.json │ │ └── y_structure_whitespace_array.json │ ├── roundtrip/ │ │ ├── roundtrip01.json │ │ ├── roundtrip02.json │ │ ├── roundtrip03.json │ │ ├── roundtrip04.json │ │ ├── roundtrip05.json │ │ ├── roundtrip06.json │ │ ├── roundtrip07.json │ │ ├── roundtrip08.json │ │ ├── roundtrip09.json │ │ ├── roundtrip10.json │ │ ├── roundtrip11.json │ │ ├── roundtrip12.json │ │ ├── roundtrip13.json │ │ ├── roundtrip14.json │ │ ├── roundtrip15.json │ │ ├── roundtrip16.json │ │ ├── roundtrip17.json │ │ ├── roundtrip18.json │ │ ├── roundtrip19.json │ │ ├── roundtrip20.json │ │ ├── roundtrip21.json │ │ ├── roundtrip22.json │ │ ├── roundtrip23.json │ │ ├── roundtrip24.json │ │ ├── roundtrip25.json │ │ ├── roundtrip26.json │ │ └── roundtrip27.json │ ├── transform/ │ │ ├── number_1.0.json │ │ ├── number_1.000000000000000005.json │ │ ├── number_1000000000000000.json │ │ ├── number_10000000000000000999.json │ │ ├── number_1e-999.json │ │ ├── number_1e6.json │ │ ├── object_key_nfc_nfd.json │ │ ├── object_key_nfd_nfc.json │ │ ├── object_same_key_different_values.json │ │ ├── object_same_key_same_value.json │ │ ├── object_same_key_unclear_values.json │ │ ├── string_1_escaped_invalid_codepoint.json │ │ ├── string_1_invalid_codepoint.json │ │ ├── string_2_escaped_invalid_codepoints.json │ │ ├── string_2_invalid_codepoints.json │ │ ├── string_3_escaped_invalid_codepoints.json │ │ ├── string_3_invalid_codepoints.json │ │ └── string_with_escaped_NULL.json │ └── twitter.json.xz ├── include/ │ └── yyjson/ │ ├── yyjson.c │ └── yyjson.h ├── integration/ │ ├── client │ ├── http │ ├── init │ ├── requirements.txt │ ├── run │ ├── thread │ ├── typestubs.py │ └── wsgi.py ├── pyproject.toml ├── pysrc/ │ └── orjson/ │ ├── __init__.py │ ├── __init__.pyi │ └── py.typed ├── requirements-lint.txt ├── requirements.txt ├── script/ │ ├── blame-to-header │ ├── cargo │ ├── check-pypi │ ├── check-version │ ├── debug │ ├── develop │ ├── graph │ ├── install-fedora │ ├── lint │ ├── profile │ ├── pybench │ ├── pycorrectness │ ├── pydataclass │ ├── pyindent │ ├── pynonstr │ ├── pynumpy │ ├── pysort │ ├── pytest │ └── valgrind ├── src/ │ ├── alloc.rs │ ├── deserialize/ │ │ ├── backend/ │ │ │ ├── ffi.rs │ │ │ ├── mod.rs │ │ │ └── yyjson.rs │ │ ├── cache.rs │ │ ├── deserializer.rs │ │ ├── error.rs │ │ ├── input.rs │ │ ├── mod.rs │ │ └── pyobject.rs │ ├── exception.rs │ ├── ffi/ │ │ ├── atomiculong.rs │ │ ├── buffer.rs │ │ ├── bytes.rs │ │ ├── compat.rs │ │ ├── fragment.rs │ │ ├── mod.rs │ │ ├── pyboolref.rs │ │ ├── pybytearrayref.rs │ │ ├── pybytesref.rs │ │ ├── pydictref.rs │ │ ├── pyfloatref.rs │ │ ├── pyfragmentref.rs │ │ ├── pyintref.rs │ │ ├── pylistref.rs │ │ ├── pymemoryview.rs │ │ ├── pynoneref.rs │ │ ├── pystrref/ │ │ │ ├── avx512.rs │ │ │ ├── mod.rs │ │ │ ├── object.rs │ │ │ ├── pyunicode_new.rs │ │ │ └── scalar.rs │ │ ├── pytupleref.rs │ │ ├── pyuuidref.rs │ │ └── utf8.rs │ ├── lib.rs │ ├── opt.rs │ ├── serialize/ │ │ ├── buffer.rs │ │ ├── error.rs │ │ ├── mod.rs │ │ ├── obtype.rs │ │ ├── per_type/ │ │ │ ├── dataclass.rs │ │ │ ├── datetime.rs │ │ │ ├── datetimelike.rs │ │ │ ├── default.rs │ │ │ ├── dict.rs │ │ │ ├── float.rs │ │ │ ├── fragment.rs │ │ │ ├── int.rs │ │ │ ├── list.rs │ │ │ ├── mod.rs │ │ │ ├── none.rs │ │ │ ├── numpy.rs │ │ │ ├── pybool.rs │ │ │ ├── pyenum.rs │ │ │ ├── unicode.rs │ │ │ └── uuid.rs │ │ ├── serializer.rs │ │ ├── state.rs │ │ └── writer/ │ │ ├── byteswriter.rs │ │ ├── formatter.rs │ │ ├── json.rs │ │ ├── mod.rs │ │ ├── num.rs │ │ └── str/ │ │ ├── avx512.rs │ │ ├── escape.rs │ │ ├── generic.rs │ │ ├── mod.rs │ │ ├── scalar.rs │ │ └── sse2.rs │ ├── typeref.rs │ └── util.rs └── test/ ├── __init__.py ├── requirements.txt ├── test_api.py ├── test_append_newline.py ├── test_buffer.py ├── test_canonical.py ├── test_circular.py ├── test_dataclass.py ├── test_datetime.py ├── test_default.py ├── test_dict.py ├── test_enum.py ├── test_error.py ├── test_escape.py ├── test_fake.py ├── test_fixture.py ├── test_fragment.py ├── test_indent.py ├── test_issue221.py ├── test_issue331.py ├── test_jsonchecker.py ├── test_memory.py ├── test_non_str_keys.py ├── test_numpy.py ├── test_parsing.py ├── test_recursion.py ├── test_reentrant.py ├── test_roundtrip.py ├── test_sort_keys.py ├── test_subclass.py ├── test_transform.py ├── test_type.py ├── test_typeddict.py ├── test_uuid.py └── util.py ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitattributes ================================================ include/yyjson/* linguist-vendored ================================================ FILE: .github/actions/manylinux/action.yaml ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2024-2025) name: manylinux inputs: arch: required: true interpreter: required: true features: required: true compatibility: required: true runs: using: "composite" steps: - name: Build and test shell: bash run: | set -eou pipefail mkdir dist export PYTHON="${{ inputs.interpreter }}" if [[ "${PYTHON}" == *t ]]; then export PYTHON_PACKAGE="$(echo ${PYTHON} | sed 's/.$//')-freethreading" else export PYTHON_PACKAGE="${PYTHON}" fi export TARGET="${{ inputs.arch }}-unknown-linux-gnu" export PATH="$PWD/.venv:$HOME/.cargo/bin:$PATH" ./script/install-fedora source "${VENV}/bin/activate" maturin build \ --release \ --strip \ --features="${{ inputs.features }}" \ --compatibility="${{ inputs.compatibility }}" \ --interpreter="${PYTHON}" \ --target="${TARGET}" uv pip install ${CARGO_TARGET_DIR}/wheels/orjson*.whl export PYTHONMALLOC="debug" pytest -v test ./integration/run thread ./integration/run http ./integration/run init cp ${CARGO_TARGET_DIR}/wheels/orjson*.whl dist ================================================ FILE: .github/workflows/artifact.yaml ================================================ # SPDX-License-Identifier: (Apache-2.0 OR MIT) # Copyright ijl (2022-2026), messense (2022), Dominic LoBue (2022) name: artifact on: push env: FORCE_COLOR: "1" ORJSON_BUILD_FREETHREADED: "1" PIP_DISABLE_PIP_VERSION_CHECK: "1" RUST_TOOLCHAIN: "nightly-2026-01-28" RUST_TOOLCHAIN_STABLE: "1.89" UNSAFE_PYO3_BUILD_FREE_THREADED: "1" UNSAFE_PYO3_SKIP_VERSION_CHECK: "1" UV_LINK_MODE: "copy" jobs: sdist: runs-on: ubuntu-24.04 timeout-minutes: 10 strategy: fail-fast: false steps: - uses: actions/setup-python@v6 with: python-version: "3.14" - name: rustup stable run: | curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain "${RUST_TOOLCHAIN_STABLE}" -y rustup default "${RUST_TOOLCHAIN_STABLE}" - uses: actions/checkout@v6 - name: Cargo.toml and pyproject.toml version must match run: ./script/check-version - run: python3 -m pip install --user --upgrade pip "maturin>=1.10,<2" wheel - name: Vendor dependencies run: | maturin build cargo fetch mkdir .cargo cp ci/sdist.toml .cargo/config.toml cargo vendor include/cargo --versioned-dirs - run: maturin sdist --out=dist - run: python3 -m pip install --user dist/orjson*.tar.gz env: CARGO_NET_OFFLINE: "true" - run: python3 -m pip install --user -r test/requirements.txt -r integration/requirements.txt mypy - run: pytest -v test env: PYTHONMALLOC: "debug" - run: ./integration/run thread - run: ./integration/run http - run: ./integration/run init - run: ./integration/run typestubs - name: Store sdist uses: actions/upload-artifact@v5 with: name: orjson_sdist path: dist overwrite: true retention-days: 1 if-no-files-found: "error" compression-level: 0 manylinux: name: "manylinux_${{ matrix.arch.arch }}_${{ matrix.python.interpreter }}" runs-on: "${{ matrix.arch.runner }}" container: image: fedora:rawhide timeout-minutes: 10 strategy: fail-fast: false matrix: python: [ { interpreter: 'python3.14', compatibility: "manylinux_2_17", publish: true }, { interpreter: 'python3.13', compatibility: "manylinux_2_17", publish: true }, { interpreter: 'python3.12', compatibility: "manylinux_2_17", publish: true }, { interpreter: 'python3.11', compatibility: "manylinux_2_17", publish: true }, { interpreter: 'python3.10', compatibility: "manylinux_2_17", publish: true }, ] arch: [ { runner: "ubuntu-24.04", arch: "x86_64", features: "avx512", }, { runner: "ubuntu-24.04-arm", arch: "aarch64", features: "generic_simd" }, ] env: CARGO_TARGET_DIR: "/tmp/orjson" CC: "clang" CFLAGS: "-O2 -fstrict-aliasing -fno-plt -emit-llvm" LDFLAGS: "-fuse-ld=lld -Wl,-plugin-opt=also-emit-llvm -Wl,--as-needed -Wl,-zrelro,-znow" RUSTFLAGS: "-Z unstable-options -C panic=immediate-abort -C linker=clang -C link-arg=-fuse-ld=lld -C linker-plugin-lto -C link-arg=-Wl,-zrelro,-znow -Z mir-opt-level=4 -Z threads=4 -D warnings" VENV: ".venv" steps: - name: CPU info run: cat /proc/cpuinfo - run: dnf install --setopt=install_weak_deps=false -y git-core - uses: actions/checkout@v6 - name: Build and test uses: ./.github/actions/manylinux with: arch: "${{ matrix.arch.arch }}" interpreter: "${{ matrix.python.interpreter }}" features: "${{ matrix.arch.features }}" compatibility: "${{ matrix.python.compatibility }}" - name: Store wheels if: matrix.python.publish == true uses: actions/upload-artifact@v5 with: name: "orjson_manylinux_${{ matrix.arch.arch }}_${{ matrix.python.interpreter }}_${{ matrix.python.compatibility }}" path: dist overwrite: true retention-days: 1 - name: Debug env: CARGO_TARGET_DIR: "/tmp/orjson" PYTHON: "${{ matrix.python.interpreter }}" TARGET: "${{ matrix.arch.arch }}-unknown-linux-gnu" run: | export PATH="$PWD/.venv:$HOME/.cargo/bin:$PATH" source .venv/bin/activate script/debug manylinux_cross: runs-on: ubuntu-24.04 timeout-minutes: 10 strategy: fail-fast: false matrix: python: [ { interpreter: 'python3.14', abi: 'cp314-cp314', manylinux: 'manylinux_2_17', publish: true }, { interpreter: 'python3.13', abi: 'cp313-cp313', manylinux: 'manylinux_2_17', publish: true }, { interpreter: 'python3.12', abi: 'cp312-cp312', manylinux: 'manylinux_2_17', publish: true }, { interpreter: 'python3.11', abi: 'cp311-cp311', manylinux: 'manylinux_2_17', publish: true }, { interpreter: 'python3.10', abi: 'cp310-cp310', manylinux: 'manylinux_2_17', publish: true }, ] target: [ { arch: 'i686', cflags: '-Os -fstrict-aliasing', features: 'no_panic', rustflags: '-Z unstable-options -C panic=immediate-abort -Z mir-opt-level=4 -D warnings', target: 'i686-unknown-linux-gnu', }, { arch: 'armv7', cflags: '-Os -fstrict-aliasing', features: 'no_panic', rustflags: '-Z unstable-options -C panic=immediate-abort -Z mir-opt-level=4 -D warnings -C opt-level=s', target: 'armv7-unknown-linux-gnueabihf', }, { arch: 'ppc64le', cflags: '-Os -fstrict-aliasing', features: 'generic_simd,no_panic', rustflags: '-Z unstable-options -C panic=immediate-abort -Z mir-opt-level=4 -D warnings', target: 'powerpc64le-unknown-linux-gnu', }, { arch: 's390x', cflags: '-Os -fstrict-aliasing -march=z10', features: 'no_panic', rustflags: '-Z unstable-options -C panic=immediate-abort -Z mir-opt-level=4 -D warnings -C target-cpu=z10', target: 's390x-unknown-linux-gnu', }, ] steps: - uses: actions/checkout@v6 - name: build-std run: | mkdir .cargo cp ci/config.toml .cargo/config.toml - name: Build uses: PyO3/maturin-action@v1 env: PYO3_CROSS_LIB_DIR: "/opt/python/${{ matrix.python.abi }}" CFLAGS: "${{ matrix.target.cflags }}" LDFLAGS: "-Wl,--as-needed" RUSTFLAGS: "${{ matrix.target.rustflags }}" with: target: "${{ matrix.target.target }}" rust-toolchain: "${{ env.RUST_TOOLCHAIN }}" rustup-components: rust-src manylinux: "${{ matrix.python.manylinux }}" args: --release --strip --out=dist --features=${{ matrix.target.features }} -i ${{ matrix.python.interpreter }} - name: Store wheels if: matrix.python.publish == true uses: actions/upload-artifact@v5 with: name: "orjson_manylinux_${{ matrix.target.arch }}_${{ matrix.python.interpreter }}" path: dist overwrite: true retention-days: 1 if-no-files-found: "error" compression-level: 0 - name: setup-qemu-container if: "matrix.target.arch == 's390x' || matrix.target.arch == 'ppc64le'" uses: sandervocke/setup-qemu-container@v1 with: container: registry.fedoraproject.org/fedora:43 arch: ${{ matrix.target.arch }} podman_args: "-v .:/orjson -v /tmp:/tmp --workdir /orjson" - name: setup-shell-wrapper uses: sandervocke/setup-shell-wrapper@v1 - name: Emulated Test if: "matrix.target.arch == 's390x' || matrix.target.arch == 'ppc64le'" shell: wrap-shell {0} env: WRAP_SHELL: run-in-container.sh run: | set -eou pipefail dnf install --setopt=install_weak_deps=false -y ${{ matrix.python.interpreter }} python3-uv uv venv --python ${{ matrix.python.interpreter }} source .venv/bin/activate uv pip install -r test/requirements.txt uv pip install dist/orjson*.whl pytest -v test musllinux_amd64: runs-on: ubuntu-24.04 timeout-minutes: 10 strategy: fail-fast: false matrix: python: [ { version: '3.14', pytest: '1', publish: true }, { version: '3.13', pytest: '1', publish: true }, { version: '3.12', pytest: '1', publish: true }, { version: '3.11', pytest: '0', publish: true }, { version: '3.10', pytest: '0', publish: true }, ] platform: - target: x86_64-unknown-linux-musl arch: x86_64 platform: linux/amd64 features: avx512,unwind - target: i686-unknown-linux-musl arch: i686 platform: linux/386 features: unwind steps: - uses: actions/checkout@v6 - name: build-std run: | mkdir .cargo cp ci/config.toml .cargo/config.toml - name: Build uses: PyO3/maturin-action@v1 env: CC: "gcc" CFLAGS: "-O2" LDFLAGS: "-Wl,--as-needed" RUSTFLAGS: "-Z unstable-options -C panic=immediate-abort -Z mir-opt-level=4 -Z threads=2 -D warnings -C target-feature=-crt-static" with: rust-toolchain: "${{ env.RUST_TOOLCHAIN }}" rustup-components: rust-src target: "${{ matrix.platform.target }}" manylinux: musllinux_1_2 args: --release --strip --out=dist --features=${{ matrix.platform.features }} -i python${{ matrix.python.version }} - name: Test uses: addnab/docker-run-action@v3 with: image: "quay.io/pypa/musllinux_1_2_${{ matrix.platform.arch }}:2026.01.28-1" options: -v ${{ github.workspace }}:/io -w /io run: | apk add tzdata sed -i '/^psutil/d' test/requirements.txt # missing 3.11, 3.12 wheels sed -i '/^numpy/d' test/requirements.txt python${{ matrix.python.version }} -m venv venv venv/bin/pip install -U pip wheel venv/bin/pip install orjson --no-index --find-links dist/ --force-reinstall # segfault on starting pytest after January 2025 on 3.11 and older; artifact works fine if [ ${{ matrix.python.pytest }} == '1' ]; then venv/bin/pip install -r test/requirements.txt PYTHONMALLOC="debug" venv/bin/python -m pytest -v test fi - name: Store wheels if: matrix.python.publish == true uses: actions/upload-artifact@v5 with: name: orjson_musllinux_${{ matrix.platform.arch }}_${{ matrix.python.version }} path: dist overwrite: true retention-days: 1 if-no-files-found: "error" compression-level: 0 musllinux_aarch64: runs-on: ubuntu-24.04-arm timeout-minutes: 10 strategy: fail-fast: false matrix: python: [ { version: '3.14', publish: true }, { version: '3.13', publish: true }, { version: '3.12', publish: true }, { version: '3.11', publish: true }, { version: '3.10', publish: true }, ] platform: - target: aarch64-unknown-linux-musl arch: aarch64 platform: linux/arm64 features: generic_simd,no_panic,unwind - target: armv7-unknown-linux-musleabihf arch: armv7l platform: linux/arm/v7 features: no_panic steps: - uses: actions/checkout@v6 - name: build-std run: | mkdir .cargo cp ci/config.toml .cargo/config.toml - name: Build uses: PyO3/maturin-action@v1 env: CC: "gcc" CFLAGS: "-O2" LDFLAGS: "-Wl,--as-needed" RUSTFLAGS: "-Z unstable-options -C panic=immediate-abort -Z mir-opt-level=4 -Z threads=2 -D warnings -C target-feature=-crt-static" with: rust-toolchain: "${{ env.RUST_TOOLCHAIN }}" rustup-components: rust-src target: "${{ matrix.platform.target }}" manylinux: musllinux_1_2 args: --release --strip --out=dist --features=${{ matrix.platform.features }} -i python${{ matrix.python.version }} - name: Test uses: addnab/docker-run-action@v3 with: image: "quay.io/pypa/musllinux_1_2_${{ matrix.platform.arch }}:2026.01.28-1" options: -v ${{ github.workspace }}:/io -w /io run: | apk add tzdata sed -i '/^psutil/d' test/requirements.txt # missing 3.11, 3.12 wheels sed -i '/^numpy/d' test/requirements.txt python${{ matrix.python.version }} -m venv venv venv/bin/pip install -U pip wheel venv/bin/pip install -r test/requirements.txt venv/bin/pip install orjson --no-index --find-links dist/ --force-reinstall export PYTHONMALLOC="debug" venv/bin/python -m pytest -v test - name: Store wheels if: matrix.python.publish == true uses: actions/upload-artifact@v5 with: name: orjson_musllinux_${{ matrix.platform.arch }}_${{ matrix.python.version }} path: dist overwrite: true retention-days: 1 if-no-files-found: "error" compression-level: 0 macos_aarch64: runs-on: macos-26 timeout-minutes: 10 strategy: fail-fast: false matrix: python: [ { version: '3.14', macosx_target: "15.0", publish: true }, { version: '3.13', macosx_target: "15.0", publish: true }, { version: '3.12', macosx_target: "15.0", publish: true }, { version: '3.11', macosx_target: "15.0", publish: true }, ] env: CC: "clang" LDFLAGS: "-Wl,--as-needed" CFLAGS: "-O2 -fstrict-aliasing -fno-plt -mcpu=apple-m1 -mtune=generic" RUSTFLAGS: "-Z unstable-options -C panic=immediate-abort -Z mir-opt-level=4 -Z threads=3 -D warnings" PATH: "/Users/runner/work/orjson/orjson/.venv/bin:/Users/runner/.cargo/bin:/usr/local/opt/curl/bin:/usr/local/bin:/usr/local/sbin:/Users/runner/bin:/Library/Frameworks/Python.framework/Versions/Current/bin:/usr/bin:/bin:/usr/sbin:/sbin" steps: - name: CPU info run: sysctl -a | grep brand - uses: actions/checkout@v6 - uses: actions/setup-python@v6 with: allow-prereleases: true python-version: "${{ matrix.python.version }}" - uses: dtolnay/rust-toolchain@master with: toolchain: "${{ env.RUST_TOOLCHAIN }}" targets: "aarch64-apple-darwin" components: "rust-src" - name: Build environment run: | cargo fetch --target aarch64-apple-darwin & export PATH=$HOME/.cargo/bin:$HOME/.local/bin:$PATH curl -LsSf https://astral.sh/uv/install.sh | sh uv venv --python python${{ matrix.python.version }} uv pip install --upgrade "maturin>=1.10,<2" -r test/requirements.txt -r integration/requirements.txt mkdir .cargo cp ci/config.toml .cargo/config.toml - name: maturin run: | export PATH=$HOME/.cargo/bin:$HOME/.local/bin:$PATH MACOSX_DEPLOYMENT_TARGET="${{ matrix.python.macosx_target }}" \ PYO3_CROSS_LIB_DIR=$(python -c "import sysconfig;print(sysconfig.get_config_var('LIBDIR'))") \ maturin build \ --release \ --strip \ --features=generic_simd,no_panic \ --interpreter python${{ matrix.python.version }} \ --target=aarch64-apple-darwin uv pip install target/wheels/orjson*.whl - run: pytest -v test env: PYTHONMALLOC: "debug" - run: source .venv/bin/activate && ./integration/run thread - run: source .venv/bin/activate && ./integration/run http - run: source .venv/bin/activate && ./integration/run init - name: Store wheels if: matrix.python.publish == true uses: actions/upload-artifact@v5 with: name: orjson_macos_aarch64_${{ matrix.python.version }} path: target/wheels overwrite: true retention-days: 1 if-no-files-found: "error" compression-level: 0 macos_universal2_aarch64: runs-on: macos-26 timeout-minutes: 10 strategy: fail-fast: false matrix: python: [ { version: '3.14', macosx_target: "10.15", publish: true }, { version: '3.13', macosx_target: "10.15", publish: true }, { version: '3.12', macosx_target: "10.15", publish: true }, { version: '3.11', macosx_target: "10.15", publish: true }, { version: '3.10', macosx_target: "10.15", publish: true }, ] env: CC: "clang" CFLAGS: "-O2 -fstrict-aliasing" LDFLAGS: "-Wl,--as-needed" CFLAGS_x86_64_apple_darwin: "-O2 -fstrict-aliasing -fno-plt -march=x86-64-v2 -mtune=generic" CFLAGS_aarch64_apple_darwin: "-O2 -fstrict-aliasing -fno-plt -mcpu=apple-m1 -mtune=generic" RUSTFLAGS: "-Z unstable-options -C panic=immediate-abort -Z mir-opt-level=4 -Z threads=3 -D warnings" PATH: "/Users/runner/work/orjson/orjson/.venv/bin:/Users/runner/.cargo/bin:/usr/local/opt/curl/bin:/usr/local/bin:/usr/local/sbin:/Users/runner/bin:/Library/Frameworks/Python.framework/Versions/Current/bin:/usr/bin:/bin:/usr/sbin:/sbin" steps: - name: CPU info run: sysctl -a | grep brand - uses: actions/checkout@v6 - uses: actions/setup-python@v6 with: allow-prereleases: true python-version: "${{ matrix.python.version }}" - uses: dtolnay/rust-toolchain@master with: toolchain: "${{ env.RUST_TOOLCHAIN }}" targets: "aarch64-apple-darwin, x86_64-apple-darwin" components: "rust-src" - name: Build environment run: | cargo fetch --target aarch64-apple-darwin & export PATH=$HOME/.cargo/bin:$HOME/.local/bin:$PATH curl -LsSf https://astral.sh/uv/install.sh | sh uv venv --python python${{ matrix.python.version }} uv pip install --upgrade "maturin>=1.10,<2" -r test/requirements.txt -r integration/requirements.txt mkdir .cargo cp ci/config.toml .cargo/config.toml - name: maturin run: | export PATH=$HOME/.cargo/bin:$HOME/.local/bin:$PATH MACOSX_DEPLOYMENT_TARGET="${{ matrix.python.macosx_target }}" \ PYO3_CROSS_LIB_DIR=$(python -c "import sysconfig;print(sysconfig.get_config_var('LIBDIR'))") \ maturin build \ --release \ --strip \ --features=generic_simd,no_panic \ --interpreter python${{ matrix.python.version }} \ --target=universal2-apple-darwin uv pip install target/wheels/orjson*.whl - run: pytest -v test env: PYTHONMALLOC: "debug" - run: source .venv/bin/activate && ./integration/run thread - run: source .venv/bin/activate && ./integration/run http - run: source .venv/bin/activate && ./integration/run init - name: Store wheels if: matrix.python.publish == true uses: actions/upload-artifact@v5 with: name: orjson_universal2_aarch64_${{ matrix.python.version }} path: target/wheels overwrite: true retention-days: 1 if-no-files-found: "error" compression-level: 0 windows_amd64: runs-on: windows-2025 timeout-minutes: 10 strategy: fail-fast: false matrix: python: [ { version: '3.14', publish: true }, { version: '3.13', publish: true }, { version: '3.12', publish: true }, { version: '3.11', publish: true }, { version: '3.10', publish: true }, ] platform: [ { arch: "x64", target: "x86_64-pc-windows-msvc", features: "avx512,no_panic" }, { arch: "x86", target: "i686-pc-windows-msvc", features: "no_panic" }, ] env: CFLAGS: "-O2" LDFLAGS: "-Wl,--as-needed" RUSTFLAGS: "-Z unstable-options -C panic=immediate-abort -Z mir-opt-level=4 -D warnings" steps: - name: CPU info shell: pwsh run: Get-WmiObject -Class Win32_Processor -ComputerName. | Select-Object -Property Name, NumberOfCores, NumberOfLogicalProcessors - uses: actions/checkout@v6 - uses: actions/setup-python@v6 with: allow-prereleases: true python-version: "${{ matrix.python.version }}" architecture: "${{ matrix.platform.arch }}" - uses: dtolnay/rust-toolchain@master with: toolchain: "${{ env.RUST_TOOLCHAIN }}" targets: "${{ matrix.platform.target }}" components: "rust-src" - name: Build environment run: | cargo fetch --target "${{ matrix.platform.target }}" & python.exe -m pip install --upgrade pip "maturin>=1.10,<2" wheel python.exe -m pip install -r test\requirements.txt mkdir .cargo cp ci\config.toml .cargo\config.toml - name: maturin run: | maturin.exe build --release --strip --features="${{ matrix.platform.features }}" --target="${{ matrix.platform.target }}" python.exe -m pip install orjson --no-index --find-links target\wheels - run: python.exe -m pytest -s -rxX -v test env: PYTHONMALLOC: "debug" - name: Store wheels if: matrix.python.publish == true uses: actions/upload-artifact@v5 with: name: orjson_windows_amd64_${{ matrix.platform.arch }}_${{ matrix.python.version }} path: target\wheels overwrite: true retention-days: 1 if-no-files-found: "error" compression-level: 0 windows_aarch64: runs-on: windows-11-arm timeout-minutes: 10 strategy: fail-fast: false matrix: python: [ { version: '3.14', publish: true }, { version: '3.13', publish: true }, { version: '3.12', publish: true }, { version: '3.11', publish: true }, ] env: CFLAGS: "-O2" LDFLAGS: "-Wl,--as-needed" RUSTFLAGS: "-Z unstable-options -C panic=immediate-abort -Z mir-opt-level=4 -D warnings" TARGET: "aarch64-pc-windows-msvc" steps: - name: CPU info shell: pwsh run: Get-WmiObject -Class Win32_Processor -ComputerName. | Select-Object -Property Name, NumberOfCores, NumberOfLogicalProcessors - uses: actions/checkout@v6 - uses: actions/setup-python@v6 with: allow-prereleases: true python-version: "${{ matrix.python.version }}" architecture: "arm64" # from maturin - shell: pwsh run: | Invoke-WebRequest -Uri "https://static.rust-lang.org/rustup/dist/$env:TARGET/rustup-init.exe" -OutFile rustup-init.exe .\rustup-init.exe --default-toolchain "$env:RUST_TOOLCHAIN-$env:TARGET" --profile minimal --component rust-src -y "$env:USERPROFILE\.cargo\bin" | Out-File -Append -Encoding ascii $env:GITHUB_PATH "CARGO_HOME=$env:USERPROFILE\.cargo" | Out-File -Append -Encoding ascii $env:GITHUB_ENV - name: Build environment run: | cargo fetch --target "$" & python.exe -m sysconfig python.exe -m pip install --upgrade pip "maturin>=1.10,<2" wheel python.exe -m pip install -r test\requirements.txt mkdir .cargo cp ci\config.toml .cargo\config.toml - name: maturin run: | maturin.exe build --release --strip --features=generic_simd,no_panic --target="$env:TARGET" python.exe -m pip install orjson --no-index --find-links target\wheels - run: python.exe -m pytest -s -rxX -v test env: PYTHONMALLOC: "debug" - name: Store wheels if: matrix.python.publish == true uses: actions/upload-artifact@v5 with: name: orjson_windows_aarch64_${{ matrix.python.version }} path: target\wheels overwrite: true retention-days: 1 if-no-files-found: "error" compression-level: 0 pypi: name: PyPI runs-on: ubuntu-24.04 timeout-minutes: 10 needs: [ macos_aarch64, macos_universal2_aarch64, manylinux, manylinux_cross, musllinux_aarch64, musllinux_amd64, sdist, windows_aarch64, windows_amd64, ] environment: name: PyPI url: https://pypi.org/p/orjson permissions: id-token: write steps: - uses: actions/checkout@v6 - uses: actions/download-artifact@v5 with: merge-multiple: true path: dist/ pattern: orjson_* - run: ls -1 dist/ - uses: actions/setup-python@v6 with: python-version: "3.14" - run: ./script/check-pypi dist - name: Publish distribution to PyPI if: "startsWith(github.ref, 'refs/tags/')" uses: pypa/gh-action-pypi-publish@release/v1 with: attestations: true packages-dir: dist skip-existing: true verbose: true ================================================ FILE: .github/workflows/lint.yaml ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2023-2025) name: lint on: push env: FORCE_COLOR: "1" PIP_DISABLE_PIP_VERSION_CHECK: "1" jobs: lint: runs-on: ubuntu-24.04 steps: - uses: actions/setup-python@v6 with: python-version: "3.14" - uses: actions/checkout@v6 - run: curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain=stable --profile=default -y - run: pip install -r requirements-lint.txt - run: cargo fmt - run: ./script/lint - run: git diff --exit-code ================================================ FILE: .github/workflows/unusual.yaml ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2023-2026) name: unusual on: push env: FORCE_COLOR: "1" PIP_DISABLE_PIP_VERSION_CHECK: "1" jobs: unusual: runs-on: ubuntu-24.04 strategy: fail-fast: false matrix: cfg: [ { rust: "1.89", python: "3.15", version_check: "1" }, { rust: "1.89", python: "3.14", version_check: "0" }, { rust: "1.89", python: "3.10", version_check: "0" }, ] steps: - run: curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain ${{ matrix.cfg.rust }} --profile minimal -y - uses: actions/setup-python@v5 with: python-version: '${{ matrix.cfg.python }}' allow-prereleases: true - run: python -m pip install --user --upgrade pip "maturin>=1.10,<2" wheel - uses: actions/checkout@v6 - name: build run: | PATH="$HOME/.cargo/bin:$PATH" UNSAFE_PYO3_SKIP_VERSION_CHECK="${{ matrix.cfg.version_check }}" \ maturin build \ --profile=dev \ --interpreter python${{ matrix.cfg.python }} \ --target=x86_64-unknown-linux-gnu - run: python -m pip install --user target/wheels/orjson*.whl - run: python -m pip install --user -r test/requirements.txt -r integration/requirements.txt - run: pytest -s -rxX -v test timeout-minutes: 4 env: PYTHONMALLOC: "debug" - run: ./integration/run thread timeout-minutes: 2 - run: ./integration/run http timeout-minutes: 2 - run: ./integration/run init timeout-minutes: 2 ================================================ FILE: .gitignore ================================================ *.patch /.benchmarks /.coverage /.mypy_cache /.pytest_cache /.venv* /build /include/cargo /perf.* /target /vendor __pycache__ ================================================ FILE: CHANGELOG.md ================================================ # Changelog ## 3.11.7 - 2026-02-02 ### Changed - Use a faster library to serialize `float`. Users with byte-exact regression tests should note positive exponents are now written using a `+`, e.g., `1.2e+30` instead of `1.2e30`. Both formats are spec-compliant. - ABI compatibility with CPython 3.15 alpha 5 free-threading. ## 3.11.6 - 2026-01-29 ### Changed - orjson now includes code licensed under the Mozilla Public License 2.0 (MPL-2.0). - Drop support for Python 3.9. - ABI compatibility with CPython 3.15 alpha 5. - Build now depends on Rust 1.89 or later instead of 1.85. ### Fixed - Fix sporadic crash serializing deeply nested `list` of `dict`. ## 3.11.5 - 2025-12-06 ### Changed - Show simple error message instead of traceback when attempting to build on unsupported Python versions. ## 3.11.4 - 2025-10-24 ### Changed - ABI compatibility with CPython 3.15 alpha 1. - Publish PyPI wheels for 3.14 and manylinux i686, manylinux arm7, manylinux ppc64le, manylinux s390x. - Build now requires a C compiler. ## 3.11.3 - 2025-08-26 ### Fixed - Fix PyPI project metadata when using maturin 1.9.2 or later. ## 3.11.2 - 2025-08-12 ### Fixed - Fix build using Rust 1.89 on amd64. ### Changed - Build now depends on Rust 1.85 or later instead of 1.82. ## 3.11.1 - 2025-07-25 ### Changed - Publish PyPI wheels for CPython 3.14. ### Fixed - Fix `str` on big-endian architectures. This was introduced in 3.11.0. ## 3.11.0 - 2025-07-15 ### Changed - Use a deserialization buffer allocated per request instead of a shared buffer allocated on import. - ABI compatibility with CPython 3.14 beta 4. ## 3.10.18 - 2025-04-29 ### Fixed - Fix incorrect escaping of the vertical tabulation character. This was introduced in 3.10.17. ## 3.10.17 - 2025-04-29 ### Changed - Publish PyPI Windows aarch64/arm64 wheels. - ABI compatibility with CPython 3.14 alpha 7. - Fix incompatibility running on Python 3.13 using WASM. ## 3.10.16 - 2025-03-24 ### Changed - Improve performance of serialization on amd64 machines with AVX-512. - ABI compatibility with CPython 3.14 alpha 6. - Drop support for Python 3.8. - Publish additional PyPI wheels for macOS that target only aarch64, macOS 15, and recent Python. ## 3.10.15 - 2025-01-08 ### Changed - Publish PyPI manylinux aarch64 wheels built and tested on aarch64. - Publish PyPI musllinux aarch64 and arm7l wheels built and tested on aarch64. - Publish PyPI manylinux Python 3.13 wheels for i686, arm7l, ppc64le, and s390x. ## 3.10.14 - 2024-12-29 ### Changed - Specify build system dependency on `maturin>=1,<2` again. - Allocate memory using `PyMem_Malloc()` and similar APIs for integration with pymalloc, mimalloc, and tracemalloc. - Source distribution does not ship compressed test documents and relevant tests skip if fixtures are not present. - Build now depends on Rust 1.82 or later instead of 1.72. ## 3.10.13 - 2024-12-29 ### Changed - Fix compatibility with maturin introducing a breaking change in 1.8.0 and specify a fixed version of maturin. Projects relying on any previous version being buildable from source by end users (via PEP 517) must upgrade to at least this version. ## 3.10.12 - 2024-11-23 ### Changed - Publish PyPI manylinux i686 wheels. - Publish PyPI musllinux i686 and arm7l wheels. - Publish PyPI macOS wheels for Python 3.10 or later built on macOS 15. - Publish PyPI Windows wheels using trusted publishing. ## 3.10.11 - 2024-11-01 ### Changed - Improve performance of UUIDs. - Publish PyPI wheels with trusted publishing and PEP 740 attestations. - Include text of licenses for vendored dependencies. ## 3.10.10 - 2024-10-22 ### Fixed - Fix `int` serialization on `s390x`. This was introduced in 3.10.8. ### Changed - Publish aarch64 manylinux_2_17 wheel for 3.13 to PyPI. ## 3.10.9 - 2024-10-19 ### Fixed - Fix `int` serialization on 32-bit Python 3.8, 3.9, 3.10. This was introduced in 3.10.8. ## 3.10.8 - 2024-10-19 ### Changed - `int` serialization no longer chains `OverflowError` to the the `__cause__` attribute of `orjson.JSONEncodeError` when range exceeded. - Compatibility with CPython 3.14 alpha 1. - Improve performance. ## 3.10.7 - 2024-08-08 ### Changed - Improve performance of stable Rust amd64 builds. ## 3.10.6 - 2024-07-02 ### Changed - Improve performance. ## 3.10.5 - 2024-06-13 ### Changed - Improve performance. ## 3.10.4 - 2024-06-10 ### Changed - Improve performance. ## 3.10.3 - 2024-05-03 ### Changed - `manylinux` amd64 builds include runtime-detected AVX-512 `str` implementation. - Tests now compatible with numpy v2. ## 3.10.2 - 2024-05-01 ### Fixed - Fix crash serializing `str` introduced in 3.10.1. ### Changed - Improve performance. - Drop support for arm7. ## 3.10.1 - 2024-04-15 ### Fixed - Serializing `numpy.ndarray` with non-native endianness raises `orjson.JSONEncodeError`. ### Changed - Improve performance of serializing. ## 3.10.0 - 2024-03-27 ### Changed - Support serializing `numpy.float16` (`numpy.half`). - sdist uses metadata 2.3 instead of 2.1. - Improve Windows PyPI builds. ## 3.9.15 - 2024-02-23 ### Fixed - Implement recursion limit of 1024 on `orjson.loads()`. - Use byte-exact read on `str` formatting SIMD path to avoid crash. ## 3.9.14 - 2024-02-14 ### Fixed - Fix crash serializing `str` introduced in 3.9.11. ### Changed - Build now depends on Rust 1.72 or later. ## 3.9.13 - 2024-02-03 ### Fixed - Serialization `str` escape uses only 128-bit SIMD. - Fix compatibility with CPython 3.13 alpha 3. ### Changed - Publish `musllinux_1_2` instead of `musllinux_1_1` wheels. - Serialization uses small integer optimization in CPython 3.12 or later. ## 3.9.12 - 2024-01-18 ### Changed - Update benchmarks in README. ### Fixed - Minimal `musllinux_1_1` build due to sporadic CI failure. ## 3.9.11 - 2024-01-18 ### Changed - Improve performance of serializing. `str` is significantly faster. Documents using `dict`, `list`, and `tuple` are somewhat faster. ## 3.9.10 - 2023-10-26 ### Fixed - Fix debug assert failure on 3.12 `--profile=dev` build. ## 3.9.9 - 2023-10-12 ### Changed - `orjson` module metadata explicitly marks subinterpreters as not supported. ## 3.9.8 - 2023-10-10 ### Changed - Improve performance. - Drop support for Python 3.7. ## 3.9.7 - 2023-09-08 ### Fixed - Fix crash in `orjson.loads()` due to non-reentrant handling of persistent buffer. This was introduced in 3.9.3. - Handle some FFI removals in CPython 3.13. ## 3.9.6 - 2023-09-07 ### Fixed - Fix numpy reference leak on unsupported array dtype. - Fix numpy.datetime64 reference handling. ### Changed - Minor performance improvements. ## 3.9.5 - 2023-08-16 ### Fixed - Remove futex from module import and initialization path. ## 3.9.4 - 2023-08-07 ### Fixed - Fix hash builder using default values. - Fix non-release builds of orjson copying large deserialization buffer from stack to heap. This was introduced in 3.9.3. ## 3.9.3 - 2023-08-06 ### Fixed - Fix compatibility with CPython 3.12. ### Changed - Support i686/x86 32-bit Python installs on Windows. ## 3.9.2 - 2023-07-07 ### Fixed - Fix the `__cause__` exception on `orjson.JSONEncodeError` possibly being denormalized, i.e., of type `str` instead of `Exception`. ## 3.9.1 - 2023-06-09 ### Fixed - Fix memory leak on chained tracebacks of exceptions raised in `default`. This was introduced in 3.8.12. ## 3.9.0 - 2023-06-01 ### Added - `orjson.Fragment` includes already-serialized JSON in a document. ## 3.8.14 - 2023-05-25 ### Changed - PyPI `manylinux` wheels are compiled for `x86-64` instead of `x86-64-v2`. ## 3.8.13 - 2023-05-23 ### Changed - Source distribution contains all source code required for an offline build. - PyPI macOS wheels use a `MACOSX_DEPLOYMENT_TARGET` of 10.15 instead of 11. - Build uses maturin v1. ## 3.8.12 - 2023-05-07 ### Changed - Exceptions raised in `default` are now chained as the `__cause__` attribute on `orjson.JSONEncodeError`. ## 3.8.11 - 2023-04-27 ### Changed - `orjson.loads()` on an empty document has a specific error message. - PyPI `manylinux_2_28_x86_64` wheels are compiled for `x86-64-v2`. - PyPI macOS wheels are only `universal2` and compiled for `x86-64-v2` and `apple-m1`. ## 3.8.10 - 2023-04-09 ### Fixed - Fix compatibility with CPython 3.12.0a7. - Fix compatibility with big-endian architectures. - Fix crash in serialization. ### Changed - Publish musllinux 3.11 wheels. - Publish s390x wheels. ## 3.8.9 - 2023-03-28 ### Fixed - Fix parallel initialization of orjson. ## 3.8.8 - 2023-03-20 ### Changed - Publish ppc64le wheels. ## 3.8.7 - 2023-02-28 ### Fixed - Use serialization backend introduced in 3.8.4 only on well-tested platforms such as glibc, macOS by default. ## 3.8.6 - 2023-02-09 ### Fixed - Fix crash serializing when using musl libc. ### Changed - Make `python-dateutil` optional in tests. - Handle failure to load system timezones in tests. ## 3.8.5 - 2023-01-10 ### Fixed - Fix `orjson.dumps()` invalid output on Windows. ## 3.8.4 - 2023-01-04 ### Changed - Improve performance. ## 3.8.3 - 2022-12-02 ### Fixed - `orjson.dumps()` accepts `option=None` per `Optional[int]` type. ## 3.8.2 - 2022-11-20 ### Fixed - Fix tests on 32-bit for `numpy.intp` and `numpy.uintp`. ### Changed - Build now depends on rustc 1.60 or later. - Support building with maturin 0.13 or 0.14. ## 3.8.1 - 2022-10-25 ### Changed - Build maintenance for Python 3.11. ## 3.8.0 - 2022-08-27 ### Changed - Support serializing `numpy.int16` and `numpy.uint16`. ## 3.7.12 - 2022-08-14 ### Fixed - Fix datetime regression tests for tzinfo 2022b. ### Changed - Improve performance. ## 3.7.11 - 2022-07-31 ### Fixed - Revert `dict` iterator implementation introduced in 3.7.9. ## 3.7.10 - 2022-07-30 ### Fixed - Fix serializing `dict` with deleted final item. This was introduced in 3.7.9. ## 3.7.9 - 2022-07-29 ### Changed - Improve performance of serializing. - Improve performance of serializing pretty-printed (`orjson.OPT_INDENT_2`) to be much nearer to compact. - Improve performance of deserializing `str` input. - orjson now requires Rust 1.57 instead of 1.54 to build. ## 3.7.8 - 2022-07-19 ### Changed - Build makes best effort instead of requiring "--features". - Build using maturin 0.13. ## 3.7.7 - 2022-07-06 ### Changed - Support Python 3.11. ## 3.7.6 - 2022-07-03 ### Changed - Handle unicode changes in CPython 3.12. - Build PyPI macOS wheels on 10.15 instead of 12 for compatibility. ## 3.7.5 - 2022-06-28 ### Fixed - Fix issue serializing dicts that had keys popped and replaced. This was introduced in 3.7.4. ## 3.7.4 - 2022-06-28 ### Changed - Improve performance. ### Fixed - Fix deallocation of `orjson.JSONDecodeError`. ## 3.7.3 - 2022-06-23 ## Changed - Improve build. - Publish aarch64 musllinux wheels. ## 3.7.2 - 2022-06-07 ## Changed - Improve deserialization performance. ## 3.7.1 - 2022-06-03 ### Fixed - Type stubs for `orjson.JSONDecodeError` now inherit from `json.JSONDecodeError` instead of `ValueError` - Null-terminate the internal buffer of `orjson.dumps()` output. ## 3.7.0 - 2022-06-03 ### Changed - Improve deserialization performance significantly through the use of a new backend. PyPI wheels for manylinux_2_28 and macOS have it enabled. Packagers are advised to see the README. ## 3.6.9 - 2022-06-01 ### Changed - Improve serialization and deserialization performance. ## 3.6.8 - 2022-04-15 ### Fixed - Fix serialization of `numpy.datetime64("NaT")` to raise on an unsupported type. ## 3.6.7 - 2022-02-14 ### Changed - Improve performance of deserializing almost-empty documents. - Publish arm7l `manylinux_2_17` wheels to PyPI. - Publish amd4 `musllinux_1_1` wheels to PyPI. ### Fixed - Fix build requiring `python` on `PATH`. ## 3.6.6 - 2022-01-21 ### Changed - Improve performance of serializing `datetime.datetime` using `tzinfo` that are `zoneinfo.ZoneInfo`. ### Fixed - Fix invalid indexing in line and column number reporting in `JSONDecodeError`. - Fix `orjson.OPT_STRICT_INTEGER` not raising an error on values exceeding a 64-bit integer maximum. ## 3.6.5 - 2021-12-05 ### Fixed - Fix build on macOS aarch64 CPython 3.10. - Fix build issue on 32-bit. ## 3.6.4 - 2021-10-01 ### Fixed - Fix serialization of `dataclass` inheriting from `abc.ABC` and using `__slots__`. - Decrement refcount for numpy `PyArrayInterface`. - Fix build on recent versions of Rust nightly. ## 3.6.3 - 2021-08-20 ### Fixed - Fix build on aarch64 using the Rust stable channel. ## 3.6.2 - 2021-08-17 ### Changed - `orjson` now compiles on Rust stable 1.54.0 or above. Use of some SIMD usage is now disabled by default and packagers are advised to add `--cargo-extra-args="--features=unstable-simd"` to the `maturin build` command if they continue to use nightly. - `orjson` built with `--features=unstable-simd` adds UTF-8 validation implementations that use AVX2 or SSE4.2. - Drop support for Python 3.6. ## 3.6.1 - 2021-08-04 ### Changed - `orjson` now includes a `pyi` type stubs file. - Publish manylinux_2_24 wheels instead of manylinux2014. ### Fixed - Fix compilation on latest Rust nightly. ## 3.6.0 - 2021-07-08 ### Added - `orjson.dumps()` serializes `numpy.datetime64` instances as RFC 3339 strings. ## 3.5.4 - 2021-06-30 ### Fixed - Fix memory leak serializing `datetime.datetime` with `tzinfo`. - Fix wrong error message when serializing an unsupported numpy type without default specified. ### Changed - Publish python3.10 and python3.9 manylinux_2_24 wheels. ## 3.5.3 - 2021-06-01 ### Fixed - `orjson.JSONDecodeError` now has `pos`, `lineno`, and `colno`. - Fix build on recent versions of Rust nightly. ## 3.5.2 - 2021-04-15 ### Changed - Improve serialization and deserialization performance. - `orjson.dumps()` serializes individual `numpy.bool_` objects. ## 3.5.1 - 2021-03-06 ### Changed - Publish `universal2` wheels for macOS supporting Apple Silicon (aarch64). ## 3.5.0 - 2021-02-24 ### Added - `orjson.loads()` supports reading from `memoryview` objects. ### Fixed - `datetime.datetime` and `datetime.date` zero pad years less than 1000 to four digits. - sdist pins maturin 0.9.0 to avoid breaks in later 0.9.x. ### Changed - `orjson.dumps()` when given a non-C contiguous `numpy.ndarray` has an error message suggesting to use `default`. ## 3.4.8 - 2021-02-04 ### Fixed - aarch64 manylinux2014 wheels are now compatible with glibc 2.17. ### Changed - Fix build warnings on ppcle64. ## 3.4.7 - 2021-01-19 ### Changed - Use vectorcall APIs for method calls on python3.9 and above. - Publish python3.10 wheels for Linux on amd64 and aarch64. ## 3.4.6 - 2020-12-07 ### Fixed - Fix compatibility with debug builds of CPython. ## 3.4.5 - 2020-12-02 ### Fixed - Fix deserializing long strings on processors without AVX2. ## 3.4.4 - 2020-11-25 ### Changed - `orjson.dumps()` serializes integers up to a 64-bit unsigned integer's maximum. It was previously the maximum of a 64-bit signed integer. ## 3.4.3 - 2020-10-30 ### Fixed - Fix regression in parsing similar `dict` keys. ## 3.4.2 - 2020-10-29 ### Changed - Improve deserialization performance. - Publish Windows python3.9 wheel. - Disable unsupported SIMD features on non-x86, non-ARM targets ## 3.4.1 - 2020-10-20 ### Fixed - Fix `orjson.dumps.__module__` and `orjson.loads.__module__` not being the `str` "orjson". ### Changed - Publish macos python3.9 wheel. - More packaging documentation. ## 3.4.0 - 2020-09-25 ### Added - Serialize `numpy.uint8` and `numpy.int8` instances. ### Fixed - Fix serializing `numpy.empty()` instances. ### Changed - No longer publish `manylinux1` wheels due to tooling dropping support. ## 3.3.1 - 2020-08-17 ### Fixed - Fix failure to deserialize some latin1 strings on some platforms. This was introduced in 3.2.0. - Fix annotation of optional parameters on `orjson.dumps()` for `help()`. ### Changed - Publish `manylinux2014` wheels for amd64 in addition to `manylinux1`. ## 3.3.0 - 2020-07-24 ### Added - `orjson.dumps()` now serializes individual numpy floats and integers, e.g., `numpy.float64(1.0)`. - `orjson.OPT_PASSTHROUGH_DATACLASS` causes `orjson.dumps()` to pass `dataclasses.dataclass` instances to `default`. ## 3.2.2 - 2020-07-13 ### Fixed - Fix serializing `dataclasses.dataclass` that have no attributes. ### Changed - Improve deserialization performance of `str`. ## 3.2.1 - 2020-07-03 ### Fixed - Fix `orjson.dumps(..., **{})` raising `TypeError` on python3.6. ## 3.2.0 - 2020-06-30 ### Added - `orjson.OPT_APPEND_NEWLINE` appends a newline to output. ### Changed - Improve deserialization performance of `str`. ## 3.1.2 - 2020-06-23 ### Fixed - Fix serializing zero-dimension `numpy.ndarray`. ## 3.1.1 - 2020-06-20 ### Fixed - Fix repeated serialization of `str` that are ASCII-only and have a legacy (non-compact) layout. ## 3.1.0 - 2020-06-08 ### Added - `orjson.OPT_PASSTHROUGH_SUBCLASS` causes `orjson.dumps()` to pass subclasses of builtin types to `default` so the caller can customize the output. - `orjson.OPT_PASSTHROUGH_DATETIME` causes `orjson.dumps()` to pass `datetime` objects to `default` so the caller can customize the output. ## 3.0.2 - 2020-05-27 ### Changed - `orjson.dumps()` does not serialize `dataclasses.dataclass` attributes that begin with a leading underscore, e.g., `_attr`. This is because of the Python idiom that a leading underscores marks an attribute as "private." - `orjson.dumps()` does not serialize `dataclasses.dataclass` attributes that are `InitVar` or `ClassVar` whether using `__slots__` or not. ## 3.0.1 - 2020-05-19 ### Fixed - `orjson.dumps()` raises an exception if the object to be serialized is not given as a positional argument. `orjson.dumps({})` is intended and ok while `orjson.dumps(obj={})` is an error. This makes it consistent with the documentation, `help()` annotation, and type annotation. - Fix orphan reference in exception creation that leaks memory until the garbage collector runs. ### Changed - Improve serialization performance marginally by using the fastcall/vectorcall calling convention on python3.7 and above. - Reduce build time. ## 3.0.0 - 2020-05-01 ### Added - `orjson.dumps()` serializes subclasses of `str`, `int`, `list`, and `dict`. ### Changed - `orjson.dumps()` serializes `dataclasses.dataclass` and `uuid.UUID` instances by default. The options `OPT_SERIALIZE_DATACLASS` and `OPT_SERIALIZE_UUID` can still be specified but have no effect. ## 2.6.8 - 2020-04-30 ### Changed - The source distribution vendors a forked dependency. ## 2.6.7 - 2020-04-30 ### Fixed - Fix integer overflows in debug builds. ### Changed - The source distribution sets the recommended RUSTFLAGS in `.cargo/config`. ## 2.6.6 - 2020-04-24 ### Fixed - Import `numpy` only on first use of `OPT_SERIALIZE_NUMPY` to reduce interpreter start time when not used. - Reduce build time by half. ## 2.6.5 - 2020-04-08 ### Fixed - Fix deserialization raising `JSONDecodeError` on some valid negative floats with large exponents. ## 2.6.4 - 2020-04-08 ### Changed - Improve deserialization performance of floats by about 40%. ## 2.6.3 - 2020-04-01 ### Changed - Serialize `enum.Enum` objects. - Minor performance improvements. ## 2.6.2 - 2020-03-27 ### Changed - Publish python3.9 `manylinux2014` wheel instead of `manylinux1` for `x86_64`. - Publish python3.9 `manylinux2014` wheel for `aarch64`. ### Fixed - Fix compilation failure on 32-bit. ## 2.6.1 - 2020-03-19 ### Changed - Serialization is 10-20% faster and uses about 50% less memory by writing directly to the returned `bytes` object. ## 2.6.0 - 2020-03-10 ### Added - `orjson.dumps()` pretty prints with an indentation of two spaces if `option=orjson.OPT_INDENT_2` is specified. ## 2.5.2 - 2020-03-07 ### Changed - Publish `manylinux2014` wheels for `aarch64`. - numpy support now includes `numpy.uint32` and `numpy.uint64`. ## 2.5.1 - 2020-02-24 ### Changed - `manylinux1` wheels for 3.6, 3.7, and 3.8 are now compliant with the spec by not depending on glibc 2.18. ## 2.5.0 - 2020-02-19 ### Added - `orjson.dumps()` serializes `dict` keys of type other than `str` if `option=orjson.OPT_NON_STR_KEYS` is specified. ## 2.4.0 - 2020-02-14 ### Added - `orjson.dumps()` serializes `numpy.ndarray` instances if `option=orjson.OPT_SERIALIZE_NUMPY` is specified. ### Fixed - Fix `dataclasses.dataclass` attributes that are `dict` to be effected by `orjson.OPT_SORT_KEYS`. ## 2.3.0 - 2020-02-12 ### Added - `orjson.dumps()` serializes `dict` instances sorted by keys, equivalent to `sort_keys` in other implementations, if `option=orjson.OPT_SORT_KEYS` is specified. ### Changed - `dataclasses.dataclass` instances without `__slots__` now serialize faster. ### Fixed - Fix documentation on `default`, in particular documenting the need to raise an exception if the type cannot be handled. ## 2.2.2 - 2020-02-10 ### Changed - Performance improvements to serializing a list containing elements of the same type. ## 2.2.1 - 2020-02-04 ### Fixed - `orjson.loads()` rejects floats that do not have a digit following the decimal, e.g., `-2.`, `2.e-3`. ### Changed - Build Linux, macOS, and Windows wheels on more recent distributions. ## 2.2.0 - 2020-01-22 ### Added - `orjson.dumps()` serializes `uuid.UUID` instances if `option=orjson.OPT_SERIALIZE_UUID` is specified. ### Changed - Minor performance improvements. - Publish Python 3.9 wheel for Linux. ## 2.1.4 - 2020-01-08 ### Fixed - Specify a text signature for `orjson.loads()`. ### Changed - Improve documentation. ## 2.1.3 - 2019-11-12 ### Changed - Publish Python 3.8 wheels for macOS and Windows. ## 2.1.2 - 2019-11-07 ### Changed - The recursion limit of `default` on `orjson.dumps()` has been increased from 5 to 254. ## 2.1.1 - 2019-10-29 ### Changed - Publish `manylinux1` wheels instead of `manylinux2010`. ## 2.1.0 - 2019-10-24 ### Added - `orjson.dumps()` serializes `dataclasses.dataclass` instances if `option=orjson.OPT_SERIALIZE_DATACLASS` is specified. - `orjson.dumps()` accepts `orjson.OPT_UTC_Z` to serialize UTC as "Z" instead of "+00:00". - `orjson.dumps()` accepts `orjson.OPT_OMIT_MICROSECONDS` to not serialize the `microseconds` attribute of `datetime.datetime` and `datetime.time` instances. - `orjson.loads()` accepts `bytearray`. ### Changed - Drop support for Python 3.5. - Publish `manylinux2010` wheels instead of `manylinux1`. ## 2.0.11 - 2019-10-01 ### Changed - Publish Python 3.8 wheel for Linux. ## 2.0.10 - 2019-09-25 ### Changed - Performance improvements and lower memory usage in deserialization by creating only one `str` object for repeated map keys. ## 2.0.9 - 2019-09-22 ### Changed - Minor performance improvements. ### Fixed - Fix inaccurate zero padding in serialization of microseconds on `datetime.time` objects. ## 2.0.8 - 2019-09-18 ### Fixed - Fix inaccurate zero padding in serialization of microseconds on `datetime.datetime` objects. ## 2.0.7 - 2019-08-29 ### Changed - Publish PEP 517 source distribution. ### Fixed - `orjson.dumps()` raises `JSONEncodeError` on circular references. ## 2.0.6 - 2019-05-11 ### Changed - Performance improvements. ## 2.0.5 - 2019-04-19 ### Fixed - Fix inaccuracy in deserializing some `float` values, e.g., 31.245270191439438 was parsed to 31.24527019143944. Serialization was unaffected. ## 2.0.4 - 2019-04-02 ### Changed - `orjson.dumps()` now serializes `datetime.datetime` objects without a `tzinfo` rather than raising `JSONEncodeError`. ## 2.0.3 - 2019-03-23 ### Changed - `orjson.loads()` uses SSE2 to validate `bytes` input. ## 2.0.2 - 2019-03-12 ### Changed - Support Python 3.5. ## 2.0.1 - 2019-02-05 ### Changed - Publish Windows wheel. ## 2.0.0 - 2019-01-28 ### Added - `orjson.dumps()` accepts a `default` callable to serialize arbitrary types. - `orjson.dumps()` accepts `datetime.datetime`, `datetime.date`, and `datetime.time`. Each is serialized to an RFC 3339 string. - `orjson.dumps(..., option=orjson.OPT_NAIVE_UTC)` allows serializing `datetime.datetime` objects that do not have a timezone set as UTC. - `orjson.dumps(..., option=orjson.OPT_STRICT_INTEGER)` available to raise an error on integer values outside the 53-bit range of all JSON implementations. ### Changed - `orjson.dumps()` no longer accepts `bytes`. ## 1.3.1 - 2019-01-03 ### Fixed - Handle invalid UTF-8 in str. ## 1.3.0 - 2019-01-02 ### Changed - Performance improvements of 15-25% on serialization, 10% on deserialization. ## 1.2.1 - 2018-12-31 ### Fixed - Fix memory leak in deserializing dict. ## 1.2.0 - 2018-12-16 ### Changed - Performance improvements. ## 1.1.0 - 2018-12-04 ### Changed - Performance improvements. ### Fixed - Dict key can only be str. ## 1.0.1 - 2018-11-26 ### Fixed - pyo3 bugfix update. ## 1.0.0 - 2018-11-23 ### Added - `orjson.dumps()` function. - `orjson.loads()` function. ================================================ FILE: Cargo.toml ================================================ [package] name = "orjson" version = "3.11.7" authors = ["ijl "] description = "Fast, correct Python JSON library supporting dataclasses, datetimes, and numpy" repository = "https://github.com/ijl/orjson" edition = "2024" resolver = "3" rust-version = "1.89" license = "MPL-2.0 AND (Apache-2.0 OR MIT)" keywords = ["fast", "json", "dataclass", "dataclasses", "datetime", "rfc", "8259", "3339"] include = [ "Cargo.toml", "CHANGELOG.md", "include/yyjson", "LICENSE-APACHE", "LICENSE-MIT", "LICENSE-MPL-2.0", "pyproject.toml", "README.md", "src", "test/*.py", "test/requirements.txt", ] [lib] name = "orjson" crate-type = ["cdylib"] [features] default = [] # Avoid bundling libgcc on musl. unwind = ["unwinding"] # Features detected by build.rs. Do not specify. avx512 = [] cold_path = [] generic_simd = [] inline_int = [] inline_str = [] no_panic = ["zmij/no-panic"] optimize = [] [dependencies] associative-cache = { version = "2", default-features = false } bytes = { version = "1", default-features = false } bytecount = { version = "^0.6.7", default-features = false, features = ["runtime-dispatch-simd"] } encoding_rs = { version = "0.8", default-features = false } half = { version = "2", default-features = false } itoa = { version = "1", default-features = false } itoap = { version = "1", default-features = false, features = ["std", "simd"] } jiff = { version = "^0.2", default-features = false } once_cell = { version = "1", default-features = false, features = ["alloc", "race"] } pyo3-ffi = { version = "0.28", default-features = false } serde = { version = "1", default-features = false } serde_json = { version = "1", default-features = false, features = ["std"] } simdutf8 = { version = "0.1", default-features = false, features = ["std", "public_imp", "aarch64_neon"] } smallvec = { version = "^1.11", default-features = false, features = ["union", "write"] } unwinding = { version = "=0.2.8", default-features = false, features = ["unwinder"], optional = true } uuid = { version = "1", default-features = false } xxhash-rust = { version = "^0.8", default-features = false, features = ["xxh3"] } zmij = { version = "1", default-features = false } [build-dependencies] cc = { version = "1" } pyo3-build-config = { version = "0.28" } version_check = { version = "0.9" } [profile.dev] codegen-units = 1 debug = 2 debug-assertions = true incremental = false lto = "off" opt-level = 3 overflow-checks = true [profile.release] codegen-units = 1 debug = false incremental = false lto = "fat" opt-level = 3 panic = "abort" [profile.release.build-override] opt-level = 0 ================================================ FILE: LICENSE-APACHE ================================================ Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ================================================ FILE: LICENSE-MIT ================================================ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: LICENSE-MPL-2.0 ================================================ Mozilla Public License Version 2.0 ================================== 1. Definitions -------------- 1.1. "Contributor" means each individual or legal entity that creates, contributes to the creation of, or owns Covered Software. 1.2. "Contributor Version" means the combination of the Contributions of others (if any) used by a Contributor and that particular Contributor's Contribution. 1.3. "Contribution" means Covered Software of a particular Contributor. 1.4. "Covered Software" means Source Code Form to which the initial Contributor has attached the notice in Exhibit A, the Executable Form of such Source Code Form, and Modifications of such Source Code Form, in each case including portions thereof. 1.5. "Incompatible With Secondary Licenses" means (a) that the initial Contributor has attached the notice described in Exhibit B to the Covered Software; or (b) that the Covered Software was made available under the terms of version 1.1 or earlier of the License, but not also under the terms of a Secondary License. 1.6. "Executable Form" means any form of the work other than Source Code Form. 1.7. "Larger Work" means a work that combines Covered Software with other material, in a separate file or files, that is not Covered Software. 1.8. "License" means this document. 1.9. "Licensable" means having the right to grant, to the maximum extent possible, whether at the time of the initial grant or subsequently, any and all of the rights conveyed by this License. 1.10. "Modifications" means any of the following: (a) any file in Source Code Form that results from an addition to, deletion from, or modification of the contents of Covered Software; or (b) any new file in Source Code Form that contains any Covered Software. 1.11. "Patent Claims" of a Contributor means any patent claim(s), including without limitation, method, process, and apparatus claims, in any patent Licensable by such Contributor that would be infringed, but for the grant of the License, by the making, using, selling, offering for sale, having made, import, or transfer of either its Contributions or its Contributor Version. 1.12. "Secondary License" means either the GNU General Public License, Version 2.0, the GNU Lesser General Public License, Version 2.1, the GNU Affero General Public License, Version 3.0, or any later versions of those licenses. 1.13. "Source Code Form" means the form of the work preferred for making modifications. 1.14. "You" (or "Your") means an individual or a legal entity exercising rights under this License. For legal entities, "You" includes any entity that controls, is controlled by, or is under common control with You. For purposes of this definition, "control" means (a) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (b) ownership of more than fifty percent (50%) of the outstanding shares or beneficial ownership of such entity. 2. License Grants and Conditions -------------------------------- 2.1. Grants Each Contributor hereby grants You a world-wide, royalty-free, non-exclusive license: (a) under intellectual property rights (other than patent or trademark) Licensable by such Contributor to use, reproduce, make available, modify, display, perform, distribute, and otherwise exploit its Contributions, either on an unmodified basis, with Modifications, or as part of a Larger Work; and (b) under Patent Claims of such Contributor to make, use, sell, offer for sale, have made, import, and otherwise transfer either its Contributions or its Contributor Version. 2.2. Effective Date The licenses granted in Section 2.1 with respect to any Contribution become effective for each Contribution on the date the Contributor first distributes such Contribution. 2.3. Limitations on Grant Scope The licenses granted in this Section 2 are the only rights granted under this License. No additional rights or licenses will be implied from the distribution or licensing of Covered Software under this License. Notwithstanding Section 2.1(b) above, no patent license is granted by a Contributor: (a) for any code that a Contributor has removed from Covered Software; or (b) for infringements caused by: (i) Your and any other third party's modifications of Covered Software, or (ii) the combination of its Contributions with other software (except as part of its Contributor Version); or (c) under Patent Claims infringed by Covered Software in the absence of its Contributions. This License does not grant any rights in the trademarks, service marks, or logos of any Contributor (except as may be necessary to comply with the notice requirements in Section 3.4). 2.4. Subsequent Licenses No Contributor makes additional grants as a result of Your choice to distribute the Covered Software under a subsequent version of this License (see Section 10.2) or under the terms of a Secondary License (if permitted under the terms of Section 3.3). 2.5. Representation Each Contributor represents that the Contributor believes its Contributions are its original creation(s) or it has sufficient rights to grant the rights to its Contributions conveyed by this License. 2.6. Fair Use This License is not intended to limit any rights You have under applicable copyright doctrines of fair use, fair dealing, or other equivalents. 2.7. Conditions Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in Section 2.1. 3. Responsibilities ------------------- 3.1. Distribution of Source Form All distribution of Covered Software in Source Code Form, including any Modifications that You create or to which You contribute, must be under the terms of this License. You must inform recipients that the Source Code Form of the Covered Software is governed by the terms of this License, and how they can obtain a copy of this License. You may not attempt to alter or restrict the recipients' rights in the Source Code Form. 3.2. Distribution of Executable Form If You distribute Covered Software in Executable Form then: (a) such Covered Software must also be made available in Source Code Form, as described in Section 3.1, and You must inform recipients of the Executable Form how they can obtain a copy of such Source Code Form by reasonable means in a timely manner, at a charge no more than the cost of distribution to the recipient; and (b) You may distribute such Executable Form under the terms of this License, or sublicense it under different terms, provided that the license for the Executable Form does not attempt to limit or alter the recipients' rights in the Source Code Form under this License. 3.3. Distribution of a Larger Work You may create and distribute a Larger Work under terms of Your choice, provided that You also comply with the requirements of this License for the Covered Software. If the Larger Work is a combination of Covered Software with a work governed by one or more Secondary Licenses, and the Covered Software is not Incompatible With Secondary Licenses, this License permits You to additionally distribute such Covered Software under the terms of such Secondary License(s), so that the recipient of the Larger Work may, at their option, further distribute the Covered Software under the terms of either this License or such Secondary License(s). 3.4. Notices You may not remove or alter the substance of any license notices (including copyright notices, patent notices, disclaimers of warranty, or limitations of liability) contained within the Source Code Form of the Covered Software, except that You may alter any license notices to the extent required to remedy known factual inaccuracies. 3.5. Application of Additional Terms You may choose to offer, and to charge a fee for, warranty, support, indemnity or liability obligations to one or more recipients of Covered Software. However, You may do so only on Your own behalf, and not on behalf of any Contributor. You must make it absolutely clear that any such warranty, support, indemnity, or liability obligation is offered by You alone, and You hereby agree to indemnify every Contributor for any liability incurred by such Contributor as a result of warranty, support, indemnity or liability terms You offer. You may include additional disclaimers of warranty and limitations of liability specific to any jurisdiction. 4. Inability to Comply Due to Statute or Regulation --------------------------------------------------- If it is impossible for You to comply with any of the terms of this License with respect to some or all of the Covered Software due to statute, judicial order, or regulation then You must: (a) comply with the terms of this License to the maximum extent possible; and (b) describe the limitations and the code they affect. Such description must be placed in a text file included with all distributions of the Covered Software under this License. Except to the extent prohibited by statute or regulation, such description must be sufficiently detailed for a recipient of ordinary skill to be able to understand it. 5. Termination -------------- 5.1. The rights granted under this License will terminate automatically if You fail to comply with any of its terms. However, if You become compliant, then the rights granted under this License from a particular Contributor are reinstated (a) provisionally, unless and until such Contributor explicitly and finally terminates Your grants, and (b) on an ongoing basis, if such Contributor fails to notify You of the non-compliance by some reasonable means prior to 60 days after You have come back into compliance. Moreover, Your grants from a particular Contributor are reinstated on an ongoing basis if such Contributor notifies You of the non-compliance by some reasonable means, this is the first time You have received notice of non-compliance with this License from such Contributor, and You become compliant prior to 30 days after Your receipt of the notice. 5.2. If You initiate litigation against any entity by asserting a patent infringement claim (excluding declaratory judgment actions, counter-claims, and cross-claims) alleging that a Contributor Version directly or indirectly infringes any patent, then the rights granted to You by any and all Contributors for the Covered Software under Section 2.1 of this License shall terminate. 5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user license agreements (excluding distributors and resellers) which have been validly granted by You or Your distributors under this License prior to termination shall survive termination. ************************************************************************ * * * 6. Disclaimer of Warranty * * ------------------------- * * * * Covered Software is provided under this License on an "as is" * * basis, without warranty of any kind, either expressed, implied, or * * statutory, including, without limitation, warranties that the * * Covered Software is free of defects, merchantable, fit for a * * particular purpose or non-infringing. The entire risk as to the * * quality and performance of the Covered Software is with You. * * Should any Covered Software prove defective in any respect, You * * (not any Contributor) assume the cost of any necessary servicing, * * repair, or correction. This disclaimer of warranty constitutes an * * essential part of this License. No use of any Covered Software is * * authorized under this License except under this disclaimer. * * * ************************************************************************ ************************************************************************ * * * 7. Limitation of Liability * * -------------------------- * * * * Under no circumstances and under no legal theory, whether tort * * (including negligence), contract, or otherwise, shall any * * Contributor, or anyone who distributes Covered Software as * * permitted above, be liable to You for any direct, indirect, * * special, incidental, or consequential damages of any character * * including, without limitation, damages for lost profits, loss of * * goodwill, work stoppage, computer failure or malfunction, or any * * and all other commercial damages or losses, even if such party * * shall have been informed of the possibility of such damages. This * * limitation of liability shall not apply to liability for death or * * personal injury resulting from such party's negligence to the * * extent applicable law prohibits such limitation. Some * * jurisdictions do not allow the exclusion or limitation of * * incidental or consequential damages, so this exclusion and * * limitation may not apply to You. * * * ************************************************************************ 8. Litigation ------------- Any litigation relating to this License may be brought only in the courts of a jurisdiction where the defendant maintains its principal place of business and such litigation shall be governed by laws of that jurisdiction, without reference to its conflict-of-law provisions. Nothing in this Section shall prevent a party's ability to bring cross-claims or counter-claims. 9. Miscellaneous ---------------- This License represents the complete agreement concerning the subject matter hereof. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. Any law or regulation which provides that the language of a contract shall be construed against the drafter shall not be used to construe this License against a Contributor. 10. Versions of the License --------------------------- 10.1. New Versions Mozilla Foundation is the license steward. Except as provided in Section 10.3, no one other than the license steward has the right to modify or publish new versions of this License. Each version will be given a distinguishing version number. 10.2. Effect of New Versions You may distribute the Covered Software under the terms of the version of the License under which You originally received the Covered Software, or under the terms of any subsequent version published by the license steward. 10.3. Modified Versions If you create software not governed by this License, and you want to create a new license for such software, you may create and use a modified version of this License if you rename the license and remove any references to the name of the license steward (except to note that such modified license differs from this License). 10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses If You choose to distribute Source Code Form that is Incompatible With Secondary Licenses under the terms of this version of the License, the notice described in Exhibit B of this License must be attached. Exhibit A - Source Code Form License Notice ------------------------------------------- This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. If it is not possible or desirable to put the notice in a particular file, then You may include the notice in a location (such as a LICENSE file in a relevant directory) where a recipient would be likely to look for such a notice. You may add additional accurate notices of copyright ownership. Exhibit B - "Incompatible With Secondary Licenses" Notice --------------------------------------------------------- This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. ================================================ FILE: README.md ================================================ # orjson orjson is a fast, correct JSON library for Python. It [benchmarks](https://github.com/ijl/orjson?tab=readme-ov-file#performance) as the fastest Python library for JSON and is more correct than the standard json library or other third-party libraries. It serializes [dataclass](https://github.com/ijl/orjson?tab=readme-ov-file#dataclass), [datetime](https://github.com/ijl/orjson?tab=readme-ov-file#datetime), [numpy](https://github.com/ijl/orjson?tab=readme-ov-file#numpy), and [UUID](https://github.com/ijl/orjson?tab=readme-ov-file#uuid) instances natively. [orjson.dumps()](https://github.com/ijl/orjson?tab=readme-ov-file#serialize) is something like 10x as fast as `json`, serializes common types and subtypes, has a `default` parameter for the caller to specify how to serialize arbitrary types, and has a number of flags controlling output. [orjson.loads()](https://github.com/ijl/orjson?tab=readme-ov-file#deserialize) is something like 2x as fast as `json`, and is strictly compliant with UTF-8 and RFC 8259 ("The JavaScript Object Notation (JSON) Data Interchange Format"). Reading from and writing to files, line-delimited JSON files, and so on is not provided by the library. orjson supports CPython 3.10, 3.11, 3.12, 3.13, 3.14, and 3.15. It distributes amd64/x86_64/x64, i686/x86, aarch64/arm64/armv8, arm7, ppc64le/POWER8, and s390x wheels for Linux, amd64 and aarch64 wheels for macOS, and amd64, i686, and aarch64 wheels for Windows. Wheels published to PyPI for amd64 run on x86-64-v1 (2003) or later, but will at runtime use AVX-512 if available for a significant performance benefit; aarch64 wheels run on ARMv8-A (2011) or later. orjson does not and will not support PyPy, embedded Python builds for Android/iOS, or PEP 554 subinterpreters. orjson may support PEP 703 free-threading when it is stable. Releases follow semantic versioning and serializing a new object type without an opt-in flag is considered a breaking change. orjson contains source code licensed under the Mozilla Public License 2.0, Apache 2.0, and MIT licenses. The repository from which PyPI artifacts are published is [github.com/ijl/orjson](https://github.com/ijl/orjson) and an alternative repository is [codeberg.org/ijl/orjson](https://codeberg.org/ijl/orjson). There is no open issue tracker or pull requests due to signal-to-noise ratio. There is a [CHANGELOG](https://github.com/ijl/orjson/blob/master/CHANGELOG.md) available in the repository. 1. [Usage](https://github.com/ijl/orjson?tab=readme-ov-file#usage) 1. [Install](https://github.com/ijl/orjson?tab=readme-ov-file#install) 2. [Quickstart](https://github.com/ijl/orjson?tab=readme-ov-file#quickstart) 3. [Migrating](https://github.com/ijl/orjson?tab=readme-ov-file#migrating) 4. [Serialize](https://github.com/ijl/orjson?tab=readme-ov-file#serialize) 1. [default](https://github.com/ijl/orjson?tab=readme-ov-file#default) 2. [option](https://github.com/ijl/orjson?tab=readme-ov-file#option) 3. [Fragment](https://github.com/ijl/orjson?tab=readme-ov-file#fragment) 5. [Deserialize](https://github.com/ijl/orjson?tab=readme-ov-file#deserialize) 2. [Types](https://github.com/ijl/orjson?tab=readme-ov-file#types) 1. [dataclass](https://github.com/ijl/orjson?tab=readme-ov-file#dataclass) 2. [datetime](https://github.com/ijl/orjson?tab=readme-ov-file#datetime) 3. [enum](https://github.com/ijl/orjson?tab=readme-ov-file#enum) 4. [float](https://github.com/ijl/orjson?tab=readme-ov-file#float) 5. [int](https://github.com/ijl/orjson?tab=readme-ov-file#int) 6. [numpy](https://github.com/ijl/orjson?tab=readme-ov-file#numpy) 7. [str](https://github.com/ijl/orjson?tab=readme-ov-file#str) 8. [uuid](https://github.com/ijl/orjson?tab=readme-ov-file#uuid) 3. [Testing](https://github.com/ijl/orjson?tab=readme-ov-file#testing) 4. [Performance](https://github.com/ijl/orjson?tab=readme-ov-file#performance) 1. [Latency](https://github.com/ijl/orjson?tab=readme-ov-file#latency) 2. [Reproducing](https://github.com/ijl/orjson?tab=readme-ov-file#reproducing) 5. [Questions](https://github.com/ijl/orjson?tab=readme-ov-file#questions) 6. [Packaging](https://github.com/ijl/orjson?tab=readme-ov-file#packaging) 7. [License](https://github.com/ijl/orjson?tab=readme-ov-file#license) ## Usage ### Install To install a wheel from PyPI, install the `orjson` package. In `requirements.in` or `requirements.txt` format, specify: ```txt orjson >= 3.10,<4 ``` In `pyproject.toml` format, specify: ```toml orjson = "^3.10" ``` To build a wheel, see [packaging](https://github.com/ijl/orjson?tab=readme-ov-file#packaging). ### Quickstart This is an example of serializing, with options specified, and deserializing: ```python >>> import orjson, datetime, numpy >>> data = { "type": "job", "created_at": datetime.datetime(1970, 1, 1), "status": "🆗", "payload": numpy.array([[1, 2], [3, 4]]), } >>> orjson.dumps(data, option=orjson.OPT_NAIVE_UTC | orjson.OPT_SERIALIZE_NUMPY) b'{"type":"job","created_at":"1970-01-01T00:00:00+00:00","status":"\xf0\x9f\x86\x97","payload":[[1,2],[3,4]]}' >>> orjson.loads(_) {'type': 'job', 'created_at': '1970-01-01T00:00:00+00:00', 'status': '🆗', 'payload': [[1, 2], [3, 4]]} ``` ### Migrating orjson version 3 serializes more types than version 2. Subclasses of `str`, `int`, `dict`, and `list` are now serialized. This is faster and more similar to the standard library. It can be disabled with `orjson.OPT_PASSTHROUGH_SUBCLASS`.`dataclasses.dataclass` instances are now serialized by default and cannot be customized in a `default` function unless `option=orjson.OPT_PASSTHROUGH_DATACLASS` is specified. `uuid.UUID` instances are serialized by default. For any type that is now serialized, implementations in a `default` function and options enabling them can be removed but do not need to be. There was no change in deserialization. To migrate from the standard library, the largest difference is that `orjson.dumps` returns `bytes` and `json.dumps` returns a `str`. Users with `dict` objects using non-`str` keys should specify `option=orjson.OPT_NON_STR_KEYS`. `sort_keys` is replaced by `option=orjson.OPT_SORT_KEYS`. `indent` is replaced by `option=orjson.OPT_INDENT_2` and other levels of indentation are not supported. `ensure_ascii` is probably not relevant today and UTF-8 characters cannot be escaped to ASCII. ### Serialize ```python def dumps( __obj: Any, default: Optional[Callable[[Any], Any]] = ..., option: Optional[int] = ..., ) -> bytes: ... ``` `dumps()` serializes Python objects to JSON. It natively serializes `str`, `dict`, `list`, `tuple`, `int`, `float`, `bool`, `None`, `dataclasses.dataclass`, `typing.TypedDict`, `datetime.datetime`, `datetime.date`, `datetime.time`, `uuid.UUID`, `numpy.ndarray`, and `orjson.Fragment` instances. It supports arbitrary types through `default`. It serializes subclasses of `str`, `int`, `dict`, `list`, `dataclasses.dataclass`, and `enum.Enum`. It does not serialize subclasses of `tuple` to avoid serializing `namedtuple` objects as arrays. To avoid serializing subclasses, specify the option `orjson.OPT_PASSTHROUGH_SUBCLASS`. The output is a `bytes` object containing UTF-8. The global interpreter lock (GIL) is held for the duration of the call. It raises `JSONEncodeError` on an unsupported type. This exception message describes the invalid object with the error message `Type is not JSON serializable: ...`. To fix this, specify [default](https://github.com/ijl/orjson?tab=readme-ov-file#default). It raises `JSONEncodeError` on a `str` that contains invalid UTF-8. It raises `JSONEncodeError` on an integer that exceeds 64 bits by default or, with `OPT_STRICT_INTEGER`, 53 bits. It raises `JSONEncodeError` if a `dict` has a key of a type other than `str`, unless `OPT_NON_STR_KEYS` is specified. It raises `JSONEncodeError` if the output of `default` recurses to handling by `default` more than 254 levels deep. It raises `JSONEncodeError` on circular references. It raises `JSONEncodeError` if a `tzinfo` on a datetime object is unsupported. `JSONEncodeError` is a subclass of `TypeError`. This is for compatibility with the standard library. If the failure was caused by an exception in `default` then `JSONEncodeError` chains the original exception as `__cause__`. #### default To serialize a subclass or arbitrary types, specify `default` as a callable that returns a supported type. `default` may be a function, lambda, or callable class instance. To specify that a type was not handled by `default`, raise an exception such as `TypeError`. ```python >>> import orjson, decimal >>> def default(obj): if isinstance(obj, decimal.Decimal): return str(obj) raise TypeError >>> orjson.dumps(decimal.Decimal("0.0842389659712649442845")) JSONEncodeError: Type is not JSON serializable: decimal.Decimal >>> orjson.dumps(decimal.Decimal("0.0842389659712649442845"), default=default) b'"0.0842389659712649442845"' >>> orjson.dumps({1, 2}, default=default) orjson.JSONEncodeError: Type is not JSON serializable: set ``` The `default` callable may return an object that itself must be handled by `default` up to 254 times before an exception is raised. It is important that `default` raise an exception if a type cannot be handled. Python otherwise implicitly returns `None`, which appears to the caller like a legitimate value and is serialized: ```python >>> import orjson, json >>> def default(obj): if isinstance(obj, decimal.Decimal): return str(obj) >>> orjson.dumps({"set":{1, 2}}, default=default) b'{"set":null}' >>> json.dumps({"set":{1, 2}}, default=default) '{"set":null}' ``` #### option To modify how data is serialized, specify `option`. Each `option` is an integer constant in `orjson`. To specify multiple options, mask them together, e.g., `option=orjson.OPT_STRICT_INTEGER | orjson.OPT_NAIVE_UTC`. ##### OPT_APPEND_NEWLINE Append `\n` to the output. This is a convenience and optimization for the pattern of `dumps(...) + "\n"`. `bytes` objects are immutable and this pattern copies the original contents. ```python >>> import orjson >>> orjson.dumps([]) b"[]" >>> orjson.dumps([], option=orjson.OPT_APPEND_NEWLINE) b"[]\n" ``` ##### OPT_INDENT_2 Pretty-print output with an indent of two spaces. This is equivalent to `indent=2` in the standard library. Pretty printing is slower and the output larger. This option is compatible with all other options. ```python >>> import orjson >>> orjson.dumps({"a": "b", "c": {"d": True}, "e": [1, 2]}) b'{"a":"b","c":{"d":true},"e":[1,2]}' >>> orjson.dumps( {"a": "b", "c": {"d": True}, "e": [1, 2]}, option=orjson.OPT_INDENT_2 ) b'{\n "a": "b",\n "c": {\n "d": true\n },\n "e": [\n 1,\n 2\n ]\n}' ``` If displayed, the indentation and linebreaks appear like this: ```json { "a": "b", "c": { "d": true }, "e": [ 1, 2 ] } ``` This measures serializing the github.json fixture as compact (52KiB) or pretty (64KiB): | Library | compact (ms) | pretty (ms) | vs. orjson | |-----------|----------------|---------------|--------------| | orjson | 0.01 | 0.02 | 1 | | json | 0.13 | 0.54 | 34 | This measures serializing the citm_catalog.json fixture, more of a worst case due to the amount of nesting and newlines, as compact (489KiB) or pretty (1.1MiB): | Library | compact (ms) | pretty (ms) | vs. orjson | |-----------|----------------|---------------|--------------| | orjson | 0.25 | 0.45 | 1 | | json | 3.01 | 24.42 | 54.4 | This can be reproduced using the `pyindent` script. ##### OPT_NAIVE_UTC Serialize `datetime.datetime` objects without a `tzinfo` as UTC. This has no effect on `datetime.datetime` objects that have `tzinfo` set. ```python >>> import orjson, datetime >>> orjson.dumps( datetime.datetime(1970, 1, 1, 0, 0, 0), ) b'"1970-01-01T00:00:00"' >>> orjson.dumps( datetime.datetime(1970, 1, 1, 0, 0, 0), option=orjson.OPT_NAIVE_UTC, ) b'"1970-01-01T00:00:00+00:00"' ``` ##### OPT_NON_STR_KEYS Serialize `dict` keys of type other than `str`. This allows `dict` keys to be one of `str`, `int`, `float`, `bool`, `None`, `datetime.datetime`, `datetime.date`, `datetime.time`, `enum.Enum`, and `uuid.UUID`. For comparison, the standard library serializes `str`, `int`, `float`, `bool` or `None` by default. orjson benchmarks as being faster at serializing non-`str` keys than other libraries. This option is slower for `str` keys than the default. ```python >>> import orjson, datetime, uuid >>> orjson.dumps( {uuid.UUID("7202d115-7ff3-4c81-a7c1-2a1f067b1ece"): [1, 2, 3]}, option=orjson.OPT_NON_STR_KEYS, ) b'{"7202d115-7ff3-4c81-a7c1-2a1f067b1ece":[1,2,3]}' >>> orjson.dumps( {datetime.datetime(1970, 1, 1, 0, 0, 0): [1, 2, 3]}, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_NAIVE_UTC, ) b'{"1970-01-01T00:00:00+00:00":[1,2,3]}' ``` These types are generally serialized how they would be as values, e.g., `datetime.datetime` is still an RFC 3339 string and respects options affecting it. The exception is that `int` serialization does not respect `OPT_STRICT_INTEGER`. This option has the risk of creating duplicate keys. This is because non-`str` objects may serialize to the same `str` as an existing key, e.g., `{"1": true, 1: false}`. The last key to be inserted to the `dict` will be serialized last and a JSON deserializer will presumably take the last occurrence of a key (in the above, `false`). The first value will be lost. This option is compatible with `orjson.OPT_SORT_KEYS`. If sorting is used, note the sort is unstable and will be unpredictable for duplicate keys. ```python >>> import orjson, datetime >>> orjson.dumps( {"other": 1, datetime.date(1970, 1, 5): 2, datetime.date(1970, 1, 3): 3}, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SORT_KEYS ) b'{"1970-01-03":3,"1970-01-05":2,"other":1}' ``` This measures serializing 589KiB of JSON comprising a `list` of 100 `dict` in which each `dict` has both 365 randomly-sorted `int` keys representing epoch timestamps as well as one `str` key and the value for each key is a single integer. In "str keys", the keys were converted to `str` before serialization, and orjson still specifes `option=orjson.OPT_NON_STR_KEYS` (which is always somewhat slower). | Library | str keys (ms) | int keys (ms) | int keys sorted (ms) | |-----------|-----------------|-----------------|------------------------| | orjson | 0.5 | 0.93 | 2.08 | | json | 2.72 | 3.59 | | json is blank because it raises `TypeError` on attempting to sort before converting all keys to `str`. This can be reproduced using the `pynonstr` script. ##### OPT_OMIT_MICROSECONDS Do not serialize the `microsecond` field on `datetime.datetime` and `datetime.time` instances. ```python >>> import orjson, datetime >>> orjson.dumps( datetime.datetime(1970, 1, 1, 0, 0, 0, 1), ) b'"1970-01-01T00:00:00.000001"' >>> orjson.dumps( datetime.datetime(1970, 1, 1, 0, 0, 0, 1), option=orjson.OPT_OMIT_MICROSECONDS, ) b'"1970-01-01T00:00:00"' ``` ##### OPT_PASSTHROUGH_DATACLASS Passthrough `dataclasses.dataclass` instances to `default`. This allows customizing their output but is much slower. ```python >>> import orjson, dataclasses >>> @dataclasses.dataclass class User: id: str name: str password: str def default(obj): if isinstance(obj, User): return {"id": obj.id, "name": obj.name} raise TypeError >>> orjson.dumps(User("3b1", "asd", "zxc")) b'{"id":"3b1","name":"asd","password":"zxc"}' >>> orjson.dumps(User("3b1", "asd", "zxc"), option=orjson.OPT_PASSTHROUGH_DATACLASS) TypeError: Type is not JSON serializable: User >>> orjson.dumps( User("3b1", "asd", "zxc"), option=orjson.OPT_PASSTHROUGH_DATACLASS, default=default, ) b'{"id":"3b1","name":"asd"}' ``` ##### OPT_PASSTHROUGH_DATETIME Passthrough `datetime.datetime`, `datetime.date`, and `datetime.time` instances to `default`. This allows serializing datetimes to a custom format, e.g., HTTP dates: ```python >>> import orjson, datetime >>> def default(obj): if isinstance(obj, datetime.datetime): return obj.strftime("%a, %d %b %Y %H:%M:%S GMT") raise TypeError >>> orjson.dumps({"created_at": datetime.datetime(1970, 1, 1)}) b'{"created_at":"1970-01-01T00:00:00"}' >>> orjson.dumps({"created_at": datetime.datetime(1970, 1, 1)}, option=orjson.OPT_PASSTHROUGH_DATETIME) TypeError: Type is not JSON serializable: datetime.datetime >>> orjson.dumps( {"created_at": datetime.datetime(1970, 1, 1)}, option=orjson.OPT_PASSTHROUGH_DATETIME, default=default, ) b'{"created_at":"Thu, 01 Jan 1970 00:00:00 GMT"}' ``` This does not affect datetimes in `dict` keys if using OPT_NON_STR_KEYS. ##### OPT_PASSTHROUGH_SUBCLASS Passthrough subclasses of builtin types to `default`. ```python >>> import orjson >>> class Secret(str): pass def default(obj): if isinstance(obj, Secret): return "******" raise TypeError >>> orjson.dumps(Secret("zxc")) b'"zxc"' >>> orjson.dumps(Secret("zxc"), option=orjson.OPT_PASSTHROUGH_SUBCLASS) TypeError: Type is not JSON serializable: Secret >>> orjson.dumps(Secret("zxc"), option=orjson.OPT_PASSTHROUGH_SUBCLASS, default=default) b'"******"' ``` This does not affect serializing subclasses as `dict` keys if using OPT_NON_STR_KEYS. ##### OPT_SERIALIZE_DATACLASS This is deprecated and has no effect in version 3. In version 2 this was required to serialize `dataclasses.dataclass` instances. For more, see [dataclass](https://github.com/ijl/orjson?tab=readme-ov-file#dataclass). ##### OPT_SERIALIZE_NUMPY Serialize `numpy.ndarray` instances. For more, see [numpy](https://github.com/ijl/orjson?tab=readme-ov-file#numpy). ##### OPT_SERIALIZE_UUID This is deprecated and has no effect in version 3. In version 2 this was required to serialize `uuid.UUID` instances. For more, see [UUID](https://github.com/ijl/orjson?tab=readme-ov-file#UUID). ##### OPT_SORT_KEYS Serialize `dict` keys in sorted order. The default is to serialize in an unspecified order. This is equivalent to `sort_keys=True` in the standard library. This can be used to ensure the order is deterministic for hashing or tests. It has a substantial performance penalty and is not recommended in general. ```python >>> import orjson >>> orjson.dumps({"b": 1, "c": 2, "a": 3}) b'{"b":1,"c":2,"a":3}' >>> orjson.dumps({"b": 1, "c": 2, "a": 3}, option=orjson.OPT_SORT_KEYS) b'{"a":3,"b":1,"c":2}' ``` This measures serializing the twitter.json fixture unsorted and sorted: | Library | unsorted (ms) | sorted (ms) | vs. orjson | |-----------|-----------------|---------------|--------------| | orjson | 0.11 | 0.3 | 1 | | json | 1.36 | 1.93 | 6.4 | The benchmark can be reproduced using the `pysort` script. The sorting is not collation/locale-aware: ```python >>> import orjson >>> orjson.dumps({"a": 1, "ä": 2, "A": 3}, option=orjson.OPT_SORT_KEYS) b'{"A":3,"a":1,"\xc3\xa4":2}' ``` This is the same sorting behavior as the standard library. `dataclass` also serialize as maps but this has no effect on them. ##### OPT_STRICT_INTEGER Enforce 53-bit limit on integers. The limit is otherwise 64 bits, the same as the Python standard library. For more, see [int](https://github.com/ijl/orjson?tab=readme-ov-file#int). ##### OPT_UTC_Z Serialize a UTC timezone on `datetime.datetime` instances as `Z` instead of `+00:00`. ```python >>> import orjson, datetime, zoneinfo >>> orjson.dumps( datetime.datetime(1970, 1, 1, 0, 0, 0, tzinfo=zoneinfo.ZoneInfo("UTC")), ) b'"1970-01-01T00:00:00+00:00"' >>> orjson.dumps( datetime.datetime(1970, 1, 1, 0, 0, 0, tzinfo=zoneinfo.ZoneInfo("UTC")), option=orjson.OPT_UTC_Z ) b'"1970-01-01T00:00:00Z"' ``` #### Fragment `orjson.Fragment` includes already-serialized JSON in a document. This is an efficient way to include JSON blobs from a cache, JSONB field, or separately serialized object without first deserializing to Python objects via `loads()`. ```python >>> import orjson >>> orjson.dumps({"key": "zxc", "data": orjson.Fragment(b'{"a": "b", "c": 1}')}) b'{"key":"zxc","data":{"a": "b", "c": 1}}' ``` It does no reformatting: `orjson.OPT_INDENT_2` will not affect a compact blob nor will a pretty-printed JSON blob be rewritten as compact. The input must be `bytes` or `str` and given as a positional argument. This raises `orjson.JSONEncodeError` if a `str` is given and the input is not valid UTF-8. It otherwise does no validation and it is possible to write invalid JSON. This does not escape characters. The implementation is tested to not crash if given invalid strings or invalid JSON. ### Deserialize ```python def loads(__obj: Union[bytes, bytearray, memoryview, str]) -> Any: ... ``` `loads()` deserializes JSON to Python objects. It deserializes to `dict`, `list`, `int`, `float`, `str`, `bool`, and `None` objects. `bytes`, `bytearray`, `memoryview`, and `str` input are accepted. If the input exists as a `memoryview`, `bytearray`, or `bytes` object, it is recommended to pass these directly rather than creating an unnecessary `str` object. That is, `orjson.loads(b"{}")` instead of `orjson.loads(b"{}".decode("utf-8"))`. This has lower memory usage and lower latency. The input must be valid UTF-8. orjson maintains a cache of map keys for the duration of the process. This causes a net reduction in memory usage by avoiding duplicate strings. The keys must be at most 64 bytes to be cached and 2048 entries are stored. The global interpreter lock (GIL) is held for the duration of the call. It raises `JSONDecodeError` if given an invalid type or invalid JSON. This includes if the input contains `NaN`, `Infinity`, or `-Infinity`, which the standard library allows, but is not valid JSON. It raises `JSONDecodeError` if a combination of array or object recurses 1024 levels deep. It raises `JSONDecodeError` if unable to allocate a buffer large enough to parse the document. `JSONDecodeError` is a subclass of `json.JSONDecodeError` and `ValueError`. This is for compatibility with the standard library. ## Types ### dataclass orjson serializes instances of `dataclasses.dataclass` natively. It serializes instances 40-50x as fast as other libraries and avoids a severe slowdown seen in other libraries compared to serializing `dict`. It is supported to pass all variants of dataclasses, including dataclasses using `__slots__`, frozen dataclasses, those with optional or default attributes, and subclasses. There is a performance benefit to not using `__slots__`. | Library | dict (ms) | dataclass (ms) | vs. orjson | |-----------|-------------|------------------|--------------| | orjson | 0.43 | 0.95 | 1 | | json | 5.81 | 38.32 | 40 | This measures serializing 555KiB of JSON, orjson natively and other libraries using `default` to serialize the output of `dataclasses.asdict()`. This can be reproduced using the `pydataclass` script. Dataclasses are serialized as maps, with every attribute serialized and in the order given on class definition: ```python >>> import dataclasses, orjson, typing @dataclasses.dataclass class Member: id: int active: bool = dataclasses.field(default=False) @dataclasses.dataclass class Object: id: int name: str members: typing.List[Member] >>> orjson.dumps(Object(1, "a", [Member(1, True), Member(2)])) b'{"id":1,"name":"a","members":[{"id":1,"active":true},{"id":2,"active":false}]}' ``` ### datetime orjson serializes `datetime.datetime` objects to [RFC 3339](https://tools.ietf.org/html/rfc3339) format, e.g., "1970-01-01T00:00:00+00:00". This is a subset of ISO 8601 and is compatible with `isoformat()` in the standard library. ```python >>> import orjson, datetime, zoneinfo >>> orjson.dumps( datetime.datetime(2018, 12, 1, 2, 3, 4, 9, tzinfo=zoneinfo.ZoneInfo("Australia/Adelaide")) ) b'"2018-12-01T02:03:04.000009+10:30"' >>> orjson.dumps( datetime.datetime(2100, 9, 1, 21, 55, 2).replace(tzinfo=zoneinfo.ZoneInfo("UTC")) ) b'"2100-09-01T21:55:02+00:00"' >>> orjson.dumps( datetime.datetime(2100, 9, 1, 21, 55, 2) ) b'"2100-09-01T21:55:02"' ``` `datetime.datetime` supports instances with a `tzinfo` that is `None`, `datetime.timezone.utc`, a timezone instance from the standard library `zoneinfo` module, or a timezone instance from the third-party `pendulum`, `pytz`, or `dateutil`/`arrow` libraries. It is fastest to use the standard library's `zoneinfo.ZoneInfo` for timezones. `datetime.time` objects must not have a `tzinfo`. ```python >>> import orjson, datetime >>> orjson.dumps(datetime.time(12, 0, 15, 290)) b'"12:00:15.000290"' ``` `datetime.date` objects will always serialize. ```python >>> import orjson, datetime >>> orjson.dumps(datetime.date(1900, 1, 2)) b'"1900-01-02"' ``` Errors with `tzinfo` result in `JSONEncodeError` being raised. To disable serialization of `datetime` objects specify the option `orjson.OPT_PASSTHROUGH_DATETIME`. To use "Z" suffix instead of "+00:00" to indicate UTC ("Zulu") time, use the option `orjson.OPT_UTC_Z`. To assume datetimes without timezone are UTC, use the option `orjson.OPT_NAIVE_UTC`. ### enum orjson serializes enums natively. Options apply to their values. ```python >>> import enum, datetime, orjson >>> class DatetimeEnum(enum.Enum): EPOCH = datetime.datetime(1970, 1, 1, 0, 0, 0) >>> orjson.dumps(DatetimeEnum.EPOCH) b'"1970-01-01T00:00:00"' >>> orjson.dumps(DatetimeEnum.EPOCH, option=orjson.OPT_NAIVE_UTC) b'"1970-01-01T00:00:00+00:00"' ``` Enums with members that are not supported types can be serialized using `default`: ```python >>> import enum, orjson >>> class Custom: def __init__(self, val): self.val = val def default(obj): if isinstance(obj, Custom): return obj.val raise TypeError class CustomEnum(enum.Enum): ONE = Custom(1) >>> orjson.dumps(CustomEnum.ONE, default=default) b'1' ``` ### float orjson serializes and deserializes double precision floats with no loss of precision and consistent rounding. `orjson.dumps()` serializes Nan, Infinity, and -Infinity, which are not compliant JSON, as `null`: ```python >>> import orjson, json >>> orjson.dumps([float("NaN"), float("Infinity"), float("-Infinity")]) b'[null,null,null]' >>> json.dumps([float("NaN"), float("Infinity"), float("-Infinity")]) '[NaN, Infinity, -Infinity]' ``` ### int orjson serializes and deserializes 64-bit integers by default. The range supported is a signed 64-bit integer's minimum (-9223372036854775807) to an unsigned 64-bit integer's maximum (18446744073709551615). This is widely compatible, but there are implementations that only support 53-bits for integers, e.g., web browsers. For those implementations, `dumps()` can be configured to raise a `JSONEncodeError` on values exceeding the 53-bit range. ```python >>> import orjson >>> orjson.dumps(9007199254740992) b'9007199254740992' >>> orjson.dumps(9007199254740992, option=orjson.OPT_STRICT_INTEGER) JSONEncodeError: Integer exceeds 53-bit range >>> orjson.dumps(-9007199254740992, option=orjson.OPT_STRICT_INTEGER) JSONEncodeError: Integer exceeds 53-bit range ``` ### numpy orjson natively serializes `numpy.ndarray` and individual `numpy.float64`, `numpy.float32`, `numpy.float16` (`numpy.half`), `numpy.int64`, `numpy.int32`, `numpy.int16`, `numpy.int8`, `numpy.uint64`, `numpy.uint32`, `numpy.uint16`, `numpy.uint8`, `numpy.uintp`, `numpy.intp`, `numpy.datetime64`, and `numpy.bool` instances. orjson is compatible with both numpy v1 and v2. orjson is faster than all compared libraries at serializing numpy instances. Serializing numpy data requires specifying `option=orjson.OPT_SERIALIZE_NUMPY`. ```python >>> import orjson, numpy >>> orjson.dumps( numpy.array([[1, 2, 3], [4, 5, 6]]), option=orjson.OPT_SERIALIZE_NUMPY, ) b'[[1,2,3],[4,5,6]]' ``` The array must be a contiguous C array (`C_CONTIGUOUS`) and one of the supported datatypes. Note a difference between serializing `numpy.float32` using `ndarray.tolist()` or `orjson.dumps(..., option=orjson.OPT_SERIALIZE_NUMPY)`: `tolist()` converts to a `double` before serializing and orjson's native path does not. This can result in different rounding. `numpy.datetime64` instances are serialized as RFC 3339 strings and datetime options affect them. ```python >>> import orjson, numpy >>> orjson.dumps( numpy.datetime64("2021-01-01T00:00:00.172"), option=orjson.OPT_SERIALIZE_NUMPY, ) b'"2021-01-01T00:00:00.172000"' >>> orjson.dumps( numpy.datetime64("2021-01-01T00:00:00.172"), option=( orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC | orjson.OPT_OMIT_MICROSECONDS ), ) b'"2021-01-01T00:00:00+00:00"' ``` If an array is not a contiguous C array, contains an unsupported datatype, or contains a `numpy.datetime64` using an unsupported representation (e.g., picoseconds), orjson falls through to `default`. In `default`, `obj.tolist()` can be specified. If an array is not in the native endianness, e.g., an array of big-endian values on a little-endian system, `orjson.JSONEncodeError` is raised. If an array is malformed, `orjson.JSONEncodeError` is raised. This measures serializing 92MiB of JSON from an `numpy.ndarray` with dimensions of `(50000, 100)` and `numpy.float64` values: | Library | Latency (ms) | RSS diff (MiB) | vs. orjson | |-----------|----------------|------------------|--------------| | orjson | 105 | 105 | 1 | | json | 1,481 | 295 | 14.2 | This measures serializing 100MiB of JSON from an `numpy.ndarray` with dimensions of `(100000, 100)` and `numpy.int32` values: | Library | Latency (ms) | RSS diff (MiB) | vs. orjson | |-----------|----------------|------------------|--------------| | orjson | 68 | 119 | 1 | | json | 684 | 501 | 10.1 | This measures serializing 105MiB of JSON from an `numpy.ndarray` with dimensions of `(100000, 200)` and `numpy.bool` values: | Library | Latency (ms) | RSS diff (MiB) | vs. orjson | |-----------|----------------|------------------|--------------| | orjson | 50 | 125 | 1 | | json | 573 | 398 | 11.5 | In these benchmarks, orjson serializes natively and `json` serializes `ndarray.tolist()` via `default`. The RSS column measures peak memory usage during serialization. This can be reproduced using the `pynumpy` script. orjson does not have an installation or compilation dependency on numpy. The implementation is independent, reading `numpy.ndarray` using `PyArrayInterface`. ### str orjson is strict about UTF-8 conformance. This is stricter than the standard library's json module, which will serialize and deserialize UTF-16 surrogates, e.g., "\ud800", that are invalid UTF-8. If `orjson.dumps()` is given a `str` that does not contain valid UTF-8, `orjson.JSONEncodeError` is raised. If `loads()` receives invalid UTF-8, `orjson.JSONDecodeError` is raised. ```python >>> import orjson, json >>> orjson.dumps('\ud800') JSONEncodeError: str is not valid UTF-8: surrogates not allowed >>> json.dumps('\ud800') '"\\ud800"' >>> orjson.loads('"\\ud800"') JSONDecodeError: unexpected end of hex escape at line 1 column 8: line 1 column 1 (char 0) >>> json.loads('"\\ud800"') '\ud800' ``` To make a best effort at deserializing bad input, first decode `bytes` using the `replace` or `lossy` argument for `errors`: ```python >>> import orjson >>> orjson.loads(b'"\xed\xa0\x80"') JSONDecodeError: str is not valid UTF-8: surrogates not allowed >>> orjson.loads(b'"\xed\xa0\x80"'.decode("utf-8", "replace")) '���' ``` ### uuid orjson serializes `uuid.UUID` instances to [RFC 4122](https://tools.ietf.org/html/rfc4122) format, e.g., "f81d4fae-7dec-11d0-a765-00a0c91e6bf6". ``` python >>> import orjson, uuid >>> orjson.dumps(uuid.uuid5(uuid.NAMESPACE_DNS, "python.org")) b'"886313e1-3b8a-5372-9b90-0c9aee199e5d"' ``` ## Testing The library has comprehensive tests. There are tests against fixtures in the [JSONTestSuite](https://github.com/nst/JSONTestSuite) and [nativejson-benchmark](https://github.com/miloyip/nativejson-benchmark) repositories. It is tested to not crash against the [Big List of Naughty Strings](https://github.com/minimaxir/big-list-of-naughty-strings). It is tested to not leak memory. It is tested to not crash against and not accept invalid UTF-8. There are integration tests exercising the library's use in web servers (gunicorn using multiprocess/forked workers) and when multithreaded. orjson is the most correct of the compared libraries. This graph shows how each library handles a combined 342 JSON fixtures from the [JSONTestSuite](https://github.com/nst/JSONTestSuite) and [nativejson-benchmark](https://github.com/miloyip/nativejson-benchmark) tests: | Library | Invalid JSON documents not rejected | Valid JSON documents not deserialized | |------------|---------------------------------------|-----------------------------------------| | orjson | 0 | 0 | | json | 17 | 0 | This shows that all libraries deserialize valid JSON but only orjson correctly rejects the given invalid JSON fixtures. Errors are largely due to accepting invalid strings and numbers. The graph above can be reproduced using the `pycorrectness` script. ## Performance Serialization and deserialization performance of orjson is consistently better than the standard library's `json`. The graphs below illustrate a few commonly used documents. ### Latency ![Serialization](doc/serialization.png) ![Deserialization](doc/deserialization.png) #### twitter.json serialization | Library | Median latency (milliseconds) | Operations per second | Relative (latency) | |-----------|---------------------------------|-------------------------|----------------------| | orjson | 0.1 | 8453 | 1 | | json | 1.3 | 765 | 11.1 | #### twitter.json deserialization | Library | Median latency (milliseconds) | Operations per second | Relative (latency) | |-----------|---------------------------------|-------------------------|----------------------| | orjson | 0.5 | 1889 | 1 | | json | 2.2 | 453 | 4.2 | #### github.json serialization | Library | Median latency (milliseconds) | Operations per second | Relative (latency) | |-----------|---------------------------------|-------------------------|----------------------| | orjson | 0.01 | 103693 | 1 | | json | 0.13 | 7648 | 13.6 | #### github.json deserialization | Library | Median latency (milliseconds) | Operations per second | Relative (latency) | |-----------|---------------------------------|-------------------------|----------------------| | orjson | 0.04 | 23264 | 1 | | json | 0.1 | 10430 | 2.2 | #### citm_catalog.json serialization | Library | Median latency (milliseconds) | Operations per second | Relative (latency) | |-----------|---------------------------------|-------------------------|----------------------| | orjson | 0.3 | 3975 | 1 | | json | 3 | 338 | 11.8 | #### citm_catalog.json deserialization | Library | Median latency (milliseconds) | Operations per second | Relative (latency) | |-----------|---------------------------------|-------------------------|----------------------| | orjson | 1.3 | 781 | 1 | | json | 4 | 250 | 3.1 | #### canada.json serialization | Library | Median latency (milliseconds) | Operations per second | Relative (latency) | |-----------|---------------------------------|-------------------------|----------------------| | orjson | 2.5 | 399 | 1 | | json | 29.8 | 33 | 11.9 | #### canada.json deserialization | Library | Median latency (milliseconds) | Operations per second | Relative (latency) | |-----------|---------------------------------|-------------------------|----------------------| | orjson | 3 | 333 | 1 | | json | 18 | 55 | 6 | ### Reproducing The above was measured using Python 3.11.10 in a Fedora 42 container on an x86-64-v4 machine using the `orjson-3.10.11-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl` artifact on PyPI. The latency results can be reproduced using the `pybench` script. ## Questions ### Will it deserialize to dataclasses, UUIDs, decimals, etc or support object_hook? No. This requires a schema specifying what types are expected and how to handle errors etc. This is addressed by data validation libraries a level above this. ### Will it serialize to `str`? No. `bytes` is the correct type for a serialized blob. ### Will it support NDJSON or JSONL? No. [orjsonl](https://github.com/umarbutler/orjsonl) may be appropriate. ### Will it support JSON5 or RJSON? No, it supports RFC 8259. ### How do I depend on orjson in a Rust project? orjson is only shipped as a Python module. The project should depend on `orjson` in its own Python requirements and should obtain pointers to functions and objects using the normal `PyImport_*` APIs. ## Packaging To package orjson requires at least [Rust](https://www.rust-lang.org/) 1.89, a C compiler, and the [maturin](https://github.com/PyO3/maturin) build tool. The recommended build command is: ```sh maturin build --release --strip ``` The project's own CI tests against `nightly-2026-01-28` and stable 1.89. It is prudent to pin the nightly version because that channel can introduce breaking changes. There is a significant performance benefit to using nightly. orjson is tested on native hardware for amd64, aarch64, and i686 on Linux. It is cross-compiled and may be tested via emulation for arm7, ppc64le, and s390x. It is tested for aarch64 on macOS and cross-compiles for amd64. For Windows it is tested on amd64, i686, and aarch64. There are no runtime dependencies other than libc. The source distribution on PyPI contains all dependencies' source and can be built without network access. The file can be downloaded from `https://files.pythonhosted.org/packages/source/o/orjson/orjson-${version}.tar.gz`. orjson's tests are included in the source distribution on PyPI. The tests require only `pytest`. There are optional packages such as `pytz` and `numpy` listed in `test/requirements.txt` and used in ~10% of tests. Not having these dependencies causes the tests needing them to skip. Tests can be run with `pytest -q test`. ## License orjson was written by ijl <>, copyright 2018 - 2026, with some source files available under the Mozilla Public License 2.0 and some available under your choice of the Apache 2 license or MIT license. ================================================ FILE: bench/__init__.py ================================================ ================================================ FILE: bench/benchmark_dumps.py ================================================ # SPDX-License-Identifier: (Apache-2.0 OR MIT) # Copyright ijl (2020-2026), Aarni Koskela (2021) from json import loads as json_loads import pytest from .data import FIXTURE_AS_OBJECTS, FIXTURE_NAMES, LIBRARIES @pytest.mark.parametrize("library", LIBRARIES) @pytest.mark.parametrize("fixture", FIXTURE_NAMES) def test_dumps(benchmark, fixture, library): dumper, _ = LIBRARIES[library] benchmark.group = f"{fixture} serialization" benchmark.extra_info["lib"] = library data = FIXTURE_AS_OBJECTS[fixture] benchmark.extra_info["correct"] = json_loads(dumper(data)) == data # type: ignore benchmark(dumper, data) ================================================ FILE: bench/benchmark_loads.py ================================================ # SPDX-License-Identifier: (Apache-2.0 OR MIT) # Copyright ijl (2020-2026), Aarni Koskela (2021) from json import loads as json_loads import pytest from .data import FIXTURE_AS_BYTES, FIXTURE_NAMES, LIBRARIES @pytest.mark.parametrize("fixture", FIXTURE_NAMES) @pytest.mark.parametrize("library", LIBRARIES) def test_loads(benchmark, fixture, library): dumper, loader = LIBRARIES[library] benchmark.group = f"{fixture} deserialization" benchmark.extra_info["lib"] = library data = FIXTURE_AS_BYTES[fixture] correct = json_loads(dumper(loader(data))) == json_loads(data) # type: ignore benchmark.extra_info["correct"] = correct benchmark(loader, data) ================================================ FILE: bench/data.py ================================================ # SPDX-License-Identifier: (Apache-2.0 OR MIT) # Copyright ijl (2019-2026), Aarni Koskela (2021) import gc from json import dumps as _json_dumps from json import loads as json_loads from orjson import dumps as orjson_dumps from orjson import loads as orjson_loads from .util import read_fixture def json_dumps(obj): return _json_dumps(obj).encode("utf-8") LIBRARIES = { "orjson": (orjson_dumps, orjson_loads), "json": (json_dumps, json_loads), } FIXTURE_NAMES = ( "canada.json", "citm_catalog.json", "github.json", "twitter.json", ) FIXTURE_AS_BYTES = {name: read_fixture(f"{name}.xz") for name in FIXTURE_NAMES} FIXTURE_AS_OBJECTS = { name: orjson_loads(FIXTURE_AS_BYTES[name]) for name in FIXTURE_NAMES } if hasattr(gc, "freeze"): gc.freeze() if hasattr(gc, "collect"): gc.collect() if hasattr(gc, "disable"): gc.disable() ================================================ FILE: bench/requirements.txt ================================================ memory-profiler; python_version<"3.15" and implementation_name=="cpython" pandas; python_version<"3.15" and implementation_name=="cpython" pytest-benchmark pytest-random-order seaborn; python_version<"3.15" and implementation_name=="cpython" tabulate ================================================ FILE: bench/run_func ================================================ #!/usr/bin/env python3 # SPDX-License-Identifier: (Apache-2.0 OR MIT) # Copyright ijl (2018-2025), Aarni Koskela (2021) import sys import lzma import os import gc if hasattr(os, "sched_setaffinity"): os.sched_setaffinity(os.getpid(), {0, 1}) from orjson import dumps, loads filename = sys.argv[1] n = int(sys.argv[3]) if len(sys.argv) >= 4 else 1000 with lzma.open(filename, "r") as fileh: file_bytes = fileh.read() if hasattr(gc, "freeze"): gc.freeze() if hasattr(gc, "collect"): gc.collect() if hasattr(gc, "disable"): gc.disable() if sys.argv[2] == "dumps": file_obj = loads(file_bytes) for _ in range(n): _ = dumps(file_obj) elif sys.argv[2] == "loads": for _ in range(n): _ = loads(file_bytes) ================================================ FILE: bench/util.py ================================================ # SPDX-License-Identifier: (Apache-2.0 OR MIT) # Copyright ijl (2018-2022), Aarni Koskela (2021) import lzma import os from pathlib import Path dirname = os.path.join(os.path.dirname(__file__), "../data") if hasattr(os, "sched_setaffinity"): os.sched_setaffinity(os.getpid(), {0, 1}) def read_fixture(filename: str) -> bytes: path = Path(dirname, filename) if path.suffix == ".xz": contents = lzma.decompress(path.read_bytes()) else: contents = path.read_bytes() return contents ================================================ FILE: build.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2021-2026) fn main() { let python_config = pyo3_build_config::get(); if python_config.is_free_threaded() && std::env::var("ORJSON_BUILD_FREETHREADED").is_err() { not_supported("free-threaded Python") } #[allow(unused_variables)] let is_64_bit_python = matches!(python_config.pointer_width, Some(64)); match python_config.implementation { pyo3_build_config::PythonImplementation::CPython => { println!("cargo:rustc-cfg=CPython"); #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] if is_64_bit_python { println!("cargo:rustc-cfg=feature=\"inline_int\""); #[cfg(target_endian = "little")] println!("cargo:rustc-cfg=feature=\"inline_str\""); } } pyo3_build_config::PythonImplementation::GraalPy => not_supported("GraalPy"), pyo3_build_config::PythonImplementation::PyPy => not_supported("PyPy"), } for cfg in python_config.build_script_outputs() { println!("{cfg}"); } println!("cargo:rerun-if-changed=build.rs"); println!("cargo:rerun-if-changed=include/yyjson/*"); println!("cargo:rerun-if-env-changed=CC"); println!("cargo:rerun-if-env-changed=CFLAGS"); println!("cargo:rerun-if-env-changed=LDFLAGS"); println!("cargo:rerun-if-env-changed=ORJSON_BUILD_FREETHREADED"); println!("cargo:rerun-if-env-changed=RUSTFLAGS"); println!("cargo:rustc-check-cfg=cfg(cold_path)"); println!("cargo:rustc-check-cfg=cfg(CPython)"); println!("cargo:rustc-check-cfg=cfg(GraalPy)"); println!("cargo:rustc-check-cfg=cfg(optimize)"); println!("cargo:rustc-check-cfg=cfg(Py_3_10)"); println!("cargo:rustc-check-cfg=cfg(Py_3_11)"); println!("cargo:rustc-check-cfg=cfg(Py_3_12)"); println!("cargo:rustc-check-cfg=cfg(Py_3_13)"); println!("cargo:rustc-check-cfg=cfg(Py_3_14)"); println!("cargo:rustc-check-cfg=cfg(Py_3_15)"); println!("cargo:rustc-check-cfg=cfg(Py_GIL_DISABLED)"); println!("cargo:rustc-check-cfg=cfg(PyPy)"); #[cfg(all(target_arch = "x86_64", not(target_os = "macos")))] if is_64_bit_python { println!("cargo:rustc-cfg=feature=\"avx512\""); } #[cfg(target_arch = "aarch64")] if version_check::supports_feature("portable_simd").unwrap_or(false) { println!("cargo:rustc-cfg=feature=\"generic_simd\""); } if version_check::supports_feature("cold_path").unwrap_or(false) { println!("cargo:rustc-cfg=feature=\"cold_path\""); } if version_check::supports_feature("optimize_attribute").unwrap_or(false) { println!("cargo:rustc-cfg=feature=\"optimize\""); } cc::Build::new() .file("include/yyjson/yyjson.c") .include("include/yyjson") .define("YYJSON_DISABLE_NON_STANDARD", "1") .define("YYJSON_DISABLE_UTF8_VALIDATION", "1") .define("YYJSON_DISABLE_UTILS", "1") .define("YYJSON_DISABLE_WRITER", "1") .compile("yyjson") } fn not_supported(flavor: &str) { let version = env!("CARGO_PKG_VERSION"); eprintln!("\n\n\norjson v{version} does not support {flavor}\n\n\n"); std::process::exit(1); } ================================================ FILE: ci/config.toml ================================================ [unstable] build-std = ["core", "std", "alloc", "proc_macro", "panic_abort"] trim-paths = true [target.x86_64-apple-darwin] linker = "clang" rustflags = ["-C", "target-cpu=x86-64-v2", "-Z", "tune-cpu=generic"] [target.aarch64-apple-darwin] linker = "clang" rustflags = ["-C", "target-cpu=apple-m1", "-Z", "tune-cpu=generic"] ================================================ FILE: ci/sdist.toml ================================================ [source.crates-io] replace-with = "vendored-sources" [source.vendored-sources] directory = "include/cargo" ================================================ FILE: data/jsonchecker/fail01.json ================================================ "A JSON payload should be an object or array, not a string." ================================================ FILE: data/jsonchecker/fail02.json ================================================ ["Unclosed array" ================================================ FILE: data/jsonchecker/fail03.json ================================================ {unquoted_key: "keys must be quoted"} ================================================ FILE: data/jsonchecker/fail04.json ================================================ ["extra comma",] ================================================ FILE: data/jsonchecker/fail05.json ================================================ ["double extra comma",,] ================================================ FILE: data/jsonchecker/fail06.json ================================================ [ , "<-- missing value"] ================================================ FILE: data/jsonchecker/fail07.json ================================================ ["Comma after the close"], ================================================ FILE: data/jsonchecker/fail08.json ================================================ ["Extra close"]] ================================================ FILE: data/jsonchecker/fail09.json ================================================ {"Extra comma": true,} ================================================ FILE: data/jsonchecker/fail10.json ================================================ {"Extra value after close": true} "misplaced quoted value" ================================================ FILE: data/jsonchecker/fail11.json ================================================ {"Illegal expression": 1 + 2} ================================================ FILE: data/jsonchecker/fail12.json ================================================ {"Illegal invocation": alert()} ================================================ FILE: data/jsonchecker/fail13.json ================================================ {"Numbers cannot have leading zeroes": 013} ================================================ FILE: data/jsonchecker/fail14.json ================================================ {"Numbers cannot be hex": 0x14} ================================================ FILE: data/jsonchecker/fail15.json ================================================ ["Illegal backslash escape: \x15"] ================================================ FILE: data/jsonchecker/fail16.json ================================================ [\naked] ================================================ FILE: data/jsonchecker/fail17.json ================================================ ["Illegal backslash escape: \017"] ================================================ FILE: data/jsonchecker/fail18.json ================================================ [[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]] ================================================ FILE: data/jsonchecker/fail19.json ================================================ {"Missing colon" null} ================================================ FILE: data/jsonchecker/fail20.json ================================================ {"Double colon":: null} ================================================ FILE: data/jsonchecker/fail21.json ================================================ {"Comma instead of colon", null} ================================================ FILE: data/jsonchecker/fail22.json ================================================ ["Colon instead of comma": false] ================================================ FILE: data/jsonchecker/fail23.json ================================================ ["Bad value", truth] ================================================ FILE: data/jsonchecker/fail24.json ================================================ ['single quote'] ================================================ FILE: data/jsonchecker/fail25.json ================================================ [" tab character in string "] ================================================ FILE: data/jsonchecker/fail26.json ================================================ ["tab\ character\ in\ string\ "] ================================================ FILE: data/jsonchecker/fail27.json ================================================ ["line break"] ================================================ FILE: data/jsonchecker/fail28.json ================================================ ["line\ break"] ================================================ FILE: data/jsonchecker/fail29.json ================================================ [0e] ================================================ FILE: data/jsonchecker/fail30.json ================================================ [0e+] ================================================ FILE: data/jsonchecker/fail31.json ================================================ [0e+-1] ================================================ FILE: data/jsonchecker/fail32.json ================================================ {"Comma instead if closing brace": true, ================================================ FILE: data/jsonchecker/fail33.json ================================================ ["mismatch"} ================================================ FILE: data/jsonchecker/pass01.json ================================================ [ "JSON Test Pattern pass1", {"object with 1 member":["array with 1 element"]}, {}, [], -42, true, false, null, { "integer": 1234567890, "real": -9876.543210, "e": 0.123456789e-12, "E": 1.234567890E+34, "": 23456789012E66, "zero": 0, "one": 1, "space": " ", "quote": "\"", "backslash": "\\", "controls": "\b\f\n\r\t", "slash": "/ & \/", "alpha": "abcdefghijklmnopqrstuvwyz", "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ", "digit": "0123456789", "0123456789": "digit", "special": "`1~!@#$%^&*()_+-={':[,]}|;.?", "hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A", "true": true, "false": false, "null": null, "array":[ ], "object":{ }, "address": "50 St. James Street", "url": "http://www.JSON.org/", "comment": "// /* */": " ", " s p a c e d " :[1,2 , 3 , 4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7], "jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}", "quotes": "" \u0022 %22 0x22 034 "", "\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?" : "A key can be any string" }, 0.5 ,98.6 , 99.44 , 1066, 1e1, 0.1e1, 1e-1, 1e00,2e+00,2e-00 ,"rosebud"] ================================================ FILE: data/jsonchecker/pass02.json ================================================ [[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]] ================================================ FILE: data/jsonchecker/pass03.json ================================================ { "JSON Test Pattern pass3": { "The outermost value": "must be an object or array.", "In this test": "It is an object." } } ================================================ FILE: data/parsing/i_number_double_huge_neg_exp.json ================================================ [123.456e-789] ================================================ FILE: data/parsing/i_number_huge_exp.json ================================================ [0.4e00669999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999969999999006] ================================================ FILE: data/parsing/i_number_neg_int_huge_exp.json ================================================ [-1e+9999] ================================================ FILE: data/parsing/i_number_pos_double_huge_exp.json ================================================ [1.5e+9999] ================================================ FILE: data/parsing/i_number_real_neg_overflow.json ================================================ [-123123e100000] ================================================ FILE: data/parsing/i_number_real_pos_overflow.json ================================================ [123123e100000] ================================================ FILE: data/parsing/i_number_real_underflow.json ================================================ [123e-10000000] ================================================ FILE: data/parsing/i_number_too_big_neg_int.json ================================================ [-123123123123123123123123123123] ================================================ FILE: data/parsing/i_number_too_big_pos_int.json ================================================ [100000000000000000000] ================================================ FILE: data/parsing/i_number_very_big_negative_int.json ================================================ [-237462374673276894279832749832423479823246327846] ================================================ FILE: data/parsing/i_object_key_lone_2nd_surrogate.json ================================================ {"\uDFAA":0} ================================================ FILE: data/parsing/i_string_1st_surrogate_but_2nd_missing.json ================================================ ["\uDADA"] ================================================ FILE: data/parsing/i_string_1st_valid_surrogate_2nd_invalid.json ================================================ ["\uD888\u1234"] ================================================ FILE: data/parsing/i_string_UTF-8_invalid_sequence.json ================================================ ["日ш"] ================================================ FILE: data/parsing/i_string_UTF8_surrogate_U+D800.json ================================================ [""] ================================================ FILE: data/parsing/i_string_incomplete_surrogate_and_escape_valid.json ================================================ ["\uD800\n"] ================================================ FILE: data/parsing/i_string_incomplete_surrogate_pair.json ================================================ ["\uDd1ea"] ================================================ FILE: data/parsing/i_string_incomplete_surrogates_escape_valid.json ================================================ ["\uD800\uD800\n"] ================================================ FILE: data/parsing/i_string_invalid_lonely_surrogate.json ================================================ ["\ud800"] ================================================ FILE: data/parsing/i_string_invalid_surrogate.json ================================================ ["\ud800abc"] ================================================ FILE: data/parsing/i_string_invalid_utf-8.json ================================================ [""] ================================================ FILE: data/parsing/i_string_inverted_surrogates_U+1D11E.json ================================================ ["\uDd1e\uD834"] ================================================ FILE: data/parsing/i_string_iso_latin_1.json ================================================ [""] ================================================ FILE: data/parsing/i_string_lone_second_surrogate.json ================================================ ["\uDFAA"] ================================================ FILE: data/parsing/i_string_lone_utf8_continuation_byte.json ================================================ [""] ================================================ FILE: data/parsing/i_string_not_in_unicode_range.json ================================================ [""] ================================================ FILE: data/parsing/i_string_overlong_sequence_2_bytes.json ================================================ [""] ================================================ FILE: data/parsing/i_string_overlong_sequence_6_bytes.json ================================================ [""] ================================================ FILE: data/parsing/i_string_overlong_sequence_6_bytes_null.json ================================================ [""] ================================================ FILE: data/parsing/i_string_truncated-utf-8.json ================================================ [""] ================================================ FILE: data/parsing/i_structure_UTF-8_BOM_empty_object.json ================================================ {} ================================================ FILE: data/parsing/n_array_1_true_without_comma.json ================================================ [1 true] ================================================ FILE: data/parsing/n_array_a_invalid_utf8.json ================================================ [a] ================================================ FILE: data/parsing/n_array_colon_instead_of_comma.json ================================================ ["": 1] ================================================ FILE: data/parsing/n_array_comma_after_close.json ================================================ [""], ================================================ FILE: data/parsing/n_array_comma_and_number.json ================================================ [,1] ================================================ FILE: data/parsing/n_array_double_comma.json ================================================ [1,,2] ================================================ FILE: data/parsing/n_array_double_extra_comma.json ================================================ ["x",,] ================================================ FILE: data/parsing/n_array_extra_close.json ================================================ ["x"]] ================================================ FILE: data/parsing/n_array_extra_comma.json ================================================ ["",] ================================================ FILE: data/parsing/n_array_incomplete.json ================================================ ["x" ================================================ FILE: data/parsing/n_array_incomplete_invalid_value.json ================================================ [x ================================================ FILE: data/parsing/n_array_inner_array_no_comma.json ================================================ [3[4]] ================================================ FILE: data/parsing/n_array_invalid_utf8.json ================================================ [] ================================================ FILE: data/parsing/n_array_items_separated_by_semicolon.json ================================================ [1:2] ================================================ FILE: data/parsing/n_array_just_comma.json ================================================ [,] ================================================ FILE: data/parsing/n_array_just_minus.json ================================================ [-] ================================================ FILE: data/parsing/n_array_missing_value.json ================================================ [ , ""] ================================================ FILE: data/parsing/n_array_newlines_unclosed.json ================================================ ["a", 4 ,1, ================================================ FILE: data/parsing/n_array_number_and_comma.json ================================================ [1,] ================================================ FILE: data/parsing/n_array_number_and_several_commas.json ================================================ [1,,] ================================================ FILE: data/parsing/n_array_spaces_vertical_tab_formfeed.json ================================================ [" a"\f] ================================================ FILE: data/parsing/n_array_star_inside.json ================================================ [*] ================================================ FILE: data/parsing/n_array_unclosed.json ================================================ ["" ================================================ FILE: data/parsing/n_array_unclosed_trailing_comma.json ================================================ [1, ================================================ FILE: data/parsing/n_array_unclosed_with_new_lines.json ================================================ [1, 1 ,1 ================================================ FILE: data/parsing/n_array_unclosed_with_object_inside.json ================================================ [{} ================================================ FILE: data/parsing/n_incomplete_false.json ================================================ [fals] ================================================ FILE: data/parsing/n_incomplete_null.json ================================================ [nul] ================================================ FILE: data/parsing/n_incomplete_true.json ================================================ [tru] ================================================ FILE: data/parsing/n_number_++.json ================================================ [++1234] ================================================ FILE: data/parsing/n_number_+1.json ================================================ [+1] ================================================ FILE: data/parsing/n_number_+Inf.json ================================================ [+Inf] ================================================ FILE: data/parsing/n_number_-01.json ================================================ [-01] ================================================ FILE: data/parsing/n_number_-1.0..json ================================================ [-1.0.] ================================================ FILE: data/parsing/n_number_-2..json ================================================ [-2.] ================================================ FILE: data/parsing/n_number_-NaN.json ================================================ [-NaN] ================================================ FILE: data/parsing/n_number_.-1.json ================================================ [.-1] ================================================ FILE: data/parsing/n_number_.2e-3.json ================================================ [.2e-3] ================================================ FILE: data/parsing/n_number_0.1.2.json ================================================ [0.1.2] ================================================ FILE: data/parsing/n_number_0.3e+.json ================================================ [0.3e+] ================================================ FILE: data/parsing/n_number_0.3e.json ================================================ [0.3e] ================================================ FILE: data/parsing/n_number_0.e1.json ================================================ [0.e1] ================================================ FILE: data/parsing/n_number_0_capital_E+.json ================================================ [0E+] ================================================ FILE: data/parsing/n_number_0_capital_E.json ================================================ [0E] ================================================ FILE: data/parsing/n_number_0e+.json ================================================ [0e+] ================================================ FILE: data/parsing/n_number_0e.json ================================================ [0e] ================================================ FILE: data/parsing/n_number_1.0e+.json ================================================ [1.0e+] ================================================ FILE: data/parsing/n_number_1.0e-.json ================================================ [1.0e-] ================================================ FILE: data/parsing/n_number_1.0e.json ================================================ [1.0e] ================================================ FILE: data/parsing/n_number_1_000.json ================================================ [1 000.0] ================================================ FILE: data/parsing/n_number_1eE2.json ================================================ [1eE2] ================================================ FILE: data/parsing/n_number_2.e+3.json ================================================ [2.e+3] ================================================ FILE: data/parsing/n_number_2.e-3.json ================================================ [2.e-3] ================================================ FILE: data/parsing/n_number_2.e3.json ================================================ [2.e3] ================================================ FILE: data/parsing/n_number_9.e+.json ================================================ [9.e+] ================================================ FILE: data/parsing/n_number_Inf.json ================================================ [Inf] ================================================ FILE: data/parsing/n_number_NaN.json ================================================ [NaN] ================================================ FILE: data/parsing/n_number_U+FF11_fullwidth_digit_one.json ================================================ [1] ================================================ FILE: data/parsing/n_number_expression.json ================================================ [1+2] ================================================ FILE: data/parsing/n_number_hex_1_digit.json ================================================ [0x1] ================================================ FILE: data/parsing/n_number_hex_2_digits.json ================================================ [0x42] ================================================ FILE: data/parsing/n_number_infinity.json ================================================ [Infinity] ================================================ FILE: data/parsing/n_number_invalid+-.json ================================================ [0e+-1] ================================================ FILE: data/parsing/n_number_invalid-negative-real.json ================================================ [-123.123foo] ================================================ FILE: data/parsing/n_number_invalid-utf-8-in-bigger-int.json ================================================ [123] ================================================ FILE: data/parsing/n_number_invalid-utf-8-in-exponent.json ================================================ [1e1] ================================================ FILE: data/parsing/n_number_invalid-utf-8-in-int.json ================================================ [0] ================================================ FILE: data/parsing/n_number_minus_infinity.json ================================================ [-Infinity] ================================================ FILE: data/parsing/n_number_minus_sign_with_trailing_garbage.json ================================================ [-foo] ================================================ FILE: data/parsing/n_number_minus_space_1.json ================================================ [- 1] ================================================ FILE: data/parsing/n_number_neg_int_starting_with_zero.json ================================================ [-012] ================================================ FILE: data/parsing/n_number_neg_real_without_int_part.json ================================================ [-.123] ================================================ FILE: data/parsing/n_number_neg_with_garbage_at_end.json ================================================ [-1x] ================================================ FILE: data/parsing/n_number_real_garbage_after_e.json ================================================ [1ea] ================================================ FILE: data/parsing/n_number_real_with_invalid_utf8_after_e.json ================================================ [1e] ================================================ FILE: data/parsing/n_number_real_without_fractional_part.json ================================================ [1.] ================================================ FILE: data/parsing/n_number_starting_with_dot.json ================================================ [.123] ================================================ FILE: data/parsing/n_number_with_alpha.json ================================================ [1.2a-3] ================================================ FILE: data/parsing/n_number_with_alpha_char.json ================================================ [1.8011670033376514H-308] ================================================ FILE: data/parsing/n_number_with_leading_zero.json ================================================ [012] ================================================ FILE: data/parsing/n_object_bad_value.json ================================================ ["x", truth] ================================================ FILE: data/parsing/n_object_bracket_key.json ================================================ {[: "x"} ================================================ FILE: data/parsing/n_object_comma_instead_of_colon.json ================================================ {"x", null} ================================================ FILE: data/parsing/n_object_double_colon.json ================================================ {"x"::"b"} ================================================ FILE: data/parsing/n_object_emoji.json ================================================ {🇨🇭} ================================================ FILE: data/parsing/n_object_garbage_at_end.json ================================================ {"a":"a" 123} ================================================ FILE: data/parsing/n_object_key_with_single_quotes.json ================================================ {key: 'value'} ================================================ FILE: data/parsing/n_object_lone_continuation_byte_in_key_and_trailing_comma.json ================================================ {"":"0",} ================================================ FILE: data/parsing/n_object_missing_colon.json ================================================ {"a" b} ================================================ FILE: data/parsing/n_object_missing_key.json ================================================ {:"b"} ================================================ FILE: data/parsing/n_object_missing_semicolon.json ================================================ {"a" "b"} ================================================ FILE: data/parsing/n_object_missing_value.json ================================================ {"a": ================================================ FILE: data/parsing/n_object_no-colon.json ================================================ {"a" ================================================ FILE: data/parsing/n_object_non_string_key.json ================================================ {1:1} ================================================ FILE: data/parsing/n_object_non_string_key_but_huge_number_instead.json ================================================ {9999E9999:1} ================================================ FILE: data/parsing/n_object_repeated_null_null.json ================================================ {null:null,null:null} ================================================ FILE: data/parsing/n_object_several_trailing_commas.json ================================================ {"id":0,,,,,} ================================================ FILE: data/parsing/n_object_single_quote.json ================================================ {'a':0} ================================================ FILE: data/parsing/n_object_trailing_comma.json ================================================ {"id":0,} ================================================ FILE: data/parsing/n_object_trailing_comment.json ================================================ {"a":"b"}/**/ ================================================ FILE: data/parsing/n_object_trailing_comment_open.json ================================================ {"a":"b"}/**// ================================================ FILE: data/parsing/n_object_trailing_comment_slash_open.json ================================================ {"a":"b"}// ================================================ FILE: data/parsing/n_object_trailing_comment_slash_open_incomplete.json ================================================ {"a":"b"}/ ================================================ FILE: data/parsing/n_object_two_commas_in_a_row.json ================================================ {"a":"b",,"c":"d"} ================================================ FILE: data/parsing/n_object_unquoted_key.json ================================================ {a: "b"} ================================================ FILE: data/parsing/n_object_unterminated-value.json ================================================ {"a":"a ================================================ FILE: data/parsing/n_object_with_single_string.json ================================================ { "foo" : "bar", "a" } ================================================ FILE: data/parsing/n_object_with_trailing_garbage.json ================================================ {"a":"b"}# ================================================ FILE: data/parsing/n_single_space.json ================================================ ================================================ FILE: data/parsing/n_string_1_surrogate_then_escape.json ================================================ ["\uD800\"] ================================================ FILE: data/parsing/n_string_1_surrogate_then_escape_u.json ================================================ ["\uD800\u"] ================================================ FILE: data/parsing/n_string_1_surrogate_then_escape_u1.json ================================================ ["\uD800\u1"] ================================================ FILE: data/parsing/n_string_1_surrogate_then_escape_u1x.json ================================================ ["\uD800\u1x"] ================================================ FILE: data/parsing/n_string_accentuated_char_no_quotes.json ================================================ [é] ================================================ FILE: data/parsing/n_string_escape_x.json ================================================ ["\x00"] ================================================ FILE: data/parsing/n_string_escaped_backslash_bad.json ================================================ ["\\\"] ================================================ FILE: data/parsing/n_string_escaped_ctrl_char_tab.json ================================================ ["\ "] ================================================ FILE: data/parsing/n_string_escaped_emoji.json ================================================ ["\🌀"] ================================================ FILE: data/parsing/n_string_incomplete_escape.json ================================================ ["\"] ================================================ FILE: data/parsing/n_string_incomplete_escaped_character.json ================================================ ["\u00A"] ================================================ FILE: data/parsing/n_string_incomplete_surrogate.json ================================================ ["\uD834\uDd"] ================================================ FILE: data/parsing/n_string_incomplete_surrogate_escape_invalid.json ================================================ ["\uD800\uD800\x"] ================================================ FILE: data/parsing/n_string_invalid-utf-8-in-escape.json ================================================ ["\u"] ================================================ FILE: data/parsing/n_string_invalid_backslash_esc.json ================================================ ["\a"] ================================================ FILE: data/parsing/n_string_invalid_unicode_escape.json ================================================ ["\uqqqq"] ================================================ FILE: data/parsing/n_string_invalid_utf8_after_escape.json ================================================ ["\"] ================================================ FILE: data/parsing/n_string_leading_uescaped_thinspace.json ================================================ [\u0020"asd"] ================================================ FILE: data/parsing/n_string_no_quotes_with_bad_escape.json ================================================ [\n] ================================================ FILE: data/parsing/n_string_single_doublequote.json ================================================ " ================================================ FILE: data/parsing/n_string_single_quote.json ================================================ ['single quote'] ================================================ FILE: data/parsing/n_string_single_string_no_double_quotes.json ================================================ abc ================================================ FILE: data/parsing/n_string_start_escape_unclosed.json ================================================ ["\ ================================================ FILE: data/parsing/n_string_unescaped_newline.json ================================================ ["new line"] ================================================ FILE: data/parsing/n_string_unescaped_tab.json ================================================ [" "] ================================================ FILE: data/parsing/n_string_unicode_CapitalU.json ================================================ "\UA66D" ================================================ FILE: data/parsing/n_string_with_trailing_garbage.json ================================================ ""x ================================================ FILE: data/parsing/n_structure_U+2060_word_joined.json ================================================ [⁠] ================================================ FILE: data/parsing/n_structure_UTF8_BOM_no_data.json ================================================  ================================================ FILE: data/parsing/n_structure_angle_bracket_..json ================================================ <.> ================================================ FILE: data/parsing/n_structure_angle_bracket_null.json ================================================ [] ================================================ FILE: data/parsing/n_structure_array_trailing_garbage.json ================================================ [1]x ================================================ FILE: data/parsing/n_structure_array_with_extra_array_close.json ================================================ [1]] ================================================ FILE: data/parsing/n_structure_array_with_unclosed_string.json ================================================ ["asd] ================================================ FILE: data/parsing/n_structure_ascii-unicode-identifier.json ================================================ aå ================================================ FILE: data/parsing/n_structure_capitalized_True.json ================================================ [True] ================================================ FILE: data/parsing/n_structure_close_unopened_array.json ================================================ 1] ================================================ FILE: data/parsing/n_structure_comma_instead_of_closing_brace.json ================================================ {"x": true, ================================================ FILE: data/parsing/n_structure_double_array.json ================================================ [][] ================================================ FILE: data/parsing/n_structure_end_array.json ================================================ ] ================================================ FILE: data/parsing/n_structure_incomplete_UTF8_BOM.json ================================================ {} ================================================ FILE: data/parsing/n_structure_lone-invalid-utf-8.json ================================================ ================================================ FILE: data/parsing/n_structure_lone-open-bracket.json ================================================ [ ================================================ FILE: data/parsing/n_structure_no_data.json ================================================ ================================================ FILE: data/parsing/n_structure_number_with_trailing_garbage.json ================================================ 2@ ================================================ FILE: data/parsing/n_structure_object_followed_by_closing_object.json ================================================ {}} ================================================ FILE: data/parsing/n_structure_object_unclosed_no_value.json ================================================ {"": ================================================ FILE: data/parsing/n_structure_object_with_comment.json ================================================ {"a":/*comment*/"b"} ================================================ FILE: data/parsing/n_structure_object_with_trailing_garbage.json ================================================ {"a": true} "x" ================================================ FILE: data/parsing/n_structure_open_array_apostrophe.json ================================================ [' ================================================ FILE: data/parsing/n_structure_open_array_comma.json ================================================ [, ================================================ FILE: data/parsing/n_structure_open_array_open_object.json ================================================ [{ ================================================ FILE: data/parsing/n_structure_open_array_open_string.json ================================================ ["a ================================================ FILE: data/parsing/n_structure_open_array_string.json ================================================ ["a" ================================================ FILE: data/parsing/n_structure_open_object.json ================================================ { ================================================ FILE: data/parsing/n_structure_open_object_close_array.json ================================================ {] ================================================ FILE: data/parsing/n_structure_open_object_comma.json ================================================ {, ================================================ FILE: data/parsing/n_structure_open_object_open_array.json ================================================ {[ ================================================ FILE: data/parsing/n_structure_open_object_open_string.json ================================================ {"a ================================================ FILE: data/parsing/n_structure_open_object_string_with_apostrophes.json ================================================ {'a' ================================================ FILE: data/parsing/n_structure_open_open.json ================================================ ["\{["\{["\{["\{ ================================================ FILE: data/parsing/n_structure_single_eacute.json ================================================ ================================================ FILE: data/parsing/n_structure_single_star.json ================================================ * ================================================ FILE: data/parsing/n_structure_trailing_#.json ================================================ {"a":"b"}#{} ================================================ FILE: data/parsing/n_structure_uescaped_LF_before_string.json ================================================ [\u000A""] ================================================ FILE: data/parsing/n_structure_unclosed_array.json ================================================ [1 ================================================ FILE: data/parsing/n_structure_unclosed_array_partial_null.json ================================================ [ false, nul ================================================ FILE: data/parsing/n_structure_unclosed_array_unfinished_false.json ================================================ [ true, fals ================================================ FILE: data/parsing/n_structure_unclosed_array_unfinished_true.json ================================================ [ false, tru ================================================ FILE: data/parsing/n_structure_unclosed_object.json ================================================ {"asd":"asd" ================================================ FILE: data/parsing/n_structure_unicode-identifier.json ================================================ å ================================================ FILE: data/parsing/n_structure_whitespace_U+2060_word_joiner.json ================================================ [⁠] ================================================ FILE: data/parsing/n_structure_whitespace_formfeed.json ================================================ [ ] ================================================ FILE: data/parsing/y_array_arraysWithSpaces.json ================================================ [[] ] ================================================ FILE: data/parsing/y_array_empty-string.json ================================================ [""] ================================================ FILE: data/parsing/y_array_empty.json ================================================ [] ================================================ FILE: data/parsing/y_array_ending_with_newline.json ================================================ ["a"] ================================================ FILE: data/parsing/y_array_false.json ================================================ [false] ================================================ FILE: data/parsing/y_array_heterogeneous.json ================================================ [null, 1, "1", {}] ================================================ FILE: data/parsing/y_array_null.json ================================================ [null] ================================================ FILE: data/parsing/y_array_with_1_and_newline.json ================================================ [1 ] ================================================ FILE: data/parsing/y_array_with_leading_space.json ================================================ [1] ================================================ FILE: data/parsing/y_array_with_several_null.json ================================================ [1,null,null,null,2] ================================================ FILE: data/parsing/y_array_with_trailing_space.json ================================================ [2] ================================================ FILE: data/parsing/y_number.json ================================================ [123e65] ================================================ FILE: data/parsing/y_number_0e+1.json ================================================ [0e+1] ================================================ FILE: data/parsing/y_number_0e1.json ================================================ [0e1] ================================================ FILE: data/parsing/y_number_after_space.json ================================================ [ 4] ================================================ FILE: data/parsing/y_number_double_close_to_zero.json ================================================ [-0.000000000000000000000000000000000000000000000000000000000000000000000000000001] ================================================ FILE: data/parsing/y_number_int_with_exp.json ================================================ [20e1] ================================================ FILE: data/parsing/y_number_minus_zero.json ================================================ [-0] ================================================ FILE: data/parsing/y_number_negative_int.json ================================================ [-123] ================================================ FILE: data/parsing/y_number_negative_one.json ================================================ [-1] ================================================ FILE: data/parsing/y_number_negative_zero.json ================================================ [-0] ================================================ FILE: data/parsing/y_number_real_capital_e.json ================================================ [1E22] ================================================ FILE: data/parsing/y_number_real_capital_e_neg_exp.json ================================================ [1E-2] ================================================ FILE: data/parsing/y_number_real_capital_e_pos_exp.json ================================================ [1E+2] ================================================ FILE: data/parsing/y_number_real_exponent.json ================================================ [123e45] ================================================ FILE: data/parsing/y_number_real_fraction_exponent.json ================================================ [123.456e78] ================================================ FILE: data/parsing/y_number_real_neg_exp.json ================================================ [1e-2] ================================================ FILE: data/parsing/y_number_real_pos_exponent.json ================================================ [1e+2] ================================================ FILE: data/parsing/y_number_simple_int.json ================================================ [123] ================================================ FILE: data/parsing/y_number_simple_real.json ================================================ [123.456789] ================================================ FILE: data/parsing/y_object.json ================================================ {"asd":"sdf", "dfg":"fgh"} ================================================ FILE: data/parsing/y_object_basic.json ================================================ {"asd":"sdf"} ================================================ FILE: data/parsing/y_object_duplicated_key.json ================================================ {"a":"b","a":"c"} ================================================ FILE: data/parsing/y_object_duplicated_key_and_value.json ================================================ {"a":"b","a":"b"} ================================================ FILE: data/parsing/y_object_empty.json ================================================ {} ================================================ FILE: data/parsing/y_object_empty_key.json ================================================ {"":0} ================================================ FILE: data/parsing/y_object_escaped_null_in_key.json ================================================ {"foo\u0000bar": 42} ================================================ FILE: data/parsing/y_object_extreme_numbers.json ================================================ { "min": -1.0e+28, "max": 1.0e+28 } ================================================ FILE: data/parsing/y_object_long_strings.json ================================================ {"x":[{"id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}], "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"} ================================================ FILE: data/parsing/y_object_simple.json ================================================ {"a":[]} ================================================ FILE: data/parsing/y_object_string_unicode.json ================================================ {"title":"\u041f\u043e\u043b\u0442\u043e\u0440\u0430 \u0417\u0435\u043c\u043b\u0435\u043a\u043e\u043f\u0430" } ================================================ FILE: data/parsing/y_object_with_newlines.json ================================================ { "a": "b" } ================================================ FILE: data/parsing/y_string_1_2_3_bytes_UTF-8_sequences.json ================================================ ["\u0060\u012a\u12AB"] ================================================ FILE: data/parsing/y_string_accepted_surrogate_pair.json ================================================ ["\uD801\udc37"] ================================================ FILE: data/parsing/y_string_accepted_surrogate_pairs.json ================================================ ["\ud83d\ude39\ud83d\udc8d"] ================================================ FILE: data/parsing/y_string_allowed_escapes.json ================================================ ["\"\\\/\b\f\n\r\t"] ================================================ FILE: data/parsing/y_string_backslash_and_u_escaped_zero.json ================================================ ["\\u0000"] ================================================ FILE: data/parsing/y_string_backslash_doublequotes.json ================================================ ["\""] ================================================ FILE: data/parsing/y_string_comments.json ================================================ ["a/*b*/c/*d//e"] ================================================ FILE: data/parsing/y_string_double_escape_a.json ================================================ ["\\a"] ================================================ FILE: data/parsing/y_string_double_escape_n.json ================================================ ["\\n"] ================================================ FILE: data/parsing/y_string_escaped_control_character.json ================================================ ["\u0012"] ================================================ FILE: data/parsing/y_string_escaped_noncharacter.json ================================================ ["\uFFFF"] ================================================ FILE: data/parsing/y_string_in_array.json ================================================ ["asd"] ================================================ FILE: data/parsing/y_string_in_array_with_leading_space.json ================================================ [ "asd"] ================================================ FILE: data/parsing/y_string_last_surrogates_1_and_2.json ================================================ ["\uDBFF\uDFFF"] ================================================ FILE: data/parsing/y_string_nbsp_uescaped.json ================================================ ["new\u00A0line"] ================================================ FILE: data/parsing/y_string_nonCharacterInUTF-8_U+10FFFF.json ================================================ ["􏿿"] ================================================ FILE: data/parsing/y_string_nonCharacterInUTF-8_U+FFFF.json ================================================ ["￿"] ================================================ FILE: data/parsing/y_string_null_escape.json ================================================ ["\u0000"] ================================================ FILE: data/parsing/y_string_one-byte-utf-8.json ================================================ ["\u002c"] ================================================ FILE: data/parsing/y_string_pi.json ================================================ ["π"] ================================================ FILE: data/parsing/y_string_reservedCharacterInUTF-8_U+1BFFF.json ================================================ ["𛿿"] ================================================ FILE: data/parsing/y_string_simple_ascii.json ================================================ ["asd "] ================================================ FILE: data/parsing/y_string_space.json ================================================ " " ================================================ FILE: data/parsing/y_string_surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF.json ================================================ ["\uD834\uDd1e"] ================================================ FILE: data/parsing/y_string_three-byte-utf-8.json ================================================ ["\u0821"] ================================================ FILE: data/parsing/y_string_two-byte-utf-8.json ================================================ ["\u0123"] ================================================ FILE: data/parsing/y_string_u+2028_line_sep.json ================================================ ["
"] ================================================ FILE: data/parsing/y_string_u+2029_par_sep.json ================================================ ["
"] ================================================ FILE: data/parsing/y_string_uEscape.json ================================================ ["\u0061\u30af\u30EA\u30b9"] ================================================ FILE: data/parsing/y_string_uescaped_newline.json ================================================ ["new\u000Aline"] ================================================ FILE: data/parsing/y_string_unescaped_char_delete.json ================================================ [""] ================================================ FILE: data/parsing/y_string_unicode.json ================================================ ["\uA66D"] ================================================ FILE: data/parsing/y_string_unicodeEscapedBackslash.json ================================================ ["\u005C"] ================================================ FILE: data/parsing/y_string_unicode_2.json ================================================ ["⍂㈴⍂"] ================================================ FILE: data/parsing/y_string_unicode_U+10FFFE_nonchar.json ================================================ ["\uDBFF\uDFFE"] ================================================ FILE: data/parsing/y_string_unicode_U+1FFFE_nonchar.json ================================================ ["\uD83F\uDFFE"] ================================================ FILE: data/parsing/y_string_unicode_U+200B_ZERO_WIDTH_SPACE.json ================================================ ["\u200B"] ================================================ FILE: data/parsing/y_string_unicode_U+2064_invisible_plus.json ================================================ ["\u2064"] ================================================ FILE: data/parsing/y_string_unicode_U+FDD0_nonchar.json ================================================ ["\uFDD0"] ================================================ FILE: data/parsing/y_string_unicode_U+FFFE_nonchar.json ================================================ ["\uFFFE"] ================================================ FILE: data/parsing/y_string_unicode_escaped_double_quote.json ================================================ ["\u0022"] ================================================ FILE: data/parsing/y_string_utf8.json ================================================ ["€𝄞"] ================================================ FILE: data/parsing/y_string_with_del_character.json ================================================ ["aa"] ================================================ FILE: data/parsing/y_structure_lonely_false.json ================================================ false ================================================ FILE: data/parsing/y_structure_lonely_int.json ================================================ 42 ================================================ FILE: data/parsing/y_structure_lonely_negative_real.json ================================================ -0.1 ================================================ FILE: data/parsing/y_structure_lonely_null.json ================================================ null ================================================ FILE: data/parsing/y_structure_lonely_string.json ================================================ "asd" ================================================ FILE: data/parsing/y_structure_lonely_true.json ================================================ true ================================================ FILE: data/parsing/y_structure_string_empty.json ================================================ "" ================================================ FILE: data/parsing/y_structure_trailing_newline.json ================================================ ["a"] ================================================ FILE: data/parsing/y_structure_true_in_array.json ================================================ [true] ================================================ FILE: data/parsing/y_structure_whitespace_array.json ================================================ [] ================================================ FILE: data/roundtrip/roundtrip01.json ================================================ [null] ================================================ FILE: data/roundtrip/roundtrip02.json ================================================ [true] ================================================ FILE: data/roundtrip/roundtrip03.json ================================================ [false] ================================================ FILE: data/roundtrip/roundtrip04.json ================================================ [0] ================================================ FILE: data/roundtrip/roundtrip05.json ================================================ ["foo"] ================================================ FILE: data/roundtrip/roundtrip06.json ================================================ [] ================================================ FILE: data/roundtrip/roundtrip07.json ================================================ {} ================================================ FILE: data/roundtrip/roundtrip08.json ================================================ [0,1] ================================================ FILE: data/roundtrip/roundtrip09.json ================================================ {"foo":"bar"} ================================================ FILE: data/roundtrip/roundtrip10.json ================================================ {"a":null,"foo":"bar"} ================================================ FILE: data/roundtrip/roundtrip11.json ================================================ [-1] ================================================ FILE: data/roundtrip/roundtrip12.json ================================================ [-2147483648] ================================================ FILE: data/roundtrip/roundtrip13.json ================================================ [-1234567890123456789] ================================================ FILE: data/roundtrip/roundtrip14.json ================================================ [-9223372036854775808] ================================================ FILE: data/roundtrip/roundtrip15.json ================================================ [1] ================================================ FILE: data/roundtrip/roundtrip16.json ================================================ [2147483647] ================================================ FILE: data/roundtrip/roundtrip17.json ================================================ [4294967295] ================================================ FILE: data/roundtrip/roundtrip18.json ================================================ [1234567890123456789] ================================================ FILE: data/roundtrip/roundtrip19.json ================================================ [9223372036854775807] ================================================ FILE: data/roundtrip/roundtrip20.json ================================================ [0.0] ================================================ FILE: data/roundtrip/roundtrip21.json ================================================ [-0.0] ================================================ FILE: data/roundtrip/roundtrip22.json ================================================ [1.2345] ================================================ FILE: data/roundtrip/roundtrip23.json ================================================ [-1.2345] ================================================ FILE: data/roundtrip/roundtrip24.json ================================================ [5e-324] ================================================ FILE: data/roundtrip/roundtrip25.json ================================================ [2.225073858507201e-308] ================================================ FILE: data/roundtrip/roundtrip26.json ================================================ [2.2250738585072014e-308] ================================================ FILE: data/roundtrip/roundtrip27.json ================================================ [1.7976931348623157e308] ================================================ FILE: data/transform/number_1.0.json ================================================ [1.0] ================================================ FILE: data/transform/number_1.000000000000000005.json ================================================ [1.000000000000000005] ================================================ FILE: data/transform/number_1000000000000000.json ================================================ [1000000000000000] ================================================ FILE: data/transform/number_10000000000000000999.json ================================================ [10000000000000000999] ================================================ FILE: data/transform/number_1e-999.json ================================================ [1E-999] ================================================ FILE: data/transform/number_1e6.json ================================================ [1E6] ================================================ FILE: data/transform/object_key_nfc_nfd.json ================================================ {"é":"NFC","é":"NFD"} ================================================ FILE: data/transform/object_key_nfd_nfc.json ================================================ {"é":"NFD","é":"NFC"} ================================================ FILE: data/transform/object_same_key_different_values.json ================================================ {"a":1,"a":2} ================================================ FILE: data/transform/object_same_key_same_value.json ================================================ {"a":1,"a":1} ================================================ FILE: data/transform/object_same_key_unclear_values.json ================================================ {"a":0, "a":-0} ================================================ FILE: data/transform/string_1_escaped_invalid_codepoint.json ================================================ ["\uD800"] ================================================ FILE: data/transform/string_1_invalid_codepoint.json ================================================ [""] ================================================ FILE: data/transform/string_2_escaped_invalid_codepoints.json ================================================ ["\uD800\uD800"] ================================================ FILE: data/transform/string_2_invalid_codepoints.json ================================================ [""] ================================================ FILE: data/transform/string_3_escaped_invalid_codepoints.json ================================================ ["\uD800\uD800\uD800"] ================================================ FILE: data/transform/string_3_invalid_codepoints.json ================================================ [""] ================================================ FILE: data/transform/string_with_escaped_NULL.json ================================================ ["A\u0000B"] ================================================ FILE: include/yyjson/yyjson.c ================================================ /*============================================================================== Copyright (c) 2020 YaoYuan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *============================================================================*/ #include "yyjson.h" #include /*============================================================================== * Warning Suppress *============================================================================*/ #if defined(__clang__) # pragma clang diagnostic ignored "-Wunused-function" # pragma clang diagnostic ignored "-Wunused-parameter" # pragma clang diagnostic ignored "-Wunused-label" # pragma clang diagnostic ignored "-Wunused-macros" # pragma clang diagnostic ignored "-Wunused-variable" #elif defined(__GNUC__) # pragma GCC diagnostic ignored "-Wunused-function" # pragma GCC diagnostic ignored "-Wunused-parameter" # pragma GCC diagnostic ignored "-Wunused-label" # pragma GCC diagnostic ignored "-Wunused-macros" # pragma GCC diagnostic ignored "-Wunused-variable" #elif defined(_MSC_VER) # pragma warning(disable:4100) /* unreferenced formal parameter */ # pragma warning(disable:4101) /* unreferenced variable */ # pragma warning(disable:4102) /* unreferenced label */ # pragma warning(disable:4127) /* conditional expression is constant */ # pragma warning(disable:4706) /* assignment within conditional expression */ #endif /*============================================================================== * Version *============================================================================*/ uint32_t yyjson_version(void) { return YYJSON_VERSION_HEX; } /*============================================================================== * Flags *============================================================================*/ /* msvc intrinsic */ #if YYJSON_MSC_VER >= 1400 # include # if defined(_M_AMD64) || defined(_M_ARM64) # define MSC_HAS_BIT_SCAN_64 1 # pragma intrinsic(_BitScanForward64) # pragma intrinsic(_BitScanReverse64) # else # define MSC_HAS_BIT_SCAN_64 0 # endif # if defined(_M_AMD64) || defined(_M_ARM64) || \ defined(_M_IX86) || defined(_M_ARM) # define MSC_HAS_BIT_SCAN 1 # pragma intrinsic(_BitScanForward) # pragma intrinsic(_BitScanReverse) # else # define MSC_HAS_BIT_SCAN 0 # endif # if defined(_M_AMD64) # define MSC_HAS_UMUL128 1 # pragma intrinsic(_umul128) # else # define MSC_HAS_UMUL128 0 # endif #else # define MSC_HAS_BIT_SCAN_64 0 # define MSC_HAS_BIT_SCAN 0 # define MSC_HAS_UMUL128 0 #endif /* gcc builtin */ #if yyjson_has_builtin(__builtin_clzll) || yyjson_gcc_available(3, 4, 0) # define GCC_HAS_CLZLL 1 #else # define GCC_HAS_CLZLL 0 #endif #if yyjson_has_builtin(__builtin_ctzll) || yyjson_gcc_available(3, 4, 0) # define GCC_HAS_CTZLL 1 #else # define GCC_HAS_CTZLL 0 #endif /* int128 type */ #if defined(__SIZEOF_INT128__) && (__SIZEOF_INT128__ == 16) && \ (defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER)) # define YYJSON_HAS_INT128 1 #else # define YYJSON_HAS_INT128 0 #endif /* IEEE 754 floating-point binary representation */ #if defined(__STDC_IEC_559__) || defined(__STDC_IEC_60559_BFP__) # define YYJSON_HAS_IEEE_754 1 #elif (FLT_RADIX == 2) && (DBL_MANT_DIG == 53) && (DBL_DIG == 15) && \ (DBL_MIN_EXP == -1021) && (DBL_MAX_EXP == 1024) && \ (DBL_MIN_10_EXP == -307) && (DBL_MAX_10_EXP == 308) # define YYJSON_HAS_IEEE_754 1 #else # define YYJSON_HAS_IEEE_754 0 #endif /* Correct rounding in double number computations. On the x86 architecture, some compilers may use x87 FPU instructions for floating-point arithmetic. The x87 FPU loads all floating point number as 80-bit double-extended precision internally, then rounds the result to original precision, which may produce inaccurate results. For a more detailed explanation, see the paper: https://arxiv.org/abs/cs/0701192 Here are some examples of double precision calculation error: 2877.0 / 1e6 == 0.002877, but x87 returns 0.0028770000000000002 43683.0 * 1e21 == 4.3683e25, but x87 returns 4.3683000000000004e25 Here are some examples of compiler flags to generate x87 instructions on x86: clang -m32 -mno-sse gcc/icc -m32 -mfpmath=387 msvc /arch:SSE or /arch:IA32 If we are sure that there's no similar error described above, we can define the YYJSON_DOUBLE_MATH_CORRECT as 1 to enable the fast path calculation. This is not an accurate detection, it's just try to avoid the error at compile-time. An accurate detection can be done at run-time: bool is_double_math_correct(void) { volatile double r = 43683.0; r *= 1e21; return r == 4.3683e25; } See also: utils.h in https://github.com/google/double-conversion/ */ #if !defined(FLT_EVAL_METHOD) && defined(__FLT_EVAL_METHOD__) # define FLT_EVAL_METHOD __FLT_EVAL_METHOD__ #endif #if defined(FLT_EVAL_METHOD) && FLT_EVAL_METHOD != 0 && FLT_EVAL_METHOD != 1 # define YYJSON_DOUBLE_MATH_CORRECT 0 #elif defined(i386) || defined(__i386) || defined(__i386__) || \ defined(_X86_) || defined(__X86__) || defined(_M_IX86) || \ defined(__I86__) || defined(__IA32__) || defined(__THW_INTEL) # if (defined(_MSC_VER) && defined(_M_IX86_FP) && _M_IX86_FP == 2) || \ (defined(__SSE2_MATH__) && __SSE2_MATH__) # define YYJSON_DOUBLE_MATH_CORRECT 1 # else # define YYJSON_DOUBLE_MATH_CORRECT 0 # endif #elif defined(__mc68000__) || defined(__pnacl__) || defined(__native_client__) # define YYJSON_DOUBLE_MATH_CORRECT 0 #else # define YYJSON_DOUBLE_MATH_CORRECT 1 #endif /* endian */ #if yyjson_has_include() # include /* POSIX */ #endif #if yyjson_has_include() # include /* Linux */ #elif yyjson_has_include() # include /* BSD, Android */ #elif yyjson_has_include() # include /* BSD, Darwin */ #endif #define YYJSON_BIG_ENDIAN 4321 #define YYJSON_LITTLE_ENDIAN 1234 #if defined(BYTE_ORDER) && BYTE_ORDER # if defined(BIG_ENDIAN) && (BYTE_ORDER == BIG_ENDIAN) # define YYJSON_ENDIAN YYJSON_BIG_ENDIAN # elif defined(LITTLE_ENDIAN) && (BYTE_ORDER == LITTLE_ENDIAN) # define YYJSON_ENDIAN YYJSON_LITTLE_ENDIAN # endif #elif defined(__BYTE_ORDER) && __BYTE_ORDER # if defined(__BIG_ENDIAN) && (__BYTE_ORDER == __BIG_ENDIAN) # define YYJSON_ENDIAN YYJSON_BIG_ENDIAN # elif defined(__LITTLE_ENDIAN) && (__BYTE_ORDER == __LITTLE_ENDIAN) # define YYJSON_ENDIAN YYJSON_LITTLE_ENDIAN # endif #elif defined(__BYTE_ORDER__) && __BYTE_ORDER__ # if defined(__ORDER_BIG_ENDIAN__) && \ (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) # define YYJSON_ENDIAN YYJSON_BIG_ENDIAN # elif defined(__ORDER_LITTLE_ENDIAN__) && \ (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) # define YYJSON_ENDIAN YYJSON_LITTLE_ENDIAN # endif #elif (defined(__LITTLE_ENDIAN__) && __LITTLE_ENDIAN__ == 1) || \ defined(__i386) || defined(__i386__) || \ defined(_X86_) || defined(__X86__) || \ defined(_M_IX86) || defined(__THW_INTEL__) || \ defined(__x86_64) || defined(__x86_64__) || \ defined(__amd64) || defined(__amd64__) || \ defined(_M_AMD64) || defined(_M_X64) || \ defined(_M_ARM) || defined(_M_ARM64) || \ defined(__ARMEL__) || defined(__THUMBEL__) || defined(__AARCH64EL__) || \ defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__) || \ defined(__EMSCRIPTEN__) || defined(__wasm__) || \ defined(__loongarch__) # define YYJSON_ENDIAN YYJSON_LITTLE_ENDIAN #elif (defined(__BIG_ENDIAN__) && __BIG_ENDIAN__ == 1) || \ defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || \ defined(_MIPSEB) || defined(__MIPSEB) || defined(__MIPSEB__) || \ defined(__or1k__) || defined(__OR1K__) # define YYJSON_ENDIAN YYJSON_BIG_ENDIAN #else # define YYJSON_ENDIAN 0 /* unknown endian, detect at run-time */ #endif /* This macro controls how yyjson handles unaligned memory accesses. By default, yyjson uses `memcpy()` for memory copying. This takes advantage of the compiler's automatic optimizations to generate unaligned memory access instructions when the target architecture supports it. However, for some older compilers or architectures where `memcpy()` isn't optimized well and may generate unnecessary function calls, consider defining this macro as 1. In such cases, yyjson switches to manual byte-by-byte access, potentially improving performance. An example of the generated assembly code on the ARM platform can be found here: https://godbolt.org/z/334jjhxPT As this flag has already been enabled for some common architectures in the following code, users typically don't need to manually specify it. If users are unsure about it, please review the generated assembly code or perform actual benchmark to make an informed decision. */ #ifndef YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS # if defined(__ia64) || defined(_IA64) || defined(__IA64__) || \ defined(__ia64__) || defined(_M_IA64) || defined(__itanium__) # define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* Itanium */ # elif (defined(__arm__) || defined(__arm64__) || defined(__aarch64__)) && \ (defined(__GNUC__) || defined(__clang__)) && \ (!defined(__ARM_FEATURE_UNALIGNED) || !__ARM_FEATURE_UNALIGNED) # define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* ARM */ # elif defined(__sparc) || defined(__sparc__) # define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* SPARC */ # elif defined(__mips) || defined(__mips__) || defined(__MIPS__) # define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* MIPS */ # elif defined(__m68k__) || defined(M68000) # define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* M68K */ # else # define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 0 # endif #endif /* Estimated initial ratio of the JSON data (data_size / value_count). For example: data: {"id":12345678,"name":"Harry"} data_size: 30 value_count: 5 ratio: 6 yyjson uses dynamic memory with a growth factor of 1.5 when reading and writing JSON, the ratios below are used to determine the initial memory size. A too large ratio will waste memory, and a too small ratio will cause multiple memory growths and degrade performance. Currently, these ratios are generated with some commonly used JSON datasets. */ #define YYJSON_READER_ESTIMATED_PRETTY_RATIO 16 #define YYJSON_READER_ESTIMATED_MINIFY_RATIO 6 #define YYJSON_WRITER_ESTIMATED_PRETTY_RATIO 32 #define YYJSON_WRITER_ESTIMATED_MINIFY_RATIO 18 /* The initial and maximum size of the memory pool's chunk in yyjson_mut_doc. */ #define YYJSON_MUT_DOC_STR_POOL_INIT_SIZE 0x100 #define YYJSON_MUT_DOC_STR_POOL_MAX_SIZE 0x10000000 #define YYJSON_MUT_DOC_VAL_POOL_INIT_SIZE (0x10 * sizeof(yyjson_mut_val)) #define YYJSON_MUT_DOC_VAL_POOL_MAX_SIZE (0x1000000 * sizeof(yyjson_mut_val)) /* The minimum size of the dynamic allocator's chunk. */ #define YYJSON_ALC_DYN_MIN_SIZE 0x1000 /* Default value for compile-time options. */ #ifndef YYJSON_DISABLE_READER #define YYJSON_DISABLE_READER 0 #endif #ifndef YYJSON_DISABLE_WRITER #define YYJSON_DISABLE_WRITER 0 #endif #ifndef YYJSON_DISABLE_UTILS #define YYJSON_DISABLE_UTILS 0 #endif #ifndef YYJSON_DISABLE_FAST_FP_CONV #define YYJSON_DISABLE_FAST_FP_CONV 0 #endif #ifndef YYJSON_DISABLE_NON_STANDARD #define YYJSON_DISABLE_NON_STANDARD 0 #endif #ifndef YYJSON_DISABLE_UTF8_VALIDATION #define YYJSON_DISABLE_UTF8_VALIDATION 0 #endif #ifndef YYJSON_READER_CONTAINER_RECURSION_LIMIT #define YYJSON_READER_CONTAINER_RECURSION_LIMIT 1024 #endif /*============================================================================== * Macros *============================================================================*/ /* Macros used for loop unrolling and other purpose. */ #define repeat2(x) { x x } #define repeat3(x) { x x x } #define repeat4(x) { x x x x } #define repeat8(x) { x x x x x x x x } #define repeat16(x) { x x x x x x x x x x x x x x x x } #define repeat2_incr(x) { x(0) x(1) } #define repeat4_incr(x) { x(0) x(1) x(2) x(3) } #define repeat8_incr(x) { x(0) x(1) x(2) x(3) x(4) x(5) x(6) x(7) } #define repeat16_incr(x) { x(0) x(1) x(2) x(3) x(4) x(5) x(6) x(7) \ x(8) x(9) x(10) x(11) x(12) x(13) x(14) x(15) } #define repeat_in_1_18(x) { x(1) x(2) x(3) x(4) x(5) x(6) x(7) x(8) \ x(9) x(10) x(11) x(12) x(13) x(14) x(15) x(16) \ x(17) x(18) } /* Macros used to provide branch prediction information for compiler. */ #undef likely #define likely(x) yyjson_likely(x) #undef unlikely #define unlikely(x) yyjson_unlikely(x) /* Macros used to provide inline information for compiler. */ #undef static_inline #define static_inline static yyjson_inline #undef static_noinline #define static_noinline static yyjson_noinline /* Macros for min and max. */ #undef yyjson_min #define yyjson_min(x, y) ((x) < (y) ? (x) : (y)) #undef yyjson_max #define yyjson_max(x, y) ((x) > (y) ? (x) : (y)) /* Used to write u64 literal for C89 which doesn't support "ULL" suffix. */ #undef U64 #define U64(hi, lo) ((((u64)hi##UL) << 32U) + lo##UL) /* Used to cast away (remove) const qualifier. */ #define constcast(type) (type)(void *)(size_t)(const void *) /* flag test */ #define has_read_flag(_flag) false #define has_write_flag(_flag) unlikely(write_flag_eq(flg, YYJSON_WRITE_##_flag)) static_inline bool read_flag_eq(yyjson_read_flag flg, yyjson_read_flag chk) { #if YYJSON_DISABLE_NON_STANDARD if (chk == YYJSON_READ_ALLOW_INF_AND_NAN || chk == YYJSON_READ_ALLOW_COMMENTS || chk == YYJSON_READ_ALLOW_TRAILING_COMMAS || chk == YYJSON_READ_ALLOW_INVALID_UNICODE) return false; /* this should be evaluated at compile-time */ #endif return (flg & chk) != 0; } static_inline bool write_flag_eq(yyjson_write_flag flg, yyjson_write_flag chk) { #if YYJSON_DISABLE_NON_STANDARD if (chk == YYJSON_WRITE_ALLOW_INF_AND_NAN || chk == YYJSON_WRITE_ALLOW_INVALID_UNICODE) return false; /* this should be evaluated at compile-time */ #endif return (flg & chk) != 0; } /*============================================================================== * Integer Constants *============================================================================*/ /* U64 constant values */ #undef U64_MAX #define U64_MAX U64(0xFFFFFFFF, 0xFFFFFFFF) #undef I64_MAX #define I64_MAX U64(0x7FFFFFFF, 0xFFFFFFFF) #undef USIZE_MAX #define USIZE_MAX ((usize)(~(usize)0)) /* Maximum number of digits for reading u32/u64/usize safety (not overflow). */ #undef U32_SAFE_DIG #define U32_SAFE_DIG 9 /* u32 max is 4294967295, 10 digits */ #undef U64_SAFE_DIG #define U64_SAFE_DIG 19 /* u64 max is 18446744073709551615, 20 digits */ #undef USIZE_SAFE_DIG #define USIZE_SAFE_DIG (sizeof(usize) == 8 ? U64_SAFE_DIG : U32_SAFE_DIG) /*============================================================================== * IEEE-754 Double Number Constants *============================================================================*/ /* Inf raw value (positive) */ #define F64_RAW_INF U64(0x7FF00000, 0x00000000) /* NaN raw value (quiet NaN, no payload, no sign) */ #if defined(__hppa__) || (defined(__mips__) && !defined(__mips_nan2008)) #define F64_RAW_NAN U64(0x7FF7FFFF, 0xFFFFFFFF) #else #define F64_RAW_NAN U64(0x7FF80000, 0x00000000) #endif /* double number bits */ #define F64_BITS 64 /* double number exponent part bits */ #define F64_EXP_BITS 11 /* double number significand part bits */ #define F64_SIG_BITS 52 /* double number significand part bits (with 1 hidden bit) */ #define F64_SIG_FULL_BITS 53 /* double number significand bit mask */ #define F64_SIG_MASK U64(0x000FFFFF, 0xFFFFFFFF) /* double number exponent bit mask */ #define F64_EXP_MASK U64(0x7FF00000, 0x00000000) /* double number exponent bias */ #define F64_EXP_BIAS 1023 /* double number significant digits count in decimal */ #define F64_DEC_DIG 17 /* max significant digits count in decimal when reading double number */ #define F64_MAX_DEC_DIG 768 /* maximum decimal power of double number (1.7976931348623157e308) */ #define F64_MAX_DEC_EXP 308 /* minimum decimal power of double number (4.9406564584124654e-324) */ #define F64_MIN_DEC_EXP (-324) /* maximum binary power of double number */ #define F64_MAX_BIN_EXP 1024 /* minimum binary power of double number */ #define F64_MIN_BIN_EXP (-1021) /*============================================================================== * Types *============================================================================*/ /** Type define for primitive types. */ typedef float f32; typedef double f64; typedef int8_t i8; typedef uint8_t u8; typedef int16_t i16; typedef uint16_t u16; typedef int32_t i32; typedef uint32_t u32; typedef int64_t i64; typedef uint64_t u64; typedef size_t usize; /** 128-bit integer, used by floating-point number reader and writer. */ #if YYJSON_HAS_INT128 __extension__ typedef __int128 i128; __extension__ typedef unsigned __int128 u128; #endif /** 16/32/64-bit vector */ typedef struct v16 { char c[2]; } v16; typedef struct v32 { char c[4]; } v32; typedef struct v64 { char c[8]; } v64; /** 16/32/64-bit vector union */ typedef union v16_uni { v16 v; u16 u; } v16_uni; typedef union v32_uni { v32 v; u32 u; } v32_uni; typedef union v64_uni { v64 v; u64 u; } v64_uni; /*============================================================================== * Load/Store Utils *============================================================================*/ #if YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS #define byte_move_idx(x) ((char *)dst)[x] = ((const char *)src)[x]; static_inline void byte_copy_2(void *dst, const void *src) { repeat2_incr(byte_move_idx) } static_inline void byte_copy_4(void *dst, const void *src) { repeat4_incr(byte_move_idx) } static_inline void byte_copy_8(void *dst, const void *src) { repeat8_incr(byte_move_idx) } static_inline void byte_copy_16(void *dst, const void *src) { repeat16_incr(byte_move_idx) } static_inline void byte_move_2(void *dst, const void *src) { repeat2_incr(byte_move_idx) } static_inline void byte_move_4(void *dst, const void *src) { repeat4_incr(byte_move_idx) } static_inline void byte_move_8(void *dst, const void *src) { repeat8_incr(byte_move_idx) } static_inline void byte_move_16(void *dst, const void *src) { repeat16_incr(byte_move_idx) } static_inline bool byte_match_2(void *buf, const char *pat) { return ((char *)buf)[0] == ((const char *)pat)[0] && ((char *)buf)[1] == ((const char *)pat)[1]; } static_inline bool byte_match_4(void *buf, const char *pat) { return ((char *)buf)[0] == ((const char *)pat)[0] && ((char *)buf)[1] == ((const char *)pat)[1] && ((char *)buf)[2] == ((const char *)pat)[2] && ((char *)buf)[3] == ((const char *)pat)[3]; } static_inline u16 byte_load_2(const void *src) { v16_uni uni; uni.v.c[0] = ((const char *)src)[0]; uni.v.c[1] = ((const char *)src)[1]; return uni.u; } static_inline u32 byte_load_3(const void *src) { v32_uni uni; uni.v.c[0] = ((const char *)src)[0]; uni.v.c[1] = ((const char *)src)[1]; uni.v.c[2] = ((const char *)src)[2]; uni.v.c[3] = 0; return uni.u; } static_inline u32 byte_load_4(const void *src) { v32_uni uni; uni.v.c[0] = ((const char *)src)[0]; uni.v.c[1] = ((const char *)src)[1]; uni.v.c[2] = ((const char *)src)[2]; uni.v.c[3] = ((const char *)src)[3]; return uni.u; } #undef byte_move_expr #else static_inline void byte_copy_2(void *dst, const void *src) { memcpy(dst, src, 2); } static_inline void byte_copy_4(void *dst, const void *src) { memcpy(dst, src, 4); } static_inline void byte_copy_8(void *dst, const void *src) { memcpy(dst, src, 8); } static_inline void byte_copy_16(void *dst, const void *src) { memcpy(dst, src, 16); } static_inline void byte_move_2(void *dst, const void *src) { u16 tmp; memcpy(&tmp, src, 2); memcpy(dst, &tmp, 2); } static_inline void byte_move_4(void *dst, const void *src) { u32 tmp; memcpy(&tmp, src, 4); memcpy(dst, &tmp, 4); } static_inline void byte_move_8(void *dst, const void *src) { u64 tmp; memcpy(&tmp, src, 8); memcpy(dst, &tmp, 8); } static_inline void byte_move_16(void *dst, const void *src) { char *pdst = (char *)dst; const char *psrc = (const char *)src; u64 tmp1, tmp2; memcpy(&tmp1, psrc, 8); memcpy(&tmp2, psrc + 8, 8); memcpy(pdst, &tmp1, 8); memcpy(pdst + 8, &tmp2, 8); } static_inline bool byte_match_2(void *buf, const char *pat) { v16_uni u1, u2; memcpy(&u1, buf, 2); memcpy(&u2, pat, 2); return u1.u == u2.u; } static_inline bool byte_match_4(void *buf, const char *pat) { v32_uni u1, u2; memcpy(&u1, buf, 4); memcpy(&u2, pat, 4); return u1.u == u2.u; } static_inline u16 byte_load_2(const void *src) { v16_uni uni; memcpy(&uni, src, 2); return uni.u; } static_inline u32 byte_load_3(const void *src) { v32_uni uni; memcpy(&uni, src, 2); uni.v.c[2] = ((const char *)src)[2]; uni.v.c[3] = 0; return uni.u; } static_inline u32 byte_load_4(const void *src) { v32_uni uni; memcpy(&uni, src, 4); return uni.u; } #endif /*============================================================================== * Number Utils * These functions are used to detect and convert NaN and Inf numbers. *============================================================================*/ /** Convert raw binary to double. */ static_inline f64 f64_from_raw(u64 u) { /* use memcpy to avoid violating the strict aliasing rule */ f64 f; memcpy(&f, &u, 8); return f; } /** Convert double to raw binary. */ static_inline u64 f64_to_raw(f64 f) { /* use memcpy to avoid violating the strict aliasing rule */ u64 u; memcpy(&u, &f, 8); return u; } /** Get raw 'infinity' with sign. */ static_inline u64 f64_raw_get_inf(bool sign) { #if YYJSON_HAS_IEEE_754 return F64_RAW_INF | ((u64)sign << 63); #elif defined(INFINITY) return f64_to_raw(sign ? -INFINITY : INFINITY); #else return f64_to_raw(sign ? -HUGE_VAL : HUGE_VAL); #endif } /** Get raw 'nan' with sign. */ static_inline u64 f64_raw_get_nan(bool sign) { #if YYJSON_HAS_IEEE_754 return F64_RAW_NAN | ((u64)sign << 63); #elif defined(NAN) return f64_to_raw(sign ? (f64)-NAN : (f64)NAN); #else return f64_to_raw((sign ? -0.0 : 0.0) / 0.0); #endif } /** Convert normalized u64 (highest bit is 1) to f64. Some compiler (such as Microsoft Visual C++ 6.0) do not support converting number from u64 to f64. This function will first convert u64 to i64 and then to f64, with `to nearest` rounding mode. */ static_inline f64 normalized_u64_to_f64(u64 val) { #if YYJSON_U64_TO_F64_NO_IMPL i64 sig = (i64)((val >> 1) | (val & 1)); return ((f64)sig) * (f64)2.0; #else return (f64)val; #endif } /*============================================================================== * Size Utils * These functions are used for memory allocation. *============================================================================*/ /** Returns whether the size is overflow after increment. */ static_inline bool size_add_is_overflow(usize size, usize add) { return size > (size + add); } /** Returns whether the size is power of 2 (size should not be 0). */ static_inline bool size_is_pow2(usize size) { return (size & (size - 1)) == 0; } /** Align size upwards (may overflow). */ static_inline usize size_align_up(usize size, usize align) { if (size_is_pow2(align)) { return (size + (align - 1)) & ~(align - 1); } else { return size + align - (size + align - 1) % align - 1; } } /** Align size downwards. */ static_inline usize size_align_down(usize size, usize align) { if (size_is_pow2(align)) { return size & ~(align - 1); } else { return size - (size % align); } } /** Align address upwards (may overflow). */ static_inline void *mem_align_up(void *mem, usize align) { usize size; memcpy(&size, &mem, sizeof(usize)); size = size_align_up(size, align); memcpy(&mem, &size, sizeof(usize)); return mem; } /*============================================================================== * Bits Utils * These functions are used by the floating-point number reader and writer. *============================================================================*/ /** Returns the number of leading 0-bits in value (input should not be 0). */ static_inline u32 u64_lz_bits(u64 v) { #if GCC_HAS_CLZLL return (u32)__builtin_clzll(v); #elif MSC_HAS_BIT_SCAN_64 unsigned long r; _BitScanReverse64(&r, v); return (u32)63 - (u32)r; #elif MSC_HAS_BIT_SCAN unsigned long hi, lo; bool hi_set = _BitScanReverse(&hi, (u32)(v >> 32)) != 0; _BitScanReverse(&lo, (u32)v); hi |= 32; return (u32)63 - (u32)(hi_set ? hi : lo); #else /* branchless, use de Bruijn sequences see: https://www.chessprogramming.org/BitScan */ const u8 table[64] = { 63, 16, 62, 7, 15, 36, 61, 3, 6, 14, 22, 26, 35, 47, 60, 2, 9, 5, 28, 11, 13, 21, 42, 19, 25, 31, 34, 40, 46, 52, 59, 1, 17, 8, 37, 4, 23, 27, 48, 10, 29, 12, 43, 20, 32, 41, 53, 18, 38, 24, 49, 30, 44, 33, 54, 39, 50, 45, 55, 51, 56, 57, 58, 0 }; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v |= v >> 32; return table[(v * U64(0x03F79D71, 0xB4CB0A89)) >> 58]; #endif } /** Returns the number of trailing 0-bits in value (input should not be 0). */ static_inline u32 u64_tz_bits(u64 v) { #if GCC_HAS_CTZLL return (u32)__builtin_ctzll(v); #elif MSC_HAS_BIT_SCAN_64 unsigned long r; _BitScanForward64(&r, v); return (u32)r; #elif MSC_HAS_BIT_SCAN unsigned long lo, hi; bool lo_set = _BitScanForward(&lo, (u32)(v)) != 0; _BitScanForward(&hi, (u32)(v >> 32)); hi += 32; return lo_set ? lo : hi; #else /* branchless, use de Bruijn sequences see: https://www.chessprogramming.org/BitScan */ const u8 table[64] = { 0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28, 62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11, 63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10, 51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12 }; return table[((v & (~v + 1)) * U64(0x022FDD63, 0xCC95386D)) >> 58]; #endif } /*============================================================================== * 128-bit Integer Utils * These functions are used by the floating-point number reader and writer. *============================================================================*/ /** Multiplies two 64-bit unsigned integers (a * b), returns the 128-bit result as 'hi' and 'lo'. */ static_inline void u128_mul(u64 a, u64 b, u64 *hi, u64 *lo) { #if YYJSON_HAS_INT128 u128 m = (u128)a * b; *hi = (u64)(m >> 64); *lo = (u64)(m); #elif MSC_HAS_UMUL128 *lo = _umul128(a, b, hi); #else u32 a0 = (u32)(a), a1 = (u32)(a >> 32); u32 b0 = (u32)(b), b1 = (u32)(b >> 32); u64 p00 = (u64)a0 * b0, p01 = (u64)a0 * b1; u64 p10 = (u64)a1 * b0, p11 = (u64)a1 * b1; u64 m0 = p01 + (p00 >> 32); u32 m00 = (u32)(m0), m01 = (u32)(m0 >> 32); u64 m1 = p10 + m00; u32 m10 = (u32)(m1), m11 = (u32)(m1 >> 32); *hi = p11 + m01 + m11; *lo = ((u64)m10 << 32) | (u32)p00; #endif } /** Multiplies two 64-bit unsigned integers and add a value (a * b + c), returns the 128-bit result as 'hi' and 'lo'. */ static_inline void u128_mul_add(u64 a, u64 b, u64 c, u64 *hi, u64 *lo) { #if YYJSON_HAS_INT128 u128 m = (u128)a * b + c; *hi = (u64)(m >> 64); *lo = (u64)(m); #else u64 h, l, t; u128_mul(a, b, &h, &l); t = l + c; h += (u64)(((t < l) | (t < c))); *hi = h; *lo = t; #endif } /*============================================================================== * File Utils * These functions are used to read and write JSON files. *============================================================================*/ #define YYJSON_FOPEN_EXT #if !defined(_MSC_VER) && defined(__GLIBC__) && defined(__GLIBC_PREREQ) # if __GLIBC_PREREQ(2, 7) # undef YYJSON_FOPEN_EXT # define YYJSON_FOPEN_EXT "e" /* glibc extension to enable O_CLOEXEC */ # endif #endif static_inline FILE *fopen_safe(const char *path, const char *mode) { #if YYJSON_MSC_VER >= 1400 FILE *file = NULL; if (fopen_s(&file, path, mode) != 0) return NULL; return file; #else return fopen(path, mode); #endif } static_inline FILE *fopen_readonly(const char *path) { return fopen_safe(path, "rb" YYJSON_FOPEN_EXT); } static_inline FILE *fopen_writeonly(const char *path) { return fopen_safe(path, "wb" YYJSON_FOPEN_EXT); } static_inline usize fread_safe(void *buf, usize size, FILE *file) { #if YYJSON_MSC_VER >= 1400 return fread_s(buf, size, 1, size, file); #else return fread(buf, 1, size, file); #endif } /*============================================================================== * Default Memory Allocator * This is a simple libc memory allocator wrapper. *============================================================================*/ static void *default_malloc(void *ctx, usize size) { return malloc(size); } static void *default_realloc(void *ctx, void *ptr, usize old_size, usize size) { return realloc(ptr, size); } static void default_free(void *ctx, void *ptr) { free(ptr); } static const yyjson_alc YYJSON_DEFAULT_ALC = { default_malloc, default_realloc, default_free, NULL }; /*============================================================================== * Null Memory Allocator * * This allocator is just a placeholder to ensure that the internal * malloc/realloc/free function pointers are not null. *============================================================================*/ static void *null_malloc(void *ctx, usize size) { return NULL; } static void *null_realloc(void *ctx, void *ptr, usize old_size, usize size) { return NULL; } static void null_free(void *ctx, void *ptr) { return; } static const yyjson_alc YYJSON_NULL_ALC = { null_malloc, null_realloc, null_free, NULL }; /*============================================================================== * Pool Memory Allocator * * This allocator is initialized with a fixed-size buffer. * The buffer is split into multiple memory chunks for memory allocation. *============================================================================*/ /** memory chunk header */ typedef struct pool_chunk { usize size; /* chunk memory size, include chunk header */ struct pool_chunk *next; /* linked list, nullable */ /* char mem[]; flexible array member */ } pool_chunk; /** allocator ctx header */ typedef struct pool_ctx { usize size; /* total memory size, include ctx header */ pool_chunk *free_list; /* linked list, nullable */ /* pool_chunk chunks[]; flexible array member */ } pool_ctx; /** align up the input size to chunk size */ static_inline void pool_size_align(usize *size) { *size = size_align_up(*size, sizeof(pool_chunk)) + sizeof(pool_chunk); } static void *pool_malloc(void *ctx_ptr, usize size) { /* assert(size != 0) */ pool_ctx *ctx = (pool_ctx *)ctx_ptr; pool_chunk *next, *prev = NULL, *cur = ctx->free_list; if (unlikely(size >= ctx->size)) return NULL; pool_size_align(&size); while (cur) { if (cur->size < size) { /* not enough space, try next chunk */ prev = cur; cur = cur->next; continue; } if (cur->size >= size + sizeof(pool_chunk) * 2) { /* too much space, split this chunk */ next = (pool_chunk *)(void *)((u8 *)cur + size); next->size = cur->size - size; next->next = cur->next; cur->size = size; } else { /* just enough space, use whole chunk */ next = cur->next; } if (prev) prev->next = next; else ctx->free_list = next; return (void *)(cur + 1); } return NULL; } static void pool_free(void *ctx_ptr, void *ptr) { /* assert(ptr != NULL) */ pool_ctx *ctx = (pool_ctx *)ctx_ptr; pool_chunk *cur = ((pool_chunk *)ptr) - 1; pool_chunk *prev = NULL, *next = ctx->free_list; while (next && next < cur) { prev = next; next = next->next; } if (prev) prev->next = cur; else ctx->free_list = cur; cur->next = next; if (next && ((u8 *)cur + cur->size) == (u8 *)next) { /* merge cur to higher chunk */ cur->size += next->size; cur->next = next->next; } if (prev && ((u8 *)prev + prev->size) == (u8 *)cur) { /* merge cur to lower chunk */ prev->size += cur->size; prev->next = cur->next; } } static void *pool_realloc(void *ctx_ptr, void *ptr, usize old_size, usize size) { /* assert(ptr != NULL && size != 0 && old_size < size) */ pool_ctx *ctx = (pool_ctx *)ctx_ptr; pool_chunk *cur = ((pool_chunk *)ptr) - 1, *prev, *next, *tmp; /* check size */ if (unlikely(size >= ctx->size)) return NULL; pool_size_align(&old_size); pool_size_align(&size); if (unlikely(old_size == size)) return ptr; /* find next and prev chunk */ prev = NULL; next = ctx->free_list; while (next && next < cur) { prev = next; next = next->next; } if ((u8 *)cur + cur->size == (u8 *)next && cur->size + next->size >= size) { /* merge to higher chunk if they are contiguous */ usize free_size = cur->size + next->size - size; if (free_size > sizeof(pool_chunk) * 2) { tmp = (pool_chunk *)(void *)((u8 *)cur + size); if (prev) prev->next = tmp; else ctx->free_list = tmp; tmp->next = next->next; tmp->size = free_size; cur->size = size; } else { if (prev) prev->next = next->next; else ctx->free_list = next->next; cur->size += next->size; } return ptr; } else { /* fallback to malloc and memcpy */ void *new_ptr = pool_malloc(ctx_ptr, size - sizeof(pool_chunk)); if (new_ptr) { memcpy(new_ptr, ptr, cur->size - sizeof(pool_chunk)); pool_free(ctx_ptr, ptr); } return new_ptr; } } bool yyjson_alc_pool_init(yyjson_alc *alc, void *buf, usize size) { pool_chunk *chunk; pool_ctx *ctx; if (unlikely(!alc)) return false; *alc = YYJSON_NULL_ALC; if (size < sizeof(pool_ctx) * 4) return false; ctx = (pool_ctx *)mem_align_up(buf, sizeof(pool_ctx)); if (unlikely(!ctx)) return false; size -= (usize)((u8 *)ctx - (u8 *)buf); size = size_align_down(size, sizeof(pool_ctx)); chunk = (pool_chunk *)(ctx + 1); chunk->size = size - sizeof(pool_ctx); chunk->next = NULL; ctx->size = size; ctx->free_list = chunk; alc->malloc = pool_malloc; alc->realloc = pool_realloc; alc->free = pool_free; alc->ctx = (void *)ctx; return true; } /*============================================================================== * Dynamic Memory Allocator * * This allocator allocates memory on demand and does not immediately release * unused memory. Instead, it places the unused memory into a freelist for * potential reuse in the future. It is only when the entire allocator is * destroyed that all previously allocated memory is released at once. *============================================================================*/ /** memory chunk header */ typedef struct dyn_chunk { usize size; /* chunk size, include header */ struct dyn_chunk *next; /* char mem[]; flexible array member */ } dyn_chunk; /** allocator ctx header */ typedef struct { dyn_chunk free_list; /* dummy header, sorted from small to large */ dyn_chunk used_list; /* dummy header */ } dyn_ctx; /** align up the input size to chunk size */ static_inline bool dyn_size_align(usize *size) { usize alc_size = *size + sizeof(dyn_chunk); alc_size = size_align_up(alc_size, YYJSON_ALC_DYN_MIN_SIZE); if (unlikely(alc_size < *size)) return false; /* overflow */ *size = alc_size; return true; } /** remove a chunk from list (the chunk must already be in the list) */ static_inline void dyn_chunk_list_remove(dyn_chunk *list, dyn_chunk *chunk) { dyn_chunk *prev = list, *cur; for (cur = prev->next; cur; cur = cur->next) { if (cur == chunk) { prev->next = cur->next; cur->next = NULL; return; } prev = cur; } } /** add a chunk to list header (the chunk must not be in the list) */ static_inline void dyn_chunk_list_add(dyn_chunk *list, dyn_chunk *chunk) { chunk->next = list->next; list->next = chunk; } static void *dyn_malloc(void *ctx_ptr, usize size) { /* assert(size != 0) */ const yyjson_alc def = YYJSON_DEFAULT_ALC; dyn_ctx *ctx = (dyn_ctx *)ctx_ptr; dyn_chunk *chunk, *prev, *next; if (unlikely(!dyn_size_align(&size))) return NULL; /* freelist is empty, create new chunk */ if (!ctx->free_list.next) { chunk = (dyn_chunk *)def.malloc(def.ctx, size); if (unlikely(!chunk)) return NULL; chunk->size = size; chunk->next = NULL; dyn_chunk_list_add(&ctx->used_list, chunk); return (void *)(chunk + 1); } /* find a large enough chunk, or resize the largest chunk */ prev = &ctx->free_list; while (true) { chunk = prev->next; if (chunk->size >= size) { /* enough size, reuse this chunk */ prev->next = chunk->next; dyn_chunk_list_add(&ctx->used_list, chunk); return (void *)(chunk + 1); } if (!chunk->next) { /* resize the largest chunk */ chunk = (dyn_chunk *)def.realloc(def.ctx, chunk, chunk->size, size); if (unlikely(!chunk)) return NULL; prev->next = NULL; chunk->size = size; dyn_chunk_list_add(&ctx->used_list, chunk); return (void *)(chunk + 1); } prev = chunk; } } static void *dyn_realloc(void *ctx_ptr, void *ptr, usize old_size, usize size) { /* assert(ptr != NULL && size != 0 && old_size < size) */ const yyjson_alc def = YYJSON_DEFAULT_ALC; dyn_ctx *ctx = (dyn_ctx *)ctx_ptr; dyn_chunk *prev, *next, *new_chunk; dyn_chunk *chunk = (dyn_chunk *)ptr - 1; if (unlikely(!dyn_size_align(&size))) return NULL; if (chunk->size >= size) return ptr; dyn_chunk_list_remove(&ctx->used_list, chunk); new_chunk = (dyn_chunk *)def.realloc(def.ctx, chunk, chunk->size, size); if (likely(new_chunk)) { new_chunk->size = size; chunk = new_chunk; } dyn_chunk_list_add(&ctx->used_list, chunk); return new_chunk ? (void *)(new_chunk + 1) : NULL; } static void dyn_free(void *ctx_ptr, void *ptr) { /* assert(ptr != NULL) */ dyn_ctx *ctx = (dyn_ctx *)ctx_ptr; dyn_chunk *chunk = (dyn_chunk *)ptr - 1, *prev; dyn_chunk_list_remove(&ctx->used_list, chunk); for (prev = &ctx->free_list; prev; prev = prev->next) { if (!prev->next || prev->next->size >= chunk->size) { chunk->next = prev->next; prev->next = chunk; break; } } } yyjson_alc *yyjson_alc_dyn_new(void) { const yyjson_alc def = YYJSON_DEFAULT_ALC; usize hdr_len = sizeof(yyjson_alc) + sizeof(dyn_ctx); yyjson_alc *alc = (yyjson_alc *)def.malloc(def.ctx, hdr_len); dyn_ctx *ctx = (dyn_ctx *)(void *)(alc + 1); if (unlikely(!alc)) return NULL; alc->malloc = dyn_malloc; alc->realloc = dyn_realloc; alc->free = dyn_free; alc->ctx = alc + 1; memset(ctx, 0, sizeof(*ctx)); return alc; } void yyjson_alc_dyn_free(yyjson_alc *alc) { const yyjson_alc def = YYJSON_DEFAULT_ALC; dyn_ctx *ctx = (dyn_ctx *)(void *)(alc + 1); dyn_chunk *chunk, *next; if (unlikely(!alc)) return; for (chunk = ctx->free_list.next; chunk; chunk = next) { next = chunk->next; def.free(def.ctx, chunk); } for (chunk = ctx->used_list.next; chunk; chunk = next) { next = chunk->next; def.free(def.ctx, chunk); } def.free(def.ctx, alc); } /*============================================================================== * JSON document and value *============================================================================*/ static_inline void unsafe_yyjson_str_pool_release(yyjson_str_pool *pool, yyjson_alc *alc) { yyjson_str_chunk *chunk = pool->chunks, *next; while (chunk) { next = chunk->next; alc->free(alc->ctx, chunk); chunk = next; } } static_inline void unsafe_yyjson_val_pool_release(yyjson_val_pool *pool, yyjson_alc *alc) { yyjson_val_chunk *chunk = pool->chunks, *next; while (chunk) { next = chunk->next; alc->free(alc->ctx, chunk); chunk = next; } } bool unsafe_yyjson_str_pool_grow(yyjson_str_pool *pool, const yyjson_alc *alc, usize len) { yyjson_str_chunk *chunk; usize size, max_len; /* create a new chunk */ max_len = USIZE_MAX - sizeof(yyjson_str_chunk); if (unlikely(len > max_len)) return false; size = len + sizeof(yyjson_str_chunk); size = yyjson_max(pool->chunk_size, size); chunk = (yyjson_str_chunk *)alc->malloc(alc->ctx, size); if (unlikely(!chunk)) return false; /* insert the new chunk as the head of the linked list */ chunk->next = pool->chunks; chunk->chunk_size = size; pool->chunks = chunk; pool->cur = (char *)chunk + sizeof(yyjson_str_chunk); pool->end = (char *)chunk + size; /* the next chunk is twice the size of the current one */ size = yyjson_min(pool->chunk_size * 2, pool->chunk_size_max); if (size < pool->chunk_size) size = pool->chunk_size_max; /* overflow */ pool->chunk_size = size; return true; } bool unsafe_yyjson_val_pool_grow(yyjson_val_pool *pool, const yyjson_alc *alc, usize count) { yyjson_val_chunk *chunk; usize size, max_count; /* create a new chunk */ max_count = USIZE_MAX / sizeof(yyjson_mut_val) - 1; if (unlikely(count > max_count)) return false; size = (count + 1) * sizeof(yyjson_mut_val); size = yyjson_max(pool->chunk_size, size); chunk = (yyjson_val_chunk *)alc->malloc(alc->ctx, size); if (unlikely(!chunk)) return false; /* insert the new chunk as the head of the linked list */ chunk->next = pool->chunks; chunk->chunk_size = size; pool->chunks = chunk; pool->cur = (yyjson_mut_val *)(void *)((u8 *)chunk) + 1; pool->end = (yyjson_mut_val *)(void *)((u8 *)chunk + size); /* the next chunk is twice the size of the current one */ size = yyjson_min(pool->chunk_size * 2, pool->chunk_size_max); if (size < pool->chunk_size) size = pool->chunk_size_max; /* overflow */ pool->chunk_size = size; return true; } bool yyjson_mut_doc_set_str_pool_size(yyjson_mut_doc *doc, size_t len) { usize max_size = USIZE_MAX - sizeof(yyjson_str_chunk); if (!doc || !len || len > max_size) return false; doc->str_pool.chunk_size = len + sizeof(yyjson_str_chunk); return true; } bool yyjson_mut_doc_set_val_pool_size(yyjson_mut_doc *doc, size_t count) { usize max_count = USIZE_MAX / sizeof(yyjson_mut_val) - 1; if (!doc || !count || count > max_count) return false; doc->val_pool.chunk_size = (count + 1) * sizeof(yyjson_mut_val); return true; } void yyjson_mut_doc_free(yyjson_mut_doc *doc) { if (doc) { yyjson_alc alc = doc->alc; memset(&doc->alc, 0, sizeof(alc)); unsafe_yyjson_str_pool_release(&doc->str_pool, &alc); unsafe_yyjson_val_pool_release(&doc->val_pool, &alc); alc.free(alc.ctx, doc); } } yyjson_mut_doc *yyjson_mut_doc_new(const yyjson_alc *alc) { yyjson_mut_doc *doc; if (!alc) alc = &YYJSON_DEFAULT_ALC; doc = (yyjson_mut_doc *)alc->malloc(alc->ctx, sizeof(yyjson_mut_doc)); if (!doc) return NULL; memset(doc, 0, sizeof(yyjson_mut_doc)); doc->alc = *alc; doc->str_pool.chunk_size = YYJSON_MUT_DOC_STR_POOL_INIT_SIZE; doc->str_pool.chunk_size_max = YYJSON_MUT_DOC_STR_POOL_MAX_SIZE; doc->val_pool.chunk_size = YYJSON_MUT_DOC_VAL_POOL_INIT_SIZE; doc->val_pool.chunk_size_max = YYJSON_MUT_DOC_VAL_POOL_MAX_SIZE; return doc; } yyjson_mut_doc *yyjson_doc_mut_copy(yyjson_doc *doc, const yyjson_alc *alc) { yyjson_mut_doc *m_doc; yyjson_mut_val *m_val; if (!doc || !doc->root) return NULL; m_doc = yyjson_mut_doc_new(alc); if (!m_doc) return NULL; m_val = yyjson_val_mut_copy(m_doc, doc->root); if (!m_val) { yyjson_mut_doc_free(m_doc); return NULL; } yyjson_mut_doc_set_root(m_doc, m_val); return m_doc; } yyjson_mut_doc *yyjson_mut_doc_mut_copy(yyjson_mut_doc *doc, const yyjson_alc *alc) { yyjson_mut_doc *m_doc; yyjson_mut_val *m_val; if (!doc) return NULL; if (!doc->root) return yyjson_mut_doc_new(alc); m_doc = yyjson_mut_doc_new(alc); if (!m_doc) return NULL; m_val = yyjson_mut_val_mut_copy(m_doc, doc->root); if (!m_val) { yyjson_mut_doc_free(m_doc); return NULL; } yyjson_mut_doc_set_root(m_doc, m_val); return m_doc; } yyjson_mut_val *yyjson_val_mut_copy(yyjson_mut_doc *m_doc, yyjson_val *i_vals) { /* The immutable object or array stores all sub-values in a contiguous memory, We copy them to another contiguous memory as mutable values, then reconnect the mutable values with the original relationship. */ usize i_vals_len; yyjson_mut_val *m_vals, *m_val; yyjson_val *i_val, *i_end; if (!m_doc || !i_vals) return NULL; i_end = unsafe_yyjson_get_next(i_vals); i_vals_len = (usize)(unsafe_yyjson_get_next(i_vals) - i_vals); m_vals = unsafe_yyjson_mut_val(m_doc, i_vals_len); if (!m_vals) return NULL; i_val = i_vals; m_val = m_vals; for (; i_val < i_end; i_val++, m_val++) { yyjson_type type = unsafe_yyjson_get_type(i_val); m_val->tag = i_val->tag; m_val->uni.u64 = i_val->uni.u64; if (type == YYJSON_TYPE_STR || type == YYJSON_TYPE_RAW) { const char *str = i_val->uni.str; usize str_len = unsafe_yyjson_get_len(i_val); m_val->uni.str = unsafe_yyjson_mut_strncpy(m_doc, str, str_len); if (!m_val->uni.str) return NULL; } else if (type == YYJSON_TYPE_ARR) { usize len = unsafe_yyjson_get_len(i_val); if (len > 0) { yyjson_val *ii_val = i_val + 1, *ii_next; yyjson_mut_val *mm_val = m_val + 1, *mm_ctn = m_val, *mm_next; while (len-- > 1) { ii_next = unsafe_yyjson_get_next(ii_val); mm_next = mm_val + (ii_next - ii_val); mm_val->next = mm_next; ii_val = ii_next; mm_val = mm_next; } mm_val->next = mm_ctn + 1; mm_ctn->uni.ptr = mm_val; } } else if (type == YYJSON_TYPE_OBJ) { usize len = unsafe_yyjson_get_len(i_val); if (len > 0) { yyjson_val *ii_key = i_val + 1, *ii_nextkey; yyjson_mut_val *mm_key = m_val + 1, *mm_ctn = m_val; yyjson_mut_val *mm_nextkey; while (len-- > 1) { ii_nextkey = unsafe_yyjson_get_next(ii_key + 1); mm_nextkey = mm_key + (ii_nextkey - ii_key); mm_key->next = mm_key + 1; mm_key->next->next = mm_nextkey; ii_key = ii_nextkey; mm_key = mm_nextkey; } mm_key->next = mm_key + 1; mm_key->next->next = mm_ctn + 1; mm_ctn->uni.ptr = mm_key; } } } return m_vals; } static yyjson_mut_val *unsafe_yyjson_mut_val_mut_copy(yyjson_mut_doc *m_doc, yyjson_mut_val *m_vals) { /* The mutable object or array stores all sub-values in a circular linked list, so we can traverse them in the same loop. The traversal starts from the last item, continues with the first item in a list, and ends with the second to last item, which needs to be linked to the last item to close the circle. */ yyjson_mut_val *m_val = unsafe_yyjson_mut_val(m_doc, 1); if (unlikely(!m_val)) return NULL; m_val->tag = m_vals->tag; switch (unsafe_yyjson_get_type(m_vals)) { case YYJSON_TYPE_OBJ: case YYJSON_TYPE_ARR: if (unsafe_yyjson_get_len(m_vals) > 0) { yyjson_mut_val *last = (yyjson_mut_val *)m_vals->uni.ptr; yyjson_mut_val *next = last->next, *prev; prev = unsafe_yyjson_mut_val_mut_copy(m_doc, last); if (!prev) return NULL; m_val->uni.ptr = (void *)prev; while (next != last) { prev->next = unsafe_yyjson_mut_val_mut_copy(m_doc, next); if (!prev->next) return NULL; prev = prev->next; next = next->next; } prev->next = (yyjson_mut_val *)m_val->uni.ptr; } break; case YYJSON_TYPE_RAW: case YYJSON_TYPE_STR: { const char *str = m_vals->uni.str; usize str_len = unsafe_yyjson_get_len(m_vals); m_val->uni.str = unsafe_yyjson_mut_strncpy(m_doc, str, str_len); if (!m_val->uni.str) return NULL; break; } default: m_val->uni = m_vals->uni; break; } return m_val; } yyjson_mut_val *yyjson_mut_val_mut_copy(yyjson_mut_doc *doc, yyjson_mut_val *val) { if (doc && val) return unsafe_yyjson_mut_val_mut_copy(doc, val); return NULL; } /* Count the number of values and the total length of the strings. */ static void yyjson_mut_stat(yyjson_mut_val *val, usize *val_sum, usize *str_sum) { yyjson_type type = unsafe_yyjson_get_type(val); *val_sum += 1; if (type == YYJSON_TYPE_ARR || type == YYJSON_TYPE_OBJ) { yyjson_mut_val *child = (yyjson_mut_val *)val->uni.ptr; usize len = unsafe_yyjson_get_len(val), i; len <<= (u8)(type == YYJSON_TYPE_OBJ); *val_sum += len; for (i = 0; i < len; i++) { yyjson_type stype = unsafe_yyjson_get_type(child); if (stype == YYJSON_TYPE_STR || stype == YYJSON_TYPE_RAW) { *str_sum += unsafe_yyjson_get_len(child) + 1; } else if (stype == YYJSON_TYPE_ARR || stype == YYJSON_TYPE_OBJ) { yyjson_mut_stat(child, val_sum, str_sum); *val_sum -= 1; } child = child->next; } } else if (type == YYJSON_TYPE_STR || type == YYJSON_TYPE_RAW) { *str_sum += unsafe_yyjson_get_len(val) + 1; } } /* Copy mutable values to immutable value pool. */ static usize yyjson_imut_copy(yyjson_val **val_ptr, char **buf_ptr, yyjson_mut_val *mval) { yyjson_val *val = *val_ptr; yyjson_type type = unsafe_yyjson_get_type(mval); if (type == YYJSON_TYPE_ARR || type == YYJSON_TYPE_OBJ) { yyjson_mut_val *child = (yyjson_mut_val *)mval->uni.ptr; usize len = unsafe_yyjson_get_len(mval), i; usize val_sum = 1; if (type == YYJSON_TYPE_OBJ) { if (len) child = child->next->next; len <<= 1; } else { if (len) child = child->next; } *val_ptr = val + 1; for (i = 0; i < len; i++) { val_sum += yyjson_imut_copy(val_ptr, buf_ptr, child); child = child->next; } val->tag = mval->tag; val->uni.ofs = val_sum * sizeof(yyjson_val); return val_sum; } else if (type == YYJSON_TYPE_STR || type == YYJSON_TYPE_RAW) { char *buf = *buf_ptr; usize len = unsafe_yyjson_get_len(mval); memcpy((void *)buf, (const void *)mval->uni.str, len); buf[len] = '\0'; val->tag = mval->tag; val->uni.str = buf; *val_ptr = val + 1; *buf_ptr = buf + len + 1; return 1; } else { val->tag = mval->tag; val->uni = mval->uni; *val_ptr = val + 1; return 1; } } yyjson_doc *yyjson_mut_doc_imut_copy(yyjson_mut_doc *mdoc, const yyjson_alc *alc) { if (!mdoc) return NULL; return yyjson_mut_val_imut_copy(mdoc->root, alc); } yyjson_doc *yyjson_mut_val_imut_copy(yyjson_mut_val *mval, const yyjson_alc *alc) { usize val_num = 0, str_sum = 0, hdr_size, buf_size; yyjson_doc *doc = NULL; yyjson_val *val_hdr = NULL; /* This value should be NULL here. Setting a non-null value suppresses warning from the clang analyzer. */ char *str_hdr = (char *)(void *)&str_sum; if (!mval) return NULL; if (!alc) alc = &YYJSON_DEFAULT_ALC; /* traverse the input value to get pool size */ yyjson_mut_stat(mval, &val_num, &str_sum); /* create doc and val pool */ hdr_size = size_align_up(sizeof(yyjson_doc), sizeof(yyjson_val)); buf_size = hdr_size + val_num * sizeof(yyjson_val); doc = (yyjson_doc *)alc->malloc(alc->ctx, buf_size); if (!doc) return NULL; memset(doc, 0, sizeof(yyjson_doc)); val_hdr = (yyjson_val *)(void *)((char *)(void *)doc + hdr_size); doc->root = val_hdr; doc->alc = *alc; /* create str pool */ if (str_sum > 0) { str_hdr = (char *)alc->malloc(alc->ctx, str_sum); doc->str_pool = str_hdr; if (!str_hdr) { alc->free(alc->ctx, (void *)doc); return NULL; } } /* copy vals and strs */ doc->val_read = yyjson_imut_copy(&val_hdr, &str_hdr, mval); doc->dat_read = str_sum + 1; return doc; } static_inline bool unsafe_yyjson_num_equals(void *lhs, void *rhs) { yyjson_val_uni *luni = &((yyjson_val *)lhs)->uni; yyjson_val_uni *runi = &((yyjson_val *)rhs)->uni; yyjson_subtype lt = unsafe_yyjson_get_subtype(lhs); yyjson_subtype rt = unsafe_yyjson_get_subtype(rhs); if (lt == rt) return luni->u64 == runi->u64; if (lt == YYJSON_SUBTYPE_SINT && rt == YYJSON_SUBTYPE_UINT) { return luni->i64 >= 0 && luni->u64 == runi->u64; } if (lt == YYJSON_SUBTYPE_UINT && rt == YYJSON_SUBTYPE_SINT) { return runi->i64 >= 0 && luni->u64 == runi->u64; } return false; } static_inline bool unsafe_yyjson_str_equals(void *lhs, void *rhs) { usize len = unsafe_yyjson_get_len(lhs); if (len != unsafe_yyjson_get_len(rhs)) return false; return !memcmp(unsafe_yyjson_get_str(lhs), unsafe_yyjson_get_str(rhs), len); } bool unsafe_yyjson_equals(yyjson_val *lhs, yyjson_val *rhs) { yyjson_type type = unsafe_yyjson_get_type(lhs); if (type != unsafe_yyjson_get_type(rhs)) return false; switch (type) { case YYJSON_TYPE_OBJ: { usize len = unsafe_yyjson_get_len(lhs); if (len != unsafe_yyjson_get_len(rhs)) return false; if (len > 0) { yyjson_obj_iter iter; yyjson_obj_iter_init(rhs, &iter); lhs = unsafe_yyjson_get_first(lhs); while (len-- > 0) { rhs = yyjson_obj_iter_getn(&iter, lhs->uni.str, unsafe_yyjson_get_len(lhs)); if (!rhs) return false; if (!unsafe_yyjson_equals(lhs + 1, rhs)) return false; lhs = unsafe_yyjson_get_next(lhs + 1); } } /* yyjson allows duplicate keys, so the check may be inaccurate */ return true; } case YYJSON_TYPE_ARR: { usize len = unsafe_yyjson_get_len(lhs); if (len != unsafe_yyjson_get_len(rhs)) return false; if (len > 0) { lhs = unsafe_yyjson_get_first(lhs); rhs = unsafe_yyjson_get_first(rhs); while (len-- > 0) { if (!unsafe_yyjson_equals(lhs, rhs)) return false; lhs = unsafe_yyjson_get_next(lhs); rhs = unsafe_yyjson_get_next(rhs); } } return true; } case YYJSON_TYPE_NUM: return unsafe_yyjson_num_equals(lhs, rhs); case YYJSON_TYPE_RAW: case YYJSON_TYPE_STR: return unsafe_yyjson_str_equals(lhs, rhs); case YYJSON_TYPE_NULL: case YYJSON_TYPE_BOOL: return lhs->tag == rhs->tag; default: return false; } } bool unsafe_yyjson_mut_equals(yyjson_mut_val *lhs, yyjson_mut_val *rhs) { yyjson_type type = unsafe_yyjson_get_type(lhs); if (type != unsafe_yyjson_get_type(rhs)) return false; switch (type) { case YYJSON_TYPE_OBJ: { usize len = unsafe_yyjson_get_len(lhs); if (len != unsafe_yyjson_get_len(rhs)) return false; if (len > 0) { yyjson_mut_obj_iter iter; yyjson_mut_obj_iter_init(rhs, &iter); lhs = (yyjson_mut_val *)lhs->uni.ptr; while (len-- > 0) { rhs = yyjson_mut_obj_iter_getn(&iter, lhs->uni.str, unsafe_yyjson_get_len(lhs)); if (!rhs) return false; if (!unsafe_yyjson_mut_equals(lhs->next, rhs)) return false; lhs = lhs->next->next; } } /* yyjson allows duplicate keys, so the check may be inaccurate */ return true; } case YYJSON_TYPE_ARR: { usize len = unsafe_yyjson_get_len(lhs); if (len != unsafe_yyjson_get_len(rhs)) return false; if (len > 0) { lhs = (yyjson_mut_val *)lhs->uni.ptr; rhs = (yyjson_mut_val *)rhs->uni.ptr; while (len-- > 0) { if (!unsafe_yyjson_mut_equals(lhs, rhs)) return false; lhs = lhs->next; rhs = rhs->next; } } return true; } case YYJSON_TYPE_NUM: return unsafe_yyjson_num_equals(lhs, rhs); case YYJSON_TYPE_RAW: case YYJSON_TYPE_STR: return unsafe_yyjson_str_equals(lhs, rhs); case YYJSON_TYPE_NULL: case YYJSON_TYPE_BOOL: return lhs->tag == rhs->tag; default: return false; } } #if !YYJSON_DISABLE_UTILS /*============================================================================== * JSON Pointer API (RFC 6901) *============================================================================*/ /** Get a token from JSON pointer string. @param ptr [in,out] in: string that points to current token prefix `/` out: string that points to next token prefix `/`, or string end @param end [in] end of the entire JSON Pointer string @param len [out] unescaped token length @param esc [out] number of escaped characters in this token @return head of the token, or NULL if syntax error */ static_inline const char *ptr_next_token(const char **ptr, const char *end, usize *len, usize *esc) { const char *hdr = *ptr + 1; const char *cur = hdr; /* skip unescaped characters */ while (cur < end && *cur != '/' && *cur != '~') cur++; if (likely(cur == end || *cur != '~')) { /* no escaped characters, return */ *ptr = cur; *len = (usize)(cur - hdr); *esc = 0; return hdr; } else { /* handle escaped characters */ usize esc_num = 0; while (cur < end && *cur != '/') { if (*cur++ == '~') { if (cur == end || (*cur != '0' && *cur != '1')) { *ptr = cur - 1; return NULL; } esc_num++; } } *ptr = cur; *len = (usize)(cur - hdr) - esc_num; *esc = esc_num; return hdr; } } /** Convert token string to index. @param cur [in] token head @param len [in] token length @param idx [out] the index number, or USIZE_MAX if token is '-' @return true if token is a valid array index */ static_inline bool ptr_token_to_idx(const char *cur, usize len, usize *idx) { const char *end = cur + len; usize num = 0, add; if (unlikely(len == 0 || len > USIZE_SAFE_DIG)) return false; if (*cur == '0') { if (unlikely(len > 1)) return false; *idx = 0; return true; } if (*cur == '-') { if (unlikely(len > 1)) return false; *idx = USIZE_MAX; return true; } for (; cur < end && (add = (usize)((u8)*cur - (u8)'0')) <= 9; cur++) { num = num * 10 + add; } if (unlikely(num == 0 || cur < end)) return false; *idx = num; return true; } /** Compare JSON key with token. @param key a string key (yyjson_val or yyjson_mut_val) @param token a JSON pointer token @param len unescaped token length @param esc number of escaped characters in this token @return true if `str` is equals to `token` */ static_inline bool ptr_token_eq(void *key, const char *token, usize len, usize esc) { yyjson_val *val = (yyjson_val *)key; if (unsafe_yyjson_get_len(val) != len) return false; if (likely(!esc)) { return memcmp(val->uni.str, token, len) == 0; } else { const char *str = val->uni.str; for (; len-- > 0; token++, str++) { if (*token == '~') { if (*str != (*++token == '0' ? '~' : '/')) return false; } else { if (*str != *token) return false; } } return true; } } /** Get a value from array by token. @param arr an array, should not be NULL or non-array type @param token a JSON pointer token @param len unescaped token length @param esc number of escaped characters in this token @return value at index, or NULL if token is not index or index is out of range */ static_inline yyjson_val *ptr_arr_get(yyjson_val *arr, const char *token, usize len, usize esc) { yyjson_val *val = unsafe_yyjson_get_first(arr); usize num = unsafe_yyjson_get_len(arr), idx = 0; if (unlikely(num == 0)) return NULL; if (unlikely(!ptr_token_to_idx(token, len, &idx))) return NULL; if (unlikely(idx >= num)) return NULL; if (unsafe_yyjson_arr_is_flat(arr)) { return val + idx; } else { while (idx-- > 0) val = unsafe_yyjson_get_next(val); return val; } } /** Get a value from object by token. @param obj [in] an object, should not be NULL or non-object type @param token [in] a JSON pointer token @param len [in] unescaped token length @param esc [in] number of escaped characters in this token @return value associated with the token, or NULL if no value */ static_inline yyjson_val *ptr_obj_get(yyjson_val *obj, const char *token, usize len, usize esc) { yyjson_val *key = unsafe_yyjson_get_first(obj); usize num = unsafe_yyjson_get_len(obj); if (unlikely(num == 0)) return NULL; for (; num > 0; num--, key = unsafe_yyjson_get_next(key + 1)) { if (ptr_token_eq(key, token, len, esc)) return key + 1; } return NULL; } /** Get a value from array by token. @param arr [in] an array, should not be NULL or non-array type @param token [in] a JSON pointer token @param len [in] unescaped token length @param esc [in] number of escaped characters in this token @param pre [out] previous (sibling) value of the returned value @param last [out] whether index is last @return value at index, or NULL if token is not index or index is out of range */ static_inline yyjson_mut_val *ptr_mut_arr_get(yyjson_mut_val *arr, const char *token, usize len, usize esc, yyjson_mut_val **pre, bool *last) { yyjson_mut_val *val = (yyjson_mut_val *)arr->uni.ptr; /* last (tail) */ usize num = unsafe_yyjson_get_len(arr), idx; if (last) *last = false; if (false) *pre = NULL; if (unlikely(num == 0)) { if (last && len == 1 && (*token == '0' || *token == '-')) *last = true; return NULL; } if (unlikely(!ptr_token_to_idx(token, len, &idx))) return NULL; if (last) *last = (idx == num || idx == USIZE_MAX); if (unlikely(idx >= num)) return NULL; while (idx-- > 0) val = val->next; *pre = val; return val->next; } /** Get a value from object by token. @param obj [in] an object, should not be NULL or non-object type @param token [in] a JSON pointer token @param len [in] unescaped token length @param esc [in] number of escaped characters in this token @param pre [out] previous (sibling) key of the returned value's key @return value associated with the token, or NULL if no value */ static_inline yyjson_mut_val *ptr_mut_obj_get(yyjson_mut_val *obj, const char *token, usize len, usize esc, yyjson_mut_val **pre) { yyjson_mut_val *pre_key = (yyjson_mut_val *)obj->uni.ptr, *key; usize num = unsafe_yyjson_get_len(obj); if (false) *pre = NULL; if (unlikely(num == 0)) return NULL; for (; num > 0; num--, pre_key = key) { key = pre_key->next->next; if (ptr_token_eq(key, token, len, esc)) { *pre = pre_key; return key->next; } } return NULL; } /** Create a string value with JSON pointer token. @param token [in] a JSON pointer token @param len [in] unescaped token length @param esc [in] number of escaped characters in this token @param doc [in] used for memory allocation when creating value @return new string value, or NULL if memory allocation failed */ static_inline yyjson_mut_val *ptr_new_key(const char *token, usize len, usize esc, yyjson_mut_doc *doc) { const char *src = token; if (likely(!esc)) { return yyjson_mut_strncpy(doc, src, len); } else { const char *end = src + len + esc; char *dst = unsafe_yyjson_mut_str_alc(doc, len + esc); char *str = dst; if (unlikely(!dst)) return NULL; for (; src < end; src++, dst++) { if (*src != '~') *dst = *src; else *dst = (*++src == '0' ? '~' : '/'); } *dst = '\0'; return yyjson_mut_strn(doc, str, len); } } /* macros for yyjson_ptr */ #define return_err(_ret, _code, _pos, _msg) do { \ if (err) { \ err->code = YYJSON_PTR_ERR_##_code; \ err->msg = _msg; \ err->pos = (usize)(_pos); \ } \ return _ret; \ } while (false) #define return_err_resolve(_ret, _pos) \ return_err(_ret, RESOLVE, _pos, "JSON pointer cannot be resolved") #define return_err_syntax(_ret, _pos) \ return_err(_ret, SYNTAX, _pos, "invalid escaped character") #define return_err_alloc(_ret) \ return_err(_ret, MEMORY_ALLOCATION, 0, "failed to create value") yyjson_val *unsafe_yyjson_ptr_getx(yyjson_val *val, const char *ptr, size_t ptr_len, yyjson_ptr_err *err) { const char *hdr = ptr, *end = ptr + ptr_len, *token; usize len, esc; yyjson_type type; while (true) { token = ptr_next_token(&ptr, end, &len, &esc); if (unlikely(!token)) return_err_syntax(NULL, ptr - hdr); type = unsafe_yyjson_get_type(val); if (type == YYJSON_TYPE_OBJ) { val = ptr_obj_get(val, token, len, esc); } else if (type == YYJSON_TYPE_ARR) { val = ptr_arr_get(val, token, len, esc); } else { val = NULL; } if (!val) return_err_resolve(NULL, token - hdr); if (ptr == end) return val; } } yyjson_mut_val *unsafe_yyjson_mut_ptr_getx(yyjson_mut_val *val, const char *ptr, size_t ptr_len, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { const char *hdr = ptr, *end = ptr + ptr_len, *token; usize len, esc; yyjson_mut_val *ctn, *pre = NULL; yyjson_type type; bool idx_is_last = false; while (true) { token = ptr_next_token(&ptr, end, &len, &esc); if (unlikely(!token)) return_err_syntax(NULL, ptr - hdr); ctn = val; type = unsafe_yyjson_get_type(val); if (type == YYJSON_TYPE_OBJ) { val = ptr_mut_obj_get(val, token, len, esc, &pre); } else if (type == YYJSON_TYPE_ARR) { val = ptr_mut_arr_get(val, token, len, esc, &pre, &idx_is_last); } else { val = NULL; } if (ctx && (ptr == end)) { if (type == YYJSON_TYPE_OBJ || (type == YYJSON_TYPE_ARR && (val || idx_is_last))) { ctx->ctn = ctn; ctx->pre = pre; } } if (!val) return_err_resolve(NULL, token - hdr); if (ptr == end) return val; } } bool unsafe_yyjson_mut_ptr_putx(yyjson_mut_val *val, const char *ptr, size_t ptr_len, yyjson_mut_val *new_val, yyjson_mut_doc *doc, bool create_parent, bool insert_new, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { const char *hdr = ptr, *end = ptr + ptr_len, *token; usize token_len, esc, ctn_len; yyjson_mut_val *ctn, *key, *pre = NULL; yyjson_mut_val *sep_ctn = NULL, *sep_key = NULL, *sep_val = NULL; yyjson_type ctn_type; bool idx_is_last = false; /* skip exist parent nodes */ while (true) { token = ptr_next_token(&ptr, end, &token_len, &esc); if (unlikely(!token)) return_err_syntax(false, ptr - hdr); ctn = val; ctn_type = unsafe_yyjson_get_type(ctn); if (ctn_type == YYJSON_TYPE_OBJ) { val = ptr_mut_obj_get(ctn, token, token_len, esc, &pre); } else if (ctn_type == YYJSON_TYPE_ARR) { val = ptr_mut_arr_get(ctn, token, token_len, esc, &pre, &idx_is_last); } else return_err_resolve(false, token - hdr); if (!val) break; if (ptr == end) break; /* is last token */ } /* create parent nodes if not exist */ if (unlikely(ptr != end)) { /* not last token */ if (!create_parent) return_err_resolve(false, token - hdr); /* add value at last index if container is array */ if (ctn_type == YYJSON_TYPE_ARR) { if (!idx_is_last || !insert_new) { return_err_resolve(false, token - hdr); } val = yyjson_mut_obj(doc); if (!val) return_err_alloc(false); /* delay attaching until all operations are completed */ sep_ctn = ctn; sep_key = NULL; sep_val = val; /* move to next token */ ctn = val; val = NULL; ctn_type = YYJSON_TYPE_OBJ; token = ptr_next_token(&ptr, end, &token_len, &esc); if (unlikely(!token)) return_err_resolve(false, token - hdr); } /* container is object, create parent nodes */ while (ptr != end) { /* not last token */ key = ptr_new_key(token, token_len, esc, doc); if (!key) return_err_alloc(false); val = yyjson_mut_obj(doc); if (!val) return_err_alloc(false); /* delay attaching until all operations are completed */ if (!sep_ctn) { sep_ctn = ctn; sep_key = key; sep_val = val; } else { yyjson_mut_obj_add(ctn, key, val); } /* move to next token */ ctn = val; val = NULL; token = ptr_next_token(&ptr, end, &token_len, &esc); if (unlikely(!token)) return_err_syntax(false, ptr - hdr); } } /* JSON pointer is resolved, insert or replace target value */ ctn_len = unsafe_yyjson_get_len(ctn); if (ctn_type == YYJSON_TYPE_OBJ) { if (ctx) ctx->ctn = ctn; if (!val || insert_new) { /* insert new key-value pair */ key = ptr_new_key(token, token_len, esc, doc); if (unlikely(!key)) return_err_alloc(false); if (ctx) ctx->pre = ctn_len ? (yyjson_mut_val *)ctn->uni.ptr : key; unsafe_yyjson_mut_obj_add(ctn, key, new_val, ctn_len); } else { /* replace exist value */ key = pre->next->next; if (ctx) ctx->pre = pre; if (ctx) ctx->old = val; yyjson_mut_obj_put(ctn, key, new_val); } } else { /* array */ if (ctx && (val || idx_is_last)) ctx->ctn = ctn; if (insert_new) { /* append new value */ if (val) { pre->next = new_val; new_val->next = val; if (ctx) ctx->pre = pre; unsafe_yyjson_set_len(ctn, ctn_len + 1); } else if (idx_is_last) { if (ctx) ctx->pre = ctn_len ? (yyjson_mut_val *)ctn->uni.ptr : new_val; yyjson_mut_arr_append(ctn, new_val); } else { return_err_resolve(false, token - hdr); } } else { /* replace exist value */ if (!val) return_err_resolve(false, token - hdr); if (ctn_len > 1) { new_val->next = val->next; pre->next = new_val; if (ctn->uni.ptr == val) ctn->uni.ptr = new_val; } else { new_val->next = new_val; ctn->uni.ptr = new_val; pre = new_val; } if (ctx) ctx->pre = pre; if (ctx) ctx->old = val; } } /* all operations are completed, attach the new components to the target */ if (unlikely(sep_ctn)) { if (sep_key) yyjson_mut_obj_add(sep_ctn, sep_key, sep_val); else yyjson_mut_arr_append(sep_ctn, sep_val); } return true; } yyjson_mut_val *unsafe_yyjson_mut_ptr_replacex( yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { yyjson_mut_val *cur_val; yyjson_ptr_ctx cur_ctx; memset(&cur_ctx, 0, sizeof(cur_ctx)); if (!ctx) ctx = &cur_ctx; cur_val = unsafe_yyjson_mut_ptr_getx(val, ptr, len, ctx, err); if (!cur_val) return NULL; if (yyjson_mut_is_obj(ctx->ctn)) { yyjson_mut_val *key = ctx->pre->next->next; yyjson_mut_obj_put(ctx->ctn, key, new_val); } else { yyjson_ptr_ctx_replace(ctx, new_val); } ctx->old = cur_val; return cur_val; } yyjson_mut_val *unsafe_yyjson_mut_ptr_removex(yyjson_mut_val *val, const char *ptr, size_t len, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { yyjson_mut_val *cur_val; yyjson_ptr_ctx cur_ctx; memset(&cur_ctx, 0, sizeof(cur_ctx)); if (!ctx) ctx = &cur_ctx; cur_val = unsafe_yyjson_mut_ptr_getx(val, ptr, len, ctx, err); if (cur_val) { if (yyjson_mut_is_obj(ctx->ctn)) { yyjson_mut_val *key = ctx->pre->next->next; yyjson_mut_obj_put(ctx->ctn, key, NULL); } else { yyjson_ptr_ctx_remove(ctx); } ctx->pre = NULL; ctx->old = cur_val; } return cur_val; } /* macros for yyjson_ptr */ #undef return_err #undef return_err_resolve #undef return_err_syntax #undef return_err_alloc /*============================================================================== * JSON Patch API (RFC 6902) *============================================================================*/ /* JSON Patch operation */ typedef enum patch_op { PATCH_OP_ADD, /* path, value */ PATCH_OP_REMOVE, /* path */ PATCH_OP_REPLACE, /* path, value */ PATCH_OP_MOVE, /* from, path */ PATCH_OP_COPY, /* from, path */ PATCH_OP_TEST, /* path, value */ PATCH_OP_NONE /* invalid */ } patch_op; static patch_op patch_op_get(yyjson_val *op) { const char *str = op->uni.str; switch (unsafe_yyjson_get_len(op)) { case 3: if (!memcmp(str, "add", 3)) return PATCH_OP_ADD; return PATCH_OP_NONE; case 4: if (!memcmp(str, "move", 4)) return PATCH_OP_MOVE; if (!memcmp(str, "copy", 4)) return PATCH_OP_COPY; if (!memcmp(str, "test", 4)) return PATCH_OP_TEST; return PATCH_OP_NONE; case 6: if (!memcmp(str, "remove", 6)) return PATCH_OP_REMOVE; return PATCH_OP_NONE; case 7: if (!memcmp(str, "replace", 7)) return PATCH_OP_REPLACE; return PATCH_OP_NONE; default: return PATCH_OP_NONE; } } /* macros for yyjson_patch */ #define return_err(_code, _msg) do { \ if (err->ptr.code == YYJSON_PTR_ERR_MEMORY_ALLOCATION) { \ err->code = YYJSON_PATCH_ERROR_MEMORY_ALLOCATION; \ err->msg = _msg; \ memset(&err->ptr, 0, sizeof(yyjson_ptr_err)); \ } else { \ err->code = YYJSON_PATCH_ERROR_##_code; \ err->msg = _msg; \ err->idx = iter.idx ? iter.idx - 1 : 0; \ } \ return NULL; \ } while (false) #define return_err_copy() \ return_err(MEMORY_ALLOCATION, "failed to copy value") #define return_err_key(_key) \ return_err(MISSING_KEY, "missing key " _key) #define return_err_val(_key) \ return_err(INVALID_MEMBER, "invalid member " _key) #define ptr_get(_ptr) yyjson_mut_ptr_getx( \ root, _ptr->uni.str, _ptr##_len, NULL, &err->ptr) #define ptr_add(_ptr, _val) yyjson_mut_ptr_addx( \ root, _ptr->uni.str, _ptr##_len, _val, doc, false, NULL, &err->ptr) #define ptr_remove(_ptr) yyjson_mut_ptr_removex( \ root, _ptr->uni.str, _ptr##_len, NULL, &err->ptr) #define ptr_replace(_ptr, _val)yyjson_mut_ptr_replacex( \ root, _ptr->uni.str, _ptr##_len, _val, NULL, &err->ptr) yyjson_mut_val *yyjson_patch(yyjson_mut_doc *doc, yyjson_val *orig, yyjson_val *patch, yyjson_patch_err *err) { yyjson_mut_val *root; yyjson_val *obj; yyjson_arr_iter iter; yyjson_patch_err err_tmp; if (!err) err = &err_tmp; memset(err, 0, sizeof(*err)); memset(&iter, 0, sizeof(iter)); if (unlikely(!doc || !orig || !patch)) { return_err(INVALID_PARAMETER, "input parameter is NULL"); } if (unlikely(!yyjson_is_arr(patch))) { return_err(INVALID_PARAMETER, "input patch is not array"); } root = yyjson_val_mut_copy(doc, orig); if (unlikely(!root)) return_err_copy(); /* iterate through the patch array */ yyjson_arr_iter_init(patch, &iter); while ((obj = yyjson_arr_iter_next(&iter))) { patch_op op_enum; yyjson_val *op, *path, *from = NULL, *value; yyjson_mut_val *val = NULL, *test; usize path_len, from_len = 0; if (unlikely(!unsafe_yyjson_is_obj(obj))) { return_err(INVALID_OPERATION, "JSON patch operation is not object"); } /* get required member: op */ op = yyjson_obj_get(obj, "op"); if (unlikely(!op)) return_err_key("`op`"); if (unlikely(!yyjson_is_str(op))) return_err_val("`op`"); op_enum = patch_op_get(op); /* get required member: path */ path = yyjson_obj_get(obj, "path"); if (unlikely(!path)) return_err_key("`path`"); if (unlikely(!yyjson_is_str(path))) return_err_val("`path`"); path_len = unsafe_yyjson_get_len(path); /* get required member: value, from */ switch ((int)op_enum) { case PATCH_OP_ADD: case PATCH_OP_REPLACE: case PATCH_OP_TEST: value = yyjson_obj_get(obj, "value"); if (unlikely(!value)) return_err_key("`value`"); val = yyjson_val_mut_copy(doc, value); if (unlikely(!val)) return_err_copy(); break; case PATCH_OP_MOVE: case PATCH_OP_COPY: from = yyjson_obj_get(obj, "from"); if (unlikely(!from)) return_err_key("`from`"); if (unlikely(!yyjson_is_str(from))) return_err_val("`from`"); from_len = unsafe_yyjson_get_len(from); break; default: break; } /* perform an operation */ switch ((int)op_enum) { case PATCH_OP_ADD: /* add(path, val) */ if (unlikely(path_len == 0)) { root = val; break; } if (unlikely(!ptr_add(path, val))) { return_err(POINTER, "failed to add `path`"); } break; case PATCH_OP_REMOVE: /* remove(path) */ if (unlikely(!ptr_remove(path))) { return_err(POINTER, "failed to remove `path`"); } break; case PATCH_OP_REPLACE: /* replace(path, val) */ if (unlikely(path_len == 0)) { root = val; break; } if (unlikely(!ptr_replace(path, val))) { return_err(POINTER, "failed to replace `path`"); } break; case PATCH_OP_MOVE: /* val = remove(from), add(path, val) */ if (unlikely(from_len == 0 && path_len == 0)) break; val = ptr_remove(from); if (unlikely(!val)) { return_err(POINTER, "failed to remove `from`"); } if (unlikely(path_len == 0)) { root = val; break; } if (unlikely(!ptr_add(path, val))) { return_err(POINTER, "failed to add `path`"); } break; case PATCH_OP_COPY: /* val = get(from).copy, add(path, val) */ val = ptr_get(from); if (unlikely(!val)) { return_err(POINTER, "failed to get `from`"); } if (unlikely(path_len == 0)) { root = val; break; } val = yyjson_mut_val_mut_copy(doc, val); if (unlikely(!val)) return_err_copy(); if (unlikely(!ptr_add(path, val))) { return_err(POINTER, "failed to add `path`"); } break; case PATCH_OP_TEST: /* test = get(path), test.eq(val) */ test = ptr_get(path); if (unlikely(!test)) { return_err(POINTER, "failed to get `path`"); } if (unlikely(!yyjson_mut_equals(val, test))) { return_err(EQUAL, "failed to test equal"); } break; default: return_err(INVALID_MEMBER, "unsupported `op`"); } } return root; } yyjson_mut_val *yyjson_mut_patch(yyjson_mut_doc *doc, yyjson_mut_val *orig, yyjson_mut_val *patch, yyjson_patch_err *err) { yyjson_mut_val *root, *obj; yyjson_mut_arr_iter iter; yyjson_patch_err err_tmp; if (!err) err = &err_tmp; memset(err, 0, sizeof(*err)); memset(&iter, 0, sizeof(iter)); if (unlikely(!doc || !orig || !patch)) { return_err(INVALID_PARAMETER, "input parameter is NULL"); } if (unlikely(!yyjson_mut_is_arr(patch))) { return_err(INVALID_PARAMETER, "input patch is not array"); } root = yyjson_mut_val_mut_copy(doc, orig); if (unlikely(!root)) return_err_copy(); /* iterate through the patch array */ yyjson_mut_arr_iter_init(patch, &iter); while ((obj = yyjson_mut_arr_iter_next(&iter))) { patch_op op_enum; yyjson_mut_val *op, *path, *from = NULL, *value; yyjson_mut_val *val = NULL, *test; usize path_len, from_len = 0; if (!unsafe_yyjson_is_obj(obj)) { return_err(INVALID_OPERATION, "JSON patch operation is not object"); } /* get required member: op */ op = yyjson_mut_obj_get(obj, "op"); if (unlikely(!op)) return_err_key("`op`"); if (unlikely(!yyjson_mut_is_str(op))) return_err_val("`op`"); op_enum = patch_op_get((yyjson_val *)(void *)op); /* get required member: path */ path = yyjson_mut_obj_get(obj, "path"); if (unlikely(!path)) return_err_key("`path`"); if (unlikely(!yyjson_mut_is_str(path))) return_err_val("`path`"); path_len = unsafe_yyjson_get_len(path); /* get required member: value, from */ switch ((int)op_enum) { case PATCH_OP_ADD: case PATCH_OP_REPLACE: case PATCH_OP_TEST: value = yyjson_mut_obj_get(obj, "value"); if (unlikely(!value)) return_err_key("`value`"); val = yyjson_mut_val_mut_copy(doc, value); if (unlikely(!val)) return_err_copy(); break; case PATCH_OP_MOVE: case PATCH_OP_COPY: from = yyjson_mut_obj_get(obj, "from"); if (unlikely(!from)) return_err_key("`from`"); if (unlikely(!yyjson_mut_is_str(from))) { return_err_val("`from`"); } from_len = unsafe_yyjson_get_len(from); break; default: break; } /* perform an operation */ switch ((int)op_enum) { case PATCH_OP_ADD: /* add(path, val) */ if (unlikely(path_len == 0)) { root = val; break; } if (unlikely(!ptr_add(path, val))) { return_err(POINTER, "failed to add `path`"); } break; case PATCH_OP_REMOVE: /* remove(path) */ if (unlikely(!ptr_remove(path))) { return_err(POINTER, "failed to remove `path`"); } break; case PATCH_OP_REPLACE: /* replace(path, val) */ if (unlikely(path_len == 0)) { root = val; break; } if (unlikely(!ptr_replace(path, val))) { return_err(POINTER, "failed to replace `path`"); } break; case PATCH_OP_MOVE: /* val = remove(from), add(path, val) */ if (unlikely(from_len == 0 && path_len == 0)) break; val = ptr_remove(from); if (unlikely(!val)) { return_err(POINTER, "failed to remove `from`"); } if (unlikely(path_len == 0)) { root = val; break; } if (unlikely(!ptr_add(path, val))) { return_err(POINTER, "failed to add `path`"); } break; case PATCH_OP_COPY: /* val = get(from).copy, add(path, val) */ val = ptr_get(from); if (unlikely(!val)) { return_err(POINTER, "failed to get `from`"); } if (unlikely(path_len == 0)) { root = val; break; } val = yyjson_mut_val_mut_copy(doc, val); if (unlikely(!val)) return_err_copy(); if (unlikely(!ptr_add(path, val))) { return_err(POINTER, "failed to add `path`"); } break; case PATCH_OP_TEST: /* test = get(path), test.eq(val) */ test = ptr_get(path); if (unlikely(!test)) { return_err(POINTER, "failed to get `path`"); } if (unlikely(!yyjson_mut_equals(val, test))) { return_err(EQUAL, "failed to test equal"); } break; default: return_err(INVALID_MEMBER, "unsupported `op`"); } } return root; } /* macros for yyjson_patch */ #undef return_err #undef return_err_copy #undef return_err_key #undef return_err_val #undef ptr_get #undef ptr_add #undef ptr_remove #undef ptr_replace /*============================================================================== * JSON Merge-Patch API (RFC 7386) *============================================================================*/ yyjson_mut_val *yyjson_merge_patch(yyjson_mut_doc *doc, yyjson_val *orig, yyjson_val *patch) { usize idx, max; yyjson_val *key, *orig_val, *patch_val, local_orig; yyjson_mut_val *builder, *mut_key, *mut_val, *merged_val; if (unlikely(!yyjson_is_obj(patch))) { return yyjson_val_mut_copy(doc, patch); } builder = yyjson_mut_obj(doc); if (unlikely(!builder)) return NULL; memset(&local_orig, 0, sizeof(local_orig)); if (!yyjson_is_obj(orig)) { orig = &local_orig; orig->tag = builder->tag; orig->uni = builder->uni; } /* If orig is contributing, copy any items not modified by the patch */ if (orig != &local_orig) { yyjson_obj_foreach(orig, idx, max, key, orig_val) { patch_val = yyjson_obj_getn(patch, unsafe_yyjson_get_str(key), unsafe_yyjson_get_len(key)); if (!patch_val) { mut_key = yyjson_val_mut_copy(doc, key); mut_val = yyjson_val_mut_copy(doc, orig_val); if (!yyjson_mut_obj_add(builder, mut_key, mut_val)) return NULL; } } } /* Merge items modified by the patch. */ yyjson_obj_foreach(patch, idx, max, key, patch_val) { /* null indicates the field is removed. */ if (unsafe_yyjson_is_null(patch_val)) { continue; } mut_key = yyjson_val_mut_copy(doc, key); orig_val = yyjson_obj_getn(orig, unsafe_yyjson_get_str(key), unsafe_yyjson_get_len(key)); merged_val = yyjson_merge_patch(doc, orig_val, patch_val); if (!yyjson_mut_obj_add(builder, mut_key, merged_val)) return NULL; } return builder; } yyjson_mut_val *yyjson_mut_merge_patch(yyjson_mut_doc *doc, yyjson_mut_val *orig, yyjson_mut_val *patch) { usize idx, max; yyjson_mut_val *key, *orig_val, *patch_val, local_orig; yyjson_mut_val *builder, *mut_key, *mut_val, *merged_val; if (unlikely(!yyjson_mut_is_obj(patch))) { return yyjson_mut_val_mut_copy(doc, patch); } builder = yyjson_mut_obj(doc); if (unlikely(!builder)) return NULL; memset(&local_orig, 0, sizeof(local_orig)); if (!yyjson_mut_is_obj(orig)) { orig = &local_orig; orig->tag = builder->tag; orig->uni = builder->uni; } /* If orig is contributing, copy any items not modified by the patch */ if (orig != &local_orig) { yyjson_mut_obj_foreach(orig, idx, max, key, orig_val) { patch_val = yyjson_mut_obj_getn(patch, unsafe_yyjson_get_str(key), unsafe_yyjson_get_len(key)); if (!patch_val) { mut_key = yyjson_mut_val_mut_copy(doc, key); mut_val = yyjson_mut_val_mut_copy(doc, orig_val); if (!yyjson_mut_obj_add(builder, mut_key, mut_val)) return NULL; } } } /* Merge items modified by the patch. */ yyjson_mut_obj_foreach(patch, idx, max, key, patch_val) { /* null indicates the field is removed. */ if (unsafe_yyjson_is_null(patch_val)) { continue; } mut_key = yyjson_mut_val_mut_copy(doc, key); orig_val = yyjson_mut_obj_getn(orig, unsafe_yyjson_get_str(key), unsafe_yyjson_get_len(key)); merged_val = yyjson_mut_merge_patch(doc, orig_val, patch_val); if (!yyjson_mut_obj_add(builder, mut_key, merged_val)) return NULL; } return builder; } #endif /* YYJSON_DISABLE_UTILS */ /*============================================================================== * Power10 Lookup Table * These data are used by the floating-point number reader and writer. *============================================================================*/ #if (!YYJSON_DISABLE_READER || !YYJSON_DISABLE_WRITER) && \ (!YYJSON_DISABLE_FAST_FP_CONV) /** Minimum decimal exponent in pow10_sig_table. */ #define POW10_SIG_TABLE_MIN_EXP -343 /** Maximum decimal exponent in pow10_sig_table. */ #define POW10_SIG_TABLE_MAX_EXP 324 /** Minimum exact decimal exponent in pow10_sig_table */ #define POW10_SIG_TABLE_MIN_EXACT_EXP 0 /** Maximum exact decimal exponent in pow10_sig_table */ #define POW10_SIG_TABLE_MAX_EXACT_EXP 55 /** Normalized significant 128 bits of pow10, no rounded up (size: 10.4KB). This lookup table is used by both the double number reader and writer. (generate with misc/make_tables.c) */ static const u64 pow10_sig_table[] = { U64(0xBF29DCAB, 0xA82FDEAE), U64(0x7432EE87, 0x3880FC33), /* ~= 10^-343 */ U64(0xEEF453D6, 0x923BD65A), U64(0x113FAA29, 0x06A13B3F), /* ~= 10^-342 */ U64(0x9558B466, 0x1B6565F8), U64(0x4AC7CA59, 0xA424C507), /* ~= 10^-341 */ U64(0xBAAEE17F, 0xA23EBF76), U64(0x5D79BCF0, 0x0D2DF649), /* ~= 10^-340 */ U64(0xE95A99DF, 0x8ACE6F53), U64(0xF4D82C2C, 0x107973DC), /* ~= 10^-339 */ U64(0x91D8A02B, 0xB6C10594), U64(0x79071B9B, 0x8A4BE869), /* ~= 10^-338 */ U64(0xB64EC836, 0xA47146F9), U64(0x9748E282, 0x6CDEE284), /* ~= 10^-337 */ U64(0xE3E27A44, 0x4D8D98B7), U64(0xFD1B1B23, 0x08169B25), /* ~= 10^-336 */ U64(0x8E6D8C6A, 0xB0787F72), U64(0xFE30F0F5, 0xE50E20F7), /* ~= 10^-335 */ U64(0xB208EF85, 0x5C969F4F), U64(0xBDBD2D33, 0x5E51A935), /* ~= 10^-334 */ U64(0xDE8B2B66, 0xB3BC4723), U64(0xAD2C7880, 0x35E61382), /* ~= 10^-333 */ U64(0x8B16FB20, 0x3055AC76), U64(0x4C3BCB50, 0x21AFCC31), /* ~= 10^-332 */ U64(0xADDCB9E8, 0x3C6B1793), U64(0xDF4ABE24, 0x2A1BBF3D), /* ~= 10^-331 */ U64(0xD953E862, 0x4B85DD78), U64(0xD71D6DAD, 0x34A2AF0D), /* ~= 10^-330 */ U64(0x87D4713D, 0x6F33AA6B), U64(0x8672648C, 0x40E5AD68), /* ~= 10^-329 */ U64(0xA9C98D8C, 0xCB009506), U64(0x680EFDAF, 0x511F18C2), /* ~= 10^-328 */ U64(0xD43BF0EF, 0xFDC0BA48), U64(0x0212BD1B, 0x2566DEF2), /* ~= 10^-327 */ U64(0x84A57695, 0xFE98746D), U64(0x014BB630, 0xF7604B57), /* ~= 10^-326 */ U64(0xA5CED43B, 0x7E3E9188), U64(0x419EA3BD, 0x35385E2D), /* ~= 10^-325 */ U64(0xCF42894A, 0x5DCE35EA), U64(0x52064CAC, 0x828675B9), /* ~= 10^-324 */ U64(0x818995CE, 0x7AA0E1B2), U64(0x7343EFEB, 0xD1940993), /* ~= 10^-323 */ U64(0xA1EBFB42, 0x19491A1F), U64(0x1014EBE6, 0xC5F90BF8), /* ~= 10^-322 */ U64(0xCA66FA12, 0x9F9B60A6), U64(0xD41A26E0, 0x77774EF6), /* ~= 10^-321 */ U64(0xFD00B897, 0x478238D0), U64(0x8920B098, 0x955522B4), /* ~= 10^-320 */ U64(0x9E20735E, 0x8CB16382), U64(0x55B46E5F, 0x5D5535B0), /* ~= 10^-319 */ U64(0xC5A89036, 0x2FDDBC62), U64(0xEB2189F7, 0x34AA831D), /* ~= 10^-318 */ U64(0xF712B443, 0xBBD52B7B), U64(0xA5E9EC75, 0x01D523E4), /* ~= 10^-317 */ U64(0x9A6BB0AA, 0x55653B2D), U64(0x47B233C9, 0x2125366E), /* ~= 10^-316 */ U64(0xC1069CD4, 0xEABE89F8), U64(0x999EC0BB, 0x696E840A), /* ~= 10^-315 */ U64(0xF148440A, 0x256E2C76), U64(0xC00670EA, 0x43CA250D), /* ~= 10^-314 */ U64(0x96CD2A86, 0x5764DBCA), U64(0x38040692, 0x6A5E5728), /* ~= 10^-313 */ U64(0xBC807527, 0xED3E12BC), U64(0xC6050837, 0x04F5ECF2), /* ~= 10^-312 */ U64(0xEBA09271, 0xE88D976B), U64(0xF7864A44, 0xC633682E), /* ~= 10^-311 */ U64(0x93445B87, 0x31587EA3), U64(0x7AB3EE6A, 0xFBE0211D), /* ~= 10^-310 */ U64(0xB8157268, 0xFDAE9E4C), U64(0x5960EA05, 0xBAD82964), /* ~= 10^-309 */ U64(0xE61ACF03, 0x3D1A45DF), U64(0x6FB92487, 0x298E33BD), /* ~= 10^-308 */ U64(0x8FD0C162, 0x06306BAB), U64(0xA5D3B6D4, 0x79F8E056), /* ~= 10^-307 */ U64(0xB3C4F1BA, 0x87BC8696), U64(0x8F48A489, 0x9877186C), /* ~= 10^-306 */ U64(0xE0B62E29, 0x29ABA83C), U64(0x331ACDAB, 0xFE94DE87), /* ~= 10^-305 */ U64(0x8C71DCD9, 0xBA0B4925), U64(0x9FF0C08B, 0x7F1D0B14), /* ~= 10^-304 */ U64(0xAF8E5410, 0x288E1B6F), U64(0x07ECF0AE, 0x5EE44DD9), /* ~= 10^-303 */ U64(0xDB71E914, 0x32B1A24A), U64(0xC9E82CD9, 0xF69D6150), /* ~= 10^-302 */ U64(0x892731AC, 0x9FAF056E), U64(0xBE311C08, 0x3A225CD2), /* ~= 10^-301 */ U64(0xAB70FE17, 0xC79AC6CA), U64(0x6DBD630A, 0x48AAF406), /* ~= 10^-300 */ U64(0xD64D3D9D, 0xB981787D), U64(0x092CBBCC, 0xDAD5B108), /* ~= 10^-299 */ U64(0x85F04682, 0x93F0EB4E), U64(0x25BBF560, 0x08C58EA5), /* ~= 10^-298 */ U64(0xA76C5823, 0x38ED2621), U64(0xAF2AF2B8, 0x0AF6F24E), /* ~= 10^-297 */ U64(0xD1476E2C, 0x07286FAA), U64(0x1AF5AF66, 0x0DB4AEE1), /* ~= 10^-296 */ U64(0x82CCA4DB, 0x847945CA), U64(0x50D98D9F, 0xC890ED4D), /* ~= 10^-295 */ U64(0xA37FCE12, 0x6597973C), U64(0xE50FF107, 0xBAB528A0), /* ~= 10^-294 */ U64(0xCC5FC196, 0xFEFD7D0C), U64(0x1E53ED49, 0xA96272C8), /* ~= 10^-293 */ U64(0xFF77B1FC, 0xBEBCDC4F), U64(0x25E8E89C, 0x13BB0F7A), /* ~= 10^-292 */ U64(0x9FAACF3D, 0xF73609B1), U64(0x77B19161, 0x8C54E9AC), /* ~= 10^-291 */ U64(0xC795830D, 0x75038C1D), U64(0xD59DF5B9, 0xEF6A2417), /* ~= 10^-290 */ U64(0xF97AE3D0, 0xD2446F25), U64(0x4B057328, 0x6B44AD1D), /* ~= 10^-289 */ U64(0x9BECCE62, 0x836AC577), U64(0x4EE367F9, 0x430AEC32), /* ~= 10^-288 */ U64(0xC2E801FB, 0x244576D5), U64(0x229C41F7, 0x93CDA73F), /* ~= 10^-287 */ U64(0xF3A20279, 0xED56D48A), U64(0x6B435275, 0x78C1110F), /* ~= 10^-286 */ U64(0x9845418C, 0x345644D6), U64(0x830A1389, 0x6B78AAA9), /* ~= 10^-285 */ U64(0xBE5691EF, 0x416BD60C), U64(0x23CC986B, 0xC656D553), /* ~= 10^-284 */ U64(0xEDEC366B, 0x11C6CB8F), U64(0x2CBFBE86, 0xB7EC8AA8), /* ~= 10^-283 */ U64(0x94B3A202, 0xEB1C3F39), U64(0x7BF7D714, 0x32F3D6A9), /* ~= 10^-282 */ U64(0xB9E08A83, 0xA5E34F07), U64(0xDAF5CCD9, 0x3FB0CC53), /* ~= 10^-281 */ U64(0xE858AD24, 0x8F5C22C9), U64(0xD1B3400F, 0x8F9CFF68), /* ~= 10^-280 */ U64(0x91376C36, 0xD99995BE), U64(0x23100809, 0xB9C21FA1), /* ~= 10^-279 */ U64(0xB5854744, 0x8FFFFB2D), U64(0xABD40A0C, 0x2832A78A), /* ~= 10^-278 */ U64(0xE2E69915, 0xB3FFF9F9), U64(0x16C90C8F, 0x323F516C), /* ~= 10^-277 */ U64(0x8DD01FAD, 0x907FFC3B), U64(0xAE3DA7D9, 0x7F6792E3), /* ~= 10^-276 */ U64(0xB1442798, 0xF49FFB4A), U64(0x99CD11CF, 0xDF41779C), /* ~= 10^-275 */ U64(0xDD95317F, 0x31C7FA1D), U64(0x40405643, 0xD711D583), /* ~= 10^-274 */ U64(0x8A7D3EEF, 0x7F1CFC52), U64(0x482835EA, 0x666B2572), /* ~= 10^-273 */ U64(0xAD1C8EAB, 0x5EE43B66), U64(0xDA324365, 0x0005EECF), /* ~= 10^-272 */ U64(0xD863B256, 0x369D4A40), U64(0x90BED43E, 0x40076A82), /* ~= 10^-271 */ U64(0x873E4F75, 0xE2224E68), U64(0x5A7744A6, 0xE804A291), /* ~= 10^-270 */ U64(0xA90DE353, 0x5AAAE202), U64(0x711515D0, 0xA205CB36), /* ~= 10^-269 */ U64(0xD3515C28, 0x31559A83), U64(0x0D5A5B44, 0xCA873E03), /* ~= 10^-268 */ U64(0x8412D999, 0x1ED58091), U64(0xE858790A, 0xFE9486C2), /* ~= 10^-267 */ U64(0xA5178FFF, 0x668AE0B6), U64(0x626E974D, 0xBE39A872), /* ~= 10^-266 */ U64(0xCE5D73FF, 0x402D98E3), U64(0xFB0A3D21, 0x2DC8128F), /* ~= 10^-265 */ U64(0x80FA687F, 0x881C7F8E), U64(0x7CE66634, 0xBC9D0B99), /* ~= 10^-264 */ U64(0xA139029F, 0x6A239F72), U64(0x1C1FFFC1, 0xEBC44E80), /* ~= 10^-263 */ U64(0xC9874347, 0x44AC874E), U64(0xA327FFB2, 0x66B56220), /* ~= 10^-262 */ U64(0xFBE91419, 0x15D7A922), U64(0x4BF1FF9F, 0x0062BAA8), /* ~= 10^-261 */ U64(0x9D71AC8F, 0xADA6C9B5), U64(0x6F773FC3, 0x603DB4A9), /* ~= 10^-260 */ U64(0xC4CE17B3, 0x99107C22), U64(0xCB550FB4, 0x384D21D3), /* ~= 10^-259 */ U64(0xF6019DA0, 0x7F549B2B), U64(0x7E2A53A1, 0x46606A48), /* ~= 10^-258 */ U64(0x99C10284, 0x4F94E0FB), U64(0x2EDA7444, 0xCBFC426D), /* ~= 10^-257 */ U64(0xC0314325, 0x637A1939), U64(0xFA911155, 0xFEFB5308), /* ~= 10^-256 */ U64(0xF03D93EE, 0xBC589F88), U64(0x793555AB, 0x7EBA27CA), /* ~= 10^-255 */ U64(0x96267C75, 0x35B763B5), U64(0x4BC1558B, 0x2F3458DE), /* ~= 10^-254 */ U64(0xBBB01B92, 0x83253CA2), U64(0x9EB1AAED, 0xFB016F16), /* ~= 10^-253 */ U64(0xEA9C2277, 0x23EE8BCB), U64(0x465E15A9, 0x79C1CADC), /* ~= 10^-252 */ U64(0x92A1958A, 0x7675175F), U64(0x0BFACD89, 0xEC191EC9), /* ~= 10^-251 */ U64(0xB749FAED, 0x14125D36), U64(0xCEF980EC, 0x671F667B), /* ~= 10^-250 */ U64(0xE51C79A8, 0x5916F484), U64(0x82B7E127, 0x80E7401A), /* ~= 10^-249 */ U64(0x8F31CC09, 0x37AE58D2), U64(0xD1B2ECB8, 0xB0908810), /* ~= 10^-248 */ U64(0xB2FE3F0B, 0x8599EF07), U64(0x861FA7E6, 0xDCB4AA15), /* ~= 10^-247 */ U64(0xDFBDCECE, 0x67006AC9), U64(0x67A791E0, 0x93E1D49A), /* ~= 10^-246 */ U64(0x8BD6A141, 0x006042BD), U64(0xE0C8BB2C, 0x5C6D24E0), /* ~= 10^-245 */ U64(0xAECC4991, 0x4078536D), U64(0x58FAE9F7, 0x73886E18), /* ~= 10^-244 */ U64(0xDA7F5BF5, 0x90966848), U64(0xAF39A475, 0x506A899E), /* ~= 10^-243 */ U64(0x888F9979, 0x7A5E012D), U64(0x6D8406C9, 0x52429603), /* ~= 10^-242 */ U64(0xAAB37FD7, 0xD8F58178), U64(0xC8E5087B, 0xA6D33B83), /* ~= 10^-241 */ U64(0xD5605FCD, 0xCF32E1D6), U64(0xFB1E4A9A, 0x90880A64), /* ~= 10^-240 */ U64(0x855C3BE0, 0xA17FCD26), U64(0x5CF2EEA0, 0x9A55067F), /* ~= 10^-239 */ U64(0xA6B34AD8, 0xC9DFC06F), U64(0xF42FAA48, 0xC0EA481E), /* ~= 10^-238 */ U64(0xD0601D8E, 0xFC57B08B), U64(0xF13B94DA, 0xF124DA26), /* ~= 10^-237 */ U64(0x823C1279, 0x5DB6CE57), U64(0x76C53D08, 0xD6B70858), /* ~= 10^-236 */ U64(0xA2CB1717, 0xB52481ED), U64(0x54768C4B, 0x0C64CA6E), /* ~= 10^-235 */ U64(0xCB7DDCDD, 0xA26DA268), U64(0xA9942F5D, 0xCF7DFD09), /* ~= 10^-234 */ U64(0xFE5D5415, 0x0B090B02), U64(0xD3F93B35, 0x435D7C4C), /* ~= 10^-233 */ U64(0x9EFA548D, 0x26E5A6E1), U64(0xC47BC501, 0x4A1A6DAF), /* ~= 10^-232 */ U64(0xC6B8E9B0, 0x709F109A), U64(0x359AB641, 0x9CA1091B), /* ~= 10^-231 */ U64(0xF867241C, 0x8CC6D4C0), U64(0xC30163D2, 0x03C94B62), /* ~= 10^-230 */ U64(0x9B407691, 0xD7FC44F8), U64(0x79E0DE63, 0x425DCF1D), /* ~= 10^-229 */ U64(0xC2109436, 0x4DFB5636), U64(0x985915FC, 0x12F542E4), /* ~= 10^-228 */ U64(0xF294B943, 0xE17A2BC4), U64(0x3E6F5B7B, 0x17B2939D), /* ~= 10^-227 */ U64(0x979CF3CA, 0x6CEC5B5A), U64(0xA705992C, 0xEECF9C42), /* ~= 10^-226 */ U64(0xBD8430BD, 0x08277231), U64(0x50C6FF78, 0x2A838353), /* ~= 10^-225 */ U64(0xECE53CEC, 0x4A314EBD), U64(0xA4F8BF56, 0x35246428), /* ~= 10^-224 */ U64(0x940F4613, 0xAE5ED136), U64(0x871B7795, 0xE136BE99), /* ~= 10^-223 */ U64(0xB9131798, 0x99F68584), U64(0x28E2557B, 0x59846E3F), /* ~= 10^-222 */ U64(0xE757DD7E, 0xC07426E5), U64(0x331AEADA, 0x2FE589CF), /* ~= 10^-221 */ U64(0x9096EA6F, 0x3848984F), U64(0x3FF0D2C8, 0x5DEF7621), /* ~= 10^-220 */ U64(0xB4BCA50B, 0x065ABE63), U64(0x0FED077A, 0x756B53A9), /* ~= 10^-219 */ U64(0xE1EBCE4D, 0xC7F16DFB), U64(0xD3E84959, 0x12C62894), /* ~= 10^-218 */ U64(0x8D3360F0, 0x9CF6E4BD), U64(0x64712DD7, 0xABBBD95C), /* ~= 10^-217 */ U64(0xB080392C, 0xC4349DEC), U64(0xBD8D794D, 0x96AACFB3), /* ~= 10^-216 */ U64(0xDCA04777, 0xF541C567), U64(0xECF0D7A0, 0xFC5583A0), /* ~= 10^-215 */ U64(0x89E42CAA, 0xF9491B60), U64(0xF41686C4, 0x9DB57244), /* ~= 10^-214 */ U64(0xAC5D37D5, 0xB79B6239), U64(0x311C2875, 0xC522CED5), /* ~= 10^-213 */ U64(0xD77485CB, 0x25823AC7), U64(0x7D633293, 0x366B828B), /* ~= 10^-212 */ U64(0x86A8D39E, 0xF77164BC), U64(0xAE5DFF9C, 0x02033197), /* ~= 10^-211 */ U64(0xA8530886, 0xB54DBDEB), U64(0xD9F57F83, 0x0283FDFC), /* ~= 10^-210 */ U64(0xD267CAA8, 0x62A12D66), U64(0xD072DF63, 0xC324FD7B), /* ~= 10^-209 */ U64(0x8380DEA9, 0x3DA4BC60), U64(0x4247CB9E, 0x59F71E6D), /* ~= 10^-208 */ U64(0xA4611653, 0x8D0DEB78), U64(0x52D9BE85, 0xF074E608), /* ~= 10^-207 */ U64(0xCD795BE8, 0x70516656), U64(0x67902E27, 0x6C921F8B), /* ~= 10^-206 */ U64(0x806BD971, 0x4632DFF6), U64(0x00BA1CD8, 0xA3DB53B6), /* ~= 10^-205 */ U64(0xA086CFCD, 0x97BF97F3), U64(0x80E8A40E, 0xCCD228A4), /* ~= 10^-204 */ U64(0xC8A883C0, 0xFDAF7DF0), U64(0x6122CD12, 0x8006B2CD), /* ~= 10^-203 */ U64(0xFAD2A4B1, 0x3D1B5D6C), U64(0x796B8057, 0x20085F81), /* ~= 10^-202 */ U64(0x9CC3A6EE, 0xC6311A63), U64(0xCBE33036, 0x74053BB0), /* ~= 10^-201 */ U64(0xC3F490AA, 0x77BD60FC), U64(0xBEDBFC44, 0x11068A9C), /* ~= 10^-200 */ U64(0xF4F1B4D5, 0x15ACB93B), U64(0xEE92FB55, 0x15482D44), /* ~= 10^-199 */ U64(0x99171105, 0x2D8BF3C5), U64(0x751BDD15, 0x2D4D1C4A), /* ~= 10^-198 */ U64(0xBF5CD546, 0x78EEF0B6), U64(0xD262D45A, 0x78A0635D), /* ~= 10^-197 */ U64(0xEF340A98, 0x172AACE4), U64(0x86FB8971, 0x16C87C34), /* ~= 10^-196 */ U64(0x9580869F, 0x0E7AAC0E), U64(0xD45D35E6, 0xAE3D4DA0), /* ~= 10^-195 */ U64(0xBAE0A846, 0xD2195712), U64(0x89748360, 0x59CCA109), /* ~= 10^-194 */ U64(0xE998D258, 0x869FACD7), U64(0x2BD1A438, 0x703FC94B), /* ~= 10^-193 */ U64(0x91FF8377, 0x5423CC06), U64(0x7B6306A3, 0x4627DDCF), /* ~= 10^-192 */ U64(0xB67F6455, 0x292CBF08), U64(0x1A3BC84C, 0x17B1D542), /* ~= 10^-191 */ U64(0xE41F3D6A, 0x7377EECA), U64(0x20CABA5F, 0x1D9E4A93), /* ~= 10^-190 */ U64(0x8E938662, 0x882AF53E), U64(0x547EB47B, 0x7282EE9C), /* ~= 10^-189 */ U64(0xB23867FB, 0x2A35B28D), U64(0xE99E619A, 0x4F23AA43), /* ~= 10^-188 */ U64(0xDEC681F9, 0xF4C31F31), U64(0x6405FA00, 0xE2EC94D4), /* ~= 10^-187 */ U64(0x8B3C113C, 0x38F9F37E), U64(0xDE83BC40, 0x8DD3DD04), /* ~= 10^-186 */ U64(0xAE0B158B, 0x4738705E), U64(0x9624AB50, 0xB148D445), /* ~= 10^-185 */ U64(0xD98DDAEE, 0x19068C76), U64(0x3BADD624, 0xDD9B0957), /* ~= 10^-184 */ U64(0x87F8A8D4, 0xCFA417C9), U64(0xE54CA5D7, 0x0A80E5D6), /* ~= 10^-183 */ U64(0xA9F6D30A, 0x038D1DBC), U64(0x5E9FCF4C, 0xCD211F4C), /* ~= 10^-182 */ U64(0xD47487CC, 0x8470652B), U64(0x7647C320, 0x0069671F), /* ~= 10^-181 */ U64(0x84C8D4DF, 0xD2C63F3B), U64(0x29ECD9F4, 0x0041E073), /* ~= 10^-180 */ U64(0xA5FB0A17, 0xC777CF09), U64(0xF4681071, 0x00525890), /* ~= 10^-179 */ U64(0xCF79CC9D, 0xB955C2CC), U64(0x7182148D, 0x4066EEB4), /* ~= 10^-178 */ U64(0x81AC1FE2, 0x93D599BF), U64(0xC6F14CD8, 0x48405530), /* ~= 10^-177 */ U64(0xA21727DB, 0x38CB002F), U64(0xB8ADA00E, 0x5A506A7C), /* ~= 10^-176 */ U64(0xCA9CF1D2, 0x06FDC03B), U64(0xA6D90811, 0xF0E4851C), /* ~= 10^-175 */ U64(0xFD442E46, 0x88BD304A), U64(0x908F4A16, 0x6D1DA663), /* ~= 10^-174 */ U64(0x9E4A9CEC, 0x15763E2E), U64(0x9A598E4E, 0x043287FE), /* ~= 10^-173 */ U64(0xC5DD4427, 0x1AD3CDBA), U64(0x40EFF1E1, 0x853F29FD), /* ~= 10^-172 */ U64(0xF7549530, 0xE188C128), U64(0xD12BEE59, 0xE68EF47C), /* ~= 10^-171 */ U64(0x9A94DD3E, 0x8CF578B9), U64(0x82BB74F8, 0x301958CE), /* ~= 10^-170 */ U64(0xC13A148E, 0x3032D6E7), U64(0xE36A5236, 0x3C1FAF01), /* ~= 10^-169 */ U64(0xF18899B1, 0xBC3F8CA1), U64(0xDC44E6C3, 0xCB279AC1), /* ~= 10^-168 */ U64(0x96F5600F, 0x15A7B7E5), U64(0x29AB103A, 0x5EF8C0B9), /* ~= 10^-167 */ U64(0xBCB2B812, 0xDB11A5DE), U64(0x7415D448, 0xF6B6F0E7), /* ~= 10^-166 */ U64(0xEBDF6617, 0x91D60F56), U64(0x111B495B, 0x3464AD21), /* ~= 10^-165 */ U64(0x936B9FCE, 0xBB25C995), U64(0xCAB10DD9, 0x00BEEC34), /* ~= 10^-164 */ U64(0xB84687C2, 0x69EF3BFB), U64(0x3D5D514F, 0x40EEA742), /* ~= 10^-163 */ U64(0xE65829B3, 0x046B0AFA), U64(0x0CB4A5A3, 0x112A5112), /* ~= 10^-162 */ U64(0x8FF71A0F, 0xE2C2E6DC), U64(0x47F0E785, 0xEABA72AB), /* ~= 10^-161 */ U64(0xB3F4E093, 0xDB73A093), U64(0x59ED2167, 0x65690F56), /* ~= 10^-160 */ U64(0xE0F218B8, 0xD25088B8), U64(0x306869C1, 0x3EC3532C), /* ~= 10^-159 */ U64(0x8C974F73, 0x83725573), U64(0x1E414218, 0xC73A13FB), /* ~= 10^-158 */ U64(0xAFBD2350, 0x644EEACF), U64(0xE5D1929E, 0xF90898FA), /* ~= 10^-157 */ U64(0xDBAC6C24, 0x7D62A583), U64(0xDF45F746, 0xB74ABF39), /* ~= 10^-156 */ U64(0x894BC396, 0xCE5DA772), U64(0x6B8BBA8C, 0x328EB783), /* ~= 10^-155 */ U64(0xAB9EB47C, 0x81F5114F), U64(0x066EA92F, 0x3F326564), /* ~= 10^-154 */ U64(0xD686619B, 0xA27255A2), U64(0xC80A537B, 0x0EFEFEBD), /* ~= 10^-153 */ U64(0x8613FD01, 0x45877585), U64(0xBD06742C, 0xE95F5F36), /* ~= 10^-152 */ U64(0xA798FC41, 0x96E952E7), U64(0x2C481138, 0x23B73704), /* ~= 10^-151 */ U64(0xD17F3B51, 0xFCA3A7A0), U64(0xF75A1586, 0x2CA504C5), /* ~= 10^-150 */ U64(0x82EF8513, 0x3DE648C4), U64(0x9A984D73, 0xDBE722FB), /* ~= 10^-149 */ U64(0xA3AB6658, 0x0D5FDAF5), U64(0xC13E60D0, 0xD2E0EBBA), /* ~= 10^-148 */ U64(0xCC963FEE, 0x10B7D1B3), U64(0x318DF905, 0x079926A8), /* ~= 10^-147 */ U64(0xFFBBCFE9, 0x94E5C61F), U64(0xFDF17746, 0x497F7052), /* ~= 10^-146 */ U64(0x9FD561F1, 0xFD0F9BD3), U64(0xFEB6EA8B, 0xEDEFA633), /* ~= 10^-145 */ U64(0xC7CABA6E, 0x7C5382C8), U64(0xFE64A52E, 0xE96B8FC0), /* ~= 10^-144 */ U64(0xF9BD690A, 0x1B68637B), U64(0x3DFDCE7A, 0xA3C673B0), /* ~= 10^-143 */ U64(0x9C1661A6, 0x51213E2D), U64(0x06BEA10C, 0xA65C084E), /* ~= 10^-142 */ U64(0xC31BFA0F, 0xE5698DB8), U64(0x486E494F, 0xCFF30A62), /* ~= 10^-141 */ U64(0xF3E2F893, 0xDEC3F126), U64(0x5A89DBA3, 0xC3EFCCFA), /* ~= 10^-140 */ U64(0x986DDB5C, 0x6B3A76B7), U64(0xF8962946, 0x5A75E01C), /* ~= 10^-139 */ U64(0xBE895233, 0x86091465), U64(0xF6BBB397, 0xF1135823), /* ~= 10^-138 */ U64(0xEE2BA6C0, 0x678B597F), U64(0x746AA07D, 0xED582E2C), /* ~= 10^-137 */ U64(0x94DB4838, 0x40B717EF), U64(0xA8C2A44E, 0xB4571CDC), /* ~= 10^-136 */ U64(0xBA121A46, 0x50E4DDEB), U64(0x92F34D62, 0x616CE413), /* ~= 10^-135 */ U64(0xE896A0D7, 0xE51E1566), U64(0x77B020BA, 0xF9C81D17), /* ~= 10^-134 */ U64(0x915E2486, 0xEF32CD60), U64(0x0ACE1474, 0xDC1D122E), /* ~= 10^-133 */ U64(0xB5B5ADA8, 0xAAFF80B8), U64(0x0D819992, 0x132456BA), /* ~= 10^-132 */ U64(0xE3231912, 0xD5BF60E6), U64(0x10E1FFF6, 0x97ED6C69), /* ~= 10^-131 */ U64(0x8DF5EFAB, 0xC5979C8F), U64(0xCA8D3FFA, 0x1EF463C1), /* ~= 10^-130 */ U64(0xB1736B96, 0xB6FD83B3), U64(0xBD308FF8, 0xA6B17CB2), /* ~= 10^-129 */ U64(0xDDD0467C, 0x64BCE4A0), U64(0xAC7CB3F6, 0xD05DDBDE), /* ~= 10^-128 */ U64(0x8AA22C0D, 0xBEF60EE4), U64(0x6BCDF07A, 0x423AA96B), /* ~= 10^-127 */ U64(0xAD4AB711, 0x2EB3929D), U64(0x86C16C98, 0xD2C953C6), /* ~= 10^-126 */ U64(0xD89D64D5, 0x7A607744), U64(0xE871C7BF, 0x077BA8B7), /* ~= 10^-125 */ U64(0x87625F05, 0x6C7C4A8B), U64(0x11471CD7, 0x64AD4972), /* ~= 10^-124 */ U64(0xA93AF6C6, 0xC79B5D2D), U64(0xD598E40D, 0x3DD89BCF), /* ~= 10^-123 */ U64(0xD389B478, 0x79823479), U64(0x4AFF1D10, 0x8D4EC2C3), /* ~= 10^-122 */ U64(0x843610CB, 0x4BF160CB), U64(0xCEDF722A, 0x585139BA), /* ~= 10^-121 */ U64(0xA54394FE, 0x1EEDB8FE), U64(0xC2974EB4, 0xEE658828), /* ~= 10^-120 */ U64(0xCE947A3D, 0xA6A9273E), U64(0x733D2262, 0x29FEEA32), /* ~= 10^-119 */ U64(0x811CCC66, 0x8829B887), U64(0x0806357D, 0x5A3F525F), /* ~= 10^-118 */ U64(0xA163FF80, 0x2A3426A8), U64(0xCA07C2DC, 0xB0CF26F7), /* ~= 10^-117 */ U64(0xC9BCFF60, 0x34C13052), U64(0xFC89B393, 0xDD02F0B5), /* ~= 10^-116 */ U64(0xFC2C3F38, 0x41F17C67), U64(0xBBAC2078, 0xD443ACE2), /* ~= 10^-115 */ U64(0x9D9BA783, 0x2936EDC0), U64(0xD54B944B, 0x84AA4C0D), /* ~= 10^-114 */ U64(0xC5029163, 0xF384A931), U64(0x0A9E795E, 0x65D4DF11), /* ~= 10^-113 */ U64(0xF64335BC, 0xF065D37D), U64(0x4D4617B5, 0xFF4A16D5), /* ~= 10^-112 */ U64(0x99EA0196, 0x163FA42E), U64(0x504BCED1, 0xBF8E4E45), /* ~= 10^-111 */ U64(0xC06481FB, 0x9BCF8D39), U64(0xE45EC286, 0x2F71E1D6), /* ~= 10^-110 */ U64(0xF07DA27A, 0x82C37088), U64(0x5D767327, 0xBB4E5A4C), /* ~= 10^-109 */ U64(0x964E858C, 0x91BA2655), U64(0x3A6A07F8, 0xD510F86F), /* ~= 10^-108 */ U64(0xBBE226EF, 0xB628AFEA), U64(0x890489F7, 0x0A55368B), /* ~= 10^-107 */ U64(0xEADAB0AB, 0xA3B2DBE5), U64(0x2B45AC74, 0xCCEA842E), /* ~= 10^-106 */ U64(0x92C8AE6B, 0x464FC96F), U64(0x3B0B8BC9, 0x0012929D), /* ~= 10^-105 */ U64(0xB77ADA06, 0x17E3BBCB), U64(0x09CE6EBB, 0x40173744), /* ~= 10^-104 */ U64(0xE5599087, 0x9DDCAABD), U64(0xCC420A6A, 0x101D0515), /* ~= 10^-103 */ U64(0x8F57FA54, 0xC2A9EAB6), U64(0x9FA94682, 0x4A12232D), /* ~= 10^-102 */ U64(0xB32DF8E9, 0xF3546564), U64(0x47939822, 0xDC96ABF9), /* ~= 10^-101 */ U64(0xDFF97724, 0x70297EBD), U64(0x59787E2B, 0x93BC56F7), /* ~= 10^-100 */ U64(0x8BFBEA76, 0xC619EF36), U64(0x57EB4EDB, 0x3C55B65A), /* ~= 10^-99 */ U64(0xAEFAE514, 0x77A06B03), U64(0xEDE62292, 0x0B6B23F1), /* ~= 10^-98 */ U64(0xDAB99E59, 0x958885C4), U64(0xE95FAB36, 0x8E45ECED), /* ~= 10^-97 */ U64(0x88B402F7, 0xFD75539B), U64(0x11DBCB02, 0x18EBB414), /* ~= 10^-96 */ U64(0xAAE103B5, 0xFCD2A881), U64(0xD652BDC2, 0x9F26A119), /* ~= 10^-95 */ U64(0xD59944A3, 0x7C0752A2), U64(0x4BE76D33, 0x46F0495F), /* ~= 10^-94 */ U64(0x857FCAE6, 0x2D8493A5), U64(0x6F70A440, 0x0C562DDB), /* ~= 10^-93 */ U64(0xA6DFBD9F, 0xB8E5B88E), U64(0xCB4CCD50, 0x0F6BB952), /* ~= 10^-92 */ U64(0xD097AD07, 0xA71F26B2), U64(0x7E2000A4, 0x1346A7A7), /* ~= 10^-91 */ U64(0x825ECC24, 0xC873782F), U64(0x8ED40066, 0x8C0C28C8), /* ~= 10^-90 */ U64(0xA2F67F2D, 0xFA90563B), U64(0x72890080, 0x2F0F32FA), /* ~= 10^-89 */ U64(0xCBB41EF9, 0x79346BCA), U64(0x4F2B40A0, 0x3AD2FFB9), /* ~= 10^-88 */ U64(0xFEA126B7, 0xD78186BC), U64(0xE2F610C8, 0x4987BFA8), /* ~= 10^-87 */ U64(0x9F24B832, 0xE6B0F436), U64(0x0DD9CA7D, 0x2DF4D7C9), /* ~= 10^-86 */ U64(0xC6EDE63F, 0xA05D3143), U64(0x91503D1C, 0x79720DBB), /* ~= 10^-85 */ U64(0xF8A95FCF, 0x88747D94), U64(0x75A44C63, 0x97CE912A), /* ~= 10^-84 */ U64(0x9B69DBE1, 0xB548CE7C), U64(0xC986AFBE, 0x3EE11ABA), /* ~= 10^-83 */ U64(0xC24452DA, 0x229B021B), U64(0xFBE85BAD, 0xCE996168), /* ~= 10^-82 */ U64(0xF2D56790, 0xAB41C2A2), U64(0xFAE27299, 0x423FB9C3), /* ~= 10^-81 */ U64(0x97C560BA, 0x6B0919A5), U64(0xDCCD879F, 0xC967D41A), /* ~= 10^-80 */ U64(0xBDB6B8E9, 0x05CB600F), U64(0x5400E987, 0xBBC1C920), /* ~= 10^-79 */ U64(0xED246723, 0x473E3813), U64(0x290123E9, 0xAAB23B68), /* ~= 10^-78 */ U64(0x9436C076, 0x0C86E30B), U64(0xF9A0B672, 0x0AAF6521), /* ~= 10^-77 */ U64(0xB9447093, 0x8FA89BCE), U64(0xF808E40E, 0x8D5B3E69), /* ~= 10^-76 */ U64(0xE7958CB8, 0x7392C2C2), U64(0xB60B1D12, 0x30B20E04), /* ~= 10^-75 */ U64(0x90BD77F3, 0x483BB9B9), U64(0xB1C6F22B, 0x5E6F48C2), /* ~= 10^-74 */ U64(0xB4ECD5F0, 0x1A4AA828), U64(0x1E38AEB6, 0x360B1AF3), /* ~= 10^-73 */ U64(0xE2280B6C, 0x20DD5232), U64(0x25C6DA63, 0xC38DE1B0), /* ~= 10^-72 */ U64(0x8D590723, 0x948A535F), U64(0x579C487E, 0x5A38AD0E), /* ~= 10^-71 */ U64(0xB0AF48EC, 0x79ACE837), U64(0x2D835A9D, 0xF0C6D851), /* ~= 10^-70 */ U64(0xDCDB1B27, 0x98182244), U64(0xF8E43145, 0x6CF88E65), /* ~= 10^-69 */ U64(0x8A08F0F8, 0xBF0F156B), U64(0x1B8E9ECB, 0x641B58FF), /* ~= 10^-68 */ U64(0xAC8B2D36, 0xEED2DAC5), U64(0xE272467E, 0x3D222F3F), /* ~= 10^-67 */ U64(0xD7ADF884, 0xAA879177), U64(0x5B0ED81D, 0xCC6ABB0F), /* ~= 10^-66 */ U64(0x86CCBB52, 0xEA94BAEA), U64(0x98E94712, 0x9FC2B4E9), /* ~= 10^-65 */ U64(0xA87FEA27, 0xA539E9A5), U64(0x3F2398D7, 0x47B36224), /* ~= 10^-64 */ U64(0xD29FE4B1, 0x8E88640E), U64(0x8EEC7F0D, 0x19A03AAD), /* ~= 10^-63 */ U64(0x83A3EEEE, 0xF9153E89), U64(0x1953CF68, 0x300424AC), /* ~= 10^-62 */ U64(0xA48CEAAA, 0xB75A8E2B), U64(0x5FA8C342, 0x3C052DD7), /* ~= 10^-61 */ U64(0xCDB02555, 0x653131B6), U64(0x3792F412, 0xCB06794D), /* ~= 10^-60 */ U64(0x808E1755, 0x5F3EBF11), U64(0xE2BBD88B, 0xBEE40BD0), /* ~= 10^-59 */ U64(0xA0B19D2A, 0xB70E6ED6), U64(0x5B6ACEAE, 0xAE9D0EC4), /* ~= 10^-58 */ U64(0xC8DE0475, 0x64D20A8B), U64(0xF245825A, 0x5A445275), /* ~= 10^-57 */ U64(0xFB158592, 0xBE068D2E), U64(0xEED6E2F0, 0xF0D56712), /* ~= 10^-56 */ U64(0x9CED737B, 0xB6C4183D), U64(0x55464DD6, 0x9685606B), /* ~= 10^-55 */ U64(0xC428D05A, 0xA4751E4C), U64(0xAA97E14C, 0x3C26B886), /* ~= 10^-54 */ U64(0xF5330471, 0x4D9265DF), U64(0xD53DD99F, 0x4B3066A8), /* ~= 10^-53 */ U64(0x993FE2C6, 0xD07B7FAB), U64(0xE546A803, 0x8EFE4029), /* ~= 10^-52 */ U64(0xBF8FDB78, 0x849A5F96), U64(0xDE985204, 0x72BDD033), /* ~= 10^-51 */ U64(0xEF73D256, 0xA5C0F77C), U64(0x963E6685, 0x8F6D4440), /* ~= 10^-50 */ U64(0x95A86376, 0x27989AAD), U64(0xDDE70013, 0x79A44AA8), /* ~= 10^-49 */ U64(0xBB127C53, 0xB17EC159), U64(0x5560C018, 0x580D5D52), /* ~= 10^-48 */ U64(0xE9D71B68, 0x9DDE71AF), U64(0xAAB8F01E, 0x6E10B4A6), /* ~= 10^-47 */ U64(0x92267121, 0x62AB070D), U64(0xCAB39613, 0x04CA70E8), /* ~= 10^-46 */ U64(0xB6B00D69, 0xBB55C8D1), U64(0x3D607B97, 0xC5FD0D22), /* ~= 10^-45 */ U64(0xE45C10C4, 0x2A2B3B05), U64(0x8CB89A7D, 0xB77C506A), /* ~= 10^-44 */ U64(0x8EB98A7A, 0x9A5B04E3), U64(0x77F3608E, 0x92ADB242), /* ~= 10^-43 */ U64(0xB267ED19, 0x40F1C61C), U64(0x55F038B2, 0x37591ED3), /* ~= 10^-42 */ U64(0xDF01E85F, 0x912E37A3), U64(0x6B6C46DE, 0xC52F6688), /* ~= 10^-41 */ U64(0x8B61313B, 0xBABCE2C6), U64(0x2323AC4B, 0x3B3DA015), /* ~= 10^-40 */ U64(0xAE397D8A, 0xA96C1B77), U64(0xABEC975E, 0x0A0D081A), /* ~= 10^-39 */ U64(0xD9C7DCED, 0x53C72255), U64(0x96E7BD35, 0x8C904A21), /* ~= 10^-38 */ U64(0x881CEA14, 0x545C7575), U64(0x7E50D641, 0x77DA2E54), /* ~= 10^-37 */ U64(0xAA242499, 0x697392D2), U64(0xDDE50BD1, 0xD5D0B9E9), /* ~= 10^-36 */ U64(0xD4AD2DBF, 0xC3D07787), U64(0x955E4EC6, 0x4B44E864), /* ~= 10^-35 */ U64(0x84EC3C97, 0xDA624AB4), U64(0xBD5AF13B, 0xEF0B113E), /* ~= 10^-34 */ U64(0xA6274BBD, 0xD0FADD61), U64(0xECB1AD8A, 0xEACDD58E), /* ~= 10^-33 */ U64(0xCFB11EAD, 0x453994BA), U64(0x67DE18ED, 0xA5814AF2), /* ~= 10^-32 */ U64(0x81CEB32C, 0x4B43FCF4), U64(0x80EACF94, 0x8770CED7), /* ~= 10^-31 */ U64(0xA2425FF7, 0x5E14FC31), U64(0xA1258379, 0xA94D028D), /* ~= 10^-30 */ U64(0xCAD2F7F5, 0x359A3B3E), U64(0x096EE458, 0x13A04330), /* ~= 10^-29 */ U64(0xFD87B5F2, 0x8300CA0D), U64(0x8BCA9D6E, 0x188853FC), /* ~= 10^-28 */ U64(0x9E74D1B7, 0x91E07E48), U64(0x775EA264, 0xCF55347D), /* ~= 10^-27 */ U64(0xC6120625, 0x76589DDA), U64(0x95364AFE, 0x032A819D), /* ~= 10^-26 */ U64(0xF79687AE, 0xD3EEC551), U64(0x3A83DDBD, 0x83F52204), /* ~= 10^-25 */ U64(0x9ABE14CD, 0x44753B52), U64(0xC4926A96, 0x72793542), /* ~= 10^-24 */ U64(0xC16D9A00, 0x95928A27), U64(0x75B7053C, 0x0F178293), /* ~= 10^-23 */ U64(0xF1C90080, 0xBAF72CB1), U64(0x5324C68B, 0x12DD6338), /* ~= 10^-22 */ U64(0x971DA050, 0x74DA7BEE), U64(0xD3F6FC16, 0xEBCA5E03), /* ~= 10^-21 */ U64(0xBCE50864, 0x92111AEA), U64(0x88F4BB1C, 0xA6BCF584), /* ~= 10^-20 */ U64(0xEC1E4A7D, 0xB69561A5), U64(0x2B31E9E3, 0xD06C32E5), /* ~= 10^-19 */ U64(0x9392EE8E, 0x921D5D07), U64(0x3AFF322E, 0x62439FCF), /* ~= 10^-18 */ U64(0xB877AA32, 0x36A4B449), U64(0x09BEFEB9, 0xFAD487C2), /* ~= 10^-17 */ U64(0xE69594BE, 0xC44DE15B), U64(0x4C2EBE68, 0x7989A9B3), /* ~= 10^-16 */ U64(0x901D7CF7, 0x3AB0ACD9), U64(0x0F9D3701, 0x4BF60A10), /* ~= 10^-15 */ U64(0xB424DC35, 0x095CD80F), U64(0x538484C1, 0x9EF38C94), /* ~= 10^-14 */ U64(0xE12E1342, 0x4BB40E13), U64(0x2865A5F2, 0x06B06FB9), /* ~= 10^-13 */ U64(0x8CBCCC09, 0x6F5088CB), U64(0xF93F87B7, 0x442E45D3), /* ~= 10^-12 */ U64(0xAFEBFF0B, 0xCB24AAFE), U64(0xF78F69A5, 0x1539D748), /* ~= 10^-11 */ U64(0xDBE6FECE, 0xBDEDD5BE), U64(0xB573440E, 0x5A884D1B), /* ~= 10^-10 */ U64(0x89705F41, 0x36B4A597), U64(0x31680A88, 0xF8953030), /* ~= 10^-9 */ U64(0xABCC7711, 0x8461CEFC), U64(0xFDC20D2B, 0x36BA7C3D), /* ~= 10^-8 */ U64(0xD6BF94D5, 0xE57A42BC), U64(0x3D329076, 0x04691B4C), /* ~= 10^-7 */ U64(0x8637BD05, 0xAF6C69B5), U64(0xA63F9A49, 0xC2C1B10F), /* ~= 10^-6 */ U64(0xA7C5AC47, 0x1B478423), U64(0x0FCF80DC, 0x33721D53), /* ~= 10^-5 */ U64(0xD1B71758, 0xE219652B), U64(0xD3C36113, 0x404EA4A8), /* ~= 10^-4 */ U64(0x83126E97, 0x8D4FDF3B), U64(0x645A1CAC, 0x083126E9), /* ~= 10^-3 */ U64(0xA3D70A3D, 0x70A3D70A), U64(0x3D70A3D7, 0x0A3D70A3), /* ~= 10^-2 */ U64(0xCCCCCCCC, 0xCCCCCCCC), U64(0xCCCCCCCC, 0xCCCCCCCC), /* ~= 10^-1 */ U64(0x80000000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^0 */ U64(0xA0000000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^1 */ U64(0xC8000000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^2 */ U64(0xFA000000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^3 */ U64(0x9C400000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^4 */ U64(0xC3500000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^5 */ U64(0xF4240000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^6 */ U64(0x98968000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^7 */ U64(0xBEBC2000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^8 */ U64(0xEE6B2800, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^9 */ U64(0x9502F900, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^10 */ U64(0xBA43B740, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^11 */ U64(0xE8D4A510, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^12 */ U64(0x9184E72A, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^13 */ U64(0xB5E620F4, 0x80000000), U64(0x00000000, 0x00000000), /* == 10^14 */ U64(0xE35FA931, 0xA0000000), U64(0x00000000, 0x00000000), /* == 10^15 */ U64(0x8E1BC9BF, 0x04000000), U64(0x00000000, 0x00000000), /* == 10^16 */ U64(0xB1A2BC2E, 0xC5000000), U64(0x00000000, 0x00000000), /* == 10^17 */ U64(0xDE0B6B3A, 0x76400000), U64(0x00000000, 0x00000000), /* == 10^18 */ U64(0x8AC72304, 0x89E80000), U64(0x00000000, 0x00000000), /* == 10^19 */ U64(0xAD78EBC5, 0xAC620000), U64(0x00000000, 0x00000000), /* == 10^20 */ U64(0xD8D726B7, 0x177A8000), U64(0x00000000, 0x00000000), /* == 10^21 */ U64(0x87867832, 0x6EAC9000), U64(0x00000000, 0x00000000), /* == 10^22 */ U64(0xA968163F, 0x0A57B400), U64(0x00000000, 0x00000000), /* == 10^23 */ U64(0xD3C21BCE, 0xCCEDA100), U64(0x00000000, 0x00000000), /* == 10^24 */ U64(0x84595161, 0x401484A0), U64(0x00000000, 0x00000000), /* == 10^25 */ U64(0xA56FA5B9, 0x9019A5C8), U64(0x00000000, 0x00000000), /* == 10^26 */ U64(0xCECB8F27, 0xF4200F3A), U64(0x00000000, 0x00000000), /* == 10^27 */ U64(0x813F3978, 0xF8940984), U64(0x40000000, 0x00000000), /* == 10^28 */ U64(0xA18F07D7, 0x36B90BE5), U64(0x50000000, 0x00000000), /* == 10^29 */ U64(0xC9F2C9CD, 0x04674EDE), U64(0xA4000000, 0x00000000), /* == 10^30 */ U64(0xFC6F7C40, 0x45812296), U64(0x4D000000, 0x00000000), /* == 10^31 */ U64(0x9DC5ADA8, 0x2B70B59D), U64(0xF0200000, 0x00000000), /* == 10^32 */ U64(0xC5371912, 0x364CE305), U64(0x6C280000, 0x00000000), /* == 10^33 */ U64(0xF684DF56, 0xC3E01BC6), U64(0xC7320000, 0x00000000), /* == 10^34 */ U64(0x9A130B96, 0x3A6C115C), U64(0x3C7F4000, 0x00000000), /* == 10^35 */ U64(0xC097CE7B, 0xC90715B3), U64(0x4B9F1000, 0x00000000), /* == 10^36 */ U64(0xF0BDC21A, 0xBB48DB20), U64(0x1E86D400, 0x00000000), /* == 10^37 */ U64(0x96769950, 0xB50D88F4), U64(0x13144480, 0x00000000), /* == 10^38 */ U64(0xBC143FA4, 0xE250EB31), U64(0x17D955A0, 0x00000000), /* == 10^39 */ U64(0xEB194F8E, 0x1AE525FD), U64(0x5DCFAB08, 0x00000000), /* == 10^40 */ U64(0x92EFD1B8, 0xD0CF37BE), U64(0x5AA1CAE5, 0x00000000), /* == 10^41 */ U64(0xB7ABC627, 0x050305AD), U64(0xF14A3D9E, 0x40000000), /* == 10^42 */ U64(0xE596B7B0, 0xC643C719), U64(0x6D9CCD05, 0xD0000000), /* == 10^43 */ U64(0x8F7E32CE, 0x7BEA5C6F), U64(0xE4820023, 0xA2000000), /* == 10^44 */ U64(0xB35DBF82, 0x1AE4F38B), U64(0xDDA2802C, 0x8A800000), /* == 10^45 */ U64(0xE0352F62, 0xA19E306E), U64(0xD50B2037, 0xAD200000), /* == 10^46 */ U64(0x8C213D9D, 0xA502DE45), U64(0x4526F422, 0xCC340000), /* == 10^47 */ U64(0xAF298D05, 0x0E4395D6), U64(0x9670B12B, 0x7F410000), /* == 10^48 */ U64(0xDAF3F046, 0x51D47B4C), U64(0x3C0CDD76, 0x5F114000), /* == 10^49 */ U64(0x88D8762B, 0xF324CD0F), U64(0xA5880A69, 0xFB6AC800), /* == 10^50 */ U64(0xAB0E93B6, 0xEFEE0053), U64(0x8EEA0D04, 0x7A457A00), /* == 10^51 */ U64(0xD5D238A4, 0xABE98068), U64(0x72A49045, 0x98D6D880), /* == 10^52 */ U64(0x85A36366, 0xEB71F041), U64(0x47A6DA2B, 0x7F864750), /* == 10^53 */ U64(0xA70C3C40, 0xA64E6C51), U64(0x999090B6, 0x5F67D924), /* == 10^54 */ U64(0xD0CF4B50, 0xCFE20765), U64(0xFFF4B4E3, 0xF741CF6D), /* == 10^55 */ U64(0x82818F12, 0x81ED449F), U64(0xBFF8F10E, 0x7A8921A4), /* ~= 10^56 */ U64(0xA321F2D7, 0x226895C7), U64(0xAFF72D52, 0x192B6A0D), /* ~= 10^57 */ U64(0xCBEA6F8C, 0xEB02BB39), U64(0x9BF4F8A6, 0x9F764490), /* ~= 10^58 */ U64(0xFEE50B70, 0x25C36A08), U64(0x02F236D0, 0x4753D5B4), /* ~= 10^59 */ U64(0x9F4F2726, 0x179A2245), U64(0x01D76242, 0x2C946590), /* ~= 10^60 */ U64(0xC722F0EF, 0x9D80AAD6), U64(0x424D3AD2, 0xB7B97EF5), /* ~= 10^61 */ U64(0xF8EBAD2B, 0x84E0D58B), U64(0xD2E08987, 0x65A7DEB2), /* ~= 10^62 */ U64(0x9B934C3B, 0x330C8577), U64(0x63CC55F4, 0x9F88EB2F), /* ~= 10^63 */ U64(0xC2781F49, 0xFFCFA6D5), U64(0x3CBF6B71, 0xC76B25FB), /* ~= 10^64 */ U64(0xF316271C, 0x7FC3908A), U64(0x8BEF464E, 0x3945EF7A), /* ~= 10^65 */ U64(0x97EDD871, 0xCFDA3A56), U64(0x97758BF0, 0xE3CBB5AC), /* ~= 10^66 */ U64(0xBDE94E8E, 0x43D0C8EC), U64(0x3D52EEED, 0x1CBEA317), /* ~= 10^67 */ U64(0xED63A231, 0xD4C4FB27), U64(0x4CA7AAA8, 0x63EE4BDD), /* ~= 10^68 */ U64(0x945E455F, 0x24FB1CF8), U64(0x8FE8CAA9, 0x3E74EF6A), /* ~= 10^69 */ U64(0xB975D6B6, 0xEE39E436), U64(0xB3E2FD53, 0x8E122B44), /* ~= 10^70 */ U64(0xE7D34C64, 0xA9C85D44), U64(0x60DBBCA8, 0x7196B616), /* ~= 10^71 */ U64(0x90E40FBE, 0xEA1D3A4A), U64(0xBC8955E9, 0x46FE31CD), /* ~= 10^72 */ U64(0xB51D13AE, 0xA4A488DD), U64(0x6BABAB63, 0x98BDBE41), /* ~= 10^73 */ U64(0xE264589A, 0x4DCDAB14), U64(0xC696963C, 0x7EED2DD1), /* ~= 10^74 */ U64(0x8D7EB760, 0x70A08AEC), U64(0xFC1E1DE5, 0xCF543CA2), /* ~= 10^75 */ U64(0xB0DE6538, 0x8CC8ADA8), U64(0x3B25A55F, 0x43294BCB), /* ~= 10^76 */ U64(0xDD15FE86, 0xAFFAD912), U64(0x49EF0EB7, 0x13F39EBE), /* ~= 10^77 */ U64(0x8A2DBF14, 0x2DFCC7AB), U64(0x6E356932, 0x6C784337), /* ~= 10^78 */ U64(0xACB92ED9, 0x397BF996), U64(0x49C2C37F, 0x07965404), /* ~= 10^79 */ U64(0xD7E77A8F, 0x87DAF7FB), U64(0xDC33745E, 0xC97BE906), /* ~= 10^80 */ U64(0x86F0AC99, 0xB4E8DAFD), U64(0x69A028BB, 0x3DED71A3), /* ~= 10^81 */ U64(0xA8ACD7C0, 0x222311BC), U64(0xC40832EA, 0x0D68CE0C), /* ~= 10^82 */ U64(0xD2D80DB0, 0x2AABD62B), U64(0xF50A3FA4, 0x90C30190), /* ~= 10^83 */ U64(0x83C7088E, 0x1AAB65DB), U64(0x792667C6, 0xDA79E0FA), /* ~= 10^84 */ U64(0xA4B8CAB1, 0xA1563F52), U64(0x577001B8, 0x91185938), /* ~= 10^85 */ U64(0xCDE6FD5E, 0x09ABCF26), U64(0xED4C0226, 0xB55E6F86), /* ~= 10^86 */ U64(0x80B05E5A, 0xC60B6178), U64(0x544F8158, 0x315B05B4), /* ~= 10^87 */ U64(0xA0DC75F1, 0x778E39D6), U64(0x696361AE, 0x3DB1C721), /* ~= 10^88 */ U64(0xC913936D, 0xD571C84C), U64(0x03BC3A19, 0xCD1E38E9), /* ~= 10^89 */ U64(0xFB587849, 0x4ACE3A5F), U64(0x04AB48A0, 0x4065C723), /* ~= 10^90 */ U64(0x9D174B2D, 0xCEC0E47B), U64(0x62EB0D64, 0x283F9C76), /* ~= 10^91 */ U64(0xC45D1DF9, 0x42711D9A), U64(0x3BA5D0BD, 0x324F8394), /* ~= 10^92 */ U64(0xF5746577, 0x930D6500), U64(0xCA8F44EC, 0x7EE36479), /* ~= 10^93 */ U64(0x9968BF6A, 0xBBE85F20), U64(0x7E998B13, 0xCF4E1ECB), /* ~= 10^94 */ U64(0xBFC2EF45, 0x6AE276E8), U64(0x9E3FEDD8, 0xC321A67E), /* ~= 10^95 */ U64(0xEFB3AB16, 0xC59B14A2), U64(0xC5CFE94E, 0xF3EA101E), /* ~= 10^96 */ U64(0x95D04AEE, 0x3B80ECE5), U64(0xBBA1F1D1, 0x58724A12), /* ~= 10^97 */ U64(0xBB445DA9, 0xCA61281F), U64(0x2A8A6E45, 0xAE8EDC97), /* ~= 10^98 */ U64(0xEA157514, 0x3CF97226), U64(0xF52D09D7, 0x1A3293BD), /* ~= 10^99 */ U64(0x924D692C, 0xA61BE758), U64(0x593C2626, 0x705F9C56), /* ~= 10^100 */ U64(0xB6E0C377, 0xCFA2E12E), U64(0x6F8B2FB0, 0x0C77836C), /* ~= 10^101 */ U64(0xE498F455, 0xC38B997A), U64(0x0B6DFB9C, 0x0F956447), /* ~= 10^102 */ U64(0x8EDF98B5, 0x9A373FEC), U64(0x4724BD41, 0x89BD5EAC), /* ~= 10^103 */ U64(0xB2977EE3, 0x00C50FE7), U64(0x58EDEC91, 0xEC2CB657), /* ~= 10^104 */ U64(0xDF3D5E9B, 0xC0F653E1), U64(0x2F2967B6, 0x6737E3ED), /* ~= 10^105 */ U64(0x8B865B21, 0x5899F46C), U64(0xBD79E0D2, 0x0082EE74), /* ~= 10^106 */ U64(0xAE67F1E9, 0xAEC07187), U64(0xECD85906, 0x80A3AA11), /* ~= 10^107 */ U64(0xDA01EE64, 0x1A708DE9), U64(0xE80E6F48, 0x20CC9495), /* ~= 10^108 */ U64(0x884134FE, 0x908658B2), U64(0x3109058D, 0x147FDCDD), /* ~= 10^109 */ U64(0xAA51823E, 0x34A7EEDE), U64(0xBD4B46F0, 0x599FD415), /* ~= 10^110 */ U64(0xD4E5E2CD, 0xC1D1EA96), U64(0x6C9E18AC, 0x7007C91A), /* ~= 10^111 */ U64(0x850FADC0, 0x9923329E), U64(0x03E2CF6B, 0xC604DDB0), /* ~= 10^112 */ U64(0xA6539930, 0xBF6BFF45), U64(0x84DB8346, 0xB786151C), /* ~= 10^113 */ U64(0xCFE87F7C, 0xEF46FF16), U64(0xE6126418, 0x65679A63), /* ~= 10^114 */ U64(0x81F14FAE, 0x158C5F6E), U64(0x4FCB7E8F, 0x3F60C07E), /* ~= 10^115 */ U64(0xA26DA399, 0x9AEF7749), U64(0xE3BE5E33, 0x0F38F09D), /* ~= 10^116 */ U64(0xCB090C80, 0x01AB551C), U64(0x5CADF5BF, 0xD3072CC5), /* ~= 10^117 */ U64(0xFDCB4FA0, 0x02162A63), U64(0x73D9732F, 0xC7C8F7F6), /* ~= 10^118 */ U64(0x9E9F11C4, 0x014DDA7E), U64(0x2867E7FD, 0xDCDD9AFA), /* ~= 10^119 */ U64(0xC646D635, 0x01A1511D), U64(0xB281E1FD, 0x541501B8), /* ~= 10^120 */ U64(0xF7D88BC2, 0x4209A565), U64(0x1F225A7C, 0xA91A4226), /* ~= 10^121 */ U64(0x9AE75759, 0x6946075F), U64(0x3375788D, 0xE9B06958), /* ~= 10^122 */ U64(0xC1A12D2F, 0xC3978937), U64(0x0052D6B1, 0x641C83AE), /* ~= 10^123 */ U64(0xF209787B, 0xB47D6B84), U64(0xC0678C5D, 0xBD23A49A), /* ~= 10^124 */ U64(0x9745EB4D, 0x50CE6332), U64(0xF840B7BA, 0x963646E0), /* ~= 10^125 */ U64(0xBD176620, 0xA501FBFF), U64(0xB650E5A9, 0x3BC3D898), /* ~= 10^126 */ U64(0xEC5D3FA8, 0xCE427AFF), U64(0xA3E51F13, 0x8AB4CEBE), /* ~= 10^127 */ U64(0x93BA47C9, 0x80E98CDF), U64(0xC66F336C, 0x36B10137), /* ~= 10^128 */ U64(0xB8A8D9BB, 0xE123F017), U64(0xB80B0047, 0x445D4184), /* ~= 10^129 */ U64(0xE6D3102A, 0xD96CEC1D), U64(0xA60DC059, 0x157491E5), /* ~= 10^130 */ U64(0x9043EA1A, 0xC7E41392), U64(0x87C89837, 0xAD68DB2F), /* ~= 10^131 */ U64(0xB454E4A1, 0x79DD1877), U64(0x29BABE45, 0x98C311FB), /* ~= 10^132 */ U64(0xE16A1DC9, 0xD8545E94), U64(0xF4296DD6, 0xFEF3D67A), /* ~= 10^133 */ U64(0x8CE2529E, 0x2734BB1D), U64(0x1899E4A6, 0x5F58660C), /* ~= 10^134 */ U64(0xB01AE745, 0xB101E9E4), U64(0x5EC05DCF, 0xF72E7F8F), /* ~= 10^135 */ U64(0xDC21A117, 0x1D42645D), U64(0x76707543, 0xF4FA1F73), /* ~= 10^136 */ U64(0x899504AE, 0x72497EBA), U64(0x6A06494A, 0x791C53A8), /* ~= 10^137 */ U64(0xABFA45DA, 0x0EDBDE69), U64(0x0487DB9D, 0x17636892), /* ~= 10^138 */ U64(0xD6F8D750, 0x9292D603), U64(0x45A9D284, 0x5D3C42B6), /* ~= 10^139 */ U64(0x865B8692, 0x5B9BC5C2), U64(0x0B8A2392, 0xBA45A9B2), /* ~= 10^140 */ U64(0xA7F26836, 0xF282B732), U64(0x8E6CAC77, 0x68D7141E), /* ~= 10^141 */ U64(0xD1EF0244, 0xAF2364FF), U64(0x3207D795, 0x430CD926), /* ~= 10^142 */ U64(0x8335616A, 0xED761F1F), U64(0x7F44E6BD, 0x49E807B8), /* ~= 10^143 */ U64(0xA402B9C5, 0xA8D3A6E7), U64(0x5F16206C, 0x9C6209A6), /* ~= 10^144 */ U64(0xCD036837, 0x130890A1), U64(0x36DBA887, 0xC37A8C0F), /* ~= 10^145 */ U64(0x80222122, 0x6BE55A64), U64(0xC2494954, 0xDA2C9789), /* ~= 10^146 */ U64(0xA02AA96B, 0x06DEB0FD), U64(0xF2DB9BAA, 0x10B7BD6C), /* ~= 10^147 */ U64(0xC83553C5, 0xC8965D3D), U64(0x6F928294, 0x94E5ACC7), /* ~= 10^148 */ U64(0xFA42A8B7, 0x3ABBF48C), U64(0xCB772339, 0xBA1F17F9), /* ~= 10^149 */ U64(0x9C69A972, 0x84B578D7), U64(0xFF2A7604, 0x14536EFB), /* ~= 10^150 */ U64(0xC38413CF, 0x25E2D70D), U64(0xFEF51385, 0x19684ABA), /* ~= 10^151 */ U64(0xF46518C2, 0xEF5B8CD1), U64(0x7EB25866, 0x5FC25D69), /* ~= 10^152 */ U64(0x98BF2F79, 0xD5993802), U64(0xEF2F773F, 0xFBD97A61), /* ~= 10^153 */ U64(0xBEEEFB58, 0x4AFF8603), U64(0xAAFB550F, 0xFACFD8FA), /* ~= 10^154 */ U64(0xEEAABA2E, 0x5DBF6784), U64(0x95BA2A53, 0xF983CF38), /* ~= 10^155 */ U64(0x952AB45C, 0xFA97A0B2), U64(0xDD945A74, 0x7BF26183), /* ~= 10^156 */ U64(0xBA756174, 0x393D88DF), U64(0x94F97111, 0x9AEEF9E4), /* ~= 10^157 */ U64(0xE912B9D1, 0x478CEB17), U64(0x7A37CD56, 0x01AAB85D), /* ~= 10^158 */ U64(0x91ABB422, 0xCCB812EE), U64(0xAC62E055, 0xC10AB33A), /* ~= 10^159 */ U64(0xB616A12B, 0x7FE617AA), U64(0x577B986B, 0x314D6009), /* ~= 10^160 */ U64(0xE39C4976, 0x5FDF9D94), U64(0xED5A7E85, 0xFDA0B80B), /* ~= 10^161 */ U64(0x8E41ADE9, 0xFBEBC27D), U64(0x14588F13, 0xBE847307), /* ~= 10^162 */ U64(0xB1D21964, 0x7AE6B31C), U64(0x596EB2D8, 0xAE258FC8), /* ~= 10^163 */ U64(0xDE469FBD, 0x99A05FE3), U64(0x6FCA5F8E, 0xD9AEF3BB), /* ~= 10^164 */ U64(0x8AEC23D6, 0x80043BEE), U64(0x25DE7BB9, 0x480D5854), /* ~= 10^165 */ U64(0xADA72CCC, 0x20054AE9), U64(0xAF561AA7, 0x9A10AE6A), /* ~= 10^166 */ U64(0xD910F7FF, 0x28069DA4), U64(0x1B2BA151, 0x8094DA04), /* ~= 10^167 */ U64(0x87AA9AFF, 0x79042286), U64(0x90FB44D2, 0xF05D0842), /* ~= 10^168 */ U64(0xA99541BF, 0x57452B28), U64(0x353A1607, 0xAC744A53), /* ~= 10^169 */ U64(0xD3FA922F, 0x2D1675F2), U64(0x42889B89, 0x97915CE8), /* ~= 10^170 */ U64(0x847C9B5D, 0x7C2E09B7), U64(0x69956135, 0xFEBADA11), /* ~= 10^171 */ U64(0xA59BC234, 0xDB398C25), U64(0x43FAB983, 0x7E699095), /* ~= 10^172 */ U64(0xCF02B2C2, 0x1207EF2E), U64(0x94F967E4, 0x5E03F4BB), /* ~= 10^173 */ U64(0x8161AFB9, 0x4B44F57D), U64(0x1D1BE0EE, 0xBAC278F5), /* ~= 10^174 */ U64(0xA1BA1BA7, 0x9E1632DC), U64(0x6462D92A, 0x69731732), /* ~= 10^175 */ U64(0xCA28A291, 0x859BBF93), U64(0x7D7B8F75, 0x03CFDCFE), /* ~= 10^176 */ U64(0xFCB2CB35, 0xE702AF78), U64(0x5CDA7352, 0x44C3D43E), /* ~= 10^177 */ U64(0x9DEFBF01, 0xB061ADAB), U64(0x3A088813, 0x6AFA64A7), /* ~= 10^178 */ U64(0xC56BAEC2, 0x1C7A1916), U64(0x088AAA18, 0x45B8FDD0), /* ~= 10^179 */ U64(0xF6C69A72, 0xA3989F5B), U64(0x8AAD549E, 0x57273D45), /* ~= 10^180 */ U64(0x9A3C2087, 0xA63F6399), U64(0x36AC54E2, 0xF678864B), /* ~= 10^181 */ U64(0xC0CB28A9, 0x8FCF3C7F), U64(0x84576A1B, 0xB416A7DD), /* ~= 10^182 */ U64(0xF0FDF2D3, 0xF3C30B9F), U64(0x656D44A2, 0xA11C51D5), /* ~= 10^183 */ U64(0x969EB7C4, 0x7859E743), U64(0x9F644AE5, 0xA4B1B325), /* ~= 10^184 */ U64(0xBC4665B5, 0x96706114), U64(0x873D5D9F, 0x0DDE1FEE), /* ~= 10^185 */ U64(0xEB57FF22, 0xFC0C7959), U64(0xA90CB506, 0xD155A7EA), /* ~= 10^186 */ U64(0x9316FF75, 0xDD87CBD8), U64(0x09A7F124, 0x42D588F2), /* ~= 10^187 */ U64(0xB7DCBF53, 0x54E9BECE), U64(0x0C11ED6D, 0x538AEB2F), /* ~= 10^188 */ U64(0xE5D3EF28, 0x2A242E81), U64(0x8F1668C8, 0xA86DA5FA), /* ~= 10^189 */ U64(0x8FA47579, 0x1A569D10), U64(0xF96E017D, 0x694487BC), /* ~= 10^190 */ U64(0xB38D92D7, 0x60EC4455), U64(0x37C981DC, 0xC395A9AC), /* ~= 10^191 */ U64(0xE070F78D, 0x3927556A), U64(0x85BBE253, 0xF47B1417), /* ~= 10^192 */ U64(0x8C469AB8, 0x43B89562), U64(0x93956D74, 0x78CCEC8E), /* ~= 10^193 */ U64(0xAF584166, 0x54A6BABB), U64(0x387AC8D1, 0x970027B2), /* ~= 10^194 */ U64(0xDB2E51BF, 0xE9D0696A), U64(0x06997B05, 0xFCC0319E), /* ~= 10^195 */ U64(0x88FCF317, 0xF22241E2), U64(0x441FECE3, 0xBDF81F03), /* ~= 10^196 */ U64(0xAB3C2FDD, 0xEEAAD25A), U64(0xD527E81C, 0xAD7626C3), /* ~= 10^197 */ U64(0xD60B3BD5, 0x6A5586F1), U64(0x8A71E223, 0xD8D3B074), /* ~= 10^198 */ U64(0x85C70565, 0x62757456), U64(0xF6872D56, 0x67844E49), /* ~= 10^199 */ U64(0xA738C6BE, 0xBB12D16C), U64(0xB428F8AC, 0x016561DB), /* ~= 10^200 */ U64(0xD106F86E, 0x69D785C7), U64(0xE13336D7, 0x01BEBA52), /* ~= 10^201 */ U64(0x82A45B45, 0x0226B39C), U64(0xECC00246, 0x61173473), /* ~= 10^202 */ U64(0xA34D7216, 0x42B06084), U64(0x27F002D7, 0xF95D0190), /* ~= 10^203 */ U64(0xCC20CE9B, 0xD35C78A5), U64(0x31EC038D, 0xF7B441F4), /* ~= 10^204 */ U64(0xFF290242, 0xC83396CE), U64(0x7E670471, 0x75A15271), /* ~= 10^205 */ U64(0x9F79A169, 0xBD203E41), U64(0x0F0062C6, 0xE984D386), /* ~= 10^206 */ U64(0xC75809C4, 0x2C684DD1), U64(0x52C07B78, 0xA3E60868), /* ~= 10^207 */ U64(0xF92E0C35, 0x37826145), U64(0xA7709A56, 0xCCDF8A82), /* ~= 10^208 */ U64(0x9BBCC7A1, 0x42B17CCB), U64(0x88A66076, 0x400BB691), /* ~= 10^209 */ U64(0xC2ABF989, 0x935DDBFE), U64(0x6ACFF893, 0xD00EA435), /* ~= 10^210 */ U64(0xF356F7EB, 0xF83552FE), U64(0x0583F6B8, 0xC4124D43), /* ~= 10^211 */ U64(0x98165AF3, 0x7B2153DE), U64(0xC3727A33, 0x7A8B704A), /* ~= 10^212 */ U64(0xBE1BF1B0, 0x59E9A8D6), U64(0x744F18C0, 0x592E4C5C), /* ~= 10^213 */ U64(0xEDA2EE1C, 0x7064130C), U64(0x1162DEF0, 0x6F79DF73), /* ~= 10^214 */ U64(0x9485D4D1, 0xC63E8BE7), U64(0x8ADDCB56, 0x45AC2BA8), /* ~= 10^215 */ U64(0xB9A74A06, 0x37CE2EE1), U64(0x6D953E2B, 0xD7173692), /* ~= 10^216 */ U64(0xE8111C87, 0xC5C1BA99), U64(0xC8FA8DB6, 0xCCDD0437), /* ~= 10^217 */ U64(0x910AB1D4, 0xDB9914A0), U64(0x1D9C9892, 0x400A22A2), /* ~= 10^218 */ U64(0xB54D5E4A, 0x127F59C8), U64(0x2503BEB6, 0xD00CAB4B), /* ~= 10^219 */ U64(0xE2A0B5DC, 0x971F303A), U64(0x2E44AE64, 0x840FD61D), /* ~= 10^220 */ U64(0x8DA471A9, 0xDE737E24), U64(0x5CEAECFE, 0xD289E5D2), /* ~= 10^221 */ U64(0xB10D8E14, 0x56105DAD), U64(0x7425A83E, 0x872C5F47), /* ~= 10^222 */ U64(0xDD50F199, 0x6B947518), U64(0xD12F124E, 0x28F77719), /* ~= 10^223 */ U64(0x8A5296FF, 0xE33CC92F), U64(0x82BD6B70, 0xD99AAA6F), /* ~= 10^224 */ U64(0xACE73CBF, 0xDC0BFB7B), U64(0x636CC64D, 0x1001550B), /* ~= 10^225 */ U64(0xD8210BEF, 0xD30EFA5A), U64(0x3C47F7E0, 0x5401AA4E), /* ~= 10^226 */ U64(0x8714A775, 0xE3E95C78), U64(0x65ACFAEC, 0x34810A71), /* ~= 10^227 */ U64(0xA8D9D153, 0x5CE3B396), U64(0x7F1839A7, 0x41A14D0D), /* ~= 10^228 */ U64(0xD31045A8, 0x341CA07C), U64(0x1EDE4811, 0x1209A050), /* ~= 10^229 */ U64(0x83EA2B89, 0x2091E44D), U64(0x934AED0A, 0xAB460432), /* ~= 10^230 */ U64(0xA4E4B66B, 0x68B65D60), U64(0xF81DA84D, 0x5617853F), /* ~= 10^231 */ U64(0xCE1DE406, 0x42E3F4B9), U64(0x36251260, 0xAB9D668E), /* ~= 10^232 */ U64(0x80D2AE83, 0xE9CE78F3), U64(0xC1D72B7C, 0x6B426019), /* ~= 10^233 */ U64(0xA1075A24, 0xE4421730), U64(0xB24CF65B, 0x8612F81F), /* ~= 10^234 */ U64(0xC94930AE, 0x1D529CFC), U64(0xDEE033F2, 0x6797B627), /* ~= 10^235 */ U64(0xFB9B7CD9, 0xA4A7443C), U64(0x169840EF, 0x017DA3B1), /* ~= 10^236 */ U64(0x9D412E08, 0x06E88AA5), U64(0x8E1F2895, 0x60EE864E), /* ~= 10^237 */ U64(0xC491798A, 0x08A2AD4E), U64(0xF1A6F2BA, 0xB92A27E2), /* ~= 10^238 */ U64(0xF5B5D7EC, 0x8ACB58A2), U64(0xAE10AF69, 0x6774B1DB), /* ~= 10^239 */ U64(0x9991A6F3, 0xD6BF1765), U64(0xACCA6DA1, 0xE0A8EF29), /* ~= 10^240 */ U64(0xBFF610B0, 0xCC6EDD3F), U64(0x17FD090A, 0x58D32AF3), /* ~= 10^241 */ U64(0xEFF394DC, 0xFF8A948E), U64(0xDDFC4B4C, 0xEF07F5B0), /* ~= 10^242 */ U64(0x95F83D0A, 0x1FB69CD9), U64(0x4ABDAF10, 0x1564F98E), /* ~= 10^243 */ U64(0xBB764C4C, 0xA7A4440F), U64(0x9D6D1AD4, 0x1ABE37F1), /* ~= 10^244 */ U64(0xEA53DF5F, 0xD18D5513), U64(0x84C86189, 0x216DC5ED), /* ~= 10^245 */ U64(0x92746B9B, 0xE2F8552C), U64(0x32FD3CF5, 0xB4E49BB4), /* ~= 10^246 */ U64(0xB7118682, 0xDBB66A77), U64(0x3FBC8C33, 0x221DC2A1), /* ~= 10^247 */ U64(0xE4D5E823, 0x92A40515), U64(0x0FABAF3F, 0xEAA5334A), /* ~= 10^248 */ U64(0x8F05B116, 0x3BA6832D), U64(0x29CB4D87, 0xF2A7400E), /* ~= 10^249 */ U64(0xB2C71D5B, 0xCA9023F8), U64(0x743E20E9, 0xEF511012), /* ~= 10^250 */ U64(0xDF78E4B2, 0xBD342CF6), U64(0x914DA924, 0x6B255416), /* ~= 10^251 */ U64(0x8BAB8EEF, 0xB6409C1A), U64(0x1AD089B6, 0xC2F7548E), /* ~= 10^252 */ U64(0xAE9672AB, 0xA3D0C320), U64(0xA184AC24, 0x73B529B1), /* ~= 10^253 */ U64(0xDA3C0F56, 0x8CC4F3E8), U64(0xC9E5D72D, 0x90A2741E), /* ~= 10^254 */ U64(0x88658996, 0x17FB1871), U64(0x7E2FA67C, 0x7A658892), /* ~= 10^255 */ U64(0xAA7EEBFB, 0x9DF9DE8D), U64(0xDDBB901B, 0x98FEEAB7), /* ~= 10^256 */ U64(0xD51EA6FA, 0x85785631), U64(0x552A7422, 0x7F3EA565), /* ~= 10^257 */ U64(0x8533285C, 0x936B35DE), U64(0xD53A8895, 0x8F87275F), /* ~= 10^258 */ U64(0xA67FF273, 0xB8460356), U64(0x8A892ABA, 0xF368F137), /* ~= 10^259 */ U64(0xD01FEF10, 0xA657842C), U64(0x2D2B7569, 0xB0432D85), /* ~= 10^260 */ U64(0x8213F56A, 0x67F6B29B), U64(0x9C3B2962, 0x0E29FC73), /* ~= 10^261 */ U64(0xA298F2C5, 0x01F45F42), U64(0x8349F3BA, 0x91B47B8F), /* ~= 10^262 */ U64(0xCB3F2F76, 0x42717713), U64(0x241C70A9, 0x36219A73), /* ~= 10^263 */ U64(0xFE0EFB53, 0xD30DD4D7), U64(0xED238CD3, 0x83AA0110), /* ~= 10^264 */ U64(0x9EC95D14, 0x63E8A506), U64(0xF4363804, 0x324A40AA), /* ~= 10^265 */ U64(0xC67BB459, 0x7CE2CE48), U64(0xB143C605, 0x3EDCD0D5), /* ~= 10^266 */ U64(0xF81AA16F, 0xDC1B81DA), U64(0xDD94B786, 0x8E94050A), /* ~= 10^267 */ U64(0x9B10A4E5, 0xE9913128), U64(0xCA7CF2B4, 0x191C8326), /* ~= 10^268 */ U64(0xC1D4CE1F, 0x63F57D72), U64(0xFD1C2F61, 0x1F63A3F0), /* ~= 10^269 */ U64(0xF24A01A7, 0x3CF2DCCF), U64(0xBC633B39, 0x673C8CEC), /* ~= 10^270 */ U64(0x976E4108, 0x8617CA01), U64(0xD5BE0503, 0xE085D813), /* ~= 10^271 */ U64(0xBD49D14A, 0xA79DBC82), U64(0x4B2D8644, 0xD8A74E18), /* ~= 10^272 */ U64(0xEC9C459D, 0x51852BA2), U64(0xDDF8E7D6, 0x0ED1219E), /* ~= 10^273 */ U64(0x93E1AB82, 0x52F33B45), U64(0xCABB90E5, 0xC942B503), /* ~= 10^274 */ U64(0xB8DA1662, 0xE7B00A17), U64(0x3D6A751F, 0x3B936243), /* ~= 10^275 */ U64(0xE7109BFB, 0xA19C0C9D), U64(0x0CC51267, 0x0A783AD4), /* ~= 10^276 */ U64(0x906A617D, 0x450187E2), U64(0x27FB2B80, 0x668B24C5), /* ~= 10^277 */ U64(0xB484F9DC, 0x9641E9DA), U64(0xB1F9F660, 0x802DEDF6), /* ~= 10^278 */ U64(0xE1A63853, 0xBBD26451), U64(0x5E7873F8, 0xA0396973), /* ~= 10^279 */ U64(0x8D07E334, 0x55637EB2), U64(0xDB0B487B, 0x6423E1E8), /* ~= 10^280 */ U64(0xB049DC01, 0x6ABC5E5F), U64(0x91CE1A9A, 0x3D2CDA62), /* ~= 10^281 */ U64(0xDC5C5301, 0xC56B75F7), U64(0x7641A140, 0xCC7810FB), /* ~= 10^282 */ U64(0x89B9B3E1, 0x1B6329BA), U64(0xA9E904C8, 0x7FCB0A9D), /* ~= 10^283 */ U64(0xAC2820D9, 0x623BF429), U64(0x546345FA, 0x9FBDCD44), /* ~= 10^284 */ U64(0xD732290F, 0xBACAF133), U64(0xA97C1779, 0x47AD4095), /* ~= 10^285 */ U64(0x867F59A9, 0xD4BED6C0), U64(0x49ED8EAB, 0xCCCC485D), /* ~= 10^286 */ U64(0xA81F3014, 0x49EE8C70), U64(0x5C68F256, 0xBFFF5A74), /* ~= 10^287 */ U64(0xD226FC19, 0x5C6A2F8C), U64(0x73832EEC, 0x6FFF3111), /* ~= 10^288 */ U64(0x83585D8F, 0xD9C25DB7), U64(0xC831FD53, 0xC5FF7EAB), /* ~= 10^289 */ U64(0xA42E74F3, 0xD032F525), U64(0xBA3E7CA8, 0xB77F5E55), /* ~= 10^290 */ U64(0xCD3A1230, 0xC43FB26F), U64(0x28CE1BD2, 0xE55F35EB), /* ~= 10^291 */ U64(0x80444B5E, 0x7AA7CF85), U64(0x7980D163, 0xCF5B81B3), /* ~= 10^292 */ U64(0xA0555E36, 0x1951C366), U64(0xD7E105BC, 0xC332621F), /* ~= 10^293 */ U64(0xC86AB5C3, 0x9FA63440), U64(0x8DD9472B, 0xF3FEFAA7), /* ~= 10^294 */ U64(0xFA856334, 0x878FC150), U64(0xB14F98F6, 0xF0FEB951), /* ~= 10^295 */ U64(0x9C935E00, 0xD4B9D8D2), U64(0x6ED1BF9A, 0x569F33D3), /* ~= 10^296 */ U64(0xC3B83581, 0x09E84F07), U64(0x0A862F80, 0xEC4700C8), /* ~= 10^297 */ U64(0xF4A642E1, 0x4C6262C8), U64(0xCD27BB61, 0x2758C0FA), /* ~= 10^298 */ U64(0x98E7E9CC, 0xCFBD7DBD), U64(0x8038D51C, 0xB897789C), /* ~= 10^299 */ U64(0xBF21E440, 0x03ACDD2C), U64(0xE0470A63, 0xE6BD56C3), /* ~= 10^300 */ U64(0xEEEA5D50, 0x04981478), U64(0x1858CCFC, 0xE06CAC74), /* ~= 10^301 */ U64(0x95527A52, 0x02DF0CCB), U64(0x0F37801E, 0x0C43EBC8), /* ~= 10^302 */ U64(0xBAA718E6, 0x8396CFFD), U64(0xD3056025, 0x8F54E6BA), /* ~= 10^303 */ U64(0xE950DF20, 0x247C83FD), U64(0x47C6B82E, 0xF32A2069), /* ~= 10^304 */ U64(0x91D28B74, 0x16CDD27E), U64(0x4CDC331D, 0x57FA5441), /* ~= 10^305 */ U64(0xB6472E51, 0x1C81471D), U64(0xE0133FE4, 0xADF8E952), /* ~= 10^306 */ U64(0xE3D8F9E5, 0x63A198E5), U64(0x58180FDD, 0xD97723A6), /* ~= 10^307 */ U64(0x8E679C2F, 0x5E44FF8F), U64(0x570F09EA, 0xA7EA7648), /* ~= 10^308 */ U64(0xB201833B, 0x35D63F73), U64(0x2CD2CC65, 0x51E513DA), /* ~= 10^309 */ U64(0xDE81E40A, 0x034BCF4F), U64(0xF8077F7E, 0xA65E58D1), /* ~= 10^310 */ U64(0x8B112E86, 0x420F6191), U64(0xFB04AFAF, 0x27FAF782), /* ~= 10^311 */ U64(0xADD57A27, 0xD29339F6), U64(0x79C5DB9A, 0xF1F9B563), /* ~= 10^312 */ U64(0xD94AD8B1, 0xC7380874), U64(0x18375281, 0xAE7822BC), /* ~= 10^313 */ U64(0x87CEC76F, 0x1C830548), U64(0x8F229391, 0x0D0B15B5), /* ~= 10^314 */ U64(0xA9C2794A, 0xE3A3C69A), U64(0xB2EB3875, 0x504DDB22), /* ~= 10^315 */ U64(0xD433179D, 0x9C8CB841), U64(0x5FA60692, 0xA46151EB), /* ~= 10^316 */ U64(0x849FEEC2, 0x81D7F328), U64(0xDBC7C41B, 0xA6BCD333), /* ~= 10^317 */ U64(0xA5C7EA73, 0x224DEFF3), U64(0x12B9B522, 0x906C0800), /* ~= 10^318 */ U64(0xCF39E50F, 0xEAE16BEF), U64(0xD768226B, 0x34870A00), /* ~= 10^319 */ U64(0x81842F29, 0xF2CCE375), U64(0xE6A11583, 0x00D46640), /* ~= 10^320 */ U64(0xA1E53AF4, 0x6F801C53), U64(0x60495AE3, 0xC1097FD0), /* ~= 10^321 */ U64(0xCA5E89B1, 0x8B602368), U64(0x385BB19C, 0xB14BDFC4), /* ~= 10^322 */ U64(0xFCF62C1D, 0xEE382C42), U64(0x46729E03, 0xDD9ED7B5), /* ~= 10^323 */ U64(0x9E19DB92, 0xB4E31BA9), U64(0x6C07A2C2, 0x6A8346D1) /* ~= 10^324 */ }; /** Get the cached pow10 value from pow10_sig_table. @param exp10 The exponent of pow(10, e). This value must in range POW10_SIG_TABLE_MIN_EXP to POW10_SIG_TABLE_MAX_EXP. @param hi The highest 64 bits of pow(10, e). @param lo The lower 64 bits after `hi`. */ static_inline void pow10_table_get_sig(i32 exp10, u64 *hi, u64 *lo) { i32 idx = exp10 - (POW10_SIG_TABLE_MIN_EXP); *hi = pow10_sig_table[idx * 2]; *lo = pow10_sig_table[idx * 2 + 1]; } /** Get the exponent (base 2) for highest 64 bits significand in pow10_sig_table. */ static_inline void pow10_table_get_exp(i32 exp10, i32 *exp2) { /* e2 = floor(log2(pow(10, e))) - 64 + 1 */ /* = floor(e * log2(10) - 63) */ *exp2 = (exp10 * 217706 - 4128768) >> 16; } #endif /*============================================================================== * JSON Character Matcher *============================================================================*/ /** Character type */ typedef u8 char_type; /** Whitespace character: ' ', '\\t', '\\n', '\\r'. */ static const char_type CHAR_TYPE_SPACE = 1 << 0; /** Number character: '-', [0-9]. */ static const char_type CHAR_TYPE_NUMBER = 1 << 1; /** JSON Escaped character: '"', '\', [0x00-0x1F]. */ static const char_type CHAR_TYPE_ESC_ASCII = 1 << 2; /** Non-ASCII character: [0x80-0xFF]. */ static const char_type CHAR_TYPE_NON_ASCII = 1 << 3; /** JSON container character: '{', '['. */ static const char_type CHAR_TYPE_CONTAINER = 1 << 4; /** Comment character: '/'. */ static const char_type CHAR_TYPE_COMMENT = 1 << 5; /** Line end character: '\\n', '\\r', '\0'. */ static const char_type CHAR_TYPE_LINE_END = 1 << 6; /** Hexadecimal numeric character: [0-9a-fA-F]. */ static const char_type CHAR_TYPE_HEX = 1 << 7; /** Character type table (generate with misc/make_tables.c) */ static const char_type char_table[256] = { 0x44, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x05, 0x45, 0x04, 0x04, 0x45, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x20, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x04, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }; /** Match a character with specified type. */ static_inline bool char_is_type(u8 c, char_type type) { return (char_table[c] & type) != 0; } /** Match a whitespace: ' ', '\\t', '\\n', '\\r'. */ static_inline bool char_is_space(u8 c) { return char_is_type(c, (char_type)CHAR_TYPE_SPACE); } /** Match a whitespace or comment: ' ', '\\t', '\\n', '\\r', '/'. */ static_inline bool char_is_space_or_comment(u8 c) { return char_is_type(c, (char_type)(CHAR_TYPE_SPACE | CHAR_TYPE_COMMENT)); } /** Match a JSON number: '-', [0-9]. */ static_inline bool char_is_number(u8 c) { return char_is_type(c, (char_type)CHAR_TYPE_NUMBER); } /** Match a JSON container: '{', '['. */ static_inline bool char_is_container(u8 c) { return char_is_type(c, (char_type)CHAR_TYPE_CONTAINER); } /** Match a stop character in ASCII string: '"', '\', [0x00-0x1F,0x80-0xFF]. */ static_inline bool char_is_ascii_stop(u8 c) { return char_is_type(c, (char_type)(CHAR_TYPE_ESC_ASCII | CHAR_TYPE_NON_ASCII)); } /** Match a line end character: '\\n', '\\r', '\0'. */ static_inline bool char_is_line_end(u8 c) { return char_is_type(c, (char_type)CHAR_TYPE_LINE_END); } /** Match a hexadecimal numeric character: [0-9a-fA-F]. */ static_inline bool char_is_hex(u8 c) { return char_is_type(c, (char_type)CHAR_TYPE_HEX); } /*============================================================================== * Digit Character Matcher *============================================================================*/ /** Digit type */ typedef u8 digi_type; /** Digit: '0'. */ static const digi_type DIGI_TYPE_ZERO = 1 << 0; /** Digit: [1-9]. */ static const digi_type DIGI_TYPE_NONZERO = 1 << 1; /** Plus sign (positive): '+'. */ static const digi_type DIGI_TYPE_POS = 1 << 2; /** Minus sign (negative): '-'. */ static const digi_type DIGI_TYPE_NEG = 1 << 3; /** Decimal point: '.' */ static const digi_type DIGI_TYPE_DOT = 1 << 4; /** Exponent sign: 'e, 'E'. */ static const digi_type DIGI_TYPE_EXP = 1 << 5; /** Digit type table (generate with misc/make_tables.c) */ static const digi_type digi_table[256] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x08, 0x10, 0x00, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; /** Match a character with specified type. */ static_inline bool digi_is_type(u8 d, digi_type type) { return (digi_table[d] & type) != 0; } /** Match a sign: '+', '-' */ static_inline bool digi_is_sign(u8 d) { return digi_is_type(d, (digi_type)(DIGI_TYPE_POS | DIGI_TYPE_NEG)); } /** Match a none zero digit: [1-9] */ static_inline bool digi_is_nonzero(u8 d) { return digi_is_type(d, (digi_type)DIGI_TYPE_NONZERO); } /** Match a digit: [0-9] */ static_inline bool digi_is_digit(u8 d) { return digi_is_type(d, (digi_type)(DIGI_TYPE_ZERO | DIGI_TYPE_NONZERO)); } /** Match an exponent sign: 'e', 'E'. */ static_inline bool digi_is_exp(u8 d) { return digi_is_type(d, (digi_type)DIGI_TYPE_EXP); } /** Match a floating point indicator: '.', 'e', 'E'. */ static_inline bool digi_is_fp(u8 d) { return digi_is_type(d, (digi_type)(DIGI_TYPE_DOT | DIGI_TYPE_EXP)); } /** Match a digit or floating point indicator: [0-9], '.', 'e', 'E'. */ static_inline bool digi_is_digit_or_fp(u8 d) { return digi_is_type(d, (digi_type)(DIGI_TYPE_ZERO | DIGI_TYPE_NONZERO | DIGI_TYPE_DOT | DIGI_TYPE_EXP)); } #if !YYJSON_DISABLE_READER /*============================================================================== * Hex Character Reader * This function is used by JSON reader to read escaped characters. *============================================================================*/ /** This table is used to convert 4 hex character sequence to a number. A valid hex character [0-9A-Fa-f] will mapped to it's raw number [0x00, 0x0F], an invalid hex character will mapped to [0xF0]. (generate with misc/make_tables.c) */ static const u8 hex_conv_table[256] = { 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0 }; /** Scans an escaped character sequence as a UTF-16 code unit (branchless). e.g. "\\u005C" should pass "005C" as `cur`. This requires the string has 4-byte zero padding. */ static_inline bool read_hex_u16(const u8 *cur, u16 *val) { u16 c0, c1, c2, c3, t0, t1; c0 = hex_conv_table[cur[0]]; c1 = hex_conv_table[cur[1]]; c2 = hex_conv_table[cur[2]]; c3 = hex_conv_table[cur[3]]; t0 = (u16)((c0 << 8) | c2); t1 = (u16)((c1 << 8) | c3); *val = (u16)((t0 << 4) | t1); return ((t0 | t1) & (u16)0xF0F0) == 0; } /*============================================================================== * JSON Reader Utils * These functions are used by JSON reader to read literals and comments. *============================================================================*/ /** Read 'true' literal, '*cur' should be 't'. */ static_inline bool read_true(u8 **ptr, yyjson_val *val) { u8 *cur = *ptr; u8 **end = ptr; if (likely(byte_match_4(cur, "true"))) { val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_TRUE; *end = cur + 4; return true; } return false; } /** Read 'false' literal, '*cur' should be 'f'. */ static_inline bool read_false(u8 **ptr, yyjson_val *val) { u8 *cur = *ptr; u8 **end = ptr; if (likely(byte_match_4(cur + 1, "alse"))) { val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_FALSE; *end = cur + 5; return true; } return false; } /** Read 'null' literal, '*cur' should be 'n'. */ static_inline bool read_null(u8 **ptr, yyjson_val *val) { u8 *cur = *ptr; u8 **end = ptr; if (likely(byte_match_4(cur, "null"))) { val->tag = YYJSON_TYPE_NULL; *end = cur + 4; return true; } return false; } /** Read 'Inf' or 'Infinity' literal (ignoring case). */ static_inline bool read_inf(bool sign, u8 **ptr, u8 **pre, yyjson_val *val) { u8 *hdr = *ptr - sign; u8 *cur = *ptr; u8 **end = ptr; if ((cur[0] == 'I' || cur[0] == 'i') && (cur[1] == 'N' || cur[1] == 'n') && (cur[2] == 'F' || cur[2] == 'f')) { if ((cur[3] == 'I' || cur[3] == 'i') && (cur[4] == 'N' || cur[4] == 'n') && (cur[5] == 'I' || cur[5] == 'i') && (cur[6] == 'T' || cur[6] == 't') && (cur[7] == 'Y' || cur[7] == 'y')) { cur += 8; } else { cur += 3; } *end = cur; if (false) { /* add null-terminator for previous raw string */ if (*pre) **pre = '\0'; *pre = cur; val->tag = ((u64)(cur - hdr) << YYJSON_TAG_BIT) | YYJSON_TYPE_RAW; val->uni.str = (const char *)hdr; } else { val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; val->uni.u64 = f64_raw_get_inf(sign); } return true; } return false; } /** Read 'NaN' literal (ignoring case). */ static_inline bool read_nan(bool sign, u8 **ptr, u8 **pre, yyjson_val *val) { u8 *hdr = *ptr - sign; u8 *cur = *ptr; u8 **end = ptr; if ((cur[0] == 'N' || cur[0] == 'n') && (cur[1] == 'A' || cur[1] == 'a') && (cur[2] == 'N' || cur[2] == 'n')) { cur += 3; *end = cur; if (false) { /* add null-terminator for previous raw string */ if (*pre) **pre = '\0'; *pre = cur; val->tag = ((u64)(cur - hdr) << YYJSON_TAG_BIT) | YYJSON_TYPE_RAW; val->uni.str = (const char *)hdr; } else { val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; val->uni.u64 = f64_raw_get_nan(sign); } return true; } return false; } /** Read a JSON number as raw string. */ static_noinline bool read_number_raw(u8 **ptr, u8 **pre, yyjson_read_flag flg, yyjson_val *val, const char **msg) { #define return_err(_pos, _msg) do { \ *msg = _msg; \ *end = _pos; \ return false; \ } while (false) #define return_raw() do { \ val->tag = ((u64)(cur - hdr) << YYJSON_TAG_BIT) | YYJSON_TYPE_RAW; \ val->uni.str = (const char *)hdr; \ *pre = cur; *end = cur; return true; \ } while (false) u8 *hdr = *ptr; u8 *cur = *ptr; u8 **end = ptr; /* add null-terminator for previous raw string */ if (*pre) **pre = '\0'; /* skip sign */ cur += (*cur == '-'); /* read first digit, check leading zero */ if (unlikely(!digi_is_digit(*cur))) { return_err(cur, "no digit after minus sign"); } /* read integral part */ if (*cur == '0') { cur++; if (unlikely(digi_is_digit(*cur))) { return_err(cur - 1, "number with leading zero is not allowed"); } if (!digi_is_fp(*cur)) return_raw(); } else { while (digi_is_digit(*cur)) cur++; if (!digi_is_fp(*cur)) return_raw(); } /* read fraction part */ if (*cur == '.') { cur++; if (!digi_is_digit(*cur++)) { return_err(cur, "no digit after decimal point"); } while (digi_is_digit(*cur)) cur++; } /* read exponent part */ if (digi_is_exp(*cur)) { cur += 1 + digi_is_sign(cur[1]); if (!digi_is_digit(*cur++)) { return_err(cur, "no digit after exponent sign"); } while (digi_is_digit(*cur)) cur++; } return_raw(); #undef return_err #undef return_raw } /** Skips spaces and comments as many as possible. It will return false in these cases: 1. No character is skipped. The 'end' pointer is set as input cursor. 2. A multiline comment is not closed. The 'end' pointer is set as the head of this comment block. */ static_noinline bool skip_spaces_and_comments(u8 **ptr) { u8 *hdr = *ptr; u8 *cur = *ptr; u8 **end = ptr; while (true) { if (byte_match_2(cur, "/*")) { hdr = cur; cur += 2; while (true) { if (byte_match_2(cur, "*/")) { cur += 2; break; } if (*cur == 0) { *end = hdr; return false; } cur++; } continue; } if (byte_match_2(cur, "//")) { cur += 2; while (!char_is_line_end(*cur)) cur++; continue; } if (char_is_space(*cur)) { cur += 1; while (char_is_space(*cur)) cur++; continue; } break; } *end = cur; return hdr != cur; } /** Check truncated string. Returns true if `cur` match `str` but is truncated. */ static_inline bool is_truncated_str(u8 *cur, u8 *end, const char *str, bool case_sensitive) { usize len = strlen(str); if (cur + len <= end || end <= cur) return false; if (case_sensitive) { return memcmp(cur, str, (usize)(end - cur)) == 0; } for (; cur < end; cur++, str++) { if ((*cur != (u8)*str) && (*cur != (u8)*str - 'a' + 'A')) { return false; } } return true; } /** Check truncated JSON on parsing errors. Returns true if the input is valid but truncated. */ static_noinline bool is_truncated_end(u8 *hdr, u8 *cur, u8 *end, yyjson_read_code code) { if (cur >= end) return true; if (code == YYJSON_READ_ERROR_LITERAL) { if (is_truncated_str(cur, end, "true", true) || is_truncated_str(cur, end, "false", true) || is_truncated_str(cur, end, "null", true)) { return true; } } if (code == YYJSON_READ_ERROR_UNEXPECTED_CHARACTER || code == YYJSON_READ_ERROR_INVALID_NUMBER || code == YYJSON_READ_ERROR_LITERAL) { if (false) { if (*cur == '-') cur++; if (is_truncated_str(cur, end, "infinity", false) || is_truncated_str(cur, end, "nan", false)) { return true; } } } if (code == YYJSON_READ_ERROR_UNEXPECTED_CONTENT) { if (false) { if (hdr + 3 <= cur && is_truncated_str(cur - 3, end, "infinity", false)) { return true; /* e.g. infin would be read as inf + in */ } } } if (code == YYJSON_READ_ERROR_INVALID_STRING) { usize len = (usize)(end - cur); /* unicode escape sequence */ if (*cur == '\\') { if (len == 1) return true; if (len <= 5) { if (*++cur != 'u') return false; for (++cur; cur < end; cur++) { if (!char_is_hex(*cur)) return false; } return true; } return false; } /* 2 to 4 bytes UTF-8, see `read_string()` for details. */ if (*cur & 0x80) { u8 c0 = cur[0], c1 = cur[1], c2 = cur[2]; if (len == 1) { /* 2 bytes UTF-8, truncated */ if ((c0 & 0xE0) == 0xC0 && (c0 & 0x1E) != 0x00) return true; /* 3 bytes UTF-8, truncated */ if ((c0 & 0xF0) == 0xE0) return true; /* 4 bytes UTF-8, truncated */ if ((c0 & 0xF8) == 0xF0 && (c0 & 0x07) <= 0x04) return true; } if (len == 2) { /* 3 bytes UTF-8, truncated */ if ((c0 & 0xF0) == 0xE0 && (c1 & 0xC0) == 0x80) { u8 pat = (u8)(((c0 & 0x0F) << 1) | ((c1 & 0x20) >> 5)); return 0x01 <= pat && pat != 0x1B; } /* 4 bytes UTF-8, truncated */ if ((c0 & 0xF8) == 0xF0 && (c1 & 0xC0) == 0x80) { u8 pat = (u8)(((c0 & 0x07) << 2) | ((c1 & 0x30) >> 4)); return 0x01 <= pat && pat <= 0x10; } } if (len == 3) { /* 4 bytes UTF-8, truncated */ if ((c0 & 0xF8) == 0xF0 && (c1 & 0xC0) == 0x80 && (c2 & 0xC0) == 0x80) { u8 pat = (u8)(((c0 & 0x07) << 2) | ((c1 & 0x30) >> 4)); return 0x01 <= pat && pat <= 0x10; } } } } return false; } #if YYJSON_HAS_IEEE_754 && !YYJSON_DISABLE_FAST_FP_CONV /* FP_READER */ /*============================================================================== * BigInt For Floating Point Number Reader * * The bigint algorithm is used by floating-point number reader to get correctly * rounded result for numbers with lots of digits. This part of code is rarely * used for common numbers. *============================================================================*/ /** Maximum exponent of exact pow10 */ #define U64_POW10_MAX_EXP 19 /** Table: [ 10^0, ..., 10^19 ] (generate with misc/make_tables.c) */ static const u64 u64_pow10_table[U64_POW10_MAX_EXP + 1] = { U64(0x00000000, 0x00000001), U64(0x00000000, 0x0000000A), U64(0x00000000, 0x00000064), U64(0x00000000, 0x000003E8), U64(0x00000000, 0x00002710), U64(0x00000000, 0x000186A0), U64(0x00000000, 0x000F4240), U64(0x00000000, 0x00989680), U64(0x00000000, 0x05F5E100), U64(0x00000000, 0x3B9ACA00), U64(0x00000002, 0x540BE400), U64(0x00000017, 0x4876E800), U64(0x000000E8, 0xD4A51000), U64(0x00000918, 0x4E72A000), U64(0x00005AF3, 0x107A4000), U64(0x00038D7E, 0xA4C68000), U64(0x002386F2, 0x6FC10000), U64(0x01634578, 0x5D8A0000), U64(0x0DE0B6B3, 0xA7640000), U64(0x8AC72304, 0x89E80000) }; /** Maximum numbers of chunks used by a bigint (58 is enough here). */ #define BIGINT_MAX_CHUNKS 64 /** Unsigned arbitrarily large integer */ typedef struct bigint { u32 used; /* used chunks count, should not be 0 */ u64 bits[BIGINT_MAX_CHUNKS]; /* chunks */ } bigint; /** Evaluate 'big += val'. @param big A big number (can be 0). @param val An unsigned integer (can be 0). */ static_inline void bigint_add_u64(bigint *big, u64 val) { u32 idx, max; u64 num = big->bits[0]; u64 add = num + val; big->bits[0] = add; if (likely((add >= num) || (add >= val))) return; for ((void)(idx = 1), max = big->used; idx < max; idx++) { if (likely(big->bits[idx] != U64_MAX)) { big->bits[idx] += 1; return; } big->bits[idx] = 0; } big->bits[big->used++] = 1; } /** Evaluate 'big *= val'. @param big A big number (can be 0). @param val An unsigned integer (cannot be 0). */ static_inline void bigint_mul_u64(bigint *big, u64 val) { u32 idx = 0, max = big->used; u64 hi, lo, carry = 0; for (; idx < max; idx++) { if (big->bits[idx]) break; } for (; idx < max; idx++) { u128_mul_add(big->bits[idx], val, carry, &hi, &lo); big->bits[idx] = lo; carry = hi; } if (carry) big->bits[big->used++] = carry; } /** Evaluate 'big *= 2^exp'. @param big A big number (can be 0). @param exp An exponent integer (can be 0). */ static_inline void bigint_mul_pow2(bigint *big, u32 exp) { u32 shft = exp % 64; u32 move = exp / 64; u32 idx = big->used; if (unlikely(shft == 0)) { for (; idx > 0; idx--) { big->bits[idx + move - 1] = big->bits[idx - 1]; } big->used += move; while (move) big->bits[--move] = 0; } else { big->bits[idx] = 0; for (; idx > 0; idx--) { u64 num = big->bits[idx] << shft; num |= big->bits[idx - 1] >> (64 - shft); big->bits[idx + move] = num; } big->bits[move] = big->bits[0] << shft; big->used += move + (big->bits[big->used + move] > 0); while (move) big->bits[--move] = 0; } } /** Evaluate 'big *= 10^exp'. @param big A big number (can be 0). @param exp An exponent integer (cannot be 0). */ static_inline void bigint_mul_pow10(bigint *big, i32 exp) { for (; exp >= U64_POW10_MAX_EXP; exp -= U64_POW10_MAX_EXP) { bigint_mul_u64(big, u64_pow10_table[U64_POW10_MAX_EXP]); } if (exp) { bigint_mul_u64(big, u64_pow10_table[exp]); } } /** Compare two bigint. @return -1 if 'a < b', +1 if 'a > b', 0 if 'a == b'. */ static_inline i32 bigint_cmp(bigint *a, bigint *b) { u32 idx = a->used; if (a->used < b->used) return -1; if (a->used > b->used) return +1; while (idx-- > 0) { u64 av = a->bits[idx]; u64 bv = b->bits[idx]; if (av < bv) return -1; if (av > bv) return +1; } return 0; } /** Evaluate 'big = val'. @param big A big number (can be 0). @param val An unsigned integer (can be 0). */ static_inline void bigint_set_u64(bigint *big, u64 val) { big->used = 1; big->bits[0] = val; } /** Set a bigint with floating point number string. */ static_noinline void bigint_set_buf(bigint *big, u64 sig, i32 *exp, u8 *sig_cut, u8 *sig_end, u8 *dot_pos) { if (unlikely(!sig_cut)) { /* no digit cut, set significant part only */ bigint_set_u64(big, sig); return; } else { /* some digits were cut, read them from 'sig_cut' to 'sig_end' */ u8 *hdr = sig_cut; u8 *cur = hdr; u32 len = 0; u64 val = 0; bool dig_big_cut = false; bool has_dot = (hdr < dot_pos) & (dot_pos < sig_end); u32 dig_len_total = U64_SAFE_DIG + (u32)(sig_end - hdr) - has_dot; sig -= (*sig_cut >= '5'); /* sig was rounded before */ if (dig_len_total > F64_MAX_DEC_DIG) { dig_big_cut = true; sig_end -= dig_len_total - (F64_MAX_DEC_DIG + 1); sig_end -= (dot_pos + 1 == sig_end); dig_len_total = (F64_MAX_DEC_DIG + 1); } *exp -= (i32)dig_len_total - U64_SAFE_DIG; big->used = 1; big->bits[0] = sig; while (cur < sig_end) { if (likely(cur != dot_pos)) { val = val * 10 + (u8)(*cur++ - '0'); len++; if (unlikely(cur == sig_end && dig_big_cut)) { /* The last digit must be non-zero, */ /* set it to '1' for correct rounding. */ val = val - (val % 10) + 1; } if (len == U64_SAFE_DIG || cur == sig_end) { bigint_mul_pow10(big, (i32)len); bigint_add_u64(big, val); val = 0; len = 0; } } else { cur++; } } } } /*============================================================================== * Diy Floating Point *============================================================================*/ /** "Do It Yourself Floating Point" struct. */ typedef struct diy_fp { u64 sig; /* significand */ i32 exp; /* exponent, base 2 */ i32 pad; /* padding, useless */ } diy_fp; /** Get cached rounded diy_fp with pow(10, e) The input value must in range [POW10_SIG_TABLE_MIN_EXP, POW10_SIG_TABLE_MAX_EXP]. */ static_inline diy_fp diy_fp_get_cached_pow10(i32 exp10) { diy_fp fp; u64 sig_ext; pow10_table_get_sig(exp10, &fp.sig, &sig_ext); pow10_table_get_exp(exp10, &fp.exp); fp.sig += (sig_ext >> 63); return fp; } /** Returns fp * fp2. */ static_inline diy_fp diy_fp_mul(diy_fp fp, diy_fp fp2) { u64 hi, lo; u128_mul(fp.sig, fp2.sig, &hi, &lo); fp.sig = hi + (lo >> 63); fp.exp += fp2.exp + 64; return fp; } /** Convert diy_fp to IEEE-754 raw value. */ static_inline u64 diy_fp_to_ieee_raw(diy_fp fp) { u64 sig = fp.sig; i32 exp = fp.exp; u32 lz_bits; if (unlikely(fp.sig == 0)) return 0; lz_bits = u64_lz_bits(sig); sig <<= lz_bits; sig >>= F64_BITS - F64_SIG_FULL_BITS; exp -= (i32)lz_bits; exp += F64_BITS - F64_SIG_FULL_BITS; exp += F64_SIG_BITS; if (unlikely(exp >= F64_MAX_BIN_EXP)) { /* overflow */ return F64_RAW_INF; } else if (likely(exp >= F64_MIN_BIN_EXP - 1)) { /* normal */ exp += F64_EXP_BIAS; return ((u64)exp << F64_SIG_BITS) | (sig & F64_SIG_MASK); } else if (likely(exp >= F64_MIN_BIN_EXP - F64_SIG_FULL_BITS)) { /* subnormal */ return sig >> (F64_MIN_BIN_EXP - exp - 1); } else { /* underflow */ return 0; } } /*============================================================================== * JSON Number Reader (IEEE-754) *============================================================================*/ /** Maximum exact pow10 exponent for double value. */ #define F64_POW10_EXP_MAX_EXACT 22 /** Cached pow10 table. */ static const f64 f64_pow10_table[] = { 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22 }; /** Read a JSON number. 1. This function assume that the floating-point number is in IEEE-754 format. 2. This function support uint64/int64/double number. If an integer number cannot fit in uint64/int64, it will returns as a double number. If a double number is infinite, the return value is based on flag. 3. This function (with inline attribute) may generate a lot of instructions. */ static_inline bool read_number(u8 **ptr, yyjson_val *val, const char **msg) { #define return_err(_pos, _msg) do { \ *msg = _msg; \ *end = _pos; \ return false; \ } while (false) #define return_0() do { \ val->tag = YYJSON_TYPE_NUM | (u8)((u8)sign << 3); \ val->uni.u64 = 0; \ *end = cur; return true; \ } while (false) #define return_i64(_v) do { \ val->tag = YYJSON_TYPE_NUM | (u8)((u8)sign << 3); \ val->uni.u64 = (u64)(sign ? (u64)(~(_v) + 1) : (u64)(_v)); \ *end = cur; return true; \ } while (false) #define return_f64(_v) do { \ val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; \ val->uni.f64 = sign ? -(f64)(_v) : (f64)(_v); \ *end = cur; return true; \ } while (false) #define return_f64_bin(_v) do { \ val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; \ val->uni.u64 = ((u64)sign << 63) | (u64)(_v); \ *end = cur; return true; \ } while (false) #define return_inf() do { \ if (false) return_f64_bin(F64_RAW_INF); \ else return_err(hdr, "number is infinity when parsed as double"); \ } while (false) u8 *sig_cut = NULL; /* significant part cutting position for long number */ u8 *sig_end = NULL; /* significant part ending position */ u8 *dot_pos = NULL; /* decimal point position */ u64 sig = 0; /* significant part of the number */ i32 exp = 0; /* exponent part of the number */ bool exp_sign; /* temporary exponent sign from literal part */ i64 exp_sig = 0; /* temporary exponent number from significant part */ i64 exp_lit = 0; /* temporary exponent number from exponent literal part */ u64 num; /* temporary number for reading */ u8 *tmp; /* temporary cursor for reading */ u8 *hdr = *ptr; u8 *cur = *ptr; u8 **end = ptr; bool sign; sign = (*hdr == '-'); cur += sign; /* begin with a leading zero or non-digit */ if (unlikely(!digi_is_nonzero(*cur))) { /* 0 or non-digit char */ if (unlikely(*cur != '0')) { /* non-digit char */ return_err(cur, "no digit after minus sign"); } /* begin with 0 */ if (likely(!digi_is_digit_or_fp(*++cur))) return_0(); if (likely(*cur == '.')) { dot_pos = cur++; if (unlikely(!digi_is_digit(*cur))) { return_err(cur, "no digit after decimal point"); } while (unlikely(*cur == '0')) cur++; if (likely(digi_is_digit(*cur))) { /* first non-zero digit after decimal point */ sig = (u64)(*cur - '0'); /* read first digit */ cur--; goto digi_frac_1; /* continue read fraction part */ } } if (unlikely(digi_is_digit(*cur))) { return_err(cur - 1, "number with leading zero is not allowed"); } if (unlikely(digi_is_exp(*cur))) { /* 0 with any exponent is still 0 */ cur += (usize)1 + digi_is_sign(cur[1]); if (unlikely(!digi_is_digit(*cur))) { return_err(cur, "no digit after exponent sign"); } while (digi_is_digit(*++cur)); } return_f64_bin(0); } /* begin with non-zero digit */ sig = (u64)(*cur - '0'); /* Read integral part, same as the following code. for (int i = 1; i <= 18; i++) { num = cur[i] - '0'; if (num <= 9) sig = num + sig * 10; else goto digi_sepr_i; } */ #define expr_intg(i) \ if (likely((num = (u64)(cur[i] - (u8)'0')) <= 9)) sig = num + sig * 10; \ else { goto digi_sepr_##i; } repeat_in_1_18(expr_intg) #undef expr_intg cur += 19; /* skip continuous 19 digits */ if (!digi_is_digit_or_fp(*cur)) { /* this number is an integer consisting of 19 digits */ if (sign && (sig > ((u64)1 << 63))) { /* overflow */ return_f64(normalized_u64_to_f64(sig)); } return_i64(sig); } goto digi_intg_more; /* read more digits in integral part */ /* process first non-digit character */ #define expr_sepr(i) \ digi_sepr_##i: \ if (likely(!digi_is_fp(cur[i]))) { cur += i; return_i64(sig); } \ dot_pos = cur + i; \ if (likely(cur[i] == '.')) goto digi_frac_##i; \ cur += i; sig_end = cur; goto digi_exp_more; repeat_in_1_18(expr_sepr) #undef expr_sepr /* read fraction part */ #define expr_frac(i) \ digi_frac_##i: \ if (likely((num = (u64)(cur[i + 1] - (u8)'0')) <= 9)) \ sig = num + sig * 10; \ else { goto digi_stop_##i; } repeat_in_1_18(expr_frac) #undef expr_frac cur += 20; /* skip 19 digits and 1 decimal point */ if (!digi_is_digit(*cur)) goto digi_frac_end; /* fraction part end */ goto digi_frac_more; /* read more digits in fraction part */ /* significant part end */ #define expr_stop(i) \ digi_stop_##i: \ cur += i + 1; \ goto digi_frac_end; repeat_in_1_18(expr_stop) #undef expr_stop /* read more digits in integral part */ digi_intg_more: if (digi_is_digit(*cur)) { if (!digi_is_digit_or_fp(cur[1])) { /* this number is an integer consisting of 20 digits */ num = (u64)(*cur - '0'); if ((sig < (U64_MAX / 10)) || (sig == (U64_MAX / 10) && num <= (U64_MAX % 10))) { sig = num + sig * 10; cur++; /* convert to double if overflow */ if (sign) { return_f64(normalized_u64_to_f64(sig)); } return_i64(sig); } } } if (digi_is_exp(*cur)) { dot_pos = cur; goto digi_exp_more; } if (*cur == '.') { dot_pos = cur++; if (!digi_is_digit(*cur)) { return_err(cur, "no digit after decimal point"); } } /* read more digits in fraction part */ digi_frac_more: sig_cut = cur; /* too large to fit in u64, excess digits need to be cut */ sig += (*cur >= '5'); /* round */ while (digi_is_digit(*++cur)); if (!dot_pos) { dot_pos = cur; if (*cur == '.') { if (!digi_is_digit(*++cur)) { return_err(cur, "no digit after decimal point"); } while (digi_is_digit(*cur)) cur++; } } exp_sig = (i64)(dot_pos - sig_cut); exp_sig += (dot_pos < sig_cut); /* ignore trailing zeros */ tmp = cur - 1; while (*tmp == '0' || *tmp == '.') tmp--; if (tmp < sig_cut) { sig_cut = NULL; } else { sig_end = cur; } if (digi_is_exp(*cur)) goto digi_exp_more; goto digi_exp_finish; /* fraction part end */ digi_frac_end: if (unlikely(dot_pos + 1 == cur)) { return_err(cur, "no digit after decimal point"); } sig_end = cur; exp_sig = -(i64)((u64)(cur - dot_pos) - 1); if (likely(!digi_is_exp(*cur))) { if (unlikely(exp_sig < F64_MIN_DEC_EXP - 19)) { return_f64_bin(0); /* underflow */ } exp = (i32)exp_sig; goto digi_finish; } else { goto digi_exp_more; } /* read exponent part */ digi_exp_more: exp_sign = (*++cur == '-'); cur += digi_is_sign(*cur); if (unlikely(!digi_is_digit(*cur))) { return_err(cur, "no digit after exponent sign"); } while (*cur == '0') cur++; /* read exponent literal */ tmp = cur; while (digi_is_digit(*cur)) { exp_lit = (i64)((u8)(*cur++ - '0') + (u64)exp_lit * 10); } if (unlikely(cur - tmp >= U64_SAFE_DIG)) { if (exp_sign) { return_f64_bin(0); /* underflow */ } else { return_inf(); /* overflow */ } } exp_sig += exp_sign ? -exp_lit : exp_lit; /* validate exponent value */ digi_exp_finish: if (unlikely(exp_sig < F64_MIN_DEC_EXP - 19)) { return_f64_bin(0); /* underflow */ } if (unlikely(exp_sig > F64_MAX_DEC_EXP)) { return_inf(); /* overflow */ } exp = (i32)exp_sig; /* all digit read finished */ digi_finish: /* Fast path 1: 1. The floating-point number calculation should be accurate, see the comments of macro `YYJSON_DOUBLE_MATH_CORRECT`. 2. Correct rounding should be performed (fegetround() == FE_TONEAREST). 3. The input of floating point number calculation does not lose precision, which means: 64 - leading_zero(input) - trailing_zero(input) < 53. We don't check all available inputs here, because that would make the code more complicated, and not friendly to branch predictor. */ #if YYJSON_DOUBLE_MATH_CORRECT if (sig < ((u64)1 << 53) && exp >= -F64_POW10_EXP_MAX_EXACT && exp <= +F64_POW10_EXP_MAX_EXACT) { f64 dbl = (f64)sig; if (exp < 0) { dbl /= f64_pow10_table[-exp]; } else { dbl *= f64_pow10_table[+exp]; } return_f64(dbl); } #endif /* Fast path 2: To keep it simple, we only accept normal number here, let the slow path to handle subnormal and infinity number. */ if (likely(!sig_cut && exp > -F64_MAX_DEC_EXP + 1 && exp < +F64_MAX_DEC_EXP - 20)) { /* The result value is exactly equal to (sig * 10^exp), the exponent part (10^exp) can be converted to (sig2 * 2^exp2). The sig2 can be an infinite length number, only the highest 128 bits is cached in the pow10_sig_table. Now we have these bits: sig1 (normalized 64bit) : aaaaaaaa sig2 (higher 64bit) : bbbbbbbb sig2_ext (lower 64bit) : cccccccc sig2_cut (extra unknown bits) : dddddddddddd.... And the calculation process is: ---------------------------------------- aaaaaaaa * bbbbbbbbccccccccdddddddddddd.... ---------------------------------------- abababababababab + acacacacacacacac + adadadadadadadadadad.... ---------------------------------------- [hi____][lo____] + [hi2___][lo2___] + [unknown___________....] ---------------------------------------- The addition with carry may affect higher bits, but if there is a 0 in higher bits, the bits higher than 0 will not be affected. `lo2` + `unknown` may get a carry bit and may affect `hi2`, the max value of `hi2` is 0xFFFFFFFFFFFFFFFE, so `hi2` will not overflow. `lo` + `hi2` may also get a carry bit and may affect `hi`, but only the highest significant 53 bits of `hi` is needed. If there is a 0 in the lower bits of `hi`, then all the following bits can be dropped. To convert the result to IEEE-754 double number, we need to perform correct rounding: 1. if bit 54 is 0, round down, 2. if bit 54 is 1 and any bit beyond bit 54 is 1, round up, 3. if bit 54 is 1 and all bits beyond bit 54 are 0, round to even, as the extra bits is unknown, this case will not be handled here. */ u64 raw; u64 sig1, sig2, sig2_ext, hi, lo, hi2, lo2, add, bits; i32 exp2; u32 lz; bool exact = false, carry, round_up; /* convert (10^exp) to (sig2 * 2^exp2) */ pow10_table_get_sig(exp, &sig2, &sig2_ext); pow10_table_get_exp(exp, &exp2); /* normalize and multiply */ lz = u64_lz_bits(sig); sig1 = sig << lz; exp2 -= (i32)lz; u128_mul(sig1, sig2, &hi, &lo); /* The `hi` is in range [0x4000000000000000, 0xFFFFFFFFFFFFFFFE], To get normalized value, `hi` should be shifted to the left by 0 or 1. The highest significant 53 bits is used by IEEE-754 double number, and the bit 54 is used to detect rounding direction. The lowest (64 - 54 - 1) bits is used to check whether it contains 0. */ bits = hi & (((u64)1 << (64 - 54 - 1)) - 1); if (bits - 1 < (((u64)1 << (64 - 54 - 1)) - 2)) { /* (bits != 0 && bits != 0x1FF) => (bits - 1 < 0x1FF - 1) The `bits` is not zero, so we don't need to check `round to even` case. The `bits` contains bit `0`, so we can drop the extra bits after `0`. */ exact = true; } else { /* (bits == 0 || bits == 0x1FF) The `bits` is filled with all `0` or all `1`, so we need to check lower bits with another 64-bit multiplication. */ u128_mul(sig1, sig2_ext, &hi2, &lo2); add = lo + hi2; if (add + 1 > (u64)1) { /* (add != 0 && add != U64_MAX) => (add + 1 > 1) The `add` is not zero, so we don't need to check `round to even` case. The `add` contains bit `0`, so we can drop the extra bits after `0`. The `hi` cannot be U64_MAX, so it will not overflow. */ carry = add < lo || add < hi2; hi += carry; exact = true; } } if (exact) { /* normalize */ lz = hi < ((u64)1 << 63); hi <<= lz; exp2 -= (i32)lz; exp2 += 64; /* test the bit 54 and get rounding direction */ round_up = (hi & ((u64)1 << (64 - 54))) > (u64)0; hi += (round_up ? ((u64)1 << (64 - 54)) : (u64)0); /* test overflow */ if (hi < ((u64)1 << (64 - 54))) { hi = ((u64)1 << 63); exp2 += 1; } /* This is a normal number, convert it to IEEE-754 format. */ hi >>= F64_BITS - F64_SIG_FULL_BITS; exp2 += F64_BITS - F64_SIG_FULL_BITS + F64_SIG_BITS; exp2 += F64_EXP_BIAS; raw = ((u64)exp2 << F64_SIG_BITS) | (hi & F64_SIG_MASK); return_f64_bin(raw); } } /* Slow path: read double number exactly with diyfp. 1. Use cached diyfp to get an approximation value. 2. Use bigcomp to check the approximation value if needed. This algorithm refers to google's double-conversion project: https://github.com/google/double-conversion */ { const i32 ERR_ULP_LOG = 3; const i32 ERR_ULP = 1 << ERR_ULP_LOG; const i32 ERR_CACHED_POW = ERR_ULP / 2; const i32 ERR_MUL_FIXED = ERR_ULP / 2; const i32 DIY_SIG_BITS = 64; const i32 EXP_BIAS = F64_EXP_BIAS + F64_SIG_BITS; const i32 EXP_SUBNORMAL = -EXP_BIAS + 1; u64 fp_err; u32 bits; i32 order_of_magnitude; i32 effective_significand_size; i32 precision_digits_count; u64 precision_bits; u64 half_way; u64 raw; diy_fp fp, fp_upper; bigint big_full, big_comp; i32 cmp; fp.sig = sig; fp.exp = 0; fp_err = sig_cut ? (u64)(ERR_ULP / 2) : (u64)0; /* normalize */ bits = u64_lz_bits(fp.sig); fp.sig <<= bits; fp.exp -= (i32)bits; fp_err <<= bits; /* multiply and add error */ fp = diy_fp_mul(fp, diy_fp_get_cached_pow10(exp)); fp_err += (u64)ERR_CACHED_POW + (fp_err != 0) + (u64)ERR_MUL_FIXED; /* normalize */ bits = u64_lz_bits(fp.sig); fp.sig <<= bits; fp.exp -= (i32)bits; fp_err <<= bits; /* effective significand */ order_of_magnitude = DIY_SIG_BITS + fp.exp; if (likely(order_of_magnitude >= EXP_SUBNORMAL + F64_SIG_FULL_BITS)) { effective_significand_size = F64_SIG_FULL_BITS; } else if (order_of_magnitude <= EXP_SUBNORMAL) { effective_significand_size = 0; } else { effective_significand_size = order_of_magnitude - EXP_SUBNORMAL; } /* precision digits count */ precision_digits_count = DIY_SIG_BITS - effective_significand_size; if (unlikely(precision_digits_count + ERR_ULP_LOG >= DIY_SIG_BITS)) { i32 shr = (precision_digits_count + ERR_ULP_LOG) - DIY_SIG_BITS + 1; fp.sig >>= shr; fp.exp += shr; fp_err = (fp_err >> shr) + 1 + (u32)ERR_ULP; precision_digits_count -= shr; } /* half way */ precision_bits = fp.sig & (((u64)1 << precision_digits_count) - 1); precision_bits *= (u32)ERR_ULP; half_way = (u64)1 << (precision_digits_count - 1); half_way *= (u32)ERR_ULP; /* rounding */ fp.sig >>= precision_digits_count; fp.sig += (precision_bits >= half_way + fp_err); fp.exp += precision_digits_count; /* get IEEE double raw value */ raw = diy_fp_to_ieee_raw(fp); if (unlikely(raw == F64_RAW_INF)) return_inf(); if (likely(precision_bits <= half_way - fp_err || precision_bits >= half_way + fp_err)) { return_f64_bin(raw); /* number is accurate */ } /* now the number is the correct value, or the next lower value */ /* upper boundary */ if (raw & F64_EXP_MASK) { fp_upper.sig = (raw & F64_SIG_MASK) + ((u64)1 << F64_SIG_BITS); fp_upper.exp = (i32)((raw & F64_EXP_MASK) >> F64_SIG_BITS); } else { fp_upper.sig = (raw & F64_SIG_MASK); fp_upper.exp = 1; } fp_upper.exp -= F64_EXP_BIAS + F64_SIG_BITS; fp_upper.sig <<= 1; fp_upper.exp -= 1; fp_upper.sig += 1; /* add half ulp */ /* compare with bigint */ bigint_set_buf(&big_full, sig, &exp, sig_cut, sig_end, dot_pos); bigint_set_u64(&big_comp, fp_upper.sig); if (exp >= 0) { bigint_mul_pow10(&big_full, +exp); } else { bigint_mul_pow10(&big_comp, -exp); } if (fp_upper.exp > 0) { bigint_mul_pow2(&big_comp, (u32)+fp_upper.exp); } else { bigint_mul_pow2(&big_full, (u32)-fp_upper.exp); } cmp = bigint_cmp(&big_full, &big_comp); if (likely(cmp != 0)) { /* round down or round up */ raw += (cmp > 0); } else { /* falls midway, round to even */ raw += (raw & 1); } if (unlikely(raw == F64_RAW_INF)) return_inf(); return_f64_bin(raw); } #undef return_err #undef return_inf #undef return_0 #undef return_i64 #undef return_f64 #undef return_f64_bin #undef return_raw } #else /* FP_READER */ /** Read a JSON number. This is a fallback function if the custom number reader is disabled. This function use libc's strtod() to read floating-point number. */ static_inline bool read_number(u8 **ptr, yyjson_val *val, const char **msg) { #define return_err(_pos, _msg) do { \ *msg = _msg; \ *end = _pos; \ return false; \ } while (false) #define return_0() do { \ val->tag = YYJSON_TYPE_NUM | (u64)((u8)sign << 3); \ val->uni.u64 = 0; \ *end = cur; return true; \ } while (false) #define return_i64(_v) do { \ val->tag = YYJSON_TYPE_NUM | (u64)((u8)sign << 3); \ val->uni.u64 = (u64)(sign ? (u64)(~(_v) + 1) : (u64)(_v)); \ *end = cur; return true; \ } while (false) #define return_f64(_v) do { \ val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; \ val->uni.f64 = sign ? -(f64)(_v) : (f64)(_v); \ *end = cur; return true; \ } while (false) #define return_f64_bin(_v) do { \ val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; \ val->uni.u64 = ((u64)sign << 63) | (u64)(_v); \ *end = cur; return true; \ } while (false) #define return_inf() do { \ if (false) return_f64_bin(F64_RAW_INF); \ else return_err(hdr, "number is infinity when parsed as double"); \ } while (false) u64 sig, num; u8 *hdr = *ptr; u8 *cur = *ptr; u8 **end = ptr; u8 *dot = NULL; u8 *f64_end = NULL; bool sign; /* read number as raw string if has `YYJSON_READ_NUMBER_AS_RAW` flag */ if (unlikely(false)) { return read_number_raw(ptr, pre, flg, val, msg); } sign = (*hdr == '-'); cur += sign; sig = (u8)(*cur - '0'); /* read first digit, check leading zero */ if (unlikely(!digi_is_digit(*cur))) { return_err(cur, "no digit after minus sign"); } if (*cur == '0') { cur++; if (unlikely(digi_is_digit(*cur))) { return_err(cur - 1, "number with leading zero is not allowed"); } if (!digi_is_fp(*cur)) return_0(); goto read_double; } /* read continuous digits, up to 19 characters */ #define expr_intg(i) \ if (likely((num = (u64)(cur[i] - (u8)'0')) <= 9)) sig = num + sig * 10; \ else { cur += i; goto intg_end; } repeat_in_1_18(expr_intg) #undef expr_intg /* here are 19 continuous digits, skip them */ cur += 19; if (digi_is_digit(cur[0]) && !digi_is_digit_or_fp(cur[1])) { /* this number is an integer consisting of 20 digits */ num = (u8)(*cur - '0'); if ((sig < (U64_MAX / 10)) || (sig == (U64_MAX / 10) && num <= (U64_MAX % 10))) { sig = num + sig * 10; cur++; if (sign) { if (false) return_raw(); return_f64(normalized_u64_to_f64(sig)); } return_i64(sig); } } intg_end: /* continuous digits ended */ if (!digi_is_digit_or_fp(*cur)) { /* this number is an integer consisting of 1 to 19 digits */ if (sign && (sig > ((u64)1 << 63))) { if (false) return_raw(); return_f64(normalized_u64_to_f64(sig)); } return_i64(sig); } read_double: /* this number should be read as double */ while (digi_is_digit(*cur)) cur++; if (*cur == '.') { /* skip fraction part */ dot = cur; cur++; if (!digi_is_digit(*cur)) { return_err(cur, "no digit after decimal point"); } cur++; while (digi_is_digit(*cur)) cur++; } if (digi_is_exp(*cur)) { /* skip exponent part */ cur += 1 + digi_is_sign(cur[1]); if (!digi_is_digit(*cur)) { return_err(cur, "no digit after exponent sign"); } cur++; while (digi_is_digit(*cur)) cur++; } /* libc's strtod() is used to parse the floating-point number. Note that the decimal point character used by strtod() is locale-dependent, and the rounding direction may affected by fesetround(). For currently known locales, (en, zh, ja, ko, am, he, hi) use '.' as the decimal point, while other locales use ',' as the decimal point. Here strtod() is called twice for different locales, but if another thread happens calls setlocale() between two strtod(), parsing may still fail. */ val->uni.f64 = strtod((const char *)hdr, (char **)&f64_end); if (unlikely(f64_end != cur)) { /* replace '.' with ',' for locale */ bool cut = (*cur == ','); if (cut) *cur = ' '; if (dot) *dot = ','; val->uni.f64 = strtod((const char *)hdr, (char **)&f64_end); /* restore ',' to '.' */ if (cut) *cur = ','; if (dot) *dot = '.'; if (unlikely(f64_end != cur)) { return_err(hdr, "strtod() failed to parse the number"); } } if (unlikely(val->uni.f64 >= HUGE_VAL || val->uni.f64 <= -HUGE_VAL)) { return_inf(); } val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; *end = cur; return true; #undef return_err #undef return_0 #undef return_i64 #undef return_f64 #undef return_f64_bin #undef return_inf #undef return_raw } #endif /* FP_READER */ /*============================================================================== * JSON String Reader *============================================================================*/ /** Read a JSON string. @param ptr The head pointer of string before '"' prefix (inout). @param lst JSON last position. @param inv Allow invalid unicode. @param val The string value to be written. @param msg The error message pointer. @return Whether success. */ static_inline bool read_string(u8 **ptr, u8 *lst, yyjson_val *val, const char **msg) { /* Each unicode code point is encoded as 1 to 4 bytes in UTF-8 encoding, we use 4-byte mask and pattern value to validate UTF-8 byte sequence, this requires the input data to have 4-byte zero padding. --------------------------------------------------- 1 byte unicode range [U+0000, U+007F] unicode min [.......0] unicode max [.1111111] bit pattern [0.......] --------------------------------------------------- 2 byte unicode range [U+0080, U+07FF] unicode min [......10 ..000000] unicode max [...11111 ..111111] bit require [...xxxx. ........] (1E 00) bit mask [xxx..... xx......] (E0 C0) bit pattern [110..... 10......] (C0 80) --------------------------------------------------- 3 byte unicode range [U+0800, U+FFFF] unicode min [........ ..100000 ..000000] unicode max [....1111 ..111111 ..111111] bit require [....xxxx ..x..... ........] (0F 20 00) bit mask [xxxx.... xx...... xx......] (F0 C0 C0) bit pattern [1110.... 10...... 10......] (E0 80 80) --------------------------------------------------- 3 byte invalid (reserved for surrogate halves) unicode range [U+D800, U+DFFF] unicode min [....1101 ..100000 ..000000] unicode max [....1101 ..111111 ..111111] bit mask [....xxxx ..x..... ........] (0F 20 00) bit pattern [....1101 ..1..... ........] (0D 20 00) --------------------------------------------------- 4 byte unicode range [U+10000, U+10FFFF] unicode min [........ ...10000 ..000000 ..000000] unicode max [.....100 ..001111 ..111111 ..111111] bit require [.....xxx ..xx.... ........ ........] (07 30 00 00) bit mask [xxxxx... xx...... xx...... xx......] (F8 C0 C0 C0) bit pattern [11110... 10...... 10...... 10......] (F0 80 80 80) --------------------------------------------------- */ #if YYJSON_ENDIAN == YYJSON_BIG_ENDIAN const u32 b1_mask = 0x80000000UL; const u32 b1_patt = 0x00000000UL; const u32 b2_mask = 0xE0C00000UL; const u32 b2_patt = 0xC0800000UL; const u32 b2_requ = 0x1E000000UL; const u32 b3_mask = 0xF0C0C000UL; const u32 b3_patt = 0xE0808000UL; const u32 b3_requ = 0x0F200000UL; const u32 b3_erro = 0x0D200000UL; const u32 b4_mask = 0xF8C0C0C0UL; const u32 b4_patt = 0xF0808080UL; const u32 b4_requ = 0x07300000UL; const u32 b4_err0 = 0x04000000UL; const u32 b4_err1 = 0x03300000UL; #elif YYJSON_ENDIAN == YYJSON_LITTLE_ENDIAN const u32 b1_mask = 0x00000080UL; const u32 b1_patt = 0x00000000UL; const u32 b2_mask = 0x0000C0E0UL; const u32 b2_patt = 0x000080C0UL; const u32 b2_requ = 0x0000001EUL; const u32 b3_mask = 0x00C0C0F0UL; const u32 b3_patt = 0x008080E0UL; const u32 b3_requ = 0x0000200FUL; const u32 b3_erro = 0x0000200DUL; const u32 b4_mask = 0xC0C0C0F8UL; const u32 b4_patt = 0x808080F0UL; const u32 b4_requ = 0x00003007UL; const u32 b4_err0 = 0x00000004UL; const u32 b4_err1 = 0x00003003UL; #else /* this should be evaluated at compile-time */ v32_uni b1_mask_uni = {{ 0x80, 0x00, 0x00, 0x00 }}; v32_uni b1_patt_uni = {{ 0x00, 0x00, 0x00, 0x00 }}; v32_uni b2_mask_uni = {{ 0xE0, 0xC0, 0x00, 0x00 }}; v32_uni b2_patt_uni = {{ 0xC0, 0x80, 0x00, 0x00 }}; v32_uni b2_requ_uni = {{ 0x1E, 0x00, 0x00, 0x00 }}; v32_uni b3_mask_uni = {{ 0xF0, 0xC0, 0xC0, 0x00 }}; v32_uni b3_patt_uni = {{ 0xE0, 0x80, 0x80, 0x00 }}; v32_uni b3_requ_uni = {{ 0x0F, 0x20, 0x00, 0x00 }}; v32_uni b3_erro_uni = {{ 0x0D, 0x20, 0x00, 0x00 }}; v32_uni b4_mask_uni = {{ 0xF8, 0xC0, 0xC0, 0xC0 }}; v32_uni b4_patt_uni = {{ 0xF0, 0x80, 0x80, 0x80 }}; v32_uni b4_requ_uni = {{ 0x07, 0x30, 0x00, 0x00 }}; v32_uni b4_err0_uni = {{ 0x04, 0x00, 0x00, 0x00 }}; v32_uni b4_err1_uni = {{ 0x03, 0x30, 0x00, 0x00 }}; u32 b1_mask = b1_mask_uni.u; u32 b1_patt = b1_patt_uni.u; u32 b2_mask = b2_mask_uni.u; u32 b2_patt = b2_patt_uni.u; u32 b2_requ = b2_requ_uni.u; u32 b3_mask = b3_mask_uni.u; u32 b3_patt = b3_patt_uni.u; u32 b3_requ = b3_requ_uni.u; u32 b3_erro = b3_erro_uni.u; u32 b4_mask = b4_mask_uni.u; u32 b4_patt = b4_patt_uni.u; u32 b4_requ = b4_requ_uni.u; u32 b4_err0 = b4_err0_uni.u; u32 b4_err1 = b4_err1_uni.u; #endif #define is_valid_seq_1(uni) ( \ ((uni & b1_mask) == b1_patt) \ ) #define is_valid_seq_2(uni) ( \ ((uni & b2_mask) == b2_patt) && \ ((uni & b2_requ)) \ ) #define is_valid_seq_3(uni) ( \ ((uni & b3_mask) == b3_patt) && \ ((tmp = (uni & b3_requ))) && \ ((tmp != b3_erro)) \ ) #define is_valid_seq_4(uni) ( \ ((uni & b4_mask) == b4_patt) && \ ((tmp = (uni & b4_requ))) && \ ((tmp & b4_err0) == 0 || (tmp & b4_err1) == 0) \ ) #define return_err(_end, _msg) do { \ *msg = _msg; \ *end = _end; \ return false; \ } while (false) u8 *cur = *ptr; u8 **end = ptr; u8 *src = ++cur, *dst; u16 hi, lo; u32 uni, tmp; skip_ascii: /* Most strings have no escaped characters, so we can jump them quickly. */ skip_ascii_begin: /* We want to make loop unrolling, as shown in the following code. Some compiler may not generate instructions as expected, so we rewrite it with explicit goto statements. We hope the compiler can generate instructions like this: https://godbolt.org/z/8vjsYq while (true) repeat16({ if (likely(!(char_is_ascii_stop(*src)))) src++; else break; }) */ #define expr_jump(i) \ if (likely(!char_is_ascii_stop(src[i]))) {} \ else goto skip_ascii_stop##i; #define expr_stop(i) \ skip_ascii_stop##i: \ src += i; \ goto skip_ascii_end; repeat16_incr(expr_jump) src += 16; goto skip_ascii_begin; repeat16_incr(expr_stop) #undef expr_jump #undef expr_stop skip_ascii_end: /* GCC may store src[i] in a register at each line of expr_jump(i) above. These instructions are useless and will degrade performance. This inline asm is a hint for gcc: "the memory has been modified, do not cache it". MSVC, Clang, ICC can generate expected instructions without this hint. */ #if YYJSON_IS_REAL_GCC __asm__ volatile("":"=m"(*src)); #endif if (likely(*src == '"')) { val->tag = ((u64)(src - cur) << YYJSON_TAG_BIT) | (u64)(YYJSON_TYPE_STR); val->uni.str = (const char *)cur; *src = '\0'; *end = src + 1; return true; } skip_utf8: if (*src & 0x80) { /* non-ASCII character */ /* Non-ASCII character appears here, which means that the text is likely to be written in non-English or emoticons. According to some common data set statistics, byte sequences of the same length may appear consecutively. We process the byte sequences of the same length in each loop, which is more friendly to branch prediction. */ #if YYJSON_DISABLE_UTF8_VALIDATION while (true) repeat8({ if (likely((*src & 0xF0) == 0xE0)) src += 3; else break; }) if (*src < 0x80) goto skip_ascii; while (true) repeat8({ if (likely((*src & 0xE0) == 0xC0)) src += 2; else break; }) while (true) repeat8({ if (likely((*src & 0xF8) == 0xF0)) src += 4; else break; }) #else uni = byte_load_4(src); while (is_valid_seq_3(uni)) { src += 3; uni = byte_load_4(src); } if (is_valid_seq_1(uni)) goto skip_ascii; while (is_valid_seq_2(uni)) { src += 2; uni = byte_load_4(src); } while (is_valid_seq_4(uni)) { src += 4; uni = byte_load_4(src); } #endif goto skip_ascii; } /* The escape character appears, we need to copy it. */ dst = src; copy_escape: if (likely(*src == '\\')) { switch (*++src) { case '"': *dst++ = '"'; src++; break; case '\\': *dst++ = '\\'; src++; break; case '/': *dst++ = '/'; src++; break; case 'b': *dst++ = '\b'; src++; break; case 'f': *dst++ = '\f'; src++; break; case 'n': *dst++ = '\n'; src++; break; case 'r': *dst++ = '\r'; src++; break; case 't': *dst++ = '\t'; src++; break; case 'u': if (unlikely(!read_hex_u16(++src, &hi))) { return_err(src - 2, "invalid escaped sequence in string"); } src += 4; if (likely((hi & 0xF800) != 0xD800)) { /* a BMP character */ if (hi >= 0x800) { *dst++ = (u8)(0xE0 | (hi >> 12)); *dst++ = (u8)(0x80 | ((hi >> 6) & 0x3F)); *dst++ = (u8)(0x80 | (hi & 0x3F)); } else if (hi >= 0x80) { *dst++ = (u8)(0xC0 | (hi >> 6)); *dst++ = (u8)(0x80 | (hi & 0x3F)); } else { *dst++ = (u8)hi; } } else { /* a non-BMP character, represented as a surrogate pair */ if (unlikely((hi & 0xFC00) != 0xD800)) { return_err(src - 6, "invalid high surrogate in string"); } if (unlikely(!byte_match_2(src, "\\u"))) { return_err(src, "no low surrogate in string"); } if (unlikely(!read_hex_u16(src + 2, &lo))) { return_err(src, "invalid escaped sequence in string"); } if (unlikely((lo & 0xFC00) != 0xDC00)) { return_err(src, "invalid low surrogate in string"); } uni = ((((u32)hi - 0xD800) << 10) | ((u32)lo - 0xDC00)) + 0x10000; *dst++ = (u8)(0xF0 | (uni >> 18)); *dst++ = (u8)(0x80 | ((uni >> 12) & 0x3F)); *dst++ = (u8)(0x80 | ((uni >> 6) & 0x3F)); *dst++ = (u8)(0x80 | (uni & 0x3F)); src += 6; } break; default: return_err(src, "invalid escaped character in string"); } } else if (likely(*src == '"')) { val->tag = ((u64)(dst - cur) << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; val->uni.str = (const char *)cur; *end = src + 1; return true; } else { return_err(src, "unexpected control character in string"); } copy_ascii: /* Copy continuous ASCII, loop unrolling, same as the following code: while (true) repeat16({ if (unlikely(char_is_ascii_stop(*src))) break; *dst++ = *src++; }) */ #if YYJSON_IS_REAL_GCC # define expr_jump(i) \ if (likely(!(char_is_ascii_stop(src[i])))) {} \ else { __asm__ volatile("":"=m"(src[i])); goto copy_ascii_stop_##i; } #else # define expr_jump(i) \ if (likely(!(char_is_ascii_stop(src[i])))) {} \ else { goto copy_ascii_stop_##i; } #endif repeat16_incr(expr_jump) #undef expr_jump byte_move_16(dst, src); src += 16; dst += 16; goto copy_ascii; /* The memory will be moved forward by at least 1 byte. So the `byte_move` can be one byte more than needed to reduce the number of instructions. */ copy_ascii_stop_0: goto copy_utf8; copy_ascii_stop_1: byte_move_2(dst, src); src += 1; dst += 1; goto copy_utf8; copy_ascii_stop_2: byte_move_2(dst, src); src += 2; dst += 2; goto copy_utf8; copy_ascii_stop_3: byte_move_4(dst, src); src += 3; dst += 3; goto copy_utf8; copy_ascii_stop_4: byte_move_4(dst, src); src += 4; dst += 4; goto copy_utf8; copy_ascii_stop_5: byte_move_4(dst, src); byte_move_2(dst + 4, src + 4); src += 5; dst += 5; goto copy_utf8; copy_ascii_stop_6: byte_move_4(dst, src); byte_move_2(dst + 4, src + 4); src += 6; dst += 6; goto copy_utf8; copy_ascii_stop_7: byte_move_8(dst, src); src += 7; dst += 7; goto copy_utf8; copy_ascii_stop_8: byte_move_8(dst, src); src += 8; dst += 8; goto copy_utf8; copy_ascii_stop_9: byte_move_8(dst, src); byte_move_2(dst + 8, src + 8); src += 9; dst += 9; goto copy_utf8; copy_ascii_stop_10: byte_move_8(dst, src); byte_move_2(dst + 8, src + 8); src += 10; dst += 10; goto copy_utf8; copy_ascii_stop_11: byte_move_8(dst, src); byte_move_4(dst + 8, src + 8); src += 11; dst += 11; goto copy_utf8; copy_ascii_stop_12: byte_move_8(dst, src); byte_move_4(dst + 8, src + 8); src += 12; dst += 12; goto copy_utf8; copy_ascii_stop_13: byte_move_8(dst, src); byte_move_4(dst + 8, src + 8); byte_move_2(dst + 12, src + 12); src += 13; dst += 13; goto copy_utf8; copy_ascii_stop_14: byte_move_8(dst, src); byte_move_4(dst + 8, src + 8); byte_move_2(dst + 12, src + 12); src += 14; dst += 14; goto copy_utf8; copy_ascii_stop_15: byte_move_16(dst, src); src += 15; dst += 15; goto copy_utf8; copy_utf8: if (*src & 0x80) { /* non-ASCII character */ uni = byte_load_4(src); #if YYJSON_DISABLE_UTF8_VALIDATION while (true) repeat4({ if ((uni & b3_mask) == b3_patt) { byte_copy_4(dst, &uni); dst += 3; src += 3; uni = byte_load_4(src); } else break; }) if ((uni & b1_mask) == b1_patt) goto copy_ascii; while (true) repeat4({ if ((uni & b2_mask) == b2_patt) { byte_copy_2(dst, &uni); dst += 2; src += 2; uni = byte_load_4(src); } else break; }) while (true) repeat4({ if ((uni & b4_mask) == b4_patt) { byte_copy_4(dst, &uni); dst += 4; src += 4; uni = byte_load_4(src); } else break; }) #else while (is_valid_seq_3(uni)) { byte_copy_4(dst, &uni); dst += 3; src += 3; uni = byte_load_4(src); } if (is_valid_seq_1(uni)) goto copy_ascii; while (is_valid_seq_2(uni)) { byte_copy_2(dst, &uni); dst += 2; src += 2; uni = byte_load_4(src); } while (is_valid_seq_4(uni)) { byte_copy_4(dst, &uni); dst += 4; src += 4; uni = byte_load_4(src); } #endif goto copy_ascii; } goto copy_escape; #undef return_err #undef is_valid_seq_1 #undef is_valid_seq_2 #undef is_valid_seq_3 #undef is_valid_seq_4 } /*============================================================================== * JSON Reader Implementation * * We use goto statements to build the finite state machine (FSM). * The FSM's state was held by program counter (PC) and the 'goto' make the * state transitions. *============================================================================*/ /** Read single value JSON document. */ static_noinline yyjson_doc *read_root_single(u8 *hdr, u8 *cur, u8 *end, yyjson_alc alc, yyjson_read_err *err) { #define return_err(_pos, _code, _msg) do { \ if (is_truncated_end(hdr, _pos, end, YYJSON_READ_ERROR_##_code)) { \ err->pos = (usize)(end - hdr); \ err->code = YYJSON_READ_ERROR_UNEXPECTED_END; \ err->msg = "unexpected end of data"; \ } else { \ err->pos = (usize)(_pos - hdr); \ err->code = YYJSON_READ_ERROR_##_code; \ err->msg = _msg; \ } \ if (val_hdr) alc.free(alc.ctx, (void *)val_hdr); \ return NULL; \ } while (false) usize hdr_len; /* value count used by doc */ usize alc_num; /* value count capacity */ yyjson_val *val_hdr; /* the head of allocated values */ yyjson_val *val; /* current value */ yyjson_doc *doc; /* the JSON document, equals to val_hdr */ const char *msg; /* error message */ hdr_len = sizeof(yyjson_doc) / sizeof(yyjson_val); hdr_len += (sizeof(yyjson_doc) % sizeof(yyjson_val)) > 0; alc_num = hdr_len + 1; /* single value */ val_hdr = (yyjson_val *)alc.malloc(alc.ctx, alc_num * sizeof(yyjson_val)); if (unlikely(!val_hdr)) goto fail_alloc; val = val_hdr + hdr_len; if (char_is_number(*cur)) { if (likely(read_number(&cur, val, &msg))) goto doc_end; goto fail_number; } if (*cur == '"') { if (likely(read_string(&cur, end, val, &msg))) goto doc_end; goto fail_string; } if (*cur == 't') { if (likely(read_true(&cur, val))) goto doc_end; goto fail_literal; } if (*cur == 'f') { if (likely(read_false(&cur, val))) goto doc_end; goto fail_literal; } if (*cur == 'n') { if (likely(read_null(&cur, val))) goto doc_end; if (false) { if (read_nan(false, &cur, 0, val)) goto doc_end; } goto fail_literal; } goto fail_character; doc_end: /* check invalid contents after json document */ if (unlikely(cur < end) && !has_read_flag(STOP_WHEN_DONE)) { if (false) { if (!skip_spaces_and_comments(&cur)) { if (byte_match_2(cur, "/*")) goto fail_comment; } } else { while (char_is_space(*cur)) cur++; } if (unlikely(cur < end)) goto fail_garbage; } doc = (yyjson_doc *)val_hdr; doc->root = val_hdr + hdr_len; doc->alc = alc; doc->dat_read = (usize)(cur - hdr); doc->val_read = 1; doc->str_pool = (char *)hdr; return doc; fail_string: return_err(cur, INVALID_STRING, msg); fail_number: return_err(cur, INVALID_NUMBER, msg); fail_alloc: return_err(cur, MEMORY_ALLOCATION, "memory allocation failed"); fail_literal: return_err(cur, LITERAL, "invalid literal"); fail_comment: return_err(cur, INVALID_COMMENT, "unclosed multiline comment"); fail_character: return_err(cur, UNEXPECTED_CHARACTER, "unexpected character"); fail_garbage: return_err(cur, UNEXPECTED_CONTENT, "unexpected content after document"); fail_recursion: return_err(cur, RECURSION_DEPTH, "array and object recursion depth exceeded"); #undef return_err } /** Read JSON document (accept all style, but optimized for minify). */ static_inline yyjson_doc *read_root_minify(u8 *hdr, u8 *cur, u8 *end, yyjson_alc alc, yyjson_read_err *err) { #define return_err(_pos, _code, _msg) do { \ if (is_truncated_end(hdr, _pos, end, YYJSON_READ_ERROR_##_code)) { \ err->pos = (usize)(end - hdr); \ err->code = YYJSON_READ_ERROR_UNEXPECTED_END; \ err->msg = "unexpected end of data"; \ } else { \ err->pos = (usize)(_pos - hdr); \ err->code = YYJSON_READ_ERROR_##_code; \ err->msg = _msg; \ } \ if (val_hdr) alc.free(alc.ctx, (void *)val_hdr); \ return NULL; \ } while (false) #define val_incr() do { \ val++; \ if (unlikely(val >= val_end)) { \ usize alc_old = alc_len; \ alc_len += alc_len / 2; \ if ((sizeof(usize) < 8) && (alc_len >= alc_max)) goto fail_alloc; \ val_tmp = (yyjson_val *)alc.realloc(alc.ctx, (void *)val_hdr, \ alc_old * sizeof(yyjson_val), \ alc_len * sizeof(yyjson_val)); \ if ((!val_tmp)) goto fail_alloc; \ val = val_tmp + (usize)(val - val_hdr); \ ctn = val_tmp + (usize)(ctn - val_hdr); \ val_hdr = val_tmp; \ val_end = val_tmp + (alc_len - 2); \ } \ } while (false) usize dat_len; /* data length in bytes, hint for allocator */ usize hdr_len; /* value count used by yyjson_doc */ usize alc_len; /* value count allocated */ usize alc_max; /* maximum value count for allocator */ usize ctn_len; /* the number of elements in current container */ yyjson_val *val_hdr; /* the head of allocated values */ yyjson_val *val_end; /* the end of allocated values */ yyjson_val *val_tmp; /* temporary pointer for realloc */ yyjson_val *val; /* current JSON value */ yyjson_val *ctn; /* current container */ yyjson_val *ctn_parent; /* parent of current container */ yyjson_doc *doc; /* the JSON document, equals to val_hdr */ const char *msg; /* error message */ u32 container_depth = 0; /* limit on number of open array and map */ bool raw; /* read number as raw */ bool inv; /* allow invalid unicode */ dat_len = has_read_flag(STOP_WHEN_DONE) ? 256 : (usize)(end - cur); hdr_len = sizeof(yyjson_doc) / sizeof(yyjson_val); hdr_len += (sizeof(yyjson_doc) % sizeof(yyjson_val)) > 0; alc_max = USIZE_MAX / sizeof(yyjson_val); alc_len = hdr_len + (dat_len / YYJSON_READER_ESTIMATED_MINIFY_RATIO) + 4; alc_len = yyjson_min(alc_len, alc_max); val_hdr = (yyjson_val *)alc.malloc(alc.ctx, alc_len * sizeof(yyjson_val)); if (unlikely(!val_hdr)) goto fail_alloc; val_end = val_hdr + (alc_len - 2); /* padding for key-value pair reading */ val = val_hdr + hdr_len; ctn = val; ctn_len = 0; if (*cur++ == '{') { ctn->tag = YYJSON_TYPE_OBJ; ctn->uni.ofs = 0; goto obj_key_begin; } else { ctn->tag = YYJSON_TYPE_ARR; ctn->uni.ofs = 0; goto arr_val_begin; } arr_begin: container_depth++; if (unlikely(container_depth >= YYJSON_READER_CONTAINER_RECURSION_LIMIT)) { goto fail_recursion; } /* save current container */ ctn->tag = (((u64)ctn_len + 1) << YYJSON_TAG_BIT) | (ctn->tag & YYJSON_TAG_MASK); /* create a new array value, save parent container offset */ val_incr(); val->tag = YYJSON_TYPE_ARR; val->uni.ofs = (usize)((u8 *)val - (u8 *)ctn); /* push the new array value as current container */ ctn = val; ctn_len = 0; arr_val_begin: if (*cur == '{') { cur++; goto obj_begin; } if (*cur == '[') { cur++; goto arr_begin; } if (char_is_number(*cur)) { val_incr(); ctn_len++; if (likely(read_number(&cur, val, &msg))) goto arr_val_end; goto fail_number; } if (*cur == '"') { val_incr(); ctn_len++; if (likely(read_string(&cur, end, val, &msg))) goto arr_val_end; goto fail_string; } if (*cur == 't') { val_incr(); ctn_len++; if (likely(read_true(&cur, val))) goto arr_val_end; goto fail_literal; } if (*cur == 'f') { val_incr(); ctn_len++; if (likely(read_false(&cur, val))) goto arr_val_end; goto fail_literal; } if (*cur == 'n') { val_incr(); ctn_len++; if (likely(read_null(&cur, val))) goto arr_val_end; goto fail_literal; } if (*cur == ']') { cur++; if (likely(ctn_len == 0)) goto arr_end; while (*cur != ',') cur--; goto fail_trailing_comma; } if (char_is_space(*cur)) { while (char_is_space(*++cur)); goto arr_val_begin; } goto fail_character; arr_val_end: if (*cur == ',') { cur++; goto arr_val_begin; } if (*cur == ']') { cur++; goto arr_end; } if (char_is_space(*cur)) { while (char_is_space(*++cur)); goto arr_val_end; } if (false) { if (skip_spaces_and_comments(&cur)) goto arr_val_end; if (byte_match_2(cur, "/*")) goto fail_comment; } goto fail_character; arr_end: container_depth--; /* get parent container */ ctn_parent = (yyjson_val *)(void *)((u8 *)ctn - ctn->uni.ofs); /* save the next sibling value offset */ ctn->uni.ofs = (usize)((u8 *)val - (u8 *)ctn) + sizeof(yyjson_val); ctn->tag = ((ctn_len) << YYJSON_TAG_BIT) | YYJSON_TYPE_ARR; if (unlikely(ctn == ctn_parent)) goto doc_end; /* pop parent as current container */ ctn = ctn_parent; ctn_len = (usize)(ctn->tag >> YYJSON_TAG_BIT); if ((ctn->tag & YYJSON_TYPE_MASK) == YYJSON_TYPE_OBJ) { goto obj_val_end; } else { goto arr_val_end; } obj_begin: container_depth++; if (unlikely(container_depth >= YYJSON_READER_CONTAINER_RECURSION_LIMIT)) { goto fail_recursion; } /* push container */ ctn->tag = (((u64)ctn_len + 1) << YYJSON_TAG_BIT) | (ctn->tag & YYJSON_TAG_MASK); val_incr(); val->tag = YYJSON_TYPE_OBJ; /* offset to the parent */ val->uni.ofs = (usize)((u8 *)val - (u8 *)ctn); ctn = val; ctn_len = 0; obj_key_begin: if (likely(*cur == '"')) { val_incr(); ctn_len++; if (likely(read_string(&cur, end, val, &msg))) goto obj_key_end; goto fail_string; } if (likely(*cur == '}')) { cur++; if (likely(ctn_len == 0)) goto obj_end; while (*cur != ',') cur--; goto fail_trailing_comma; } if (char_is_space(*cur)) { while (char_is_space(*++cur)); goto obj_key_begin; } if (false) { if (skip_spaces_and_comments(&cur)) goto obj_key_begin; if (byte_match_2(cur, "/*")) goto fail_comment; } goto fail_character; obj_key_end: if (*cur == ':') { cur++; goto obj_val_begin; } if (char_is_space(*cur)) { while (char_is_space(*++cur)); goto obj_key_end; } if (false) { if (skip_spaces_and_comments(&cur)) goto obj_key_end; if (byte_match_2(cur, "/*")) goto fail_comment; } goto fail_character; obj_val_begin: if (*cur == '"') { val++; ctn_len++; if (likely(read_string(&cur, end, val, &msg))) goto obj_val_end; goto fail_string; } if (char_is_number(*cur)) { val++; ctn_len++; if (likely(read_number(&cur, val, &msg))) goto obj_val_end; goto fail_number; } if (*cur == '{') { cur++; goto obj_begin; } if (*cur == '[') { cur++; goto arr_begin; } if (*cur == 't') { val++; ctn_len++; if (likely(read_true(&cur, val))) goto obj_val_end; goto fail_literal; } if (*cur == 'f') { val++; ctn_len++; if (likely(read_false(&cur, val))) goto obj_val_end; goto fail_literal; } if (*cur == 'n') { val++; ctn_len++; if (likely(read_null(&cur, val))) goto obj_val_end; goto fail_literal; } if (char_is_space(*cur)) { while (char_is_space(*++cur)); goto obj_val_begin; } goto fail_character; obj_val_end: if (likely(*cur == ',')) { cur++; goto obj_key_begin; } if (likely(*cur == '}')) { cur++; goto obj_end; } if (char_is_space(*cur)) { while (char_is_space(*++cur)); goto obj_val_end; } if (false) { if (skip_spaces_and_comments(&cur)) goto obj_val_end; if (byte_match_2(cur, "/*")) goto fail_comment; } goto fail_character; obj_end: container_depth--; /* pop container */ ctn_parent = (yyjson_val *)(void *)((u8 *)ctn - ctn->uni.ofs); /* point to the next value */ ctn->uni.ofs = (usize)((u8 *)val - (u8 *)ctn) + sizeof(yyjson_val); ctn->tag = (ctn_len << (YYJSON_TAG_BIT - 1)) | YYJSON_TYPE_OBJ; if (unlikely(ctn == ctn_parent)) goto doc_end; ctn = ctn_parent; ctn_len = (usize)(ctn->tag >> YYJSON_TAG_BIT); if ((ctn->tag & YYJSON_TYPE_MASK) == YYJSON_TYPE_OBJ) { goto obj_val_end; } else { goto arr_val_end; } doc_end: /* check invalid contents after json document */ if (unlikely(cur < end) && !has_read_flag(STOP_WHEN_DONE)) { if (false) { skip_spaces_and_comments(&cur); if (byte_match_2(cur, "/*")) goto fail_comment; } else { while (char_is_space(*cur)) cur++; } if (unlikely(cur < end)) goto fail_garbage; } doc = (yyjson_doc *)val_hdr; doc->root = val_hdr + hdr_len; doc->alc = alc; doc->dat_read = (usize)(cur - hdr); doc->val_read = (usize)((val - doc->root) + 1); doc->str_pool = has_read_flag(INSITU) ? NULL : (char *)hdr; return doc; fail_string: return_err(cur, INVALID_STRING, msg); fail_number: return_err(cur, INVALID_NUMBER, msg); fail_alloc: return_err(cur, MEMORY_ALLOCATION, "memory allocation failed"); fail_trailing_comma: return_err(cur, JSON_STRUCTURE, "trailing comma is not allowed"); fail_literal: return_err(cur, LITERAL, "invalid literal"); fail_comment: return_err(cur, INVALID_COMMENT, "unclosed multiline comment"); fail_character: return_err(cur, UNEXPECTED_CHARACTER, "unexpected character"); fail_garbage: return_err(cur, UNEXPECTED_CONTENT, "unexpected content after document"); fail_recursion: return_err(cur, RECURSION_DEPTH, "array and object recursion depth exceeded"); #undef val_incr #undef return_err } /** Read JSON document (accept all style, but optimized for pretty). */ static_inline yyjson_doc *read_root_pretty(u8 *hdr, u8 *cur, u8 *end, yyjson_alc alc, yyjson_read_err *err) { #define return_err(_pos, _code, _msg) do { \ if (is_truncated_end(hdr, _pos, end, YYJSON_READ_ERROR_##_code)) { \ err->pos = (usize)(end - hdr); \ err->code = YYJSON_READ_ERROR_UNEXPECTED_END; \ err->msg = "unexpected end of data"; \ } else { \ err->pos = (usize)(_pos - hdr); \ err->code = YYJSON_READ_ERROR_##_code; \ err->msg = _msg; \ } \ if (val_hdr) alc.free(alc.ctx, (void *)val_hdr); \ return NULL; \ } while (false) #define val_incr() do { \ val++; \ if (unlikely(val >= val_end)) { \ usize alc_old = alc_len; \ alc_len += alc_len / 2; \ if ((sizeof(usize) < 8) && (alc_len >= alc_max)) goto fail_alloc; \ val_tmp = (yyjson_val *)alc.realloc(alc.ctx, (void *)val_hdr, \ alc_old * sizeof(yyjson_val), \ alc_len * sizeof(yyjson_val)); \ if ((!val_tmp)) goto fail_alloc; \ val = val_tmp + (usize)(val - val_hdr); \ ctn = val_tmp + (usize)(ctn - val_hdr); \ val_hdr = val_tmp; \ val_end = val_tmp + (alc_len - 2); \ } \ } while (false) usize dat_len; /* data length in bytes, hint for allocator */ usize hdr_len; /* value count used by yyjson_doc */ usize alc_len; /* value count allocated */ usize alc_max; /* maximum value count for allocator */ usize ctn_len; /* the number of elements in current container */ yyjson_val *val_hdr; /* the head of allocated values */ yyjson_val *val_end; /* the end of allocated values */ yyjson_val *val_tmp; /* temporary pointer for realloc */ yyjson_val *val; /* current JSON value */ yyjson_val *ctn; /* current container */ yyjson_val *ctn_parent; /* parent of current container */ yyjson_doc *doc; /* the JSON document, equals to val_hdr */ const char *msg; /* error message */ u32 container_depth = 0; /* limit on number of open array and map */ dat_len = has_read_flag(STOP_WHEN_DONE) ? 256 : (usize)(end - cur); hdr_len = sizeof(yyjson_doc) / sizeof(yyjson_val); hdr_len += (sizeof(yyjson_doc) % sizeof(yyjson_val)) > 0; alc_max = USIZE_MAX / sizeof(yyjson_val); alc_len = hdr_len + (dat_len / YYJSON_READER_ESTIMATED_PRETTY_RATIO) + 4; alc_len = yyjson_min(alc_len, alc_max); val_hdr = (yyjson_val *)alc.malloc(alc.ctx, alc_len * sizeof(yyjson_val)); if (unlikely(!val_hdr)) goto fail_alloc; val_end = val_hdr + (alc_len - 2); /* padding for key-value pair reading */ val = val_hdr + hdr_len; ctn = val; ctn_len = 0; if (*cur++ == '{') { ctn->tag = YYJSON_TYPE_OBJ; ctn->uni.ofs = 0; if (*cur == '\n') cur++; goto obj_key_begin; } else { ctn->tag = YYJSON_TYPE_ARR; ctn->uni.ofs = 0; if (*cur == '\n') cur++; goto arr_val_begin; } arr_begin: container_depth++; if (unlikely(container_depth >= YYJSON_READER_CONTAINER_RECURSION_LIMIT)) { goto fail_recursion; } /* save current container */ ctn->tag = (((u64)ctn_len + 1) << YYJSON_TAG_BIT) | (ctn->tag & YYJSON_TAG_MASK); /* create a new array value, save parent container offset */ val_incr(); val->tag = YYJSON_TYPE_ARR; val->uni.ofs = (usize)((u8 *)val - (u8 *)ctn); /* push the new array value as current container */ ctn = val; ctn_len = 0; if (*cur == '\n') cur++; arr_val_begin: #if YYJSON_IS_REAL_GCC while (true) repeat16({ if (byte_match_2(cur, " ")) cur += 2; else break; }) #else while (true) repeat16({ if (likely(byte_match_2(cur, " "))) cur += 2; else break; }) #endif if (*cur == '{') { cur++; goto obj_begin; } if (*cur == '[') { cur++; goto arr_begin; } if (char_is_number(*cur)) { val_incr(); ctn_len++; if (likely(read_number(&cur, val, &msg))) goto arr_val_end; goto fail_number; } if (*cur == '"') { val_incr(); ctn_len++; if (likely(read_string(&cur, end, val, &msg))) goto arr_val_end; goto fail_string; } if (*cur == 't') { val_incr(); ctn_len++; if (likely(read_true(&cur, val))) goto arr_val_end; goto fail_literal; } if (*cur == 'f') { val_incr(); ctn_len++; if (likely(read_false(&cur, val))) goto arr_val_end; goto fail_literal; } if (*cur == 'n') { val_incr(); ctn_len++; if (likely(read_null(&cur, val))) goto arr_val_end; if (false) { if (read_nan(false, &cur, 0, val)) goto arr_val_end; } goto fail_literal; } if (*cur == ']') { cur++; if (likely(ctn_len == 0)) goto arr_end; while (*cur != ',') cur--; goto fail_trailing_comma; } if (char_is_space(*cur)) { while (char_is_space(*++cur)); goto arr_val_begin; } goto fail_character; arr_val_end: if (byte_match_2(cur, ",\n")) { cur += 2; goto arr_val_begin; } if (*cur == ',') { cur++; goto arr_val_begin; } if (*cur == ']') { cur++; goto arr_end; } if (char_is_space(*cur)) { while (char_is_space(*++cur)); goto arr_val_end; } if (false) { if (skip_spaces_and_comments(&cur)) goto arr_val_end; if (byte_match_2(cur, "/*")) goto fail_comment; } goto fail_character; arr_end: container_depth--; /* get parent container */ ctn_parent = (yyjson_val *)(void *)((u8 *)ctn - ctn->uni.ofs); /* save the next sibling value offset */ ctn->uni.ofs = (usize)((u8 *)val - (u8 *)ctn) + sizeof(yyjson_val); ctn->tag = ((ctn_len) << YYJSON_TAG_BIT) | YYJSON_TYPE_ARR; if (unlikely(ctn == ctn_parent)) goto doc_end; /* pop parent as current container */ ctn = ctn_parent; ctn_len = (usize)(ctn->tag >> YYJSON_TAG_BIT); if (*cur == '\n') cur++; if ((ctn->tag & YYJSON_TYPE_MASK) == YYJSON_TYPE_OBJ) { goto obj_val_end; } else { goto arr_val_end; } obj_begin: container_depth++; if (unlikely(container_depth >= YYJSON_READER_CONTAINER_RECURSION_LIMIT)) { goto fail_recursion; } /* push container */ ctn->tag = (((u64)ctn_len + 1) << YYJSON_TAG_BIT) | (ctn->tag & YYJSON_TAG_MASK); val_incr(); val->tag = YYJSON_TYPE_OBJ; /* offset to the parent */ val->uni.ofs = (usize)((u8 *)val - (u8 *)ctn); ctn = val; ctn_len = 0; if (*cur == '\n') cur++; obj_key_begin: #if YYJSON_IS_REAL_GCC while (true) repeat16({ if (byte_match_2(cur, " ")) cur += 2; else break; }) #else while (true) repeat16({ if (likely(byte_match_2(cur, " "))) cur += 2; else break; }) #endif if (likely(*cur == '"')) { val_incr(); ctn_len++; if (likely(read_string(&cur, end, val, &msg))) goto obj_key_end; goto fail_string; } if (likely(*cur == '}')) { cur++; if (likely(ctn_len == 0)) goto obj_end; while (*cur != ',') cur--; goto fail_trailing_comma; } if (char_is_space(*cur)) { while (char_is_space(*++cur)); goto obj_key_begin; } if (false) { if (skip_spaces_and_comments(&cur)) goto obj_key_begin; if (byte_match_2(cur, "/*")) goto fail_comment; } goto fail_character; obj_key_end: if (byte_match_2(cur, ": ")) { cur += 2; goto obj_val_begin; } if (*cur == ':') { cur++; goto obj_val_begin; } if (char_is_space(*cur)) { while (char_is_space(*++cur)); goto obj_key_end; } goto fail_character; obj_val_begin: if (*cur == '"') { val++; ctn_len++; if (likely(read_string(&cur, end, val, &msg))) goto obj_val_end; goto fail_string; } if (char_is_number(*cur)) { val++; ctn_len++; if (likely(read_number(&cur, val, &msg))) goto obj_val_end; goto fail_number; } if (*cur == '{') { cur++; goto obj_begin; } if (*cur == '[') { cur++; goto arr_begin; } if (*cur == 't') { val++; ctn_len++; if (likely(read_true(&cur, val))) goto obj_val_end; goto fail_literal; } if (*cur == 'f') { val++; ctn_len++; if (likely(read_false(&cur, val))) goto obj_val_end; goto fail_literal; } if (*cur == 'n') { val++; ctn_len++; if (likely(read_null(&cur, val))) goto obj_val_end; goto fail_literal; } if (char_is_space(*cur)) { while (char_is_space(*++cur)); goto obj_val_begin; } goto fail_character; obj_val_end: if (byte_match_2(cur, ",\n")) { cur += 2; goto obj_key_begin; } if (likely(*cur == ',')) { cur++; goto obj_key_begin; } if (likely(*cur == '}')) { cur++; goto obj_end; } if (char_is_space(*cur)) { while (char_is_space(*++cur)); goto obj_val_end; } goto fail_character; obj_end: container_depth--; /* pop container */ ctn_parent = (yyjson_val *)(void *)((u8 *)ctn - ctn->uni.ofs); /* point to the next value */ ctn->uni.ofs = (usize)((u8 *)val - (u8 *)ctn) + sizeof(yyjson_val); ctn->tag = (ctn_len << (YYJSON_TAG_BIT - 1)) | YYJSON_TYPE_OBJ; if (unlikely(ctn == ctn_parent)) goto doc_end; ctn = ctn_parent; ctn_len = (usize)(ctn->tag >> YYJSON_TAG_BIT); if (*cur == '\n') cur++; if ((ctn->tag & YYJSON_TYPE_MASK) == YYJSON_TYPE_OBJ) { goto obj_val_end; } else { goto arr_val_end; } doc_end: /* check invalid contents after json document */ if (unlikely(cur < end) && !has_read_flag(STOP_WHEN_DONE)) { if (false) { skip_spaces_and_comments(&cur); if (byte_match_2(cur, "/*")) goto fail_comment; } else { while (char_is_space(*cur)) cur++; } if (unlikely(cur < end)) goto fail_garbage; } doc = (yyjson_doc *)val_hdr; doc->root = val_hdr + hdr_len; doc->alc = alc; doc->dat_read = (usize)(cur - hdr); doc->val_read = (usize)((val - val_hdr)) - hdr_len + 1; doc->str_pool = has_read_flag(INSITU) ? NULL : (char *)hdr; return doc; fail_string: return_err(cur, INVALID_STRING, msg); fail_number: return_err(cur, INVALID_NUMBER, msg); fail_alloc: return_err(cur, MEMORY_ALLOCATION, "memory allocation failed"); fail_trailing_comma: return_err(cur, JSON_STRUCTURE, "trailing comma is not allowed"); fail_literal: return_err(cur, LITERAL, "invalid literal"); fail_comment: return_err(cur, INVALID_COMMENT, "unclosed multiline comment"); fail_character: return_err(cur, UNEXPECTED_CHARACTER, "unexpected character"); fail_garbage: return_err(cur, UNEXPECTED_CONTENT, "unexpected content after document"); fail_recursion: return_err(cur, RECURSION_DEPTH, "array and object recursion depth exceeded"); #undef val_incr #undef return_err } /*============================================================================== * JSON Reader Entrance *============================================================================*/ yyjson_doc *yyjson_read_opts(char *dat, usize len, const yyjson_alc *alc_ptr, yyjson_read_err *err) { #define return_err(_pos, _code, _msg) do { \ err->pos = (usize)(_pos); \ err->msg = _msg; \ err->code = YYJSON_READ_ERROR_##_code; \ if (!has_read_flag(INSITU) && hdr) alc.free(alc.ctx, (void *)hdr); \ return NULL; \ } while (false) yyjson_alc alc; yyjson_doc *doc; u8 *hdr = NULL, *end, *cur; if (!alc_ptr) { alc = YYJSON_DEFAULT_ALC; } else { alc = *alc_ptr; } hdr = (u8 *)alc.malloc(alc.ctx, len + YYJSON_PADDING_SIZE); end = hdr + len; cur = hdr; memcpy(hdr, dat, len); memset(end, 0, YYJSON_PADDING_SIZE); /* skip empty contents before json document */ if (unlikely(char_is_space_or_comment(*cur))) { if (likely(char_is_space(*cur))) { while (char_is_space(*++cur)); } if (unlikely(cur >= end)) { return_err(0, EMPTY_CONTENT, "input data is empty"); } } /* read json document */ if (likely(char_is_container(*cur))) { if (char_is_space(cur[1]) && char_is_space(cur[2])) { doc = read_root_pretty(hdr, cur, end, alc, err); } else { doc = read_root_minify(hdr, cur, end, alc, err); } } else { doc = read_root_single(hdr, cur, end, alc, err); } /* check result */ if (unlikely(!doc)) { alc.free(alc.ctx, (void *)hdr); } return doc; #undef return_err } yyjson_doc *yyjson_read_file(const char *path, yyjson_read_flag flg, const yyjson_alc *alc_ptr, yyjson_read_err *err) { #define return_err(_code, _msg) do { \ err->pos = 0; \ err->msg = _msg; \ err->code = YYJSON_READ_ERROR_##_code; \ return NULL; \ } while (false) yyjson_doc *doc; FILE *file; if (unlikely(!path)) return_err(INVALID_PARAMETER, "input path is NULL"); file = fopen_readonly(path); if (unlikely(!file)) return_err(FILE_OPEN, "file opening failed"); doc = yyjson_read_fp(file, flg, alc_ptr, err); fclose(file); return doc; #undef return_err } yyjson_doc *yyjson_read_fp(FILE *file, yyjson_read_flag flg, const yyjson_alc *alc_ptr, yyjson_read_err *err) { #define return_err(_code, _msg) do { \ err->pos = 0; \ err->msg = _msg; \ err->code = YYJSON_READ_ERROR_##_code; \ if (buf) alc.free(alc.ctx, buf); \ return NULL; \ } while (false) yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; yyjson_doc *doc; long file_size = 0, file_pos; void *buf = NULL; usize buf_size = 0; /* validate input parameters */ if (unlikely(!file)) return_err(INVALID_PARAMETER, "input file is NULL"); /* get current position */ file_pos = ftell(file); if (file_pos != -1) { /* get total file size, may fail */ if (fseek(file, 0, SEEK_END) == 0) file_size = ftell(file); /* reset to original position, may fail */ if (fseek(file, file_pos, SEEK_SET) != 0) file_size = 0; /* get file size from current postion to end */ if (file_size > 0) file_size -= file_pos; } /* read file */ if (file_size > 0) { /* read the entire file in one call */ buf_size = (usize)file_size + YYJSON_PADDING_SIZE; buf = alc.malloc(alc.ctx, buf_size); if (buf == NULL) { return_err(MEMORY_ALLOCATION, "fail to alloc memory"); } if (fread_safe(buf, (usize)file_size, file) != (usize)file_size) { return_err(FILE_READ, "file reading failed"); } } else { /* failed to get file size, read it as a stream */ usize chunk_min = (usize)64; usize chunk_max = (usize)512 * 1024 * 1024; usize chunk_now = chunk_min; usize read_size; void *tmp; buf_size = YYJSON_PADDING_SIZE; while (true) { if (buf_size + chunk_now < buf_size) { /* overflow */ return_err(MEMORY_ALLOCATION, "fail to alloc memory"); } buf_size += chunk_now; if (!buf) { buf = alc.malloc(alc.ctx, buf_size); if (!buf) return_err(MEMORY_ALLOCATION, "fail to alloc memory"); } else { tmp = alc.realloc(alc.ctx, buf, buf_size - chunk_now, buf_size); if (!tmp) return_err(MEMORY_ALLOCATION, "fail to alloc memory"); buf = tmp; } tmp = ((u8 *)buf) + buf_size - YYJSON_PADDING_SIZE - chunk_now; read_size = fread_safe(tmp, chunk_now, file); file_size += (long)read_size; if (read_size != chunk_now) break; chunk_now *= 2; if (chunk_now > chunk_max) chunk_now = chunk_max; } } /* read JSON */ memset((u8 *)buf + file_size, 0, YYJSON_PADDING_SIZE); flg |= YYJSON_READ_INSITU; doc = yyjson_read_opts((char *)buf, (usize)file_size, &alc, err); if (doc) { doc->str_pool = (char *)buf; return doc; } else { alc.free(alc.ctx, buf); return NULL; } #undef return_err } const char *yyjson_read_number(const char *dat, yyjson_val *val, yyjson_read_flag flg, const yyjson_alc *alc, yyjson_read_err *err) { #define return_err(_pos, _code, _msg) do { \ err->pos = _pos > hdr ? (usize)(_pos - hdr) : 0; \ err->msg = _msg; \ err->code = YYJSON_READ_ERROR_##_code; \ return NULL; \ } while (false) u8 *hdr = constcast(u8 *)dat, *cur = hdr; bool raw; /* read number as raw */ u8 *raw_end; /* raw end for null-terminator */ u8 **pre; /* previous raw end pointer */ const char *msg; #if !YYJSON_HAS_IEEE_754 || YYJSON_DISABLE_FAST_FP_CONV u8 buf[128]; usize dat_len; #endif if (unlikely(!dat)) { return_err(cur, INVALID_PARAMETER, "input data is NULL"); } if (unlikely(!val)) { return_err(cur, INVALID_PARAMETER, "output value is NULL"); } #if !YYJSON_HAS_IEEE_754 || YYJSON_DISABLE_FAST_FP_CONV if (!alc) alc = &YYJSON_DEFAULT_ALC; dat_len = strlen(dat); if (dat_len < sizeof(buf)) { memcpy(buf, dat, dat_len + 1); hdr = buf; cur = hdr; } else { hdr = (u8 *)alc->malloc(alc->ctx, dat_len + 1); cur = hdr; if (unlikely(!hdr)) { return_err(cur, MEMORY_ALLOCATION, "memory allocation failed"); } memcpy(hdr, dat, dat_len + 1); } hdr[dat_len] = 0; #endif #if !YYJSON_HAS_IEEE_754 || YYJSON_DISABLE_FAST_FP_CONV if (!read_number(&cur, val, &msg)) { if (dat_len >= sizeof(buf)) alc->free(alc->ctx, hdr); return_err(cur, INVALID_NUMBER, msg); } if (dat_len >= sizeof(buf)) alc->free(alc->ctx, hdr); if (yyjson_is_raw(val)) val->uni.str = dat; return dat + (cur - hdr); #else if (!read_number(&cur, val, &msg)) { return_err(cur, INVALID_NUMBER, msg); } return (const char *)cur; #endif #undef return_err } #endif /* YYJSON_DISABLE_READER */ #if !YYJSON_DISABLE_WRITER /*============================================================================== * Integer Writer * * The maximum value of uint32_t is 4294967295 (10 digits), * these digits are named as 'aabbccddee' here. * * Although most compilers may convert the "division by constant value" into * "multiply and shift", manual conversion can still help some compilers * generate fewer and better instructions. * * Reference: * Division by Invariant Integers using Multiplication, 1994. * https://gmplib.org/~tege/divcnst-pldi94.pdf * Improved division by invariant integers, 2011. * https://gmplib.org/~tege/division-paper.pdf *============================================================================*/ /** Digit table from 00 to 99. */ yyjson_align(2) static const char digit_table[200] = { '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', '0', '5', '0', '6', '0', '7', '0', '8', '0', '9', '1', '0', '1', '1', '1', '2', '1', '3', '1', '4', '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', '2', '0', '2', '1', '2', '2', '2', '3', '2', '4', '2', '5', '2', '6', '2', '7', '2', '8', '2', '9', '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', '3', '5', '3', '6', '3', '7', '3', '8', '3', '9', '4', '0', '4', '1', '4', '2', '4', '3', '4', '4', '4', '5', '4', '6', '4', '7', '4', '8', '4', '9', '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', '5', '5', '5', '6', '5', '7', '5', '8', '5', '9', '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', '6', '5', '6', '6', '6', '7', '6', '8', '6', '9', '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', '7', '5', '7', '6', '7', '7', '7', '8', '7', '9', '8', '0', '8', '1', '8', '2', '8', '3', '8', '4', '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', '9', '5', '9', '6', '9', '7', '9', '8', '9', '9' }; static_inline u8 *write_u32_len_8(u32 val, u8 *buf) { u32 aa, bb, cc, dd, aabb, ccdd; /* 8 digits: aabbccdd */ aabb = (u32)(((u64)val * 109951163) >> 40); /* (val / 10000) */ ccdd = val - aabb * 10000; /* (val % 10000) */ aa = (aabb * 5243) >> 19; /* (aabb / 100) */ cc = (ccdd * 5243) >> 19; /* (ccdd / 100) */ bb = aabb - aa * 100; /* (aabb % 100) */ dd = ccdd - cc * 100; /* (ccdd % 100) */ byte_copy_2(buf + 0, digit_table + aa * 2); byte_copy_2(buf + 2, digit_table + bb * 2); byte_copy_2(buf + 4, digit_table + cc * 2); byte_copy_2(buf + 6, digit_table + dd * 2); return buf + 8; } static_inline u8 *write_u32_len_4(u32 val, u8 *buf) { u32 aa, bb; /* 4 digits: aabb */ aa = (val * 5243) >> 19; /* (val / 100) */ bb = val - aa * 100; /* (val % 100) */ byte_copy_2(buf + 0, digit_table + aa * 2); byte_copy_2(buf + 2, digit_table + bb * 2); return buf + 4; } static_inline u8 *write_u32_len_1_8(u32 val, u8 *buf) { u32 aa, bb, cc, dd, aabb, bbcc, ccdd, lz; if (val < 100) { /* 1-2 digits: aa */ lz = val < 10; /* leading zero: 0 or 1 */ byte_copy_2(buf + 0, digit_table + val * 2 + lz); buf -= lz; return buf + 2; } else if (val < 10000) { /* 3-4 digits: aabb */ aa = (val * 5243) >> 19; /* (val / 100) */ bb = val - aa * 100; /* (val % 100) */ lz = aa < 10; /* leading zero: 0 or 1 */ byte_copy_2(buf + 0, digit_table + aa * 2 + lz); buf -= lz; byte_copy_2(buf + 2, digit_table + bb * 2); return buf + 4; } else if (val < 1000000) { /* 5-6 digits: aabbcc */ aa = (u32)(((u64)val * 429497) >> 32); /* (val / 10000) */ bbcc = val - aa * 10000; /* (val % 10000) */ bb = (bbcc * 5243) >> 19; /* (bbcc / 100) */ cc = bbcc - bb * 100; /* (bbcc % 100) */ lz = aa < 10; /* leading zero: 0 or 1 */ byte_copy_2(buf + 0, digit_table + aa * 2 + lz); buf -= lz; byte_copy_2(buf + 2, digit_table + bb * 2); byte_copy_2(buf + 4, digit_table + cc * 2); return buf + 6; } else { /* 7-8 digits: aabbccdd */ aabb = (u32)(((u64)val * 109951163) >> 40); /* (val / 10000) */ ccdd = val - aabb * 10000; /* (val % 10000) */ aa = (aabb * 5243) >> 19; /* (aabb / 100) */ cc = (ccdd * 5243) >> 19; /* (ccdd / 100) */ bb = aabb - aa * 100; /* (aabb % 100) */ dd = ccdd - cc * 100; /* (ccdd % 100) */ lz = aa < 10; /* leading zero: 0 or 1 */ byte_copy_2(buf + 0, digit_table + aa * 2 + lz); buf -= lz; byte_copy_2(buf + 2, digit_table + bb * 2); byte_copy_2(buf + 4, digit_table + cc * 2); byte_copy_2(buf + 6, digit_table + dd * 2); return buf + 8; } } static_inline u8 *write_u64_len_5_8(u32 val, u8 *buf) { u32 aa, bb, cc, dd, aabb, bbcc, ccdd, lz; if (val < 1000000) { /* 5-6 digits: aabbcc */ aa = (u32)(((u64)val * 429497) >> 32); /* (val / 10000) */ bbcc = val - aa * 10000; /* (val % 10000) */ bb = (bbcc * 5243) >> 19; /* (bbcc / 100) */ cc = bbcc - bb * 100; /* (bbcc % 100) */ lz = aa < 10; /* leading zero: 0 or 1 */ byte_copy_2(buf + 0, digit_table + aa * 2 + lz); buf -= lz; byte_copy_2(buf + 2, digit_table + bb * 2); byte_copy_2(buf + 4, digit_table + cc * 2); return buf + 6; } else { /* 7-8 digits: aabbccdd */ aabb = (u32)(((u64)val * 109951163) >> 40); /* (val / 10000) */ ccdd = val - aabb * 10000; /* (val % 10000) */ aa = (aabb * 5243) >> 19; /* (aabb / 100) */ cc = (ccdd * 5243) >> 19; /* (ccdd / 100) */ bb = aabb - aa * 100; /* (aabb % 100) */ dd = ccdd - cc * 100; /* (ccdd % 100) */ lz = aa < 10; /* leading zero: 0 or 1 */ byte_copy_2(buf + 0, digit_table + aa * 2 + lz); buf -= lz; byte_copy_2(buf + 2, digit_table + bb * 2); byte_copy_2(buf + 4, digit_table + cc * 2); byte_copy_2(buf + 6, digit_table + dd * 2); return buf + 8; } } static_inline u8 *write_u64(u64 val, u8 *buf) { u64 tmp, hgh; u32 mid, low; if (val < 100000000) { /* 1-8 digits */ buf = write_u32_len_1_8((u32)val, buf); return buf; } else if (val < (u64)100000000 * 100000000) { /* 9-16 digits */ hgh = val / 100000000; /* (val / 100000000) */ low = (u32)(val - hgh * 100000000); /* (val % 100000000) */ buf = write_u32_len_1_8((u32)hgh, buf); buf = write_u32_len_8(low, buf); return buf; } else { /* 17-20 digits */ tmp = val / 100000000; /* (val / 100000000) */ low = (u32)(val - tmp * 100000000); /* (val % 100000000) */ hgh = (u32)(tmp / 10000); /* (tmp / 10000) */ mid = (u32)(tmp - hgh * 10000); /* (tmp % 10000) */ buf = write_u64_len_5_8((u32)hgh, buf); buf = write_u32_len_4(mid, buf); buf = write_u32_len_8(low, buf); return buf; } } /*============================================================================== * Number Writer *============================================================================*/ #if YYJSON_HAS_IEEE_754 && !YYJSON_DISABLE_FAST_FP_CONV /* FP_WRITER */ /** Trailing zero count table for number 0 to 99. (generate with misc/make_tables.c) */ static const u8 dec_trailing_zero_table[] = { 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; /** Write an unsigned integer with a length of 1 to 16. */ static_inline u8 *write_u64_len_1_to_16(u64 val, u8 *buf) { u64 hgh; u32 low; if (val < 100000000) { /* 1-8 digits */ buf = write_u32_len_1_8((u32)val, buf); return buf; } else { /* 9-16 digits */ hgh = val / 100000000; /* (val / 100000000) */ low = (u32)(val - hgh * 100000000); /* (val % 100000000) */ buf = write_u32_len_1_8((u32)hgh, buf); buf = write_u32_len_8(low, buf); return buf; } } /** Write an unsigned integer with a length of 1 to 17. */ static_inline u8 *write_u64_len_1_to_17(u64 val, u8 *buf) { u64 hgh; u32 mid, low, one; if (val >= (u64)100000000 * 10000000) { /* len: 16 to 17 */ hgh = val / 100000000; /* (val / 100000000) */ low = (u32)(val - hgh * 100000000); /* (val % 100000000) */ one = (u32)(hgh / 100000000); /* (hgh / 100000000) */ mid = (u32)(hgh - (u64)one * 100000000); /* (hgh % 100000000) */ *buf = (u8)((u8)one + (u8)'0'); buf += one > 0; buf = write_u32_len_8(mid, buf); buf = write_u32_len_8(low, buf); return buf; } else if (val >= (u64)100000000){ /* len: 9 to 15 */ hgh = val / 100000000; /* (val / 100000000) */ low = (u32)(val - hgh * 100000000); /* (val % 100000000) */ buf = write_u32_len_1_8((u32)hgh, buf); buf = write_u32_len_8(low, buf); return buf; } else { /* len: 1 to 8 */ buf = write_u32_len_1_8((u32)val, buf); return buf; } } /** Write an unsigned integer with a length of 15 to 17 with trailing zero trimmed. These digits are named as "aabbccddeeffgghhii" here. For example, input 1234567890123000, output "1234567890123". */ static_inline u8 *write_u64_len_15_to_17_trim(u8 *buf, u64 sig) { bool lz; /* leading zero */ u32 tz1, tz2, tz; /* trailing zero */ u32 abbccddee = (u32)(sig / 100000000); u32 ffgghhii = (u32)(sig - (u64)abbccddee * 100000000); u32 abbcc = abbccddee / 10000; /* (abbccddee / 10000) */ u32 ddee = abbccddee - abbcc * 10000; /* (abbccddee % 10000) */ u32 abb = (u32)(((u64)abbcc * 167773) >> 24); /* (abbcc / 100) */ u32 a = (abb * 41) >> 12; /* (abb / 100) */ u32 bb = abb - a * 100; /* (abb % 100) */ u32 cc = abbcc - abb * 100; /* (abbcc % 100) */ /* write abbcc */ buf[0] = (u8)(a + '0'); buf += a > 0; lz = bb < 10 && a == 0; byte_copy_2(buf + 0, digit_table + bb * 2 + lz); buf -= lz; byte_copy_2(buf + 2, digit_table + cc * 2); if (ffgghhii) { u32 dd = (ddee * 5243) >> 19; /* (ddee / 100) */ u32 ee = ddee - dd * 100; /* (ddee % 100) */ u32 ffgg = (u32)(((u64)ffgghhii * 109951163) >> 40); /* (val / 10000) */ u32 hhii = ffgghhii - ffgg * 10000; /* (val % 10000) */ u32 ff = (ffgg * 5243) >> 19; /* (aabb / 100) */ u32 gg = ffgg - ff * 100; /* (aabb % 100) */ byte_copy_2(buf + 4, digit_table + dd * 2); byte_copy_2(buf + 6, digit_table + ee * 2); byte_copy_2(buf + 8, digit_table + ff * 2); byte_copy_2(buf + 10, digit_table + gg * 2); if (hhii) { u32 hh = (hhii * 5243) >> 19; /* (ccdd / 100) */ u32 ii = hhii - hh * 100; /* (ccdd % 100) */ byte_copy_2(buf + 12, digit_table + hh * 2); byte_copy_2(buf + 14, digit_table + ii * 2); tz1 = dec_trailing_zero_table[hh]; tz2 = dec_trailing_zero_table[ii]; tz = ii ? tz2 : (tz1 + 2); buf += 16 - tz; return buf; } else { tz1 = dec_trailing_zero_table[ff]; tz2 = dec_trailing_zero_table[gg]; tz = gg ? tz2 : (tz1 + 2); buf += 12 - tz; return buf; } } else { if (ddee) { u32 dd = (ddee * 5243) >> 19; /* (ddee / 100) */ u32 ee = ddee - dd * 100; /* (ddee % 100) */ byte_copy_2(buf + 4, digit_table + dd * 2); byte_copy_2(buf + 6, digit_table + ee * 2); tz1 = dec_trailing_zero_table[dd]; tz2 = dec_trailing_zero_table[ee]; tz = ee ? tz2 : (tz1 + 2); buf += 8 - tz; return buf; } else { tz1 = dec_trailing_zero_table[bb]; tz2 = dec_trailing_zero_table[cc]; tz = cc ? tz2 : (tz1 + tz2); buf += 4 - tz; return buf; } } } /** Write a signed integer in the range -324 to 308. */ static_inline u8 *write_f64_exp(i32 exp, u8 *buf) { buf[0] = '-'; buf += exp < 0; exp = exp < 0 ? -exp : exp; if (exp < 100) { u32 lz = exp < 10; byte_copy_2(buf + 0, digit_table + (u32)exp * 2 + lz); return buf + 2 - lz; } else { u32 hi = ((u32)exp * 656) >> 16; /* exp / 100 */ u32 lo = (u32)exp - hi * 100; /* exp % 100 */ buf[0] = (u8)((u8)hi + (u8)'0'); byte_copy_2(buf + 1, digit_table + lo * 2); return buf + 3; } } /** Multiplies 128-bit integer and returns highest 64-bit rounded value. */ static_inline u64 round_to_odd(u64 hi, u64 lo, u64 cp) { u64 x_hi, x_lo, y_hi, y_lo; u128_mul(cp, lo, &x_hi, &x_lo); u128_mul_add(cp, hi, x_hi, &y_hi, &y_lo); return y_hi | (y_lo > 1); } /** Convert double number from binary to decimal. The output significand is shortest decimal but may have trailing zeros. This function use the Schubfach algorithm: Raffaello Giulietti, The Schubfach way to render doubles (5th version), 2022. https://drive.google.com/file/d/1gp5xv4CAa78SVgCeWfGqqI4FfYYYuNFb https://mail.openjdk.java.net/pipermail/core-libs-dev/2021-November/083536.html https://github.com/openjdk/jdk/pull/3402 (Java implementation) https://github.com/abolz/Drachennest (C++ implementation) See also: Dragonbox: A New Floating-Point Binary-to-Decimal Conversion Algorithm, 2022. https://github.com/jk-jeon/dragonbox/blob/master/other_files/Dragonbox.pdf https://github.com/jk-jeon/dragonbox @param sig_raw The raw value of significand in IEEE 754 format. @param exp_raw The raw value of exponent in IEEE 754 format. @param sig_bin The decoded value of significand in binary. @param exp_bin The decoded value of exponent in binary. @param sig_dec The output value of significand in decimal. @param exp_dec The output value of exponent in decimal. @warning The input double number should not be 0, inf, nan. */ static_inline void f64_bin_to_dec(u64 sig_raw, u32 exp_raw, u64 sig_bin, i32 exp_bin, u64 *sig_dec, i32 *exp_dec) { bool is_even, regular_spacing, u_inside, w_inside, round_up; u64 s, sp, cb, cbl, cbr, vb, vbl, vbr, pow10hi, pow10lo, upper, lower, mid; i32 k, h, exp10; is_even = !(sig_bin & 1); regular_spacing = (sig_raw == 0 && exp_raw > 1); cbl = 4 * sig_bin - 2 + regular_spacing; cb = 4 * sig_bin; cbr = 4 * sig_bin + 2; /* exp_bin: [-1074, 971] */ /* k = regular_spacing ? floor(log10(pow(2, exp_bin))) */ /* : floor(log10(pow(2, exp_bin) * 3.0 / 4.0)) */ /* = regular_spacing ? floor(exp_bin * log10(2)) */ /* : floor(exp_bin * log10(2) + log10(3.0 / 4.0)) */ k = (i32)(exp_bin * 315653 - (regular_spacing ? 131237 : 0)) >> 20; /* k: [-324, 292] */ /* h = exp_bin + floor(log2(pow(10, e))) */ /* = exp_bin + floor(log2(10) * e) */ exp10 = -k; h = exp_bin + ((exp10 * 217707) >> 16) + 1; pow10_table_get_sig(exp10, &pow10hi, &pow10lo); pow10lo += (exp10 < POW10_SIG_TABLE_MIN_EXACT_EXP || exp10 > POW10_SIG_TABLE_MAX_EXACT_EXP); vbl = round_to_odd(pow10hi, pow10lo, cbl << h); vb = round_to_odd(pow10hi, pow10lo, cb << h); vbr = round_to_odd(pow10hi, pow10lo, cbr << h); lower = vbl + !is_even; upper = vbr - !is_even; s = vb / 4; if (s >= 10) { sp = s / 10; u_inside = (lower <= 40 * sp); w_inside = (upper >= 40 * sp + 40); if (u_inside != w_inside) { *sig_dec = sp + w_inside; *exp_dec = k + 1; return; } } u_inside = (lower <= 4 * s); w_inside = (upper >= 4 * s + 4); mid = 4 * s + 2; round_up = (vb > mid) || (vb == mid && (s & 1) != 0); *sig_dec = s + ((u_inside != w_inside) ? w_inside : round_up); *exp_dec = k; } /** Write a double number (requires 32 bytes buffer). We follows the ECMAScript specification to print floating point numbers, but with the following changes: 1. Keep the negative sign of 0.0 to preserve input information. 2. Keep decimal point to indicate the number is floating point. 3. Remove positive sign of exponent part. */ static_inline u8 *write_f64_raw(u8 *buf, u64 raw, yyjson_write_flag flg) { u64 sig_bin, sig_dec, sig_raw; i32 exp_bin, exp_dec, sig_len, dot_pos, i, max; u32 exp_raw, hi, lo; u8 *hdr, *num_hdr, *num_end, *dot_end; bool sign; /* decode raw bytes from IEEE-754 double format. */ sign = (bool)(raw >> (F64_BITS - 1)); sig_raw = raw & F64_SIG_MASK; exp_raw = (u32)((raw & F64_EXP_MASK) >> F64_SIG_BITS); /* return inf and nan */ if (unlikely(exp_raw == ((u32)1 << F64_EXP_BITS) - 1)) { if (has_write_flag(INF_AND_NAN_AS_NULL)) { byte_copy_4(buf, "null"); return buf + 4; } else if (has_write_flag(ALLOW_INF_AND_NAN)) { if (sig_raw == 0) { buf[0] = '-'; buf += sign; byte_copy_8(buf, "Infinity"); buf += 8; return buf; } else { byte_copy_4(buf, "NaN"); return buf + 3; } } return NULL; } /* add sign for all finite double value, including 0.0 and inf */ buf[0] = '-'; buf += sign; hdr = buf; /* return zero */ if ((raw << 1) == 0) { byte_copy_4(buf, "0.0"); buf += 3; return buf; } if (likely(exp_raw != 0)) { /* normal number */ sig_bin = sig_raw | ((u64)1 << F64_SIG_BITS); exp_bin = (i32)exp_raw - F64_EXP_BIAS - F64_SIG_BITS; /* fast path for small integer number without fraction */ if (-F64_SIG_BITS <= exp_bin && exp_bin <= 0) { if (u64_tz_bits(sig_bin) >= (u32)-exp_bin) { /* number is integer in range 1 to 0x1FFFFFFFFFFFFF */ sig_dec = sig_bin >> -exp_bin; buf = write_u64_len_1_to_16(sig_dec, buf); byte_copy_2(buf, ".0"); buf += 2; return buf; } } /* binary to decimal */ f64_bin_to_dec(sig_raw, exp_raw, sig_bin, exp_bin, &sig_dec, &exp_dec); /* the sig length is 15 to 17 */ sig_len = 17; sig_len -= (sig_dec < (u64)100000000 * 100000000); sig_len -= (sig_dec < (u64)100000000 * 10000000); /* the decimal point position relative to the first digit */ dot_pos = sig_len + exp_dec; if (-6 < dot_pos && dot_pos <= 21) { /* no need to write exponent part */ if (dot_pos <= 0) { /* dot before first digit */ /* such as 0.1234, 0.000001234 */ num_hdr = hdr + (2 - dot_pos); num_end = write_u64_len_15_to_17_trim(num_hdr, sig_dec); hdr[0] = '0'; hdr[1] = '.'; hdr += 2; max = -dot_pos; for (i = 0; i < max; i++) hdr[i] = '0'; return num_end; } else { /* dot after first digit */ /* such as 1.234, 1234.0, 123400000000000000000.0 */ memset(hdr + 0, '0', 8); memset(hdr + 8, '0', 8); memset(hdr + 16, '0', 8); num_hdr = hdr + 1; num_end = write_u64_len_15_to_17_trim(num_hdr, sig_dec); for (i = 0; i < dot_pos; i++) hdr[i] = hdr[i + 1]; hdr[dot_pos] = '.'; dot_end = hdr + dot_pos + 2; return dot_end < num_end ? num_end : dot_end; } } else { /* write with scientific notation */ /* such as 1.234e56 */ u8 *end = write_u64_len_15_to_17_trim(buf + 1, sig_dec); end -= (end == buf + 2); /* remove '.0', e.g. 2.0e34 -> 2e34 */ exp_dec += sig_len - 1; hdr[0] = hdr[1]; hdr[1] = '.'; end[0] = 'e'; buf = write_f64_exp(exp_dec, end + 1); return buf; } } else { /* subnormal number */ sig_bin = sig_raw; exp_bin = 1 - F64_EXP_BIAS - F64_SIG_BITS; /* binary to decimal */ f64_bin_to_dec(sig_raw, exp_raw, sig_bin, exp_bin, &sig_dec, &exp_dec); /* write significand part */ buf = write_u64_len_1_to_17(sig_dec, buf + 1); hdr[0] = hdr[1]; hdr[1] = '.'; do { buf--; exp_dec++; } while (*buf == '0'); exp_dec += (i32)(buf - hdr - 2); buf += (*buf != '.'); buf[0] = 'e'; buf++; /* write exponent part */ buf[0] = '-'; buf++; exp_dec = -exp_dec; hi = ((u32)exp_dec * 656) >> 16; /* exp / 100 */ lo = (u32)exp_dec - hi * 100; /* exp % 100 */ buf[0] = (u8)((u8)hi + (u8)'0'); byte_copy_2(buf + 1, digit_table + lo * 2); buf += 3; return buf; } } #else /* FP_WRITER */ /** Write a double number (requires 32 bytes buffer). */ static_inline u8 *write_f64_raw(u8 *buf, u64 raw, yyjson_write_flag flg) { /* For IEEE 754, `DBL_DECIMAL_DIG` is 17 for round-trip. For non-IEEE formats, 17 is used to avoid buffer overflow, round-trip is not guaranteed. */ #if defined(DBL_DECIMAL_DIG) && DBL_DECIMAL_DIG != 17 int dig = DBL_DECIMAL_DIG > 17 ? 17 : DBL_DECIMAL_DIG; #else int dig = 17; #endif /* The snprintf() function is locale-dependent. For currently known locales, (en, zh, ja, ko, am, he, hi) use '.' as the decimal point, while other locales use ',' as the decimal point. we need to replace ',' with '.' to avoid the locale setting. */ f64 val = f64_from_raw(raw); #if YYJSON_MSC_VER >= 1400 int len = sprintf_s((char *)buf, 32, "%.*g", dig, val); #elif defined(snprintf) || (YYJSON_STDC_VER >= 199901L) int len = snprintf((char *)buf, 32, "%.*g", dig, val); #else int len = sprintf((char *)buf, "%.*g", dig, val); #endif u8 *cur = buf; if (unlikely(len < 1)) return NULL; cur += (*cur == '-'); if (unlikely(!digi_is_digit(*cur))) { /* nan, inf, or bad output */ if (has_write_flag(INF_AND_NAN_AS_NULL)) { byte_copy_4(buf, "null"); return buf + 4; } else if (has_write_flag(ALLOW_INF_AND_NAN)) { if (*cur == 'i') { byte_copy_8(cur, "Infinity"); cur += 8; return cur; } else if (*cur == 'n') { byte_copy_4(buf, "NaN"); return buf + 3; } } return NULL; } else { /* finite number */ int i = 0; bool fp = false; for (; i < len; i++) { if (buf[i] == ',') buf[i] = '.'; if (digi_is_fp((u8)buf[i])) fp = true; } if (!fp) { buf[len++] = '.'; buf[len++] = '0'; } } return buf + len; } #endif /* FP_WRITER */ /** Write a JSON number (requires 32 bytes buffer). */ static_inline u8 *write_number(u8 *cur, yyjson_val *val, yyjson_write_flag flg) { if (val->tag & YYJSON_SUBTYPE_REAL) { u64 raw = val->uni.u64; return write_f64_raw(cur, raw, flg); } else { u64 pos = val->uni.u64; u64 neg = ~pos + 1; usize sgn = ((val->tag & YYJSON_SUBTYPE_SINT) > 0) & ((i64)pos < 0); *cur = '-'; return write_u64(sgn ? neg : pos, cur + sgn); } } /*============================================================================== * String Writer *============================================================================*/ /** Character encode type, if (type > CHAR_ENC_ERR_1) bytes = type / 2; */ typedef u8 char_enc_type; #define CHAR_ENC_CPY_1 0 /* 1-byte UTF-8, copy. */ #define CHAR_ENC_ERR_1 1 /* 1-byte UTF-8, error. */ #define CHAR_ENC_ESC_A 2 /* 1-byte ASCII, escaped as '\x'. */ #define CHAR_ENC_ESC_1 3 /* 1-byte UTF-8, escaped as '\uXXXX'. */ #define CHAR_ENC_CPY_2 4 /* 2-byte UTF-8, copy. */ #define CHAR_ENC_ESC_2 5 /* 2-byte UTF-8, escaped as '\uXXXX'. */ #define CHAR_ENC_CPY_3 6 /* 3-byte UTF-8, copy. */ #define CHAR_ENC_ESC_3 7 /* 3-byte UTF-8, escaped as '\uXXXX'. */ #define CHAR_ENC_CPY_4 8 /* 4-byte UTF-8, copy. */ #define CHAR_ENC_ESC_4 9 /* 4-byte UTF-8, escaped as '\uXXXX\uXXXX'. */ /** Character encode type table: don't escape unicode, don't escape '/'. (generate with misc/make_tables.c) */ static const char_enc_type enc_table_cpy[256] = { 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 3, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 8, 8, 8, 8, 8, 8, 8, 8, 1, 1, 1, 1, 1, 1, 1, 1 }; /** Character encode type table: don't escape unicode, escape '/'. (generate with misc/make_tables.c) */ static const char_enc_type enc_table_cpy_slash[256] = { 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 3, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 8, 8, 8, 8, 8, 8, 8, 8, 1, 1, 1, 1, 1, 1, 1, 1 }; /** Character encode type table: escape unicode, don't escape '/'. (generate with misc/make_tables.c) */ static const char_enc_type enc_table_esc[256] = { 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 3, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1, 1, 1, 1, 1, 1 }; /** Character encode type table: escape unicode, escape '/'. (generate with misc/make_tables.c) */ static const char_enc_type enc_table_esc_slash[256] = { 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 3, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1, 1, 1, 1, 1, 1 }; /** Escaped hex character table: ["00" "01" "02" ... "FD" "FE" "FF"]. (generate with misc/make_tables.c) */ yyjson_align(2) static const u8 esc_hex_char_table[512] = { '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', '0', '5', '0', '6', '0', '7', '0', '8', '0', '9', '0', 'A', '0', 'B', '0', 'C', '0', 'D', '0', 'E', '0', 'F', '1', '0', '1', '1', '1', '2', '1', '3', '1', '4', '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', '1', 'A', '1', 'B', '1', 'C', '1', 'D', '1', 'E', '1', 'F', '2', '0', '2', '1', '2', '2', '2', '3', '2', '4', '2', '5', '2', '6', '2', '7', '2', '8', '2', '9', '2', 'A', '2', 'B', '2', 'C', '2', 'D', '2', 'E', '2', 'F', '3', '0', '3', '1', '3', '2', '3', '3', '3', '4', '3', '5', '3', '6', '3', '7', '3', '8', '3', '9', '3', 'A', '3', 'B', '3', 'C', '3', 'D', '3', 'E', '3', 'F', '4', '0', '4', '1', '4', '2', '4', '3', '4', '4', '4', '5', '4', '6', '4', '7', '4', '8', '4', '9', '4', 'A', '4', 'B', '4', 'C', '4', 'D', '4', 'E', '4', 'F', '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', '5', '5', '5', '6', '5', '7', '5', '8', '5', '9', '5', 'A', '5', 'B', '5', 'C', '5', 'D', '5', 'E', '5', 'F', '6', '0', '6', '1', '6', '2', '6', '3', '6', '4', '6', '5', '6', '6', '6', '7', '6', '8', '6', '9', '6', 'A', '6', 'B', '6', 'C', '6', 'D', '6', 'E', '6', 'F', '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', '7', '5', '7', '6', '7', '7', '7', '8', '7', '9', '7', 'A', '7', 'B', '7', 'C', '7', 'D', '7', 'E', '7', 'F', '8', '0', '8', '1', '8', '2', '8', '3', '8', '4', '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', '8', 'A', '8', 'B', '8', 'C', '8', 'D', '8', 'E', '8', 'F', '9', '0', '9', '1', '9', '2', '9', '3', '9', '4', '9', '5', '9', '6', '9', '7', '9', '8', '9', '9', '9', 'A', '9', 'B', '9', 'C', '9', 'D', '9', 'E', '9', 'F', 'A', '0', 'A', '1', 'A', '2', 'A', '3', 'A', '4', 'A', '5', 'A', '6', 'A', '7', 'A', '8', 'A', '9', 'A', 'A', 'A', 'B', 'A', 'C', 'A', 'D', 'A', 'E', 'A', 'F', 'B', '0', 'B', '1', 'B', '2', 'B', '3', 'B', '4', 'B', '5', 'B', '6', 'B', '7', 'B', '8', 'B', '9', 'B', 'A', 'B', 'B', 'B', 'C', 'B', 'D', 'B', 'E', 'B', 'F', 'C', '0', 'C', '1', 'C', '2', 'C', '3', 'C', '4', 'C', '5', 'C', '6', 'C', '7', 'C', '8', 'C', '9', 'C', 'A', 'C', 'B', 'C', 'C', 'C', 'D', 'C', 'E', 'C', 'F', 'D', '0', 'D', '1', 'D', '2', 'D', '3', 'D', '4', 'D', '5', 'D', '6', 'D', '7', 'D', '8', 'D', '9', 'D', 'A', 'D', 'B', 'D', 'C', 'D', 'D', 'D', 'E', 'D', 'F', 'E', '0', 'E', '1', 'E', '2', 'E', '3', 'E', '4', 'E', '5', 'E', '6', 'E', '7', 'E', '8', 'E', '9', 'E', 'A', 'E', 'B', 'E', 'C', 'E', 'D', 'E', 'E', 'E', 'F', 'F', '0', 'F', '1', 'F', '2', 'F', '3', 'F', '4', 'F', '5', 'F', '6', 'F', '7', 'F', '8', 'F', '9', 'F', 'A', 'F', 'B', 'F', 'C', 'F', 'D', 'F', 'E', 'F', 'F' }; /** Escaped single character table. (generate with misc/make_tables.c) */ yyjson_align(2) static const u8 esc_single_char_table[512] = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\\', 'b', '\\', 't', '\\', 'n', ' ', ' ', '\\', 'f', '\\', 'r', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\\', '"', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\\', '/', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\\', '\\', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' }; /** Returns the encode table with options. */ static_inline const char_enc_type *get_enc_table_with_flag( yyjson_read_flag flg) { if (has_write_flag(ESCAPE_UNICODE)) { if (has_write_flag(ESCAPE_SLASHES)) { return enc_table_esc_slash; } else { return enc_table_esc; } } else { if (has_write_flag(ESCAPE_SLASHES)) { return enc_table_cpy_slash; } else { return enc_table_cpy; } } } /** Write raw string. */ static_inline u8 *write_raw(u8 *cur, const u8 *raw, usize raw_len) { memcpy(cur, raw, raw_len); return cur + raw_len; } /** Write string no-escape. @param cur Buffer cursor. @param str A UTF-8 string, null-terminator is not required. @param str_len Length of string in bytes. @return The buffer cursor after string. */ static_inline u8 *write_string_noesc(u8 *cur, const u8 *str, usize str_len) { *cur++ = '"'; while (str_len >= 16) { byte_copy_16(cur, str); cur += 16; str += 16; str_len -= 16; } while (str_len >= 4) { byte_copy_4(cur, str); cur += 4; str += 4; str_len -= 4; } while (str_len) { *cur++ = *str++; str_len -= 1; } *cur++ = '"'; return cur; } /** Write UTF-8 string (requires len * 6 + 2 bytes buffer). @param cur Buffer cursor. @param esc Escape unicode. @param inv Allow invalid unicode. @param str A UTF-8 string, null-terminator is not required. @param str_len Length of string in bytes. @param enc_table Encode type table for character. @return The buffer cursor after string, or NULL on invalid unicode. */ static_inline u8 *write_string(u8 *cur, bool esc, bool inv, const u8 *str, usize str_len, const char_enc_type *enc_table) { /* UTF-8 character mask and pattern, see `read_string()` for details. */ #if YYJSON_ENDIAN == YYJSON_BIG_ENDIAN const u16 b2_mask = 0xE0C0UL; const u16 b2_patt = 0xC080UL; const u16 b2_requ = 0x1E00UL; const u32 b3_mask = 0xF0C0C000UL; const u32 b3_patt = 0xE0808000UL; const u32 b3_requ = 0x0F200000UL; const u32 b3_erro = 0x0D200000UL; const u32 b4_mask = 0xF8C0C0C0UL; const u32 b4_patt = 0xF0808080UL; const u32 b4_requ = 0x07300000UL; const u32 b4_err0 = 0x04000000UL; const u32 b4_err1 = 0x03300000UL; #elif YYJSON_ENDIAN == YYJSON_LITTLE_ENDIAN const u16 b2_mask = 0xC0E0UL; const u16 b2_patt = 0x80C0UL; const u16 b2_requ = 0x001EUL; const u32 b3_mask = 0x00C0C0F0UL; const u32 b3_patt = 0x008080E0UL; const u32 b3_requ = 0x0000200FUL; const u32 b3_erro = 0x0000200DUL; const u32 b4_mask = 0xC0C0C0F8UL; const u32 b4_patt = 0x808080F0UL; const u32 b4_requ = 0x00003007UL; const u32 b4_err0 = 0x00000004UL; const u32 b4_err1 = 0x00003003UL; #else /* this should be evaluated at compile-time */ v16_uni b2_mask_uni = {{ 0xE0, 0xC0 }}; v16_uni b2_patt_uni = {{ 0xC0, 0x80 }}; v16_uni b2_requ_uni = {{ 0x1E, 0x00 }}; v32_uni b3_mask_uni = {{ 0xF0, 0xC0, 0xC0, 0x00 }}; v32_uni b3_patt_uni = {{ 0xE0, 0x80, 0x80, 0x00 }}; v32_uni b3_requ_uni = {{ 0x0F, 0x20, 0x00, 0x00 }}; v32_uni b3_erro_uni = {{ 0x0D, 0x20, 0x00, 0x00 }}; v32_uni b4_mask_uni = {{ 0xF8, 0xC0, 0xC0, 0xC0 }}; v32_uni b4_patt_uni = {{ 0xF0, 0x80, 0x80, 0x80 }}; v32_uni b4_requ_uni = {{ 0x07, 0x30, 0x00, 0x00 }}; v32_uni b4_err0_uni = {{ 0x04, 0x00, 0x00, 0x00 }}; v32_uni b4_err1_uni = {{ 0x03, 0x30, 0x00, 0x00 }}; u16 b2_mask = b2_mask_uni.u; u16 b2_patt = b2_patt_uni.u; u16 b2_requ = b2_requ_uni.u; u32 b3_mask = b3_mask_uni.u; u32 b3_patt = b3_patt_uni.u; u32 b3_requ = b3_requ_uni.u; u32 b3_erro = b3_erro_uni.u; u32 b4_mask = b4_mask_uni.u; u32 b4_patt = b4_patt_uni.u; u32 b4_requ = b4_requ_uni.u; u32 b4_err0 = b4_err0_uni.u; u32 b4_err1 = b4_err1_uni.u; #endif #define is_valid_seq_2(uni) ( \ ((uni & b2_mask) == b2_patt) && \ ((uni & b2_requ)) \ ) #define is_valid_seq_3(uni) ( \ ((uni & b3_mask) == b3_patt) && \ ((tmp = (uni & b3_requ))) && \ ((tmp != b3_erro)) \ ) #define is_valid_seq_4(uni) ( \ ((uni & b4_mask) == b4_patt) && \ ((tmp = (uni & b4_requ))) && \ ((tmp & b4_err0) == 0 || (tmp & b4_err1) == 0) \ ) /* The replacement character U+FFFD, used to indicate invalid character. */ const v32 rep = {{ 'F', 'F', 'F', 'D' }}; const v32 pre = {{ '\\', 'u', '0', '0' }}; const u8 *src = str; const u8 *end = str + str_len; *cur++ = '"'; copy_ascii: /* Copy continuous ASCII, loop unrolling, same as the following code: while (end > src) ( if (unlikely(enc_table[*src])) break; *cur++ = *src++; ); */ #define expr_jump(i) \ if (unlikely(enc_table[src[i]])) goto stop_char_##i; #define expr_stop(i) \ stop_char_##i: \ memcpy(cur, src, i); \ cur += i; src += i; goto copy_utf8; while (end - src >= 16) { repeat16_incr(expr_jump) byte_copy_16(cur, src); cur += 16; src += 16; } while (end - src >= 4) { repeat4_incr(expr_jump) byte_copy_4(cur, src); cur += 4; src += 4; } while (end > src) { expr_jump(0) *cur++ = *src++; } *cur++ = '"'; return cur; repeat16_incr(expr_stop) #undef expr_jump #undef expr_stop copy_utf8: if (unlikely(src + 4 > end)) { if (end == src) goto copy_end; if (end - src < enc_table[*src] / 2) goto err_one; } switch (enc_table[*src]) { case CHAR_ENC_CPY_1: { *cur++ = *src++; goto copy_ascii; } case CHAR_ENC_CPY_2: { u16 v; #if YYJSON_DISABLE_UTF8_VALIDATION byte_copy_2(cur, src); #else v = byte_load_2(src); if (unlikely(!is_valid_seq_2(v))) goto err_cpy; byte_copy_2(cur, src); #endif cur += 2; src += 2; goto copy_utf8; } case CHAR_ENC_CPY_3: { u32 v, tmp; #if YYJSON_DISABLE_UTF8_VALIDATION if (likely(src + 4 <= end)) { byte_copy_4(cur, src); } else { byte_copy_2(cur, src); cur[2] = src[2]; } #else if (likely(src + 4 <= end)) { v = byte_load_4(src); if (unlikely(!is_valid_seq_3(v))) goto err_cpy; byte_copy_4(cur, src); } else { v = byte_load_3(src); if (unlikely(!is_valid_seq_3(v))) goto err_cpy; byte_copy_4(cur, &v); } #endif cur += 3; src += 3; goto copy_utf8; } case CHAR_ENC_CPY_4: { u32 v, tmp; #if YYJSON_DISABLE_UTF8_VALIDATION byte_copy_4(cur, src); #else v = byte_load_4(src); if (unlikely(!is_valid_seq_4(v))) goto err_cpy; byte_copy_4(cur, src); #endif cur += 4; src += 4; goto copy_utf8; } case CHAR_ENC_ESC_A: { byte_copy_2(cur, &esc_single_char_table[*src * 2]); cur += 2; src += 1; goto copy_utf8; } case CHAR_ENC_ESC_1: { byte_copy_4(cur + 0, &pre); byte_copy_2(cur + 4, &esc_hex_char_table[*src * 2]); cur += 6; src += 1; goto copy_utf8; } case CHAR_ENC_ESC_2: { u16 u, v; #if !YYJSON_DISABLE_UTF8_VALIDATION v = byte_load_2(src); if (unlikely(!is_valid_seq_2(v))) goto err_esc; #endif u = (u16)(((u16)(src[0] & 0x1F) << 6) | ((u16)(src[1] & 0x3F) << 0)); byte_copy_2(cur + 0, &pre); byte_copy_2(cur + 2, &esc_hex_char_table[(u >> 8) * 2]); byte_copy_2(cur + 4, &esc_hex_char_table[(u & 0xFF) * 2]); cur += 6; src += 2; goto copy_utf8; } case CHAR_ENC_ESC_3: { u16 u; u32 v, tmp; #if !YYJSON_DISABLE_UTF8_VALIDATION v = byte_load_3(src); if (unlikely(!is_valid_seq_3(v))) goto err_esc; #endif u = (u16)(((u16)(src[0] & 0x0F) << 12) | ((u16)(src[1] & 0x3F) << 6) | ((u16)(src[2] & 0x3F) << 0)); byte_copy_2(cur + 0, &pre); byte_copy_2(cur + 2, &esc_hex_char_table[(u >> 8) * 2]); byte_copy_2(cur + 4, &esc_hex_char_table[(u & 0xFF) * 2]); cur += 6; src += 3; goto copy_utf8; } case CHAR_ENC_ESC_4: { u32 hi, lo, u, v, tmp; #if !YYJSON_DISABLE_UTF8_VALIDATION v = byte_load_4(src); if (unlikely(!is_valid_seq_4(v))) goto err_esc; #endif u = ((u32)(src[0] & 0x07) << 18) | ((u32)(src[1] & 0x3F) << 12) | ((u32)(src[2] & 0x3F) << 6) | ((u32)(src[3] & 0x3F) << 0); u -= 0x10000; hi = (u >> 10) + 0xD800; lo = (u & 0x3FF) + 0xDC00; byte_copy_2(cur + 0, &pre); byte_copy_2(cur + 2, &esc_hex_char_table[(hi >> 8) * 2]); byte_copy_2(cur + 4, &esc_hex_char_table[(hi & 0xFF) * 2]); byte_copy_2(cur + 6, &pre); byte_copy_2(cur + 8, &esc_hex_char_table[(lo >> 8) * 2]); byte_copy_2(cur + 10, &esc_hex_char_table[(lo & 0xFF) * 2]); cur += 12; src += 4; goto copy_utf8; } case CHAR_ENC_ERR_1: { goto err_one; } default: break; } copy_end: *cur++ = '"'; return cur; err_one: if (esc) goto err_esc; else goto err_cpy; err_cpy: if (!inv) return NULL; *cur++ = *src++; goto copy_utf8; err_esc: if (!inv) return NULL; byte_copy_2(cur + 0, &pre); byte_copy_4(cur + 2, &rep); cur += 6; src += 1; goto copy_utf8; #undef is_valid_seq_2 #undef is_valid_seq_3 #undef is_valid_seq_4 } /*============================================================================== * Writer Utilities *============================================================================*/ /** Write null (requires 8 bytes buffer). */ static_inline u8 *write_null(u8 *cur) { v64 v = {{ 'n', 'u', 'l', 'l', ',', '\n', 0, 0 }}; byte_copy_8(cur, &v); return cur + 4; } /** Write bool (requires 8 bytes buffer). */ static_inline u8 *write_bool(u8 *cur, bool val) { v64 v0 = {{ 'f', 'a', 'l', 's', 'e', ',', '\n', 0 }}; v64 v1 = {{ 't', 'r', 'u', 'e', ',', '\n', 0, 0 }}; if (val) { byte_copy_8(cur, &v1); } else { byte_copy_8(cur, &v0); } return cur + 5 - val; } /** Write indent (requires level x 4 bytes buffer). Param spaces should not larger than 4. */ static_inline u8 *write_indent(u8 *cur, usize level, usize spaces) { while (level-- > 0) { byte_copy_4(cur, " "); cur += spaces; } return cur; } /** Write data to file pointer. */ static bool write_dat_to_fp(FILE *fp, u8 *dat, usize len, yyjson_write_err *err) { if (fwrite(dat, len, 1, fp) != 1) { err->msg = "file writing failed"; err->code = YYJSON_WRITE_ERROR_FILE_WRITE; return false; } return true; } /** Write data to file. */ static bool write_dat_to_file(const char *path, u8 *dat, usize len, yyjson_write_err *err) { #define return_err(_code, _msg) do { \ err->msg = _msg; \ err->code = YYJSON_WRITE_ERROR_##_code; \ if (file) fclose(file); \ return false; \ } while (false) FILE *file = fopen_writeonly(path); if (file == NULL) { return_err(FILE_OPEN, "file opening failed"); } if (fwrite(dat, len, 1, file) != 1) { return_err(FILE_WRITE, "file writing failed"); } if (fclose(file) != 0) { file = NULL; return_err(FILE_WRITE, "file closing failed"); } return true; #undef return_err } /*============================================================================== * JSON Writer Implementation *============================================================================*/ typedef struct yyjson_write_ctx { usize tag; } yyjson_write_ctx; static_inline void yyjson_write_ctx_set(yyjson_write_ctx *ctx, usize size, bool is_obj) { ctx->tag = (size << 1) | (usize)is_obj; } static_inline void yyjson_write_ctx_get(yyjson_write_ctx *ctx, usize *size, bool *is_obj) { usize tag = ctx->tag; *size = tag >> 1; *is_obj = (bool)(tag & 1); } /** Write single JSON value. */ static_inline u8 *yyjson_write_single(yyjson_val *val, yyjson_write_flag flg, yyjson_alc alc, usize *dat_len, yyjson_write_err *err) { #define return_err(_code, _msg) do { \ if (hdr) alc.free(alc.ctx, (void *)hdr); \ *dat_len = 0; \ err->code = YYJSON_WRITE_ERROR_##_code; \ err->msg = _msg; \ return NULL; \ } while (false) #define incr_len(_len) do { \ hdr = (u8 *)alc.malloc(alc.ctx, _len); \ if (!hdr) goto fail_alloc; \ cur = hdr; \ } while (false) #define check_str_len(_len) do { \ if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \ goto fail_alloc; \ } while (false) u8 *hdr = NULL, *cur; usize str_len; const u8 *str_ptr; const char_enc_type *enc_table = get_enc_table_with_flag(flg); bool cpy = (enc_table == enc_table_cpy); bool esc = has_write_flag(ESCAPE_UNICODE) != 0; bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0; bool newline = has_write_flag(NEWLINE_AT_END) != 0; const usize end_len = 2; /* '\n' and '\0' */ switch (unsafe_yyjson_get_type(val)) { case YYJSON_TYPE_RAW: str_len = unsafe_yyjson_get_len(val); str_ptr = (const u8 *)unsafe_yyjson_get_str(val); check_str_len(str_len); incr_len(str_len + end_len); cur = write_raw(cur, str_ptr, str_len); break; case YYJSON_TYPE_STR: str_len = unsafe_yyjson_get_len(val); str_ptr = (const u8 *)unsafe_yyjson_get_str(val); check_str_len(str_len); incr_len(str_len * 6 + 2 + end_len); if (likely(cpy) && unsafe_yyjson_get_subtype(val)) { cur = write_string_noesc(cur, str_ptr, str_len); } else { cur = write_string(cur, esc, inv, str_ptr, str_len, enc_table); if (unlikely(!cur)) goto fail_str; } break; case YYJSON_TYPE_NUM: incr_len(32 + end_len); cur = write_number(cur, val, flg); if (unlikely(!cur)) goto fail_num; break; case YYJSON_TYPE_BOOL: incr_len(8); cur = write_bool(cur, unsafe_yyjson_get_bool(val)); break; case YYJSON_TYPE_NULL: incr_len(8); cur = write_null(cur); break; case YYJSON_TYPE_ARR: incr_len(2 + end_len); byte_copy_2(cur, "[]"); cur += 2; break; case YYJSON_TYPE_OBJ: incr_len(2 + end_len); byte_copy_2(cur, "{}"); cur += 2; break; default: goto fail_type; } if (newline) *cur++ = '\n'; *cur = '\0'; *dat_len = (usize)(cur - hdr); memset(err, 0, sizeof(yyjson_write_err)); return hdr; fail_alloc: return_err(MEMORY_ALLOCATION, "memory allocation failed"); fail_type: return_err(INVALID_VALUE_TYPE, "invalid JSON value type"); fail_num: return_err(NAN_OR_INF, "nan or inf number is not allowed"); fail_str: return_err(INVALID_STRING, "invalid utf-8 encoding in string"); #undef return_err #undef check_str_len #undef incr_len } /** Write JSON document minify. The root of this document should be a non-empty container. */ static_inline u8 *yyjson_write_minify(const yyjson_val *root, const yyjson_write_flag flg, const yyjson_alc alc, usize *dat_len, yyjson_write_err *err) { #define return_err(_code, _msg) do { \ *dat_len = 0; \ err->code = YYJSON_WRITE_ERROR_##_code; \ err->msg = _msg; \ if (hdr) alc.free(alc.ctx, hdr); \ return NULL; \ } while (false) #define incr_len(_len) do { \ ext_len = (usize)(_len); \ if (unlikely((u8 *)(cur + ext_len) >= (u8 *)ctx)) { \ alc_inc = yyjson_max(alc_len / 2, ext_len); \ alc_inc = size_align_up(alc_inc, sizeof(yyjson_write_ctx)); \ if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \ goto fail_alloc; \ alc_len += alc_inc; \ tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ if (unlikely(!tmp)) goto fail_alloc; \ ctx_len = (usize)(end - (u8 *)ctx); \ ctx_tmp = (yyjson_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \ memmove((void *)ctx_tmp, (void *)(tmp + ((u8 *)ctx - hdr)), ctx_len); \ ctx = ctx_tmp; \ cur = tmp + (cur - hdr); \ end = tmp + alc_len; \ hdr = tmp; \ } \ } while (false) #define check_str_len(_len) do { \ if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \ goto fail_alloc; \ } while (false) yyjson_val *val; yyjson_type val_type; usize ctn_len, ctn_len_tmp; bool ctn_obj, ctn_obj_tmp, is_key; u8 *hdr, *cur, *end, *tmp; yyjson_write_ctx *ctx, *ctx_tmp; usize alc_len, alc_inc, ctx_len, ext_len, str_len; const u8 *str_ptr; const char_enc_type *enc_table = get_enc_table_with_flag(flg); bool cpy = (enc_table == enc_table_cpy); bool esc = has_write_flag(ESCAPE_UNICODE) != 0; bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0; bool newline = has_write_flag(NEWLINE_AT_END) != 0; alc_len = root->uni.ofs / sizeof(yyjson_val); alc_len = alc_len * YYJSON_WRITER_ESTIMATED_MINIFY_RATIO + 64; alc_len = size_align_up(alc_len, sizeof(yyjson_write_ctx)); hdr = (u8 *)alc.malloc(alc.ctx, alc_len); if (!hdr) goto fail_alloc; cur = hdr; end = hdr + alc_len; ctx = (yyjson_write_ctx *)(void *)end; doc_begin: val = constcast(yyjson_val *)root; val_type = unsafe_yyjson_get_type(val); ctn_obj = (val_type == YYJSON_TYPE_OBJ); ctn_len = unsafe_yyjson_get_len(val) << (u8)ctn_obj; *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); val++; val_begin: val_type = unsafe_yyjson_get_type(val); if (val_type == YYJSON_TYPE_STR) { is_key = ((u8)ctn_obj & (u8)~ctn_len); str_len = unsafe_yyjson_get_len(val); str_ptr = (const u8 *)unsafe_yyjson_get_str(val); check_str_len(str_len); incr_len(str_len * 6 + 16); if (likely(cpy) && unsafe_yyjson_get_subtype(val)) { cur = write_string_noesc(cur, str_ptr, str_len); } else { cur = write_string(cur, esc, inv, str_ptr, str_len, enc_table); if (unlikely(!cur)) goto fail_str; } *cur++ = is_key ? ':' : ','; goto val_end; } if (val_type == YYJSON_TYPE_NUM) { incr_len(32); cur = write_number(cur, val, flg); if (unlikely(!cur)) goto fail_num; *cur++ = ','; goto val_end; } if ((val_type & (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) == (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) { ctn_len_tmp = unsafe_yyjson_get_len(val); ctn_obj_tmp = (val_type == YYJSON_TYPE_OBJ); incr_len(16); if (unlikely(ctn_len_tmp == 0)) { /* write empty container */ *cur++ = (u8)('[' | ((u8)ctn_obj_tmp << 5)); *cur++ = (u8)(']' | ((u8)ctn_obj_tmp << 5)); *cur++ = ','; goto val_end; } else { /* push context, setup new container */ yyjson_write_ctx_set(--ctx, ctn_len, ctn_obj); ctn_len = ctn_len_tmp << (u8)ctn_obj_tmp; ctn_obj = ctn_obj_tmp; *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); val++; goto val_begin; } } if (val_type == YYJSON_TYPE_BOOL) { incr_len(16); cur = write_bool(cur, unsafe_yyjson_get_bool(val)); cur++; goto val_end; } if (val_type == YYJSON_TYPE_NULL) { incr_len(16); cur = write_null(cur); cur++; goto val_end; } if (val_type == YYJSON_TYPE_RAW) { str_len = unsafe_yyjson_get_len(val); str_ptr = (const u8 *)unsafe_yyjson_get_str(val); check_str_len(str_len); incr_len(str_len + 2); cur = write_raw(cur, str_ptr, str_len); *cur++ = ','; goto val_end; } goto fail_type; val_end: val++; ctn_len--; if (unlikely(ctn_len == 0)) goto ctn_end; goto val_begin; ctn_end: cur--; *cur++ = (u8)(']' | ((u8)ctn_obj << 5)); *cur++ = ','; if (unlikely((u8 *)ctx >= end)) goto doc_end; yyjson_write_ctx_get(ctx++, &ctn_len, &ctn_obj); ctn_len--; if (likely(ctn_len > 0)) { goto val_begin; } else { goto ctn_end; } doc_end: if (newline) { incr_len(2); *(cur - 1) = '\n'; cur++; } *--cur = '\0'; *dat_len = (usize)(cur - hdr); memset(err, 0, sizeof(yyjson_write_err)); return hdr; fail_alloc: return_err(MEMORY_ALLOCATION, "memory allocation failed"); fail_type: return_err(INVALID_VALUE_TYPE, "invalid JSON value type"); fail_num: return_err(NAN_OR_INF, "nan or inf number is not allowed"); fail_str: return_err(INVALID_STRING, "invalid utf-8 encoding in string"); #undef return_err #undef incr_len #undef check_str_len } /** Write JSON document pretty. The root of this document should be a non-empty container. */ static_inline u8 *yyjson_write_pretty(const yyjson_val *root, const yyjson_write_flag flg, const yyjson_alc alc, usize *dat_len, yyjson_write_err *err) { #define return_err(_code, _msg) do { \ *dat_len = 0; \ err->code = YYJSON_WRITE_ERROR_##_code; \ err->msg = _msg; \ if (hdr) alc.free(alc.ctx, hdr); \ return NULL; \ } while (false) #define incr_len(_len) do { \ ext_len = (usize)(_len); \ if (unlikely((u8 *)(cur + ext_len) >= (u8 *)ctx)) { \ alc_inc = yyjson_max(alc_len / 2, ext_len); \ alc_inc = size_align_up(alc_inc, sizeof(yyjson_write_ctx)); \ if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \ goto fail_alloc; \ alc_len += alc_inc; \ tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ if (unlikely(!tmp)) goto fail_alloc; \ ctx_len = (usize)(end - (u8 *)ctx); \ ctx_tmp = (yyjson_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \ memmove((void *)ctx_tmp, (void *)(tmp + ((u8 *)ctx - hdr)), ctx_len); \ ctx = ctx_tmp; \ cur = tmp + (cur - hdr); \ end = tmp + alc_len; \ hdr = tmp; \ } \ } while (false) #define check_str_len(_len) do { \ if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \ goto fail_alloc; \ } while (false) yyjson_val *val; yyjson_type val_type; usize ctn_len, ctn_len_tmp; bool ctn_obj, ctn_obj_tmp, is_key, no_indent; u8 *hdr, *cur, *end, *tmp; yyjson_write_ctx *ctx, *ctx_tmp; usize alc_len, alc_inc, ctx_len, ext_len, str_len, level; const u8 *str_ptr; const char_enc_type *enc_table = get_enc_table_with_flag(flg); bool cpy = (enc_table == enc_table_cpy); bool esc = has_write_flag(ESCAPE_UNICODE) != 0; bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0; usize spaces = has_write_flag(PRETTY_TWO_SPACES) ? 2 : 4; bool newline = has_write_flag(NEWLINE_AT_END) != 0; alc_len = root->uni.ofs / sizeof(yyjson_val); alc_len = alc_len * YYJSON_WRITER_ESTIMATED_PRETTY_RATIO + 64; alc_len = size_align_up(alc_len, sizeof(yyjson_write_ctx)); hdr = (u8 *)alc.malloc(alc.ctx, alc_len); if (!hdr) goto fail_alloc; cur = hdr; end = hdr + alc_len; ctx = (yyjson_write_ctx *)(void *)end; doc_begin: val = constcast(yyjson_val *)root; val_type = unsafe_yyjson_get_type(val); ctn_obj = (val_type == YYJSON_TYPE_OBJ); ctn_len = unsafe_yyjson_get_len(val) << (u8)ctn_obj; *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); *cur++ = '\n'; val++; level = 1; val_begin: val_type = unsafe_yyjson_get_type(val); if (val_type == YYJSON_TYPE_STR) { is_key = (bool)((u8)ctn_obj & (u8)~ctn_len); no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); str_len = unsafe_yyjson_get_len(val); str_ptr = (const u8 *)unsafe_yyjson_get_str(val); check_str_len(str_len); incr_len(str_len * 6 + 16 + (no_indent ? 0 : level * 4)); cur = write_indent(cur, no_indent ? 0 : level, spaces); if (likely(cpy) && unsafe_yyjson_get_subtype(val)) { cur = write_string_noesc(cur, str_ptr, str_len); } else { cur = write_string(cur, esc, inv, str_ptr, str_len, enc_table); if (unlikely(!cur)) goto fail_str; } *cur++ = is_key ? ':' : ','; *cur++ = is_key ? ' ' : '\n'; goto val_end; } if (val_type == YYJSON_TYPE_NUM) { no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); incr_len(32 + (no_indent ? 0 : level * 4)); cur = write_indent(cur, no_indent ? 0 : level, spaces); cur = write_number(cur, val, flg); if (unlikely(!cur)) goto fail_num; *cur++ = ','; *cur++ = '\n'; goto val_end; } if ((val_type & (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) == (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) { no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); ctn_len_tmp = unsafe_yyjson_get_len(val); ctn_obj_tmp = (val_type == YYJSON_TYPE_OBJ); if (unlikely(ctn_len_tmp == 0)) { /* write empty container */ incr_len(16 + (no_indent ? 0 : level * 4)); cur = write_indent(cur, no_indent ? 0 : level, spaces); *cur++ = (u8)('[' | ((u8)ctn_obj_tmp << 5)); *cur++ = (u8)(']' | ((u8)ctn_obj_tmp << 5)); *cur++ = ','; *cur++ = '\n'; goto val_end; } else { /* push context, setup new container */ incr_len(32 + (no_indent ? 0 : level * 4)); yyjson_write_ctx_set(--ctx, ctn_len, ctn_obj); ctn_len = ctn_len_tmp << (u8)ctn_obj_tmp; ctn_obj = ctn_obj_tmp; cur = write_indent(cur, no_indent ? 0 : level, spaces); level++; *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); *cur++ = '\n'; val++; goto val_begin; } } if (val_type == YYJSON_TYPE_BOOL) { no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); incr_len(16 + (no_indent ? 0 : level * 4)); cur = write_indent(cur, no_indent ? 0 : level, spaces); cur = write_bool(cur, unsafe_yyjson_get_bool(val)); cur += 2; goto val_end; } if (val_type == YYJSON_TYPE_NULL) { no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); incr_len(16 + (no_indent ? 0 : level * 4)); cur = write_indent(cur, no_indent ? 0 : level, spaces); cur = write_null(cur); cur += 2; goto val_end; } if (val_type == YYJSON_TYPE_RAW) { str_len = unsafe_yyjson_get_len(val); str_ptr = (const u8 *)unsafe_yyjson_get_str(val); check_str_len(str_len); incr_len(str_len + 3); cur = write_raw(cur, str_ptr, str_len); *cur++ = ','; *cur++ = '\n'; goto val_end; } goto fail_type; val_end: val++; ctn_len--; if (unlikely(ctn_len == 0)) goto ctn_end; goto val_begin; ctn_end: cur -= 2; *cur++ = '\n'; incr_len(level * 4); cur = write_indent(cur, --level, spaces); *cur++ = (u8)(']' | ((u8)ctn_obj << 5)); if (unlikely((u8 *)ctx >= end)) goto doc_end; yyjson_write_ctx_get(ctx++, &ctn_len, &ctn_obj); ctn_len--; *cur++ = ','; *cur++ = '\n'; if (likely(ctn_len > 0)) { goto val_begin; } else { goto ctn_end; } doc_end: if (newline) { incr_len(2); *cur++ = '\n'; } *cur = '\0'; *dat_len = (usize)(cur - hdr); memset(err, 0, sizeof(yyjson_write_err)); return hdr; fail_alloc: return_err(MEMORY_ALLOCATION, "memory allocation failed"); fail_type: return_err(INVALID_VALUE_TYPE, "invalid JSON value type"); fail_num: return_err(NAN_OR_INF, "nan or inf number is not allowed"); fail_str: return_err(INVALID_STRING, "invalid utf-8 encoding in string"); #undef return_err #undef incr_len #undef check_str_len } char *yyjson_val_write_opts(const yyjson_val *val, yyjson_write_flag flg, const yyjson_alc *alc_ptr, usize *dat_len, yyjson_write_err *err) { yyjson_write_err dummy_err; usize dummy_dat_len; yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; yyjson_val *root = constcast(yyjson_val *)val; err = err ? err : &dummy_err; dat_len = dat_len ? dat_len : &dummy_dat_len; if (unlikely(!root)) { *dat_len = 0; err->msg = "input JSON is NULL"; err->code = YYJSON_READ_ERROR_INVALID_PARAMETER; return NULL; } if (!unsafe_yyjson_is_ctn(root) || unsafe_yyjson_get_len(root) == 0) { return (char *)yyjson_write_single(root, flg, alc, dat_len, err); } else if (flg & (YYJSON_WRITE_PRETTY | YYJSON_WRITE_PRETTY_TWO_SPACES)) { return (char *)yyjson_write_pretty(root, flg, alc, dat_len, err); } else { return (char *)yyjson_write_minify(root, flg, alc, dat_len, err); } } char *yyjson_write_opts(const yyjson_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc_ptr, usize *dat_len, yyjson_write_err *err) { yyjson_val *root = doc ? doc->root : NULL; return yyjson_val_write_opts(root, flg, alc_ptr, dat_len, err); } bool yyjson_val_write_file(const char *path, const yyjson_val *val, yyjson_write_flag flg, const yyjson_alc *alc_ptr, yyjson_write_err *err) { yyjson_write_err dummy_err; u8 *dat; usize dat_len = 0; yyjson_val *root = constcast(yyjson_val *)val; bool suc; alc_ptr = alc_ptr ? alc_ptr : &YYJSON_DEFAULT_ALC; err = err ? err : &dummy_err; if (unlikely(!path || !*path)) { err->msg = "input path is invalid"; err->code = YYJSON_READ_ERROR_INVALID_PARAMETER; return false; } dat = (u8 *)yyjson_val_write_opts(root, flg, alc_ptr, &dat_len, err); if (unlikely(!dat)) return false; suc = write_dat_to_file(path, dat, dat_len, err); alc_ptr->free(alc_ptr->ctx, dat); return suc; } bool yyjson_val_write_fp(FILE *fp, const yyjson_val *val, yyjson_write_flag flg, const yyjson_alc *alc_ptr, yyjson_write_err *err) { yyjson_write_err dummy_err; u8 *dat; usize dat_len = 0; yyjson_val *root = constcast(yyjson_val *)val; bool suc; alc_ptr = alc_ptr ? alc_ptr : &YYJSON_DEFAULT_ALC; err = err ? err : &dummy_err; if (unlikely(!fp)) { err->msg = "input fp is invalid"; err->code = YYJSON_READ_ERROR_INVALID_PARAMETER; return false; } dat = (u8 *)yyjson_val_write_opts(root, flg, alc_ptr, &dat_len, err); if (unlikely(!dat)) return false; suc = write_dat_to_fp(fp, dat, dat_len, err); alc_ptr->free(alc_ptr->ctx, dat); return suc; } bool yyjson_write_file(const char *path, const yyjson_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc_ptr, yyjson_write_err *err) { yyjson_val *root = doc ? doc->root : NULL; return yyjson_val_write_file(path, root, flg, alc_ptr, err); } bool yyjson_write_fp(FILE *fp, const yyjson_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc_ptr, yyjson_write_err *err) { yyjson_val *root = doc ? doc->root : NULL; return yyjson_val_write_fp(fp, root, flg, alc_ptr, err); } /*============================================================================== * Mutable JSON Writer Implementation *============================================================================*/ typedef struct yyjson_mut_write_ctx { usize tag; yyjson_mut_val *ctn; } yyjson_mut_write_ctx; static_inline void yyjson_mut_write_ctx_set(yyjson_mut_write_ctx *ctx, yyjson_mut_val *ctn, usize size, bool is_obj) { ctx->tag = (size << 1) | (usize)is_obj; ctx->ctn = ctn; } static_inline void yyjson_mut_write_ctx_get(yyjson_mut_write_ctx *ctx, yyjson_mut_val **ctn, usize *size, bool *is_obj) { usize tag = ctx->tag; *size = tag >> 1; *is_obj = (bool)(tag & 1); *ctn = ctx->ctn; } /** Get the estimated number of values for the mutable JSON document. */ static_inline usize yyjson_mut_doc_estimated_val_num( const yyjson_mut_doc *doc) { usize sum = 0; yyjson_val_chunk *chunk = doc->val_pool.chunks; while (chunk) { sum += chunk->chunk_size / sizeof(yyjson_mut_val) - 1; if (chunk == doc->val_pool.chunks) { sum -= (usize)(doc->val_pool.end - doc->val_pool.cur); } chunk = chunk->next; } return sum; } /** Write single JSON value. */ static_inline u8 *yyjson_mut_write_single(yyjson_mut_val *val, yyjson_write_flag flg, yyjson_alc alc, usize *dat_len, yyjson_write_err *err) { return yyjson_write_single((yyjson_val *)val, flg, alc, dat_len, err); } /** Write JSON document minify. The root of this document should be a non-empty container. */ static_inline u8 *yyjson_mut_write_minify(const yyjson_mut_val *root, usize estimated_val_num, yyjson_write_flag flg, yyjson_alc alc, usize *dat_len, yyjson_write_err *err) { #define return_err(_code, _msg) do { \ *dat_len = 0; \ err->code = YYJSON_WRITE_ERROR_##_code; \ err->msg = _msg; \ if (hdr) alc.free(alc.ctx, hdr); \ return NULL; \ } while (false) #define incr_len(_len) do { \ ext_len = (usize)(_len); \ if (unlikely((u8 *)(cur + ext_len) >= (u8 *)ctx)) { \ alc_inc = yyjson_max(alc_len / 2, ext_len); \ alc_inc = size_align_up(alc_inc, sizeof(yyjson_mut_write_ctx)); \ if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \ goto fail_alloc; \ alc_len += alc_inc; \ tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ if (unlikely(!tmp)) goto fail_alloc; \ ctx_len = (usize)(end - (u8 *)ctx); \ ctx_tmp = (yyjson_mut_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \ memmove((void *)ctx_tmp, (void *)(tmp + ((u8 *)ctx - hdr)), ctx_len); \ ctx = ctx_tmp; \ cur = tmp + (cur - hdr); \ end = tmp + alc_len; \ hdr = tmp; \ } \ } while (false) #define check_str_len(_len) do { \ if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \ goto fail_alloc; \ } while (false) yyjson_mut_val *val, *ctn; yyjson_type val_type; usize ctn_len, ctn_len_tmp; bool ctn_obj, ctn_obj_tmp, is_key; u8 *hdr, *cur, *end, *tmp; yyjson_mut_write_ctx *ctx, *ctx_tmp; usize alc_len, alc_inc, ctx_len, ext_len, str_len; const u8 *str_ptr; const char_enc_type *enc_table = get_enc_table_with_flag(flg); bool cpy = (enc_table == enc_table_cpy); bool esc = has_write_flag(ESCAPE_UNICODE) != 0; bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0; bool newline = has_write_flag(NEWLINE_AT_END) != 0; alc_len = estimated_val_num * YYJSON_WRITER_ESTIMATED_MINIFY_RATIO + 64; alc_len = size_align_up(alc_len, sizeof(yyjson_mut_write_ctx)); hdr = (u8 *)alc.malloc(alc.ctx, alc_len); if (!hdr) goto fail_alloc; cur = hdr; end = hdr + alc_len; ctx = (yyjson_mut_write_ctx *)(void *)end; doc_begin: val = constcast(yyjson_mut_val *)root; val_type = unsafe_yyjson_get_type(val); ctn_obj = (val_type == YYJSON_TYPE_OBJ); ctn_len = unsafe_yyjson_get_len(val) << (u8)ctn_obj; *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); ctn = val; val = (yyjson_mut_val *)val->uni.ptr; /* tail */ val = ctn_obj ? val->next->next : val->next; val_begin: val_type = unsafe_yyjson_get_type(val); if (val_type == YYJSON_TYPE_STR) { is_key = ((u8)ctn_obj & (u8)~ctn_len); str_len = unsafe_yyjson_get_len(val); str_ptr = (const u8 *)unsafe_yyjson_get_str(val); check_str_len(str_len); incr_len(str_len * 6 + 16); if (likely(cpy) && unsafe_yyjson_get_subtype(val)) { cur = write_string_noesc(cur, str_ptr, str_len); } else { cur = write_string(cur, esc, inv, str_ptr, str_len, enc_table); if (unlikely(!cur)) goto fail_str; } *cur++ = is_key ? ':' : ','; goto val_end; } if (val_type == YYJSON_TYPE_NUM) { incr_len(32); cur = write_number(cur, (yyjson_val *)val, flg); if (unlikely(!cur)) goto fail_num; *cur++ = ','; goto val_end; } if ((val_type & (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) == (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) { ctn_len_tmp = unsafe_yyjson_get_len(val); ctn_obj_tmp = (val_type == YYJSON_TYPE_OBJ); incr_len(16); if (unlikely(ctn_len_tmp == 0)) { /* write empty container */ *cur++ = (u8)('[' | ((u8)ctn_obj_tmp << 5)); *cur++ = (u8)(']' | ((u8)ctn_obj_tmp << 5)); *cur++ = ','; goto val_end; } else { /* push context, setup new container */ yyjson_mut_write_ctx_set(--ctx, ctn, ctn_len, ctn_obj); ctn_len = ctn_len_tmp << (u8)ctn_obj_tmp; ctn_obj = ctn_obj_tmp; *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); ctn = val; val = (yyjson_mut_val *)ctn->uni.ptr; /* tail */ val = ctn_obj ? val->next->next : val->next; goto val_begin; } } if (val_type == YYJSON_TYPE_BOOL) { incr_len(16); cur = write_bool(cur, unsafe_yyjson_get_bool(val)); cur++; goto val_end; } if (val_type == YYJSON_TYPE_NULL) { incr_len(16); cur = write_null(cur); cur++; goto val_end; } if (val_type == YYJSON_TYPE_RAW) { str_len = unsafe_yyjson_get_len(val); str_ptr = (const u8 *)unsafe_yyjson_get_str(val); check_str_len(str_len); incr_len(str_len + 2); cur = write_raw(cur, str_ptr, str_len); *cur++ = ','; goto val_end; } goto fail_type; val_end: ctn_len--; if (unlikely(ctn_len == 0)) goto ctn_end; val = val->next; goto val_begin; ctn_end: cur--; *cur++ = (u8)(']' | ((u8)ctn_obj << 5)); *cur++ = ','; if (unlikely((u8 *)ctx >= end)) goto doc_end; val = ctn->next; yyjson_mut_write_ctx_get(ctx++, &ctn, &ctn_len, &ctn_obj); ctn_len--; if (likely(ctn_len > 0)) { goto val_begin; } else { goto ctn_end; } doc_end: if (newline) { incr_len(2); *(cur - 1) = '\n'; cur++; } *--cur = '\0'; *dat_len = (usize)(cur - hdr); err->code = YYJSON_WRITE_SUCCESS; err->msg = "success"; return hdr; fail_alloc: return_err(MEMORY_ALLOCATION, "memory allocation failed"); fail_type: return_err(INVALID_VALUE_TYPE, "invalid JSON value type"); fail_num: return_err(NAN_OR_INF, "nan or inf number is not allowed"); fail_str: return_err(INVALID_STRING, "invalid utf-8 encoding in string"); #undef return_err #undef incr_len #undef check_str_len } /** Write JSON document pretty. The root of this document should be a non-empty container. */ static_inline u8 *yyjson_mut_write_pretty(const yyjson_mut_val *root, usize estimated_val_num, yyjson_write_flag flg, yyjson_alc alc, usize *dat_len, yyjson_write_err *err) { #define return_err(_code, _msg) do { \ *dat_len = 0; \ err->code = YYJSON_WRITE_ERROR_##_code; \ err->msg = _msg; \ if (hdr) alc.free(alc.ctx, hdr); \ return NULL; \ } while (false) #define incr_len(_len) do { \ ext_len = (usize)(_len); \ if (unlikely((u8 *)(cur + ext_len) >= (u8 *)ctx)) { \ alc_inc = yyjson_max(alc_len / 2, ext_len); \ alc_inc = size_align_up(alc_inc, sizeof(yyjson_mut_write_ctx)); \ if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \ goto fail_alloc; \ alc_len += alc_inc; \ tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \ if (unlikely(!tmp)) goto fail_alloc; \ ctx_len = (usize)(end - (u8 *)ctx); \ ctx_tmp = (yyjson_mut_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \ memmove((void *)ctx_tmp, (void *)(tmp + ((u8 *)ctx - hdr)), ctx_len); \ ctx = ctx_tmp; \ cur = tmp + (cur - hdr); \ end = tmp + alc_len; \ hdr = tmp; \ } \ } while (false) #define check_str_len(_len) do { \ if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \ goto fail_alloc; \ } while (false) yyjson_mut_val *val, *ctn; yyjson_type val_type; usize ctn_len, ctn_len_tmp; bool ctn_obj, ctn_obj_tmp, is_key, no_indent; u8 *hdr, *cur, *end, *tmp; yyjson_mut_write_ctx *ctx, *ctx_tmp; usize alc_len, alc_inc, ctx_len, ext_len, str_len, level; const u8 *str_ptr; const char_enc_type *enc_table = get_enc_table_with_flag(flg); bool cpy = (enc_table == enc_table_cpy); bool esc = has_write_flag(ESCAPE_UNICODE) != 0; bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0; usize spaces = has_write_flag(PRETTY_TWO_SPACES) ? 2 : 4; bool newline = has_write_flag(NEWLINE_AT_END) != 0; alc_len = estimated_val_num * YYJSON_WRITER_ESTIMATED_PRETTY_RATIO + 64; alc_len = size_align_up(alc_len, sizeof(yyjson_mut_write_ctx)); hdr = (u8 *)alc.malloc(alc.ctx, alc_len); if (!hdr) goto fail_alloc; cur = hdr; end = hdr + alc_len; ctx = (yyjson_mut_write_ctx *)(void *)end; doc_begin: val = constcast(yyjson_mut_val *)root; val_type = unsafe_yyjson_get_type(val); ctn_obj = (val_type == YYJSON_TYPE_OBJ); ctn_len = unsafe_yyjson_get_len(val) << (u8)ctn_obj; *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); *cur++ = '\n'; ctn = val; val = (yyjson_mut_val *)val->uni.ptr; /* tail */ val = ctn_obj ? val->next->next : val->next; level = 1; val_begin: val_type = unsafe_yyjson_get_type(val); if (val_type == YYJSON_TYPE_STR) { is_key = (bool)((u8)ctn_obj & (u8)~ctn_len); no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); str_len = unsafe_yyjson_get_len(val); str_ptr = (const u8 *)unsafe_yyjson_get_str(val); check_str_len(str_len); incr_len(str_len * 6 + 16 + (no_indent ? 0 : level * 4)); cur = write_indent(cur, no_indent ? 0 : level, spaces); if (likely(cpy) && unsafe_yyjson_get_subtype(val)) { cur = write_string_noesc(cur, str_ptr, str_len); } else { cur = write_string(cur, esc, inv, str_ptr, str_len, enc_table); if (unlikely(!cur)) goto fail_str; } *cur++ = is_key ? ':' : ','; *cur++ = is_key ? ' ' : '\n'; goto val_end; } if (val_type == YYJSON_TYPE_NUM) { no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); incr_len(32 + (no_indent ? 0 : level * 4)); cur = write_indent(cur, no_indent ? 0 : level, spaces); cur = write_number(cur, (yyjson_val *)val, flg); if (unlikely(!cur)) goto fail_num; *cur++ = ','; *cur++ = '\n'; goto val_end; } if ((val_type & (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) == (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) { no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); ctn_len_tmp = unsafe_yyjson_get_len(val); ctn_obj_tmp = (val_type == YYJSON_TYPE_OBJ); if (unlikely(ctn_len_tmp == 0)) { /* write empty container */ incr_len(16 + (no_indent ? 0 : level * 4)); cur = write_indent(cur, no_indent ? 0 : level, spaces); *cur++ = (u8)('[' | ((u8)ctn_obj_tmp << 5)); *cur++ = (u8)(']' | ((u8)ctn_obj_tmp << 5)); *cur++ = ','; *cur++ = '\n'; goto val_end; } else { /* push context, setup new container */ incr_len(32 + (no_indent ? 0 : level * 4)); yyjson_mut_write_ctx_set(--ctx, ctn, ctn_len, ctn_obj); ctn_len = ctn_len_tmp << (u8)ctn_obj_tmp; ctn_obj = ctn_obj_tmp; cur = write_indent(cur, no_indent ? 0 : level, spaces); level++; *cur++ = (u8)('[' | ((u8)ctn_obj << 5)); *cur++ = '\n'; ctn = val; val = (yyjson_mut_val *)ctn->uni.ptr; /* tail */ val = ctn_obj ? val->next->next : val->next; goto val_begin; } } if (val_type == YYJSON_TYPE_BOOL) { no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); incr_len(16 + (no_indent ? 0 : level * 4)); cur = write_indent(cur, no_indent ? 0 : level, spaces); cur = write_bool(cur, unsafe_yyjson_get_bool(val)); cur += 2; goto val_end; } if (val_type == YYJSON_TYPE_NULL) { no_indent = (bool)((u8)ctn_obj & (u8)ctn_len); incr_len(16 + (no_indent ? 0 : level * 4)); cur = write_indent(cur, no_indent ? 0 : level, spaces); cur = write_null(cur); cur += 2; goto val_end; } if (val_type == YYJSON_TYPE_RAW) { str_len = unsafe_yyjson_get_len(val); str_ptr = (const u8 *)unsafe_yyjson_get_str(val); check_str_len(str_len); incr_len(str_len + 3); cur = write_raw(cur, str_ptr, str_len); *cur++ = ','; *cur++ = '\n'; goto val_end; } goto fail_type; val_end: ctn_len--; if (unlikely(ctn_len == 0)) goto ctn_end; val = val->next; goto val_begin; ctn_end: cur -= 2; *cur++ = '\n'; incr_len(level * 4); cur = write_indent(cur, --level, spaces); *cur++ = (u8)(']' | ((u8)ctn_obj << 5)); if (unlikely((u8 *)ctx >= end)) goto doc_end; val = ctn->next; yyjson_mut_write_ctx_get(ctx++, &ctn, &ctn_len, &ctn_obj); ctn_len--; *cur++ = ','; *cur++ = '\n'; if (likely(ctn_len > 0)) { goto val_begin; } else { goto ctn_end; } doc_end: if (newline) { incr_len(2); *cur++ = '\n'; } *cur = '\0'; *dat_len = (usize)(cur - hdr); err->code = YYJSON_WRITE_SUCCESS; err->msg = "success"; return hdr; fail_alloc: return_err(MEMORY_ALLOCATION, "memory allocation failed"); fail_type: return_err(INVALID_VALUE_TYPE, "invalid JSON value type"); fail_num: return_err(NAN_OR_INF, "nan or inf number is not allowed"); fail_str: return_err(INVALID_STRING, "invalid utf-8 encoding in string"); #undef return_err #undef incr_len #undef check_str_len } static char *yyjson_mut_write_opts_impl(const yyjson_mut_val *val, usize estimated_val_num, yyjson_write_flag flg, const yyjson_alc *alc_ptr, usize *dat_len, yyjson_write_err *err) { yyjson_write_err dummy_err; usize dummy_dat_len; yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC; yyjson_mut_val *root = constcast(yyjson_mut_val *)val; err = err ? err : &dummy_err; dat_len = dat_len ? dat_len : &dummy_dat_len; if (unlikely(!root)) { *dat_len = 0; err->msg = "input JSON is NULL"; err->code = YYJSON_WRITE_ERROR_INVALID_PARAMETER; return NULL; } if (!unsafe_yyjson_is_ctn(root) || unsafe_yyjson_get_len(root) == 0) { return (char *)yyjson_mut_write_single(root, flg, alc, dat_len, err); } else if (flg & (YYJSON_WRITE_PRETTY | YYJSON_WRITE_PRETTY_TWO_SPACES)) { return (char *)yyjson_mut_write_pretty(root, estimated_val_num, flg, alc, dat_len, err); } else { return (char *)yyjson_mut_write_minify(root, estimated_val_num, flg, alc, dat_len, err); } } char *yyjson_mut_val_write_opts(const yyjson_mut_val *val, yyjson_write_flag flg, const yyjson_alc *alc_ptr, usize *dat_len, yyjson_write_err *err) { return yyjson_mut_write_opts_impl(val, 0, flg, alc_ptr, dat_len, err); } char *yyjson_mut_write_opts(const yyjson_mut_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc_ptr, usize *dat_len, yyjson_write_err *err) { yyjson_mut_val *root; usize estimated_val_num; if (likely(doc)) { root = doc->root; estimated_val_num = yyjson_mut_doc_estimated_val_num(doc); } else { root = NULL; estimated_val_num = 0; } return yyjson_mut_write_opts_impl(root, estimated_val_num, flg, alc_ptr, dat_len, err); } bool yyjson_mut_val_write_file(const char *path, const yyjson_mut_val *val, yyjson_write_flag flg, const yyjson_alc *alc_ptr, yyjson_write_err *err) { yyjson_write_err dummy_err; u8 *dat; usize dat_len = 0; yyjson_mut_val *root = constcast(yyjson_mut_val *)val; bool suc; alc_ptr = alc_ptr ? alc_ptr : &YYJSON_DEFAULT_ALC; err = err ? err : &dummy_err; if (unlikely(!path || !*path)) { err->msg = "input path is invalid"; err->code = YYJSON_WRITE_ERROR_INVALID_PARAMETER; return false; } dat = (u8 *)yyjson_mut_val_write_opts(root, flg, alc_ptr, &dat_len, err); if (unlikely(!dat)) return false; suc = write_dat_to_file(path, dat, dat_len, err); alc_ptr->free(alc_ptr->ctx, dat); return suc; } bool yyjson_mut_val_write_fp(FILE *fp, const yyjson_mut_val *val, yyjson_write_flag flg, const yyjson_alc *alc_ptr, yyjson_write_err *err) { yyjson_write_err dummy_err; u8 *dat; usize dat_len = 0; yyjson_mut_val *root = constcast(yyjson_mut_val *)val; bool suc; alc_ptr = alc_ptr ? alc_ptr : &YYJSON_DEFAULT_ALC; err = err ? err : &dummy_err; if (unlikely(!fp)) { err->msg = "input fp is invalid"; err->code = YYJSON_WRITE_ERROR_INVALID_PARAMETER; return false; } dat = (u8 *)yyjson_mut_val_write_opts(root, flg, alc_ptr, &dat_len, err); if (unlikely(!dat)) return false; suc = write_dat_to_fp(fp, dat, dat_len, err); alc_ptr->free(alc_ptr->ctx, dat); return suc; } bool yyjson_mut_write_file(const char *path, const yyjson_mut_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc_ptr, yyjson_write_err *err) { yyjson_mut_val *root = doc ? doc->root : NULL; return yyjson_mut_val_write_file(path, root, flg, alc_ptr, err); } bool yyjson_mut_write_fp(FILE *fp, const yyjson_mut_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc_ptr, yyjson_write_err *err) { yyjson_mut_val *root = doc ? doc->root : NULL; return yyjson_mut_val_write_fp(fp, root, flg, alc_ptr, err); } #endif /* YYJSON_DISABLE_WRITER */ ================================================ FILE: include/yyjson/yyjson.h ================================================ /*============================================================================== Copyright (c) 2020 YaoYuan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *============================================================================*/ /** @file yyjson.h @date 2019-03-09 @author YaoYuan */ #ifndef YYJSON_H #define YYJSON_H /*============================================================================== * Header Files *============================================================================*/ #include #include #include #include #include #include /*============================================================================== * Compile-time Options *============================================================================*/ /* Define as 1 to disable JSON reader if JSON parsing is not required. This will disable these functions at compile-time: - yyjson_read() - yyjson_read_opts() - yyjson_read_file() - yyjson_read_number() - yyjson_mut_read_number() This will reduce the binary size by about 60%. */ #ifndef YYJSON_DISABLE_READER #endif /* Define as 1 to disable JSON writer if JSON serialization is not required. This will disable these functions at compile-time: - yyjson_write() - yyjson_write_file() - yyjson_write_opts() - yyjson_val_write() - yyjson_val_write_file() - yyjson_val_write_opts() - yyjson_mut_write() - yyjson_mut_write_file() - yyjson_mut_write_opts() - yyjson_mut_val_write() - yyjson_mut_val_write_file() - yyjson_mut_val_write_opts() This will reduce the binary size by about 30%. */ #ifndef YYJSON_DISABLE_WRITER #endif /* Define as 1 to disable JSON Pointer, JSON Patch and JSON Merge Patch supports. This will disable these functions at compile-time: - yyjson_ptr_xxx() - yyjson_mut_ptr_xxx() - yyjson_doc_ptr_xxx() - yyjson_mut_doc_ptr_xxx() - yyjson_patch() - yyjson_mut_patch() - yyjson_merge_patch() - yyjson_mut_merge_patch() */ #ifndef YYJSON_DISABLE_UTILS #endif /* Define as 1 to disable the fast floating-point number conversion in yyjson, and use libc's `strtod/snprintf` instead. This will reduce the binary size by about 30%, but significantly slow down the floating-point read/write speed. */ #ifndef YYJSON_DISABLE_FAST_FP_CONV #endif /* Define as 1 to disable non-standard JSON support at compile-time: - Reading and writing inf/nan literal, such as `NaN`, `-Infinity`. - Single line and multiple line comments. - Single trailing comma at the end of an object or array. - Invalid unicode in string value. This will also invalidate these run-time options: - YYJSON_READ_ALLOW_INF_AND_NAN - YYJSON_READ_ALLOW_COMMENTS - YYJSON_READ_ALLOW_TRAILING_COMMAS - YYJSON_READ_ALLOW_INVALID_UNICODE - YYJSON_WRITE_ALLOW_INF_AND_NAN - YYJSON_WRITE_ALLOW_INVALID_UNICODE This will reduce the binary size by about 10%, and speed up the reading and writing speed by about 2% to 6%. */ #ifndef YYJSON_DISABLE_NON_STANDARD #endif /* Define as 1 to disable UTF-8 validation at compile time. If all input strings are guaranteed to be valid UTF-8 encoding (for example, some language's String object has already validated the encoding), using this flag can avoid redundant UTF-8 validation in yyjson. This flag can speed up the reading and writing speed of non-ASCII encoded strings by about 3% to 7%. Note: If this flag is used while passing in illegal UTF-8 strings, the following errors may occur: - Escaped characters may be ignored when parsing JSON strings. - Ending quotes may be ignored when parsing JSON strings, causing the string to be concatenated to the next value. - When accessing `yyjson_mut_val` for serialization, the string ending may be accessed out of bounds, causing a segmentation fault. */ #ifndef YYJSON_DISABLE_UTF8_VALIDATION #endif /* Define as 1 to indicate that the target architecture does not support unaligned memory access. Please refer to the comments in the C file for details. */ #ifndef YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS #endif /* Define as 1 to export symbols when building this library as Windows DLL. */ #ifndef YYJSON_EXPORTS #endif /* Define as 1 to import symbols when using this library as Windows DLL. */ #ifndef YYJSON_IMPORTS #endif /* Define as 1 to include for compiler which doesn't support C99. */ #ifndef YYJSON_HAS_STDINT_H #endif /* Define as 1 to include for compiler which doesn't support C99. */ #ifndef YYJSON_HAS_STDBOOL_H #endif /*============================================================================== * Compiler Macros *============================================================================*/ /** compiler version (MSVC) */ #ifdef _MSC_VER # define YYJSON_MSC_VER _MSC_VER #else # define YYJSON_MSC_VER 0 #endif /** compiler version (GCC) */ #ifdef __GNUC__ # define YYJSON_GCC_VER __GNUC__ # if defined(__GNUC_PATCHLEVEL__) # define yyjson_gcc_available(major, minor, patch) \ ((__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) \ >= (major * 10000 + minor * 100 + patch)) # else # define yyjson_gcc_available(major, minor, patch) \ ((__GNUC__ * 10000 + __GNUC_MINOR__ * 100) \ >= (major * 10000 + minor * 100 + patch)) # endif #else # define YYJSON_GCC_VER 0 # define yyjson_gcc_available(major, minor, patch) 0 #endif /** real gcc check */ #if !defined(__clang__) && !defined(__INTEL_COMPILER) && !defined(__ICC) && \ defined(__GNUC__) # define YYJSON_IS_REAL_GCC 1 #else # define YYJSON_IS_REAL_GCC 0 #endif /** C version (STDC) */ #if defined(__STDC__) && (__STDC__ >= 1) && defined(__STDC_VERSION__) # define YYJSON_STDC_VER __STDC_VERSION__ #else # define YYJSON_STDC_VER 0 #endif /** C++ version */ #if defined(__cplusplus) # define YYJSON_CPP_VER __cplusplus #else # define YYJSON_CPP_VER 0 #endif /** compiler builtin check (since gcc 10.0, clang 2.6, icc 2021) */ #ifndef yyjson_has_builtin # ifdef __has_builtin # define yyjson_has_builtin(x) __has_builtin(x) # else # define yyjson_has_builtin(x) 0 # endif #endif /** compiler attribute check (since gcc 5.0, clang 2.9, icc 17) */ #ifndef yyjson_has_attribute # ifdef __has_attribute # define yyjson_has_attribute(x) __has_attribute(x) # else # define yyjson_has_attribute(x) 0 # endif #endif /** compiler feature check (since clang 2.6, icc 17) */ #ifndef yyjson_has_feature # ifdef __has_feature # define yyjson_has_feature(x) __has_feature(x) # else # define yyjson_has_feature(x) 0 # endif #endif /** include check (since gcc 5.0, clang 2.7, icc 16, msvc 2017 15.3) */ #ifndef yyjson_has_include # ifdef __has_include # define yyjson_has_include(x) __has_include(x) # else # define yyjson_has_include(x) 0 # endif #endif /** inline for compiler */ #ifndef yyjson_inline # if YYJSON_MSC_VER >= 1200 # define yyjson_inline __forceinline # elif defined(_MSC_VER) # define yyjson_inline __inline # elif yyjson_has_attribute(always_inline) || YYJSON_GCC_VER >= 4 # define yyjson_inline __inline__ __attribute__((always_inline)) # elif defined(__clang__) || defined(__GNUC__) # define yyjson_inline __inline__ # elif defined(__cplusplus) || YYJSON_STDC_VER >= 199901L # define yyjson_inline inline # else # define yyjson_inline # endif #endif /** noinline for compiler */ #ifndef yyjson_noinline # if YYJSON_MSC_VER >= 1400 # define yyjson_noinline __declspec(noinline) # elif yyjson_has_attribute(noinline) || YYJSON_GCC_VER >= 4 # define yyjson_noinline __attribute__((noinline)) # else # define yyjson_noinline # endif #endif /** align for compiler */ #ifndef yyjson_align # if YYJSON_MSC_VER >= 1300 # define yyjson_align(x) __declspec(align(x)) # elif yyjson_has_attribute(aligned) || defined(__GNUC__) # define yyjson_align(x) __attribute__((aligned(x))) # elif YYJSON_CPP_VER >= 201103L # define yyjson_align(x) alignas(x) # else # define yyjson_align(x) # endif #endif /** likely for compiler */ #ifndef yyjson_likely # if yyjson_has_builtin(__builtin_expect) || \ (YYJSON_GCC_VER >= 4 && YYJSON_GCC_VER != 5) # define yyjson_likely(expr) __builtin_expect(!!(expr), 1) # else # define yyjson_likely(expr) (expr) # endif #endif /** unlikely for compiler */ #ifndef yyjson_unlikely # if yyjson_has_builtin(__builtin_expect) || \ (YYJSON_GCC_VER >= 4 && YYJSON_GCC_VER != 5) # define yyjson_unlikely(expr) __builtin_expect(!!(expr), 0) # else # define yyjson_unlikely(expr) (expr) # endif #endif /** compile-time constant check for compiler */ #ifndef yyjson_constant_p # if yyjson_has_builtin(__builtin_constant_p) || (YYJSON_GCC_VER >= 3) # define YYJSON_HAS_CONSTANT_P 1 # define yyjson_constant_p(value) __builtin_constant_p(value) # else # define YYJSON_HAS_CONSTANT_P 0 # define yyjson_constant_p(value) 0 # endif #endif /** deprecate warning */ #ifndef yyjson_deprecated # if YYJSON_MSC_VER >= 1400 # define yyjson_deprecated(msg) __declspec(deprecated(msg)) # elif yyjson_has_feature(attribute_deprecated_with_message) || \ (YYJSON_GCC_VER > 4 || (YYJSON_GCC_VER == 4 && __GNUC_MINOR__ >= 5)) # define yyjson_deprecated(msg) __attribute__((deprecated(msg))) # elif YYJSON_GCC_VER >= 3 # define yyjson_deprecated(msg) __attribute__((deprecated)) # else # define yyjson_deprecated(msg) # endif #endif /** function export */ #ifndef yyjson_api # if defined(_WIN32) # if defined(YYJSON_EXPORTS) && YYJSON_EXPORTS # define yyjson_api __declspec(dllexport) # elif defined(YYJSON_IMPORTS) && YYJSON_IMPORTS # define yyjson_api __declspec(dllimport) # else # define yyjson_api # endif # elif yyjson_has_attribute(visibility) || YYJSON_GCC_VER >= 4 # define yyjson_api __attribute__((visibility("default"))) # else # define yyjson_api # endif #endif /** inline function export */ #ifndef yyjson_api_inline # define yyjson_api_inline static yyjson_inline #endif /** stdint (C89 compatible) */ #if (defined(YYJSON_HAS_STDINT_H) && YYJSON_HAS_STDINT_H) || \ YYJSON_MSC_VER >= 1600 || YYJSON_STDC_VER >= 199901L || \ defined(_STDINT_H) || defined(_STDINT_H_) || \ defined(__CLANG_STDINT_H) || defined(_STDINT_H_INCLUDED) || \ yyjson_has_include() # include #elif defined(_MSC_VER) # if _MSC_VER < 1300 typedef signed char int8_t; typedef signed short int16_t; typedef signed int int32_t; typedef unsigned char uint8_t; typedef unsigned short uint16_t; typedef unsigned int uint32_t; typedef signed __int64 int64_t; typedef unsigned __int64 uint64_t; # else typedef signed __int8 int8_t; typedef signed __int16 int16_t; typedef signed __int32 int32_t; typedef unsigned __int8 uint8_t; typedef unsigned __int16 uint16_t; typedef unsigned __int32 uint32_t; typedef signed __int64 int64_t; typedef unsigned __int64 uint64_t; # endif #else # if UCHAR_MAX == 0xFFU typedef signed char int8_t; typedef unsigned char uint8_t; # else # error cannot find 8-bit integer type # endif # if USHRT_MAX == 0xFFFFU typedef unsigned short uint16_t; typedef signed short int16_t; # elif UINT_MAX == 0xFFFFU typedef unsigned int uint16_t; typedef signed int int16_t; # else # error cannot find 16-bit integer type # endif # if UINT_MAX == 0xFFFFFFFFUL typedef unsigned int uint32_t; typedef signed int int32_t; # elif ULONG_MAX == 0xFFFFFFFFUL typedef unsigned long uint32_t; typedef signed long int32_t; # elif USHRT_MAX == 0xFFFFFFFFUL typedef unsigned short uint32_t; typedef signed short int32_t; # else # error cannot find 32-bit integer type # endif # if defined(__INT64_TYPE__) && defined(__UINT64_TYPE__) typedef __INT64_TYPE__ int64_t; typedef __UINT64_TYPE__ uint64_t; # elif defined(__GNUC__) || defined(__clang__) # if !defined(_SYS_TYPES_H) && !defined(__int8_t_defined) __extension__ typedef long long int64_t; # endif __extension__ typedef unsigned long long uint64_t; # elif defined(_LONG_LONG) || defined(__MWERKS__) || defined(_CRAYC) || \ defined(__SUNPRO_C) || defined(__SUNPRO_CC) typedef long long int64_t; typedef unsigned long long uint64_t; # elif (defined(__BORLANDC__) && __BORLANDC__ > 0x460) || \ defined(__WATCOM_INT64__) || defined (__alpha) || defined (__DECC) typedef __int64 int64_t; typedef unsigned __int64 uint64_t; # else # error cannot find 64-bit integer type # endif #endif /** stdbool (C89 compatible) */ #if (defined(YYJSON_HAS_STDBOOL_H) && YYJSON_HAS_STDBOOL_H) || \ (yyjson_has_include() && !defined(__STRICT_ANSI__)) || \ YYJSON_MSC_VER >= 1800 || YYJSON_STDC_VER >= 199901L # include #elif !defined(__bool_true_false_are_defined) # define __bool_true_false_are_defined 1 # if defined(__cplusplus) # if defined(__GNUC__) && !defined(__STRICT_ANSI__) # define _Bool bool # if __cplusplus < 201103L # define bool bool # define false false # define true true # endif # endif # else # define bool unsigned char # define true 1 # define false 0 # endif #endif /** char bit check */ #if defined(CHAR_BIT) # if CHAR_BIT != 8 # error non 8-bit char is not supported # endif #endif /** Microsoft Visual C++ 6.0 doesn't support converting number from u64 to f64: error C2520: conversion from unsigned __int64 to double not implemented. */ #ifndef YYJSON_U64_TO_F64_NO_IMPL # if (0 < YYJSON_MSC_VER) && (YYJSON_MSC_VER <= 1200) # define YYJSON_U64_TO_F64_NO_IMPL 1 # else # define YYJSON_U64_TO_F64_NO_IMPL 0 # endif #endif /*============================================================================== * Compile Hint Begin *============================================================================*/ /* extern "C" begin */ #ifdef __cplusplus extern "C" { #endif /* warning suppress begin */ #if defined(__clang__) # pragma clang diagnostic push # pragma clang diagnostic ignored "-Wunused-function" # pragma clang diagnostic ignored "-Wunused-parameter" #elif defined(__GNUC__) # if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) # pragma GCC diagnostic push # endif # pragma GCC diagnostic ignored "-Wunused-function" # pragma GCC diagnostic ignored "-Wunused-parameter" #elif defined(_MSC_VER) # pragma warning(push) # pragma warning(disable:4800) /* 'int': forcing value to 'true' or 'false' */ #endif /*============================================================================== * Version *============================================================================*/ /** The major version of yyjson. */ #define YYJSON_VERSION_MAJOR 0 /** The minor version of yyjson. */ #define YYJSON_VERSION_MINOR 9 /** The patch version of yyjson. */ #define YYJSON_VERSION_PATCH 0 /** The version of yyjson in hex: `(major << 16) | (minor << 8) | (patch)`. */ #define YYJSON_VERSION_HEX 0x000900 /** The version string of yyjson. */ #define YYJSON_VERSION_STRING "0.9.0" /** The version of yyjson in hex, same as `YYJSON_VERSION_HEX`. */ yyjson_api uint32_t yyjson_version(void); /*============================================================================== * JSON Types *============================================================================*/ /** Type of a JSON value (3 bit). */ typedef uint8_t yyjson_type; /** No type, invalid. */ #define YYJSON_TYPE_NONE ((uint8_t)0) /* _____000 */ /** Raw string type, no subtype. */ #define YYJSON_TYPE_RAW ((uint8_t)1) /* _____001 */ /** Null type: `null` literal, no subtype. */ #define YYJSON_TYPE_NULL ((uint8_t)2) /* _____010 */ /** Boolean type, subtype: TRUE, FALSE. */ #define YYJSON_TYPE_BOOL ((uint8_t)3) /* _____011 */ /** Number type, subtype: UINT, SINT, REAL. */ #define YYJSON_TYPE_NUM ((uint8_t)4) /* _____100 */ /** String type, subtype: NONE, NOESC. */ #define YYJSON_TYPE_STR ((uint8_t)5) /* _____101 */ /** Array type, no subtype. */ #define YYJSON_TYPE_ARR ((uint8_t)6) /* _____110 */ /** Object type, no subtype. */ #define YYJSON_TYPE_OBJ ((uint8_t)7) /* _____111 */ /** Subtype of a JSON value (2 bit). */ typedef uint8_t yyjson_subtype; /** No subtype. */ #define YYJSON_SUBTYPE_NONE ((uint8_t)(0 << 3)) /* ___00___ */ /** False subtype: `false` literal. */ #define YYJSON_SUBTYPE_FALSE ((uint8_t)(0 << 3)) /* ___00___ */ /** True subtype: `true` literal. */ #define YYJSON_SUBTYPE_TRUE ((uint8_t)(1 << 3)) /* ___01___ */ /** Unsigned integer subtype: `uint64_t`. */ #define YYJSON_SUBTYPE_UINT ((uint8_t)(0 << 3)) /* ___00___ */ /** Signed integer subtype: `int64_t`. */ #define YYJSON_SUBTYPE_SINT ((uint8_t)(1 << 3)) /* ___01___ */ /** Real number subtype: `double`. */ #define YYJSON_SUBTYPE_REAL ((uint8_t)(2 << 3)) /* ___10___ */ /** String that do not need to be escaped for writing (internal use). */ #define YYJSON_SUBTYPE_NOESC ((uint8_t)(1 << 3)) /* ___01___ */ /** The mask used to extract the type of a JSON value. */ #define YYJSON_TYPE_MASK ((uint8_t)0x07) /* _____111 */ /** The number of bits used by the type. */ #define YYJSON_TYPE_BIT ((uint8_t)3) /** The mask used to extract the subtype of a JSON value. */ #define YYJSON_SUBTYPE_MASK ((uint8_t)0x18) /* ___11___ */ /** The number of bits used by the subtype. */ #define YYJSON_SUBTYPE_BIT ((uint8_t)2) /** The mask used to extract the reserved bits of a JSON value. */ #define YYJSON_RESERVED_MASK ((uint8_t)0xE0) /* 111_____ */ /** The number of reserved bits. */ #define YYJSON_RESERVED_BIT ((uint8_t)3) /** The mask used to extract the tag of a JSON value. */ #define YYJSON_TAG_MASK ((uint8_t)0xFF) /* 11111111 */ /** The number of bits used by the tag. */ #define YYJSON_TAG_BIT ((uint8_t)8) /** Padding size for JSON reader. */ #define YYJSON_PADDING_SIZE 4 /*============================================================================== * Allocator *============================================================================*/ /** A memory allocator. Typically you don't need to use it, unless you want to customize your own memory allocator. */ typedef struct yyjson_alc { /** Same as libc's malloc(size), should not be NULL. */ void *(*malloc)(void *ctx, size_t size); /** Same as libc's realloc(ptr, size), should not be NULL. */ void *(*realloc)(void *ctx, void *ptr, size_t old_size, size_t size); /** Same as libc's free(ptr), should not be NULL. */ void (*free)(void *ctx, void *ptr); /** A context for malloc/realloc/free, can be NULL. */ void *ctx; } yyjson_alc; /** A pool allocator uses fixed length pre-allocated memory. This allocator may be used to avoid malloc/realloc calls. The pre-allocated memory should be held by the caller. The maximum amount of memory required to read a JSON can be calculated using the `yyjson_read_max_memory_usage()` function, but the amount of memory required to write a JSON cannot be directly calculated. This is not a general-purpose allocator. It is designed to handle a single JSON data at a time. If it is used for overly complex memory tasks, such as parsing multiple JSON documents using the same allocator but releasing only a few of them, it may cause memory fragmentation, resulting in performance degradation and memory waste. @param alc The allocator to be initialized. If this parameter is NULL, the function will fail and return false. If `buf` or `size` is invalid, this will be set to an empty allocator. @param buf The buffer memory for this allocator. If this parameter is NULL, the function will fail and return false. @param size The size of `buf`, in bytes. If this parameter is less than 8 words (32/64 bytes on 32/64-bit OS), the function will fail and return false. @return true if the `alc` has been successfully initialized. @par Example @code // parse JSON with stack memory char buf[1024]; yyjson_alc alc; yyjson_alc_pool_init(&alc, buf, 1024); const char *json = "{\"name\":\"Helvetica\",\"size\":16}" yyjson_doc *doc = yyjson_read_opts(json, strlen(json), 0, &alc, NULL); // the memory of `doc` is on the stack @endcode @warning This Allocator is not thread-safe. */ yyjson_api bool yyjson_alc_pool_init(yyjson_alc *alc, void *buf, size_t size); /** A dynamic allocator. This allocator has a similar usage to the pool allocator above. However, when there is not enough memory, this allocator will dynamically request more memory using libc's `malloc` function, and frees it all at once when it is destroyed. @return A new dynamic allocator, or NULL if memory allocation failed. @note The returned value should be freed with `yyjson_alc_dyn_free()`. @warning This Allocator is not thread-safe. */ yyjson_api yyjson_alc *yyjson_alc_dyn_new(void); /** Free a dynamic allocator which is created by `yyjson_alc_dyn_new()`. @param alc The dynamic allocator to be destroyed. */ yyjson_api void yyjson_alc_dyn_free(yyjson_alc *alc); /*============================================================================== * JSON Structure *============================================================================*/ /** An immutable document for reading JSON. This document holds memory for all its JSON values and strings. When it is no longer used, the user should call `yyjson_doc_free()` to free its memory. */ typedef struct yyjson_doc yyjson_doc; /** An immutable value for reading JSON. A JSON Value has the same lifetime as its document. The memory is held by its document and and cannot be freed alone. */ typedef struct yyjson_val yyjson_val; /** A mutable document for building JSON. This document holds memory for all its JSON values and strings. When it is no longer used, the user should call `yyjson_mut_doc_free()` to free its memory. */ typedef struct yyjson_mut_doc yyjson_mut_doc; /** A mutable value for building JSON. A JSON Value has the same lifetime as its document. The memory is held by its document and and cannot be freed alone. */ typedef struct yyjson_mut_val yyjson_mut_val; /*============================================================================== * JSON Reader API *============================================================================*/ /** Run-time options for JSON reader. */ typedef uint32_t yyjson_read_flag; /** Default option (RFC 8259 compliant): - Read positive integer as uint64_t. - Read negative integer as int64_t. - Read floating-point number as double with round-to-nearest mode. - Read integer which cannot fit in uint64_t or int64_t as double. - Report error if double number is infinity. - Report error if string contains invalid UTF-8 character or BOM. - Report error on trailing commas, comments, inf and nan literals. */ static const yyjson_read_flag YYJSON_READ_NOFLAG = 0; /** Read the input data in-situ. This option allows the reader to modify and use input data to store string values, which can increase reading speed slightly. The caller should hold the input data before free the document. The input data must be padded by at least `YYJSON_PADDING_SIZE` bytes. For example: `[1,2]` should be `[1,2]\0\0\0\0`, input length should be 5. */ static const yyjson_read_flag YYJSON_READ_INSITU = 1 << 0; /** Stop when done instead of issuing an error if there's additional content after a JSON document. This option may be used to parse small pieces of JSON in larger data, such as `NDJSON`. */ static const yyjson_read_flag YYJSON_READ_STOP_WHEN_DONE = 1 << 1; /** Allow single trailing comma at the end of an object or array, such as `[1,2,3,]`, `{"a":1,"b":2,}` (non-standard). */ static const yyjson_read_flag YYJSON_READ_ALLOW_TRAILING_COMMAS = 1 << 2; /** Allow C-style single line and multiple line comments (non-standard). */ static const yyjson_read_flag YYJSON_READ_ALLOW_COMMENTS = 1 << 3; /** Allow inf/nan number and literal, case-insensitive, such as 1e999, NaN, inf, -Infinity (non-standard). */ static const yyjson_read_flag YYJSON_READ_ALLOW_INF_AND_NAN = 1 << 4; /** Read all numbers as raw strings (value with `YYJSON_TYPE_RAW` type), inf/nan literal is also read as raw with `ALLOW_INF_AND_NAN` flag. */ static const yyjson_read_flag YYJSON_READ_NUMBER_AS_RAW = 1 << 5; /** Allow reading invalid unicode when parsing string values (non-standard). Invalid characters will be allowed to appear in the string values, but invalid escape sequences will still be reported as errors. This flag does not affect the performance of correctly encoded strings. @warning Strings in JSON values may contain incorrect encoding when this option is used, you need to handle these strings carefully to avoid security risks. */ static const yyjson_read_flag YYJSON_READ_ALLOW_INVALID_UNICODE = 1 << 6; /** Read big numbers as raw strings. These big numbers include integers that cannot be represented by `int64_t` and `uint64_t`, and floating-point numbers that cannot be represented by finite `double`. The flag will be overridden by `YYJSON_READ_NUMBER_AS_RAW` flag. */ static const yyjson_read_flag YYJSON_READ_BIGNUM_AS_RAW = 1 << 7; /** Result code for JSON reader. */ typedef uint32_t yyjson_read_code; /** Success, no error. */ static const yyjson_read_code YYJSON_READ_SUCCESS = 0; /** Invalid parameter, such as NULL input string or 0 input length. */ static const yyjson_read_code YYJSON_READ_ERROR_INVALID_PARAMETER = 1; /** Memory allocation failure occurs. */ static const yyjson_read_code YYJSON_READ_ERROR_MEMORY_ALLOCATION = 2; /** Input JSON string is empty. */ static const yyjson_read_code YYJSON_READ_ERROR_EMPTY_CONTENT = 3; /** Unexpected content after document, such as `[123]abc`. */ static const yyjson_read_code YYJSON_READ_ERROR_UNEXPECTED_CONTENT = 4; /** Unexpected ending, such as `[123`. */ static const yyjson_read_code YYJSON_READ_ERROR_UNEXPECTED_END = 5; /** Unexpected character inside the document, such as `[abc]`. */ static const yyjson_read_code YYJSON_READ_ERROR_UNEXPECTED_CHARACTER = 6; /** Invalid JSON structure, such as `[1,]`. */ static const yyjson_read_code YYJSON_READ_ERROR_JSON_STRUCTURE = 7; /** Invalid comment, such as unclosed multi-line comment. */ static const yyjson_read_code YYJSON_READ_ERROR_INVALID_COMMENT = 8; /** Invalid number, such as `123.e12`, `000`. */ static const yyjson_read_code YYJSON_READ_ERROR_INVALID_NUMBER = 9; /** Invalid string, such as invalid escaped character inside a string. */ static const yyjson_read_code YYJSON_READ_ERROR_INVALID_STRING = 10; /** Invalid JSON literal, such as `truu`. */ static const yyjson_read_code YYJSON_READ_ERROR_LITERAL = 11; /** Failed to open a file. */ static const yyjson_read_code YYJSON_READ_ERROR_FILE_OPEN = 12; /** Failed to read a file. */ static const yyjson_read_code YYJSON_READ_ERROR_FILE_READ = 13; /** Document exceeded YYJSON_READER_CONTAINER_RECURSION_LIMIT. */ static const yyjson_read_code YYJSON_READ_ERROR_RECURSION_DEPTH = 14; /** Error information for JSON reader. */ typedef struct yyjson_read_err { /** Error code, see `yyjson_read_code` for all possible values. */ yyjson_read_code code; /** Error message, constant, no need to free (NULL if success). */ const char *msg; /** Error byte position for input data (0 if success). */ size_t pos; } yyjson_read_err; /** Read JSON with options. This function is thread-safe when: 1. The `dat` is not modified by other threads. 2. The `alc` is thread-safe or NULL. @param dat The JSON data (UTF-8 without BOM), null-terminator is not required. If this parameter is NULL, the function will fail and return NULL. The `dat` will not be modified without the flag `YYJSON_READ_INSITU`, so you can pass a `const char *` string and case it to `char *` if you don't use the `YYJSON_READ_INSITU` flag. @param len The length of JSON data in bytes. If this parameter is 0, the function will fail and return NULL. @param alc The memory allocator used by JSON reader. Pass NULL to use the libc's default allocator. @param err A pointer to receive error information. Pass NULL if you don't need error information. @return A new JSON document, or NULL if an error occurs. When it's no longer needed, it should be freed with `yyjson_doc_free()`. */ yyjson_api yyjson_doc *yyjson_read_opts(char *dat, size_t len, const yyjson_alc *alc, yyjson_read_err *err); /** Read a JSON file. This function is thread-safe when: 1. The file is not modified by other threads. 2. The `alc` is thread-safe or NULL. @param path The JSON file's path. If this path is NULL or invalid, the function will fail and return NULL. @param flg The JSON read options. Multiple options can be combined with `|` operator. 0 means no options. @param alc The memory allocator used by JSON reader. Pass NULL to use the libc's default allocator. @param err A pointer to receive error information. Pass NULL if you don't need error information. @return A new JSON document, or NULL if an error occurs. When it's no longer needed, it should be freed with `yyjson_doc_free()`. @warning On 32-bit operating system, files larger than 2GB may fail to read. */ yyjson_api yyjson_doc *yyjson_read_file(const char *path, yyjson_read_flag flg, const yyjson_alc *alc, yyjson_read_err *err); /** Read JSON from a file pointer. @param fp The file pointer. The data will be read from the current position of the FILE to the end. If this fp is NULL or invalid, the function will fail and return NULL. @param flg The JSON read options. Multiple options can be combined with `|` operator. 0 means no options. @param alc The memory allocator used by JSON reader. Pass NULL to use the libc's default allocator. @param err A pointer to receive error information. Pass NULL if you don't need error information. @return A new JSON document, or NULL if an error occurs. When it's no longer needed, it should be freed with `yyjson_doc_free()`. @warning On 32-bit operating system, files larger than 2GB may fail to read. */ yyjson_api yyjson_doc *yyjson_read_fp(FILE *fp, yyjson_read_flag flg, const yyjson_alc *alc, yyjson_read_err *err); /** Read a JSON string. This function is thread-safe. @param dat The JSON data (UTF-8 without BOM), null-terminator is not required. If this parameter is NULL, the function will fail and return NULL. @param len The length of JSON data in bytes. If this parameter is 0, the function will fail and return NULL. @param flg The JSON read options. Multiple options can be combined with `|` operator. 0 means no options. @return A new JSON document, or NULL if an error occurs. When it's no longer needed, it should be freed with `yyjson_doc_free()`. */ yyjson_api_inline yyjson_doc *yyjson_read(const char *dat, size_t len, yyjson_read_flag flg) { flg &= ~YYJSON_READ_INSITU; /* const string cannot be modified */ return yyjson_read_opts((char *)(void *)(size_t)(const void *)dat, len, NULL, NULL); } /** Returns the size of maximum memory usage to read a JSON data. You may use this value to avoid malloc() or calloc() call inside the reader to get better performance, or read multiple JSON with one piece of memory. @param len The length of JSON data in bytes. @param flg The JSON read options. @return The maximum memory size to read this JSON, or 0 if overflow. @par Example @code // read multiple JSON with same pre-allocated memory char *dat1, *dat2, *dat3; // JSON data size_t len1, len2, len3; // JSON length size_t max_len = MAX(len1, MAX(len2, len3)); yyjson_doc *doc; // use one allocator for multiple JSON size_t size = yyjson_read_max_memory_usage(max_len, 0); void *buf = malloc(size); yyjson_alc alc; yyjson_alc_pool_init(&alc, buf, size); // no more alloc() or realloc() call during reading doc = yyjson_read_opts(dat1, len1, 0, &alc, NULL); yyjson_doc_free(doc); doc = yyjson_read_opts(dat2, len2, 0, &alc, NULL); yyjson_doc_free(doc); doc = yyjson_read_opts(dat3, len3, 0, &alc, NULL); yyjson_doc_free(doc); free(buf); @endcode @see yyjson_alc_pool_init() */ yyjson_api_inline size_t yyjson_read_max_memory_usage(size_t len, yyjson_read_flag flg) { /* 1. The max value count is (json_size / 2 + 1), for example: "[1,2,3,4]" size is 9, value count is 5. 2. Some broken JSON may cost more memory during reading, but fail at end, for example: "[[[[[[[[". 3. yyjson use 16 bytes per value, see struct yyjson_val. 4. yyjson use dynamic memory with a growth factor of 1.5. The max memory size is (json_size / 2 * 16 * 1.5 + padding). */ size_t mul = (size_t)12 + !(flg & YYJSON_READ_INSITU); size_t pad = 256; size_t max = (size_t)(~(size_t)0); if (flg & YYJSON_READ_STOP_WHEN_DONE) len = len < 256 ? 256 : len; if (len >= (max - pad - mul) / mul) return 0; return len * mul + pad; } /** Read a JSON number. This function is thread-safe when data is not modified by other threads. @param dat The JSON data (UTF-8 without BOM), null-terminator is required. If this parameter is NULL, the function will fail and return NULL. @param val The output value where result is stored. If this parameter is NULL, the function will fail and return NULL. The value will hold either UINT or SINT or REAL number; @param flg The JSON read options. Multiple options can be combined with `|` operator. 0 means no options. Supports `YYJSON_READ_NUMBER_AS_RAW` and `YYJSON_READ_ALLOW_INF_AND_NAN`. @param alc The memory allocator used for long number. It is only used when the built-in floating point reader is disabled. Pass NULL to use the libc's default allocator. @param err A pointer to receive error information. Pass NULL if you don't need error information. @return If successful, a pointer to the character after the last character used in the conversion, NULL if an error occurs. */ yyjson_api const char *yyjson_read_number(const char *dat, yyjson_val *val, yyjson_read_flag flg, const yyjson_alc *alc, yyjson_read_err *err); /** Read a JSON number. This function is thread-safe when data is not modified by other threads. @param dat The JSON data (UTF-8 without BOM), null-terminator is required. If this parameter is NULL, the function will fail and return NULL. @param val The output value where result is stored. If this parameter is NULL, the function will fail and return NULL. The value will hold either UINT or SINT or REAL number; @param flg The JSON read options. Multiple options can be combined with `|` operator. 0 means no options. Supports `YYJSON_READ_NUMBER_AS_RAW` and `YYJSON_READ_ALLOW_INF_AND_NAN`. @param alc The memory allocator used for long number. It is only used when the built-in floating point reader is disabled. Pass NULL to use the libc's default allocator. @param err A pointer to receive error information. Pass NULL if you don't need error information. @return If successful, a pointer to the character after the last character used in the conversion, NULL if an error occurs. */ yyjson_api_inline const char *yyjson_mut_read_number(const char *dat, yyjson_mut_val *val, yyjson_read_flag flg, const yyjson_alc *alc, yyjson_read_err *err) { return yyjson_read_number(dat, (yyjson_val *)val, flg, alc, err); } /*============================================================================== * JSON Writer API *============================================================================*/ /** Run-time options for JSON writer. */ typedef uint32_t yyjson_write_flag; /** Default option: - Write JSON minify. - Report error on inf or nan number. - Report error on invalid UTF-8 string. - Do not escape unicode or slash. */ static const yyjson_write_flag YYJSON_WRITE_NOFLAG = 0; /** Write JSON pretty with 4 space indent. */ static const yyjson_write_flag YYJSON_WRITE_PRETTY = 1 << 0; /** Escape unicode as `uXXXX`, make the output ASCII only. */ static const yyjson_write_flag YYJSON_WRITE_ESCAPE_UNICODE = 1 << 1; /** Escape '/' as '\/'. */ static const yyjson_write_flag YYJSON_WRITE_ESCAPE_SLASHES = 1 << 2; /** Write inf and nan number as 'Infinity' and 'NaN' literal (non-standard). */ static const yyjson_write_flag YYJSON_WRITE_ALLOW_INF_AND_NAN = 1 << 3; /** Write inf and nan number as null literal. This flag will override `YYJSON_WRITE_ALLOW_INF_AND_NAN` flag. */ static const yyjson_write_flag YYJSON_WRITE_INF_AND_NAN_AS_NULL = 1 << 4; /** Allow invalid unicode when encoding string values (non-standard). Invalid characters in string value will be copied byte by byte. If `YYJSON_WRITE_ESCAPE_UNICODE` flag is also set, invalid character will be escaped as `U+FFFD` (replacement character). This flag does not affect the performance of correctly encoded strings. */ static const yyjson_write_flag YYJSON_WRITE_ALLOW_INVALID_UNICODE = 1 << 5; /** Write JSON pretty with 2 space indent. This flag will override `YYJSON_WRITE_PRETTY` flag. */ static const yyjson_write_flag YYJSON_WRITE_PRETTY_TWO_SPACES = 1 << 6; /** Adds a newline character `\n` at the end of the JSON. This can be helpful for text editors or NDJSON. */ static const yyjson_write_flag YYJSON_WRITE_NEWLINE_AT_END = 1 << 7; /** Result code for JSON writer */ typedef uint32_t yyjson_write_code; /** Success, no error. */ static const yyjson_write_code YYJSON_WRITE_SUCCESS = 0; /** Invalid parameter, such as NULL document. */ static const yyjson_write_code YYJSON_WRITE_ERROR_INVALID_PARAMETER = 1; /** Memory allocation failure occurs. */ static const yyjson_write_code YYJSON_WRITE_ERROR_MEMORY_ALLOCATION = 2; /** Invalid value type in JSON document. */ static const yyjson_write_code YYJSON_WRITE_ERROR_INVALID_VALUE_TYPE = 3; /** NaN or Infinity number occurs. */ static const yyjson_write_code YYJSON_WRITE_ERROR_NAN_OR_INF = 4; /** Failed to open a file. */ static const yyjson_write_code YYJSON_WRITE_ERROR_FILE_OPEN = 5; /** Failed to write a file. */ static const yyjson_write_code YYJSON_WRITE_ERROR_FILE_WRITE = 6; /** Invalid unicode in string. */ static const yyjson_write_code YYJSON_WRITE_ERROR_INVALID_STRING = 7; /** Error information for JSON writer. */ typedef struct yyjson_write_err { /** Error code, see `yyjson_write_code` for all possible values. */ yyjson_write_code code; /** Error message, constant, no need to free (NULL if success). */ const char *msg; } yyjson_write_err; /*============================================================================== * JSON Document Writer API *============================================================================*/ /** Write a document to JSON string with options. This function is thread-safe when: The `alc` is thread-safe or NULL. @param doc The JSON document. If this doc is NULL or has no root, the function will fail and return false. @param flg The JSON write options. Multiple options can be combined with `|` operator. 0 means no options. @param alc The memory allocator used by JSON writer. Pass NULL to use the libc's default allocator. @param len A pointer to receive output length in bytes (not including the null-terminator). Pass NULL if you don't need length information. @param err A pointer to receive error information. Pass NULL if you don't need error information. @return A new JSON string, or NULL if an error occurs. This string is encoded as UTF-8 with a null-terminator. When it's no longer needed, it should be freed with free() or alc->free(). */ yyjson_api char *yyjson_write_opts(const yyjson_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc, size_t *len, yyjson_write_err *err); /** Write a document to JSON file with options. This function is thread-safe when: 1. The file is not accessed by other threads. 2. The `alc` is thread-safe or NULL. @param path The JSON file's path. If this path is NULL or invalid, the function will fail and return false. If this file is not empty, the content will be discarded. @param doc The JSON document. If this doc is NULL or has no root, the function will fail and return false. @param flg The JSON write options. Multiple options can be combined with `|` operator. 0 means no options. @param alc The memory allocator used by JSON writer. Pass NULL to use the libc's default allocator. @param err A pointer to receive error information. Pass NULL if you don't need error information. @return true if successful, false if an error occurs. @warning On 32-bit operating system, files larger than 2GB may fail to write. */ yyjson_api bool yyjson_write_file(const char *path, const yyjson_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err); /** Write a document to file pointer with options. @param fp The file pointer. The data will be written to the current position of the file. If this fp is NULL or invalid, the function will fail and return false. @param doc The JSON document. If this doc is NULL or has no root, the function will fail and return false. @param flg The JSON write options. Multiple options can be combined with `|` operator. 0 means no options. @param alc The memory allocator used by JSON writer. Pass NULL to use the libc's default allocator. @param err A pointer to receive error information. Pass NULL if you don't need error information. @return true if successful, false if an error occurs. @warning On 32-bit operating system, files larger than 2GB may fail to write. */ yyjson_api bool yyjson_write_fp(FILE *fp, const yyjson_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err); /** Write a document to JSON string. This function is thread-safe. @param doc The JSON document. If this doc is NULL or has no root, the function will fail and return false. @param flg The JSON write options. Multiple options can be combined with `|` operator. 0 means no options. @param len A pointer to receive output length in bytes (not including the null-terminator). Pass NULL if you don't need length information. @return A new JSON string, or NULL if an error occurs. This string is encoded as UTF-8 with a null-terminator. When it's no longer needed, it should be freed with free(). */ yyjson_api_inline char *yyjson_write(const yyjson_doc *doc, yyjson_write_flag flg, size_t *len) { return yyjson_write_opts(doc, flg, NULL, len, NULL); } /** Write a document to JSON string with options. This function is thread-safe when: 1. The `doc` is not modified by other threads. 2. The `alc` is thread-safe or NULL. @param doc The mutable JSON document. If this doc is NULL or has no root, the function will fail and return false. @param flg The JSON write options. Multiple options can be combined with `|` operator. 0 means no options. @param alc The memory allocator used by JSON writer. Pass NULL to use the libc's default allocator. @param len A pointer to receive output length in bytes (not including the null-terminator). Pass NULL if you don't need length information. @param err A pointer to receive error information. Pass NULL if you don't need error information. @return A new JSON string, or NULL if an error occurs. This string is encoded as UTF-8 with a null-terminator. When it's no longer needed, it should be freed with free() or alc->free(). */ yyjson_api char *yyjson_mut_write_opts(const yyjson_mut_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc, size_t *len, yyjson_write_err *err); /** Write a document to JSON file with options. This function is thread-safe when: 1. The file is not accessed by other threads. 2. The `doc` is not modified by other threads. 3. The `alc` is thread-safe or NULL. @param path The JSON file's path. If this path is NULL or invalid, the function will fail and return false. If this file is not empty, the content will be discarded. @param doc The mutable JSON document. If this doc is NULL or has no root, the function will fail and return false. @param flg The JSON write options. Multiple options can be combined with `|` operator. 0 means no options. @param alc The memory allocator used by JSON writer. Pass NULL to use the libc's default allocator. @param err A pointer to receive error information. Pass NULL if you don't need error information. @return true if successful, false if an error occurs. @warning On 32-bit operating system, files larger than 2GB may fail to write. */ yyjson_api bool yyjson_mut_write_file(const char *path, const yyjson_mut_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err); /** Write a document to file pointer with options. @param fp The file pointer. The data will be written to the current position of the file. If this fp is NULL or invalid, the function will fail and return false. @param doc The mutable JSON document. If this doc is NULL or has no root, the function will fail and return false. @param flg The JSON write options. Multiple options can be combined with `|` operator. 0 means no options. @param alc The memory allocator used by JSON writer. Pass NULL to use the libc's default allocator. @param err A pointer to receive error information. Pass NULL if you don't need error information. @return true if successful, false if an error occurs. @warning On 32-bit operating system, files larger than 2GB may fail to write. */ yyjson_api bool yyjson_mut_write_fp(FILE *fp, const yyjson_mut_doc *doc, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err); /** Write a document to JSON string. This function is thread-safe when: The `doc` is not modified by other threads. @param doc The JSON document. If this doc is NULL or has no root, the function will fail and return false. @param flg The JSON write options. Multiple options can be combined with `|` operator. 0 means no options. @param len A pointer to receive output length in bytes (not including the null-terminator). Pass NULL if you don't need length information. @return A new JSON string, or NULL if an error occurs. This string is encoded as UTF-8 with a null-terminator. When it's no longer needed, it should be freed with free(). */ yyjson_api_inline char *yyjson_mut_write(const yyjson_mut_doc *doc, yyjson_write_flag flg, size_t *len) { return yyjson_mut_write_opts(doc, flg, NULL, len, NULL); } /*============================================================================== * JSON Value Writer API *============================================================================*/ /** Write a value to JSON string with options. This function is thread-safe when: The `alc` is thread-safe or NULL. @param val The JSON root value. If this parameter is NULL, the function will fail and return NULL. @param flg The JSON write options. Multiple options can be combined with `|` operator. 0 means no options. @param alc The memory allocator used by JSON writer. Pass NULL to use the libc's default allocator. @param len A pointer to receive output length in bytes (not including the null-terminator). Pass NULL if you don't need length information. @param err A pointer to receive error information. Pass NULL if you don't need error information. @return A new JSON string, or NULL if an error occurs. This string is encoded as UTF-8 with a null-terminator. When it's no longer needed, it should be freed with free() or alc->free(). */ yyjson_api char *yyjson_val_write_opts(const yyjson_val *val, yyjson_write_flag flg, const yyjson_alc *alc, size_t *len, yyjson_write_err *err); /** Write a value to JSON file with options. This function is thread-safe when: 1. The file is not accessed by other threads. 2. The `alc` is thread-safe or NULL. @param path The JSON file's path. If this path is NULL or invalid, the function will fail and return false. If this file is not empty, the content will be discarded. @param val The JSON root value. If this parameter is NULL, the function will fail and return NULL. @param flg The JSON write options. Multiple options can be combined with `|` operator. 0 means no options. @param alc The memory allocator used by JSON writer. Pass NULL to use the libc's default allocator. @param err A pointer to receive error information. Pass NULL if you don't need error information. @return true if successful, false if an error occurs. @warning On 32-bit operating system, files larger than 2GB may fail to write. */ yyjson_api bool yyjson_val_write_file(const char *path, const yyjson_val *val, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err); /** Write a value to file pointer with options. @param fp The file pointer. The data will be written to the current position of the file. If this path is NULL or invalid, the function will fail and return false. @param val The JSON root value. If this parameter is NULL, the function will fail and return NULL. @param flg The JSON write options. Multiple options can be combined with `|` operator. 0 means no options. @param alc The memory allocator used by JSON writer. Pass NULL to use the libc's default allocator. @param err A pointer to receive error information. Pass NULL if you don't need error information. @return true if successful, false if an error occurs. @warning On 32-bit operating system, files larger than 2GB may fail to write. */ yyjson_api bool yyjson_val_write_fp(FILE *fp, const yyjson_val *val, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err); /** Write a value to JSON string. This function is thread-safe. @param val The JSON root value. If this parameter is NULL, the function will fail and return NULL. @param flg The JSON write options. Multiple options can be combined with `|` operator. 0 means no options. @param len A pointer to receive output length in bytes (not including the null-terminator). Pass NULL if you don't need length information. @return A new JSON string, or NULL if an error occurs. This string is encoded as UTF-8 with a null-terminator. When it's no longer needed, it should be freed with free(). */ yyjson_api_inline char *yyjson_val_write(const yyjson_val *val, yyjson_write_flag flg, size_t *len) { return yyjson_val_write_opts(val, flg, NULL, len, NULL); } /** Write a value to JSON string with options. This function is thread-safe when: 1. The `val` is not modified by other threads. 2. The `alc` is thread-safe or NULL. @param val The mutable JSON root value. If this parameter is NULL, the function will fail and return NULL. @param flg The JSON write options. Multiple options can be combined with `|` operator. 0 means no options. @param alc The memory allocator used by JSON writer. Pass NULL to use the libc's default allocator. @param len A pointer to receive output length in bytes (not including the null-terminator). Pass NULL if you don't need length information. @param err A pointer to receive error information. Pass NULL if you don't need error information. @return A new JSON string, or NULL if an error occurs. This string is encoded as UTF-8 with a null-terminator. When it's no longer needed, it should be freed with free() or alc->free(). */ yyjson_api char *yyjson_mut_val_write_opts(const yyjson_mut_val *val, yyjson_write_flag flg, const yyjson_alc *alc, size_t *len, yyjson_write_err *err); /** Write a value to JSON file with options. This function is thread-safe when: 1. The file is not accessed by other threads. 2. The `val` is not modified by other threads. 3. The `alc` is thread-safe or NULL. @param path The JSON file's path. If this path is NULL or invalid, the function will fail and return false. If this file is not empty, the content will be discarded. @param val The mutable JSON root value. If this parameter is NULL, the function will fail and return NULL. @param flg The JSON write options. Multiple options can be combined with `|` operator. 0 means no options. @param alc The memory allocator used by JSON writer. Pass NULL to use the libc's default allocator. @param err A pointer to receive error information. Pass NULL if you don't need error information. @return true if successful, false if an error occurs. @warning On 32-bit operating system, files larger than 2GB may fail to write. */ yyjson_api bool yyjson_mut_val_write_file(const char *path, const yyjson_mut_val *val, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err); /** Write a value to JSON file with options. @param fp The file pointer. The data will be written to the current position of the file. If this path is NULL or invalid, the function will fail and return false. @param val The mutable JSON root value. If this parameter is NULL, the function will fail and return NULL. @param flg The JSON write options. Multiple options can be combined with `|` operator. 0 means no options. @param alc The memory allocator used by JSON writer. Pass NULL to use the libc's default allocator. @param err A pointer to receive error information. Pass NULL if you don't need error information. @return true if successful, false if an error occurs. @warning On 32-bit operating system, files larger than 2GB may fail to write. */ yyjson_api bool yyjson_mut_val_write_fp(FILE *fp, const yyjson_mut_val *val, yyjson_write_flag flg, const yyjson_alc *alc, yyjson_write_err *err); /** Write a value to JSON string. This function is thread-safe when: The `val` is not modified by other threads. @param val The JSON root value. If this parameter is NULL, the function will fail and return NULL. @param flg The JSON write options. Multiple options can be combined with `|` operator. 0 means no options. @param len A pointer to receive output length in bytes (not including the null-terminator). Pass NULL if you don't need length information. @return A new JSON string, or NULL if an error occurs. This string is encoded as UTF-8 with a null-terminator. When it's no longer needed, it should be freed with free(). */ yyjson_api_inline char *yyjson_mut_val_write(const yyjson_mut_val *val, yyjson_write_flag flg, size_t *len) { return yyjson_mut_val_write_opts(val, flg, NULL, len, NULL); } /*============================================================================== * JSON Document API *============================================================================*/ /** Returns the root value of this JSON document. Returns NULL if `doc` is NULL. */ yyjson_api_inline yyjson_val *yyjson_doc_get_root(yyjson_doc *doc); /** Returns read size of input JSON data. Returns 0 if `doc` is NULL. For example: the read size of `[1,2,3]` is 7 bytes. */ yyjson_api_inline size_t yyjson_doc_get_read_size(yyjson_doc *doc); /** Returns total value count in this JSON document. Returns 0 if `doc` is NULL. For example: the value count of `[1,2,3]` is 4. */ yyjson_api_inline size_t yyjson_doc_get_val_count(yyjson_doc *doc); /** Release the JSON document and free the memory. After calling this function, the `doc` and all values from the `doc` are no longer available. This function will do nothing if the `doc` is NULL. */ yyjson_api void yyjson_doc_free(yyjson_doc *doc); /*============================================================================== * JSON Value Type API *============================================================================*/ /** Returns whether the JSON value is raw. Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_is_raw(yyjson_val *val); /** Returns whether the JSON value is `null`. Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_is_null(yyjson_val *val); /** Returns whether the JSON value is `true`. Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_is_true(yyjson_val *val); /** Returns whether the JSON value is `false`. Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_is_false(yyjson_val *val); /** Returns whether the JSON value is bool (true/false). Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_is_bool(yyjson_val *val); /** Returns whether the JSON value is unsigned integer (uint64_t). Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_is_uint(yyjson_val *val); /** Returns whether the JSON value is signed integer (int64_t). Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_is_sint(yyjson_val *val); /** Returns whether the JSON value is integer (uint64_t/int64_t). Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_is_int(yyjson_val *val); /** Returns whether the JSON value is real number (double). Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_is_real(yyjson_val *val); /** Returns whether the JSON value is number (uint64_t/int64_t/double). Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_is_num(yyjson_val *val); /** Returns whether the JSON value is string. Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_is_str(yyjson_val *val); /** Returns whether the JSON value is array. Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_is_arr(yyjson_val *val); /** Returns whether the JSON value is object. Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_is_obj(yyjson_val *val); /** Returns whether the JSON value is container (array/object). Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_is_ctn(yyjson_val *val); /*============================================================================== * JSON Value Content API *============================================================================*/ /** Returns the JSON value's type. Returns YYJSON_TYPE_NONE if `val` is NULL. */ yyjson_api_inline yyjson_type yyjson_get_type(yyjson_val *val); /** Returns the JSON value's subtype. Returns YYJSON_SUBTYPE_NONE if `val` is NULL. */ yyjson_api_inline yyjson_subtype yyjson_get_subtype(yyjson_val *val); /** Returns the JSON value's tag. Returns 0 if `val` is NULL. */ yyjson_api_inline uint8_t yyjson_get_tag(yyjson_val *val); /** Returns the JSON value's type description. The return value should be one of these strings: "raw", "null", "string", "array", "object", "true", "false", "uint", "sint", "real", "unknown". */ yyjson_api_inline const char *yyjson_get_type_desc(yyjson_val *val); /** Returns the content if the value is raw. Returns NULL if `val` is NULL or type is not raw. */ yyjson_api_inline const char *yyjson_get_raw(yyjson_val *val); /** Returns the content if the value is bool. Returns NULL if `val` is NULL or type is not bool. */ yyjson_api_inline bool yyjson_get_bool(yyjson_val *val); /** Returns the content and cast to uint64_t. Returns 0 if `val` is NULL or type is not integer(sint/uint). */ yyjson_api_inline uint64_t yyjson_get_uint(yyjson_val *val); /** Returns the content and cast to int64_t. Returns 0 if `val` is NULL or type is not integer(sint/uint). */ yyjson_api_inline int64_t yyjson_get_sint(yyjson_val *val); /** Returns the content and cast to int. Returns 0 if `val` is NULL or type is not integer(sint/uint). */ yyjson_api_inline int yyjson_get_int(yyjson_val *val); /** Returns the content if the value is real number, or 0.0 on error. Returns 0.0 if `val` is NULL or type is not real(double). */ yyjson_api_inline double yyjson_get_real(yyjson_val *val); /** Returns the content and typecast to `double` if the value is number. Returns 0.0 if `val` is NULL or type is not number(uint/sint/real). */ yyjson_api_inline double yyjson_get_num(yyjson_val *val); /** Returns the content if the value is string. Returns NULL if `val` is NULL or type is not string. */ yyjson_api_inline const char *yyjson_get_str(yyjson_val *val); /** Returns the content length (string length, array size, object size. Returns 0 if `val` is NULL or type is not string/array/object. */ yyjson_api_inline size_t yyjson_get_len(yyjson_val *val); /** Returns whether the JSON value is equals to a string. Returns false if input is NULL or type is not string. */ yyjson_api_inline bool yyjson_equals_str(yyjson_val *val, const char *str); /** Returns whether the JSON value is equals to a string. The `str` should be a UTF-8 string, null-terminator is not required. Returns false if input is NULL or type is not string. */ yyjson_api_inline bool yyjson_equals_strn(yyjson_val *val, const char *str, size_t len); /** Returns whether two JSON values are equal (deep compare). Returns false if input is NULL. @note the result may be inaccurate if object has duplicate keys. @warning This function is recursive and may cause a stack overflow if the object level is too deep. */ yyjson_api_inline bool yyjson_equals(yyjson_val *lhs, yyjson_val *rhs); /** Set the value to raw. Returns false if input is NULL or `val` is object or array. @warning This will modify the `immutable` value, use with caution. */ yyjson_api_inline bool yyjson_set_raw(yyjson_val *val, const char *raw, size_t len); /** Set the value to null. Returns false if input is NULL or `val` is object or array. @warning This will modify the `immutable` value, use with caution. */ yyjson_api_inline bool yyjson_set_null(yyjson_val *val); /** Set the value to bool. Returns false if input is NULL or `val` is object or array. @warning This will modify the `immutable` value, use with caution. */ yyjson_api_inline bool yyjson_set_bool(yyjson_val *val, bool num); /** Set the value to uint. Returns false if input is NULL or `val` is object or array. @warning This will modify the `immutable` value, use with caution. */ yyjson_api_inline bool yyjson_set_uint(yyjson_val *val, uint64_t num); /** Set the value to sint. Returns false if input is NULL or `val` is object or array. @warning This will modify the `immutable` value, use with caution. */ yyjson_api_inline bool yyjson_set_sint(yyjson_val *val, int64_t num); /** Set the value to int. Returns false if input is NULL or `val` is object or array. @warning This will modify the `immutable` value, use with caution. */ yyjson_api_inline bool yyjson_set_int(yyjson_val *val, int num); /** Set the value to real. Returns false if input is NULL or `val` is object or array. @warning This will modify the `immutable` value, use with caution. */ yyjson_api_inline bool yyjson_set_real(yyjson_val *val, double num); /** Set the value to string (null-terminated). Returns false if input is NULL or `val` is object or array. @warning This will modify the `immutable` value, use with caution. */ yyjson_api_inline bool yyjson_set_str(yyjson_val *val, const char *str); /** Set the value to string (with length). Returns false if input is NULL or `val` is object or array. @warning This will modify the `immutable` value, use with caution. */ yyjson_api_inline bool yyjson_set_strn(yyjson_val *val, const char *str, size_t len); /*============================================================================== * JSON Array API *============================================================================*/ /** Returns the number of elements in this array. Returns 0 if `arr` is NULL or type is not array. */ yyjson_api_inline size_t yyjson_arr_size(yyjson_val *arr); /** Returns the element at the specified position in this array. Returns NULL if array is NULL/empty or the index is out of bounds. @warning This function takes a linear search time if array is not flat. For example: `[1,{},3]` is flat, `[1,[2],3]` is not flat. */ yyjson_api_inline yyjson_val *yyjson_arr_get(yyjson_val *arr, size_t idx); /** Returns the first element of this array. Returns NULL if `arr` is NULL/empty or type is not array. */ yyjson_api_inline yyjson_val *yyjson_arr_get_first(yyjson_val *arr); /** Returns the last element of this array. Returns NULL if `arr` is NULL/empty or type is not array. @warning This function takes a linear search time if array is not flat. For example: `[1,{},3]` is flat, `[1,[2],3]` is not flat.*/ yyjson_api_inline yyjson_val *yyjson_arr_get_last(yyjson_val *arr); /*============================================================================== * JSON Array Iterator API *============================================================================*/ /** A JSON array iterator. @par Example @code yyjson_val *val; yyjson_arr_iter iter = yyjson_arr_iter_with(arr); while ((val = yyjson_arr_iter_next(&iter))) { your_func(val); } @endcode */ typedef struct yyjson_arr_iter { size_t idx; /**< next value's index */ size_t max; /**< maximum index (arr.size) */ yyjson_val *cur; /**< next value */ } yyjson_arr_iter; /** Initialize an iterator for this array. @param arr The array to be iterated over. If this parameter is NULL or not an array, `iter` will be set to empty. @param iter The iterator to be initialized. If this parameter is NULL, the function will fail and return false. @return true if the `iter` has been successfully initialized. @note The iterator does not need to be destroyed. */ yyjson_api_inline bool yyjson_arr_iter_init(yyjson_val *arr, yyjson_arr_iter *iter); /** Create an iterator with an array , same as `yyjson_arr_iter_init()`. @param arr The array to be iterated over. If this parameter is NULL or not an array, an empty iterator will returned. @return A new iterator for the array. @note The iterator does not need to be destroyed. */ yyjson_api_inline yyjson_arr_iter yyjson_arr_iter_with(yyjson_val *arr); /** Returns whether the iteration has more elements. If `iter` is NULL, this function will return false. */ yyjson_api_inline bool yyjson_arr_iter_has_next(yyjson_arr_iter *iter); /** Returns the next element in the iteration, or NULL on end. If `iter` is NULL, this function will return NULL. */ yyjson_api_inline yyjson_val *yyjson_arr_iter_next(yyjson_arr_iter *iter); /** Macro for iterating over an array. It works like iterator, but with a more intuitive API. @par Example @code size_t idx, max; yyjson_val *val; yyjson_arr_foreach(arr, idx, max, val) { your_func(idx, val); } @endcode */ #define yyjson_arr_foreach(arr, idx, max, val) \ for ((idx) = 0, \ (max) = yyjson_arr_size(arr), \ (val) = yyjson_arr_get_first(arr); \ (idx) < (max); \ (idx)++, \ (val) = unsafe_yyjson_get_next(val)) /*============================================================================== * JSON Object API *============================================================================*/ /** Returns the number of key-value pairs in this object. Returns 0 if `obj` is NULL or type is not object. */ yyjson_api_inline size_t yyjson_obj_size(yyjson_val *obj); /** Returns the value to which the specified key is mapped. Returns NULL if this object contains no mapping for the key. Returns NULL if `obj/key` is NULL, or type is not object. The `key` should be a null-terminated UTF-8 string. @warning This function takes a linear search time. */ yyjson_api_inline yyjson_val *yyjson_obj_get(yyjson_val *obj, const char *key); /** Returns the value to which the specified key is mapped. Returns NULL if this object contains no mapping for the key. Returns NULL if `obj/key` is NULL, or type is not object. The `key` should be a UTF-8 string, null-terminator is not required. The `key_len` should be the length of the key, in bytes. @warning This function takes a linear search time. */ yyjson_api_inline yyjson_val *yyjson_obj_getn(yyjson_val *obj, const char *key, size_t key_len); /*============================================================================== * JSON Object Iterator API *============================================================================*/ /** A JSON object iterator. @par Example @code yyjson_val *key, *val; yyjson_obj_iter iter = yyjson_obj_iter_with(obj); while ((key = yyjson_obj_iter_next(&iter))) { val = yyjson_obj_iter_get_val(key); your_func(key, val); } @endcode If the ordering of the keys is known at compile-time, you can use this method to speed up value lookups: @code // {"k1":1, "k2": 3, "k3": 3} yyjson_val *key, *val; yyjson_obj_iter iter = yyjson_obj_iter_with(obj); yyjson_val *v1 = yyjson_obj_iter_get(&iter, "k1"); yyjson_val *v3 = yyjson_obj_iter_get(&iter, "k3"); @endcode @see yyjson_obj_iter_get() and yyjson_obj_iter_getn() */ typedef struct yyjson_obj_iter { size_t idx; /**< next key's index */ size_t max; /**< maximum key index (obj.size) */ yyjson_val *cur; /**< next key */ yyjson_val *obj; /**< the object being iterated */ } yyjson_obj_iter; /** Initialize an iterator for this object. @param obj The object to be iterated over. If this parameter is NULL or not an object, `iter` will be set to empty. @param iter The iterator to be initialized. If this parameter is NULL, the function will fail and return false. @return true if the `iter` has been successfully initialized. @note The iterator does not need to be destroyed. */ yyjson_api_inline bool yyjson_obj_iter_init(yyjson_val *obj, yyjson_obj_iter *iter); /** Create an iterator with an object, same as `yyjson_obj_iter_init()`. @param obj The object to be iterated over. If this parameter is NULL or not an object, an empty iterator will returned. @return A new iterator for the object. @note The iterator does not need to be destroyed. */ yyjson_api_inline yyjson_obj_iter yyjson_obj_iter_with(yyjson_val *obj); /** Returns whether the iteration has more elements. If `iter` is NULL, this function will return false. */ yyjson_api_inline bool yyjson_obj_iter_has_next(yyjson_obj_iter *iter); /** Returns the next key in the iteration, or NULL on end. If `iter` is NULL, this function will return NULL. */ yyjson_api_inline yyjson_val *yyjson_obj_iter_next(yyjson_obj_iter *iter); /** Returns the value for key inside the iteration. If `iter` is NULL, this function will return NULL. */ yyjson_api_inline yyjson_val *yyjson_obj_iter_get_val(yyjson_val *key); /** Iterates to a specified key and returns the value. This function does the same thing as `yyjson_obj_get()`, but is much faster if the ordering of the keys is known at compile-time and you are using the same order to look up the values. If the key exists in this object, then the iterator will stop at the next key, otherwise the iterator will not change and NULL is returned. @param iter The object iterator, should not be NULL. @param key The key, should be a UTF-8 string with null-terminator. @return The value to which the specified key is mapped. NULL if this object contains no mapping for the key or input is invalid. @warning This function takes a linear search time if the key is not nearby. */ yyjson_api_inline yyjson_val *yyjson_obj_iter_get(yyjson_obj_iter *iter, const char *key); /** Iterates to a specified key and returns the value. This function does the same thing as `yyjson_obj_getn()`, but is much faster if the ordering of the keys is known at compile-time and you are using the same order to look up the values. If the key exists in this object, then the iterator will stop at the next key, otherwise the iterator will not change and NULL is returned. @param iter The object iterator, should not be NULL. @param key The key, should be a UTF-8 string, null-terminator is not required. @param key_len The the length of `key`, in bytes. @return The value to which the specified key is mapped. NULL if this object contains no mapping for the key or input is invalid. @warning This function takes a linear search time if the key is not nearby. */ yyjson_api_inline yyjson_val *yyjson_obj_iter_getn(yyjson_obj_iter *iter, const char *key, size_t key_len); /** Macro for iterating over an object. It works like iterator, but with a more intuitive API. @par Example @code size_t idx, max; yyjson_val *key, *val; yyjson_obj_foreach(obj, idx, max, key, val) { your_func(key, val); } @endcode */ #define yyjson_obj_foreach(obj, idx, max, key, val) \ for ((idx) = 0, \ (max) = yyjson_obj_size(obj), \ (key) = (obj) ? unsafe_yyjson_get_first(obj) : NULL, \ (val) = (key) + 1; \ (idx) < (max); \ (idx)++, \ (key) = unsafe_yyjson_get_next(val), \ (val) = (key) + 1) /*============================================================================== * Mutable JSON Document API *============================================================================*/ /** Returns the root value of this JSON document. Returns NULL if `doc` is NULL. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_get_root(yyjson_mut_doc *doc); /** Sets the root value of this JSON document. Pass NULL to clear root value of the document. */ yyjson_api_inline void yyjson_mut_doc_set_root(yyjson_mut_doc *doc, yyjson_mut_val *root); /** Set the string pool size for a mutable document. This function does not allocate memory immediately, but uses the size when the next memory allocation is needed. If the caller knows the approximate bytes of strings that the document needs to store (e.g. copy string with `yyjson_mut_strcpy` function), setting a larger size can avoid multiple memory allocations and improve performance. @param doc The mutable document. @param len The desired string pool size in bytes (total string length). @return true if successful, false if size is 0 or overflow. */ yyjson_api bool yyjson_mut_doc_set_str_pool_size(yyjson_mut_doc *doc, size_t len); /** Set the value pool size for a mutable document. This function does not allocate memory immediately, but uses the size when the next memory allocation is needed. If the caller knows the approximate number of values that the document needs to store (e.g. create new value with `yyjson_mut_xxx` functions), setting a larger size can avoid multiple memory allocations and improve performance. @param doc The mutable document. @param count The desired value pool size (number of `yyjson_mut_val`). @return true if successful, false if size is 0 or overflow. */ yyjson_api bool yyjson_mut_doc_set_val_pool_size(yyjson_mut_doc *doc, size_t count); /** Release the JSON document and free the memory. After calling this function, the `doc` and all values from the `doc` are no longer available. This function will do nothing if the `doc` is NULL. */ yyjson_api void yyjson_mut_doc_free(yyjson_mut_doc *doc); /** Creates and returns a new mutable JSON document, returns NULL on error. If allocator is NULL, the default allocator will be used. */ yyjson_api yyjson_mut_doc *yyjson_mut_doc_new(const yyjson_alc *alc); /** Copies and returns a new mutable document from input, returns NULL on error. This makes a `deep-copy` on the immutable document. If allocator is NULL, the default allocator will be used. @note `imut_doc` -> `mut_doc`. */ yyjson_api yyjson_mut_doc *yyjson_doc_mut_copy(yyjson_doc *doc, const yyjson_alc *alc); /** Copies and returns a new mutable document from input, returns NULL on error. This makes a `deep-copy` on the mutable document. If allocator is NULL, the default allocator will be used. @note `mut_doc` -> `mut_doc`. */ yyjson_api yyjson_mut_doc *yyjson_mut_doc_mut_copy(yyjson_mut_doc *doc, const yyjson_alc *alc); /** Copies and returns a new mutable value from input, returns NULL on error. This makes a `deep-copy` on the immutable value. The memory was managed by mutable document. @note `imut_val` -> `mut_val`. */ yyjson_api yyjson_mut_val *yyjson_val_mut_copy(yyjson_mut_doc *doc, yyjson_val *val); /** Copies and returns a new mutable value from input, returns NULL on error. This makes a `deep-copy` on the mutable value. The memory was managed by mutable document. @note `mut_val` -> `mut_val`. @warning This function is recursive and may cause a stack overflow if the object level is too deep. */ yyjson_api yyjson_mut_val *yyjson_mut_val_mut_copy(yyjson_mut_doc *doc, yyjson_mut_val *val); /** Copies and returns a new immutable document from input, returns NULL on error. This makes a `deep-copy` on the mutable document. The returned document should be freed with `yyjson_doc_free()`. @note `mut_doc` -> `imut_doc`. @warning This function is recursive and may cause a stack overflow if the object level is too deep. */ yyjson_api yyjson_doc *yyjson_mut_doc_imut_copy(yyjson_mut_doc *doc, const yyjson_alc *alc); /** Copies and returns a new immutable document from input, returns NULL on error. This makes a `deep-copy` on the mutable value. The returned document should be freed with `yyjson_doc_free()`. @note `mut_val` -> `imut_doc`. @warning This function is recursive and may cause a stack overflow if the object level is too deep. */ yyjson_api yyjson_doc *yyjson_mut_val_imut_copy(yyjson_mut_val *val, const yyjson_alc *alc); /*============================================================================== * Mutable JSON Value Type API *============================================================================*/ /** Returns whether the JSON value is raw. Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_mut_is_raw(yyjson_mut_val *val); /** Returns whether the JSON value is `null`. Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_mut_is_null(yyjson_mut_val *val); /** Returns whether the JSON value is `true`. Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_mut_is_true(yyjson_mut_val *val); /** Returns whether the JSON value is `false`. Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_mut_is_false(yyjson_mut_val *val); /** Returns whether the JSON value is bool (true/false). Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_mut_is_bool(yyjson_mut_val *val); /** Returns whether the JSON value is unsigned integer (uint64_t). Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_mut_is_uint(yyjson_mut_val *val); /** Returns whether the JSON value is signed integer (int64_t). Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_mut_is_sint(yyjson_mut_val *val); /** Returns whether the JSON value is integer (uint64_t/int64_t). Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_mut_is_int(yyjson_mut_val *val); /** Returns whether the JSON value is real number (double). Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_mut_is_real(yyjson_mut_val *val); /** Returns whether the JSON value is number (uint/sint/real). Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_mut_is_num(yyjson_mut_val *val); /** Returns whether the JSON value is string. Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_mut_is_str(yyjson_mut_val *val); /** Returns whether the JSON value is array. Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_mut_is_arr(yyjson_mut_val *val); /** Returns whether the JSON value is object. Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_mut_is_obj(yyjson_mut_val *val); /** Returns whether the JSON value is container (array/object). Returns false if `val` is NULL. */ yyjson_api_inline bool yyjson_mut_is_ctn(yyjson_mut_val *val); /*============================================================================== * Mutable JSON Value Content API *============================================================================*/ /** Returns the JSON value's type. Returns `YYJSON_TYPE_NONE` if `val` is NULL. */ yyjson_api_inline yyjson_type yyjson_mut_get_type(yyjson_mut_val *val); /** Returns the JSON value's subtype. Returns `YYJSON_SUBTYPE_NONE` if `val` is NULL. */ yyjson_api_inline yyjson_subtype yyjson_mut_get_subtype(yyjson_mut_val *val); /** Returns the JSON value's tag. Returns 0 if `val` is NULL. */ yyjson_api_inline uint8_t yyjson_mut_get_tag(yyjson_mut_val *val); /** Returns the JSON value's type description. The return value should be one of these strings: "raw", "null", "string", "array", "object", "true", "false", "uint", "sint", "real", "unknown". */ yyjson_api_inline const char *yyjson_mut_get_type_desc(yyjson_mut_val *val); /** Returns the content if the value is raw. Returns NULL if `val` is NULL or type is not raw. */ yyjson_api_inline const char *yyjson_mut_get_raw(yyjson_mut_val *val); /** Returns the content if the value is bool. Returns NULL if `val` is NULL or type is not bool. */ yyjson_api_inline bool yyjson_mut_get_bool(yyjson_mut_val *val); /** Returns the content and cast to uint64_t. Returns 0 if `val` is NULL or type is not integer(sint/uint). */ yyjson_api_inline uint64_t yyjson_mut_get_uint(yyjson_mut_val *val); /** Returns the content and cast to int64_t. Returns 0 if `val` is NULL or type is not integer(sint/uint). */ yyjson_api_inline int64_t yyjson_mut_get_sint(yyjson_mut_val *val); /** Returns the content and cast to int. Returns 0 if `val` is NULL or type is not integer(sint/uint). */ yyjson_api_inline int yyjson_mut_get_int(yyjson_mut_val *val); /** Returns the content if the value is real number. Returns 0.0 if `val` is NULL or type is not real(double). */ yyjson_api_inline double yyjson_mut_get_real(yyjson_mut_val *val); /** Returns the content and typecast to `double` if the value is number. Returns 0.0 if `val` is NULL or type is not number(uint/sint/real). */ yyjson_api_inline double yyjson_mut_get_num(yyjson_mut_val *val); /** Returns the content if the value is string. Returns NULL if `val` is NULL or type is not string. */ yyjson_api_inline const char *yyjson_mut_get_str(yyjson_mut_val *val); /** Returns the content length (string length, array size, object size. Returns 0 if `val` is NULL or type is not string/array/object. */ yyjson_api_inline size_t yyjson_mut_get_len(yyjson_mut_val *val); /** Returns whether the JSON value is equals to a string. The `str` should be a null-terminated UTF-8 string. Returns false if input is NULL or type is not string. */ yyjson_api_inline bool yyjson_mut_equals_str(yyjson_mut_val *val, const char *str); /** Returns whether the JSON value is equals to a string. The `str` should be a UTF-8 string, null-terminator is not required. Returns false if input is NULL or type is not string. */ yyjson_api_inline bool yyjson_mut_equals_strn(yyjson_mut_val *val, const char *str, size_t len); /** Returns whether two JSON values are equal (deep compare). Returns false if input is NULL. @note the result may be inaccurate if object has duplicate keys. @warning This function is recursive and may cause a stack overflow if the object level is too deep. */ yyjson_api_inline bool yyjson_mut_equals(yyjson_mut_val *lhs, yyjson_mut_val *rhs); /** Set the value to raw. Returns false if input is NULL. @warning This function should not be used on an existing object or array. */ yyjson_api_inline bool yyjson_mut_set_raw(yyjson_mut_val *val, const char *raw, size_t len); /** Set the value to null. Returns false if input is NULL. @warning This function should not be used on an existing object or array. */ yyjson_api_inline bool yyjson_mut_set_null(yyjson_mut_val *val); /** Set the value to bool. Returns false if input is NULL. @warning This function should not be used on an existing object or array. */ yyjson_api_inline bool yyjson_mut_set_bool(yyjson_mut_val *val, bool num); /** Set the value to uint. Returns false if input is NULL. @warning This function should not be used on an existing object or array. */ yyjson_api_inline bool yyjson_mut_set_uint(yyjson_mut_val *val, uint64_t num); /** Set the value to sint. Returns false if input is NULL. @warning This function should not be used on an existing object or array. */ yyjson_api_inline bool yyjson_mut_set_sint(yyjson_mut_val *val, int64_t num); /** Set the value to int. Returns false if input is NULL. @warning This function should not be used on an existing object or array. */ yyjson_api_inline bool yyjson_mut_set_int(yyjson_mut_val *val, int num); /** Set the value to real. Returns false if input is NULL. @warning This function should not be used on an existing object or array. */ yyjson_api_inline bool yyjson_mut_set_real(yyjson_mut_val *val, double num); /** Set the value to string (null-terminated). Returns false if input is NULL. @warning This function should not be used on an existing object or array. */ yyjson_api_inline bool yyjson_mut_set_str(yyjson_mut_val *val, const char *str); /** Set the value to string (with length). Returns false if input is NULL. @warning This function should not be used on an existing object or array. */ yyjson_api_inline bool yyjson_mut_set_strn(yyjson_mut_val *val, const char *str, size_t len); /** Set the value to array. Returns false if input is NULL. @warning This function should not be used on an existing object or array. */ yyjson_api_inline bool yyjson_mut_set_arr(yyjson_mut_val *val); /** Set the value to array. Returns false if input is NULL. @warning This function should not be used on an existing object or array. */ yyjson_api_inline bool yyjson_mut_set_obj(yyjson_mut_val *val); /*============================================================================== * Mutable JSON Value Creation API *============================================================================*/ /** Creates and returns a raw value, returns NULL on error. The `str` should be a null-terminated UTF-8 string. @warning The input string is not copied, you should keep this string unmodified for the lifetime of this JSON document. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_raw(yyjson_mut_doc *doc, const char *str); /** Creates and returns a raw value, returns NULL on error. The `str` should be a UTF-8 string, null-terminator is not required. @warning The input string is not copied, you should keep this string unmodified for the lifetime of this JSON document. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_rawn(yyjson_mut_doc *doc, const char *str, size_t len); /** Creates and returns a raw value, returns NULL on error. The `str` should be a null-terminated UTF-8 string. The input string is copied and held by the document. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_rawcpy(yyjson_mut_doc *doc, const char *str); /** Creates and returns a raw value, returns NULL on error. The `str` should be a UTF-8 string, null-terminator is not required. The input string is copied and held by the document. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_rawncpy(yyjson_mut_doc *doc, const char *str, size_t len); /** Creates and returns a null value, returns NULL on error. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_null(yyjson_mut_doc *doc); /** Creates and returns a true value, returns NULL on error. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_true(yyjson_mut_doc *doc); /** Creates and returns a false value, returns NULL on error. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_false(yyjson_mut_doc *doc); /** Creates and returns a bool value, returns NULL on error. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_bool(yyjson_mut_doc *doc, bool val); /** Creates and returns an unsigned integer value, returns NULL on error. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_uint(yyjson_mut_doc *doc, uint64_t num); /** Creates and returns a signed integer value, returns NULL on error. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_sint(yyjson_mut_doc *doc, int64_t num); /** Creates and returns a signed integer value, returns NULL on error. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_int(yyjson_mut_doc *doc, int64_t num); /** Creates and returns an real number value, returns NULL on error. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_real(yyjson_mut_doc *doc, double num); /** Creates and returns a string value, returns NULL on error. The `str` should be a null-terminated UTF-8 string. @warning The input string is not copied, you should keep this string unmodified for the lifetime of this JSON document. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_str(yyjson_mut_doc *doc, const char *str); /** Creates and returns a string value, returns NULL on error. The `str` should be a UTF-8 string, null-terminator is not required. @warning The input string is not copied, you should keep this string unmodified for the lifetime of this JSON document. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_strn(yyjson_mut_doc *doc, const char *str, size_t len); /** Creates and returns a string value, returns NULL on error. The `str` should be a null-terminated UTF-8 string. The input string is copied and held by the document. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_strcpy(yyjson_mut_doc *doc, const char *str); /** Creates and returns a string value, returns NULL on error. The `str` should be a UTF-8 string, null-terminator is not required. The input string is copied and held by the document. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_strncpy(yyjson_mut_doc *doc, const char *str, size_t len); /*============================================================================== * Mutable JSON Array API *============================================================================*/ /** Returns the number of elements in this array. Returns 0 if `arr` is NULL or type is not array. */ yyjson_api_inline size_t yyjson_mut_arr_size(yyjson_mut_val *arr); /** Returns the element at the specified position in this array. Returns NULL if array is NULL/empty or the index is out of bounds. @warning This function takes a linear search time. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get(yyjson_mut_val *arr, size_t idx); /** Returns the first element of this array. Returns NULL if `arr` is NULL/empty or type is not array. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_first(yyjson_mut_val *arr); /** Returns the last element of this array. Returns NULL if `arr` is NULL/empty or type is not array. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_last(yyjson_mut_val *arr); /*============================================================================== * Mutable JSON Array Iterator API *============================================================================*/ /** A mutable JSON array iterator. @warning You should not modify the array while iterating over it, but you can use `yyjson_mut_arr_iter_remove()` to remove current value. @par Example @code yyjson_mut_val *val; yyjson_mut_arr_iter iter = yyjson_mut_arr_iter_with(arr); while ((val = yyjson_mut_arr_iter_next(&iter))) { your_func(val); if (your_val_is_unused(val)) { yyjson_mut_arr_iter_remove(&iter); } } @endcode */ typedef struct yyjson_mut_arr_iter { size_t idx; /**< next value's index */ size_t max; /**< maximum index (arr.size) */ yyjson_mut_val *cur; /**< current value */ yyjson_mut_val *pre; /**< previous value */ yyjson_mut_val *arr; /**< the array being iterated */ } yyjson_mut_arr_iter; /** Initialize an iterator for this array. @param arr The array to be iterated over. If this parameter is NULL or not an array, `iter` will be set to empty. @param iter The iterator to be initialized. If this parameter is NULL, the function will fail and return false. @return true if the `iter` has been successfully initialized. @note The iterator does not need to be destroyed. */ yyjson_api_inline bool yyjson_mut_arr_iter_init(yyjson_mut_val *arr, yyjson_mut_arr_iter *iter); /** Create an iterator with an array , same as `yyjson_mut_arr_iter_init()`. @param arr The array to be iterated over. If this parameter is NULL or not an array, an empty iterator will returned. @return A new iterator for the array. @note The iterator does not need to be destroyed. */ yyjson_api_inline yyjson_mut_arr_iter yyjson_mut_arr_iter_with( yyjson_mut_val *arr); /** Returns whether the iteration has more elements. If `iter` is NULL, this function will return false. */ yyjson_api_inline bool yyjson_mut_arr_iter_has_next( yyjson_mut_arr_iter *iter); /** Returns the next element in the iteration, or NULL on end. If `iter` is NULL, this function will return NULL. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_next( yyjson_mut_arr_iter *iter); /** Removes and returns current element in the iteration. If `iter` is NULL, this function will return NULL. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_remove( yyjson_mut_arr_iter *iter); /** Macro for iterating over an array. It works like iterator, but with a more intuitive API. @warning You should not modify the array while iterating over it. @par Example @code size_t idx, max; yyjson_mut_val *val; yyjson_mut_arr_foreach(arr, idx, max, val) { your_func(idx, val); } @endcode */ #define yyjson_mut_arr_foreach(arr, idx, max, val) \ for ((idx) = 0, \ (max) = yyjson_mut_arr_size(arr), \ (val) = yyjson_mut_arr_get_first(arr); \ (idx) < (max); \ (idx)++, \ (val) = (val)->next) /*============================================================================== * Mutable JSON Array Creation API *============================================================================*/ /** Creates and returns an empty mutable array. @param doc A mutable document, used for memory allocation only. @return The new array. NULL if input is NULL or memory allocation failed. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr(yyjson_mut_doc *doc); /** Creates and returns a new mutable array with the given boolean values. @param doc A mutable document, used for memory allocation only. If this parameter is NULL, the function will fail and return NULL. @param vals A C array of boolean values. @param count The value count. If this value is 0, an empty array will return. @return The new array. NULL if input is invalid or memory allocation failed. @par Example @code const bool vals[3] = { true, false, true }; yyjson_mut_val *arr = yyjson_mut_arr_with_bool(doc, vals, 3); @endcode */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_bool( yyjson_mut_doc *doc, const bool *vals, size_t count); /** Creates and returns a new mutable array with the given sint numbers. @param doc A mutable document, used for memory allocation only. If this parameter is NULL, the function will fail and return NULL. @param vals A C array of sint numbers. @param count The number count. If this value is 0, an empty array will return. @return The new array. NULL if input is invalid or memory allocation failed. @par Example @code const int64_t vals[3] = { -1, 0, 1 }; yyjson_mut_val *arr = yyjson_mut_arr_with_sint64(doc, vals, 3); @endcode */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint( yyjson_mut_doc *doc, const int64_t *vals, size_t count); /** Creates and returns a new mutable array with the given uint numbers. @param doc A mutable document, used for memory allocation only. If this parameter is NULL, the function will fail and return NULL. @param vals A C array of uint numbers. @param count The number count. If this value is 0, an empty array will return. @return The new array. NULL if input is invalid or memory allocation failed. @par Example @code const uint64_t vals[3] = { 0, 1, 0 }; yyjson_mut_val *arr = yyjson_mut_arr_with_uint(doc, vals, 3); @endcode */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint( yyjson_mut_doc *doc, const uint64_t *vals, size_t count); /** Creates and returns a new mutable array with the given real numbers. @param doc A mutable document, used for memory allocation only. If this parameter is NULL, the function will fail and return NULL. @param vals A C array of real numbers. @param count The number count. If this value is 0, an empty array will return. @return The new array. NULL if input is invalid or memory allocation failed. @par Example @code const double vals[3] = { 0.1, 0.2, 0.3 }; yyjson_mut_val *arr = yyjson_mut_arr_with_real(doc, vals, 3); @endcode */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_real( yyjson_mut_doc *doc, const double *vals, size_t count); /** Creates and returns a new mutable array with the given int8 numbers. @param doc A mutable document, used for memory allocation only. If this parameter is NULL, the function will fail and return NULL. @param vals A C array of int8 numbers. @param count The number count. If this value is 0, an empty array will return. @return The new array. NULL if input is invalid or memory allocation failed. @par Example @code const int8_t vals[3] = { -1, 0, 1 }; yyjson_mut_val *arr = yyjson_mut_arr_with_sint8(doc, vals, 3); @endcode */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint8( yyjson_mut_doc *doc, const int8_t *vals, size_t count); /** Creates and returns a new mutable array with the given int16 numbers. @param doc A mutable document, used for memory allocation only. If this parameter is NULL, the function will fail and return NULL. @param vals A C array of int16 numbers. @param count The number count. If this value is 0, an empty array will return. @return The new array. NULL if input is invalid or memory allocation failed. @par Example @code const int16_t vals[3] = { -1, 0, 1 }; yyjson_mut_val *arr = yyjson_mut_arr_with_sint16(doc, vals, 3); @endcode */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint16( yyjson_mut_doc *doc, const int16_t *vals, size_t count); /** Creates and returns a new mutable array with the given int32 numbers. @param doc A mutable document, used for memory allocation only. If this parameter is NULL, the function will fail and return NULL. @param vals A C array of int32 numbers. @param count The number count. If this value is 0, an empty array will return. @return The new array. NULL if input is invalid or memory allocation failed. @par Example @code const int32_t vals[3] = { -1, 0, 1 }; yyjson_mut_val *arr = yyjson_mut_arr_with_sint32(doc, vals, 3); @endcode */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint32( yyjson_mut_doc *doc, const int32_t *vals, size_t count); /** Creates and returns a new mutable array with the given int64 numbers. @param doc A mutable document, used for memory allocation only. If this parameter is NULL, the function will fail and return NULL. @param vals A C array of int64 numbers. @param count The number count. If this value is 0, an empty array will return. @return The new array. NULL if input is invalid or memory allocation failed. @par Example @code const int64_t vals[3] = { -1, 0, 1 }; yyjson_mut_val *arr = yyjson_mut_arr_with_sint64(doc, vals, 3); @endcode */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint64( yyjson_mut_doc *doc, const int64_t *vals, size_t count); /** Creates and returns a new mutable array with the given uint8 numbers. @param doc A mutable document, used for memory allocation only. If this parameter is NULL, the function will fail and return NULL. @param vals A C array of uint8 numbers. @param count The number count. If this value is 0, an empty array will return. @return The new array. NULL if input is invalid or memory allocation failed. @par Example @code const uint8_t vals[3] = { 0, 1, 0 }; yyjson_mut_val *arr = yyjson_mut_arr_with_uint8(doc, vals, 3); @endcode */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint8( yyjson_mut_doc *doc, const uint8_t *vals, size_t count); /** Creates and returns a new mutable array with the given uint16 numbers. @param doc A mutable document, used for memory allocation only. If this parameter is NULL, the function will fail and return NULL. @param vals A C array of uint16 numbers. @param count The number count. If this value is 0, an empty array will return. @return The new array. NULL if input is invalid or memory allocation failed. @par Example @code const uint16_t vals[3] = { 0, 1, 0 }; yyjson_mut_val *arr = yyjson_mut_arr_with_uint16(doc, vals, 3); @endcode */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint16( yyjson_mut_doc *doc, const uint16_t *vals, size_t count); /** Creates and returns a new mutable array with the given uint32 numbers. @param doc A mutable document, used for memory allocation only. If this parameter is NULL, the function will fail and return NULL. @param vals A C array of uint32 numbers. @param count The number count. If this value is 0, an empty array will return. @return The new array. NULL if input is invalid or memory allocation failed. @par Example @code const uint32_t vals[3] = { 0, 1, 0 }; yyjson_mut_val *arr = yyjson_mut_arr_with_uint32(doc, vals, 3); @endcode */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint32( yyjson_mut_doc *doc, const uint32_t *vals, size_t count); /** Creates and returns a new mutable array with the given uint64 numbers. @param doc A mutable document, used for memory allocation only. If this parameter is NULL, the function will fail and return NULL. @param vals A C array of uint64 numbers. @param count The number count. If this value is 0, an empty array will return. @return The new array. NULL if input is invalid or memory allocation failed. @par Example @code const uint64_t vals[3] = { 0, 1, 0 }; yyjson_mut_val *arr = yyjson_mut_arr_with_uint64(doc, vals, 3); @endcode */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint64( yyjson_mut_doc *doc, const uint64_t *vals, size_t count); /** Creates and returns a new mutable array with the given float numbers. @param doc A mutable document, used for memory allocation only. If this parameter is NULL, the function will fail and return NULL. @param vals A C array of float numbers. @param count The number count. If this value is 0, an empty array will return. @return The new array. NULL if input is invalid or memory allocation failed. @par Example @code const float vals[3] = { -1.0f, 0.0f, 1.0f }; yyjson_mut_val *arr = yyjson_mut_arr_with_float(doc, vals, 3); @endcode */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_float( yyjson_mut_doc *doc, const float *vals, size_t count); /** Creates and returns a new mutable array with the given double numbers. @param doc A mutable document, used for memory allocation only. If this parameter is NULL, the function will fail and return NULL. @param vals A C array of double numbers. @param count The number count. If this value is 0, an empty array will return. @return The new array. NULL if input is invalid or memory allocation failed. @par Example @code const double vals[3] = { -1.0, 0.0, 1.0 }; yyjson_mut_val *arr = yyjson_mut_arr_with_double(doc, vals, 3); @endcode */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_double( yyjson_mut_doc *doc, const double *vals, size_t count); /** Creates and returns a new mutable array with the given strings, these strings will not be copied. @param doc A mutable document, used for memory allocation only. If this parameter is NULL, the function will fail and return NULL. @param vals A C array of UTF-8 null-terminator strings. If this array contains NULL, the function will fail and return NULL. @param count The number of values in `vals`. If this value is 0, an empty array will return. @return The new array. NULL if input is invalid or memory allocation failed. @warning The input strings are not copied, you should keep these strings unmodified for the lifetime of this JSON document. If these strings will be modified, you should use `yyjson_mut_arr_with_strcpy()` instead. @par Example @code const char *vals[3] = { "a", "b", "c" }; yyjson_mut_val *arr = yyjson_mut_arr_with_str(doc, vals, 3); @endcode */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_str( yyjson_mut_doc *doc, const char **vals, size_t count); /** Creates and returns a new mutable array with the given strings and string lengths, these strings will not be copied. @param doc A mutable document, used for memory allocation only. If this parameter is NULL, the function will fail and return NULL. @param vals A C array of UTF-8 strings, null-terminator is not required. If this array contains NULL, the function will fail and return NULL. @param lens A C array of string lengths, in bytes. @param count The number of strings in `vals`. If this value is 0, an empty array will return. @return The new array. NULL if input is invalid or memory allocation failed. @warning The input strings are not copied, you should keep these strings unmodified for the lifetime of this JSON document. If these strings will be modified, you should use `yyjson_mut_arr_with_strncpy()` instead. @par Example @code const char *vals[3] = { "a", "bb", "c" }; const size_t lens[3] = { 1, 2, 1 }; yyjson_mut_val *arr = yyjson_mut_arr_with_strn(doc, vals, lens, 3); @endcode */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strn( yyjson_mut_doc *doc, const char **vals, const size_t *lens, size_t count); /** Creates and returns a new mutable array with the given strings, these strings will be copied. @param doc A mutable document, used for memory allocation only. If this parameter is NULL, the function will fail and return NULL. @param vals A C array of UTF-8 null-terminator strings. If this array contains NULL, the function will fail and return NULL. @param count The number of values in `vals`. If this value is 0, an empty array will return. @return The new array. NULL if input is invalid or memory allocation failed. @par Example @code const char *vals[3] = { "a", "b", "c" }; yyjson_mut_val *arr = yyjson_mut_arr_with_strcpy(doc, vals, 3); @endcode */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strcpy( yyjson_mut_doc *doc, const char **vals, size_t count); /** Creates and returns a new mutable array with the given strings and string lengths, these strings will be copied. @param doc A mutable document, used for memory allocation only. If this parameter is NULL, the function will fail and return NULL. @param vals A C array of UTF-8 strings, null-terminator is not required. If this array contains NULL, the function will fail and return NULL. @param lens A C array of string lengths, in bytes. @param count The number of strings in `vals`. If this value is 0, an empty array will return. @return The new array. NULL if input is invalid or memory allocation failed. @par Example @code const char *vals[3] = { "a", "bb", "c" }; const size_t lens[3] = { 1, 2, 1 }; yyjson_mut_val *arr = yyjson_mut_arr_with_strn(doc, vals, lens, 3); @endcode */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strncpy( yyjson_mut_doc *doc, const char **vals, const size_t *lens, size_t count); /*============================================================================== * Mutable JSON Array Modification API *============================================================================*/ /** Inserts a value into an array at a given index. @param arr The array to which the value is to be inserted. Returns false if it is NULL or not an array. @param val The value to be inserted. Returns false if it is NULL. @param idx The index to which to insert the new value. Returns false if the index is out of range. @return Whether successful. @warning This function takes a linear search time. */ yyjson_api_inline bool yyjson_mut_arr_insert(yyjson_mut_val *arr, yyjson_mut_val *val, size_t idx); /** Inserts a value at the end of the array. @param arr The array to which the value is to be inserted. Returns false if it is NULL or not an array. @param val The value to be inserted. Returns false if it is NULL. @return Whether successful. */ yyjson_api_inline bool yyjson_mut_arr_append(yyjson_mut_val *arr, yyjson_mut_val *val); /** Inserts a value at the head of the array. @param arr The array to which the value is to be inserted. Returns false if it is NULL or not an array. @param val The value to be inserted. Returns false if it is NULL. @return Whether successful. */ yyjson_api_inline bool yyjson_mut_arr_prepend(yyjson_mut_val *arr, yyjson_mut_val *val); /** Replaces a value at index and returns old value. @param arr The array to which the value is to be replaced. Returns false if it is NULL or not an array. @param idx The index to which to replace the value. Returns false if the index is out of range. @param val The new value to replace. Returns false if it is NULL. @return Old value, or NULL on error. @warning This function takes a linear search time. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_replace(yyjson_mut_val *arr, size_t idx, yyjson_mut_val *val); /** Removes and returns a value at index. @param arr The array from which the value is to be removed. Returns false if it is NULL or not an array. @param idx The index from which to remove the value. Returns false if the index is out of range. @return Old value, or NULL on error. @warning This function takes a linear search time. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove(yyjson_mut_val *arr, size_t idx); /** Removes and returns the first value in this array. @param arr The array from which the value is to be removed. Returns false if it is NULL or not an array. @return The first value, or NULL on error. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove_first( yyjson_mut_val *arr); /** Removes and returns the last value in this array. @param arr The array from which the value is to be removed. Returns false if it is NULL or not an array. @return The last value, or NULL on error. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove_last( yyjson_mut_val *arr); /** Removes all values within a specified range in the array. @param arr The array from which the value is to be removed. Returns false if it is NULL or not an array. @param idx The start index of the range (0 is the first). @param len The number of items in the range (can be 0). @return Whether successful. @warning This function takes a linear search time. */ yyjson_api_inline bool yyjson_mut_arr_remove_range(yyjson_mut_val *arr, size_t idx, size_t len); /** Removes all values in this array. @param arr The array from which all of the values are to be removed. Returns false if it is NULL or not an array. @return Whether successful. */ yyjson_api_inline bool yyjson_mut_arr_clear(yyjson_mut_val *arr); /** Rotates values in this array for the given number of times. For example: `[1,2,3,4,5]` rotate 2 is `[3,4,5,1,2]`. @param arr The array to be rotated. @param idx Index (or times) to rotate. @warning This function takes a linear search time. */ yyjson_api_inline bool yyjson_mut_arr_rotate(yyjson_mut_val *arr, size_t idx); /*============================================================================== * Mutable JSON Array Modification Convenience API *============================================================================*/ /** Adds a value at the end of the array. @param arr The array to which the value is to be inserted. Returns false if it is NULL or not an array. @param val The value to be inserted. Returns false if it is NULL. @return Whether successful. */ yyjson_api_inline bool yyjson_mut_arr_add_val(yyjson_mut_val *arr, yyjson_mut_val *val); /** Adds a `null` value at the end of the array. @param doc The `doc` is only used for memory allocation. @param arr The array to which the value is to be inserted. Returns false if it is NULL or not an array. @return Whether successful. */ yyjson_api_inline bool yyjson_mut_arr_add_null(yyjson_mut_doc *doc, yyjson_mut_val *arr); /** Adds a `true` value at the end of the array. @param doc The `doc` is only used for memory allocation. @param arr The array to which the value is to be inserted. Returns false if it is NULL or not an array. @return Whether successful. */ yyjson_api_inline bool yyjson_mut_arr_add_true(yyjson_mut_doc *doc, yyjson_mut_val *arr); /** Adds a `false` value at the end of the array. @param doc The `doc` is only used for memory allocation. @param arr The array to which the value is to be inserted. Returns false if it is NULL or not an array. @return Whether successful. */ yyjson_api_inline bool yyjson_mut_arr_add_false(yyjson_mut_doc *doc, yyjson_mut_val *arr); /** Adds a bool value at the end of the array. @param doc The `doc` is only used for memory allocation. @param arr The array to which the value is to be inserted. Returns false if it is NULL or not an array. @param val The bool value to be added. @return Whether successful. */ yyjson_api_inline bool yyjson_mut_arr_add_bool(yyjson_mut_doc *doc, yyjson_mut_val *arr, bool val); /** Adds an unsigned integer value at the end of the array. @param doc The `doc` is only used for memory allocation. @param arr The array to which the value is to be inserted. Returns false if it is NULL or not an array. @param num The number to be added. @return Whether successful. */ yyjson_api_inline bool yyjson_mut_arr_add_uint(yyjson_mut_doc *doc, yyjson_mut_val *arr, uint64_t num); /** Adds a signed integer value at the end of the array. @param doc The `doc` is only used for memory allocation. @param arr The array to which the value is to be inserted. Returns false if it is NULL or not an array. @param num The number to be added. @return Whether successful. */ yyjson_api_inline bool yyjson_mut_arr_add_sint(yyjson_mut_doc *doc, yyjson_mut_val *arr, int64_t num); /** Adds a integer value at the end of the array. @param doc The `doc` is only used for memory allocation. @param arr The array to which the value is to be inserted. Returns false if it is NULL or not an array. @param num The number to be added. @return Whether successful. */ yyjson_api_inline bool yyjson_mut_arr_add_int(yyjson_mut_doc *doc, yyjson_mut_val *arr, int64_t num); /** Adds a double value at the end of the array. @param doc The `doc` is only used for memory allocation. @param arr The array to which the value is to be inserted. Returns false if it is NULL or not an array. @param num The number to be added. @return Whether successful. */ yyjson_api_inline bool yyjson_mut_arr_add_real(yyjson_mut_doc *doc, yyjson_mut_val *arr, double num); /** Adds a string value at the end of the array (no copy). @param doc The `doc` is only used for memory allocation. @param arr The array to which the value is to be inserted. Returns false if it is NULL or not an array. @param str A null-terminated UTF-8 string. @return Whether successful. @warning The input string is not copied, you should keep this string unmodified for the lifetime of this JSON document. */ yyjson_api_inline bool yyjson_mut_arr_add_str(yyjson_mut_doc *doc, yyjson_mut_val *arr, const char *str); /** Adds a string value at the end of the array (no copy). @param doc The `doc` is only used for memory allocation. @param arr The array to which the value is to be inserted. Returns false if it is NULL or not an array. @param str A UTF-8 string, null-terminator is not required. @param len The length of the string, in bytes. @return Whether successful. @warning The input string is not copied, you should keep this string unmodified for the lifetime of this JSON document. */ yyjson_api_inline bool yyjson_mut_arr_add_strn(yyjson_mut_doc *doc, yyjson_mut_val *arr, const char *str, size_t len); /** Adds a string value at the end of the array (copied). @param doc The `doc` is only used for memory allocation. @param arr The array to which the value is to be inserted. Returns false if it is NULL or not an array. @param str A null-terminated UTF-8 string. @return Whether successful. */ yyjson_api_inline bool yyjson_mut_arr_add_strcpy(yyjson_mut_doc *doc, yyjson_mut_val *arr, const char *str); /** Adds a string value at the end of the array (copied). @param doc The `doc` is only used for memory allocation. @param arr The array to which the value is to be inserted. Returns false if it is NULL or not an array. @param str A UTF-8 string, null-terminator is not required. @param len The length of the string, in bytes. @return Whether successful. */ yyjson_api_inline bool yyjson_mut_arr_add_strncpy(yyjson_mut_doc *doc, yyjson_mut_val *arr, const char *str, size_t len); /** Creates and adds a new array at the end of the array. @param doc The `doc` is only used for memory allocation. @param arr The array to which the value is to be inserted. Returns false if it is NULL or not an array. @return The new array, or NULL on error. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_add_arr(yyjson_mut_doc *doc, yyjson_mut_val *arr); /** Creates and adds a new object at the end of the array. @param doc The `doc` is only used for memory allocation. @param arr The array to which the value is to be inserted. Returns false if it is NULL or not an array. @return The new object, or NULL on error. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_add_obj(yyjson_mut_doc *doc, yyjson_mut_val *arr); /*============================================================================== * Mutable JSON Object API *============================================================================*/ /** Returns the number of key-value pairs in this object. Returns 0 if `obj` is NULL or type is not object. */ yyjson_api_inline size_t yyjson_mut_obj_size(yyjson_mut_val *obj); /** Returns the value to which the specified key is mapped. Returns NULL if this object contains no mapping for the key. Returns NULL if `obj/key` is NULL, or type is not object. The `key` should be a null-terminated UTF-8 string. @warning This function takes a linear search time. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_get(yyjson_mut_val *obj, const char *key); /** Returns the value to which the specified key is mapped. Returns NULL if this object contains no mapping for the key. Returns NULL if `obj/key` is NULL, or type is not object. The `key` should be a UTF-8 string, null-terminator is not required. The `key_len` should be the length of the key, in bytes. @warning This function takes a linear search time. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_getn(yyjson_mut_val *obj, const char *key, size_t key_len); /*============================================================================== * Mutable JSON Object Iterator API *============================================================================*/ /** A mutable JSON object iterator. @warning You should not modify the object while iterating over it, but you can use `yyjson_mut_obj_iter_remove()` to remove current value. @par Example @code yyjson_mut_val *key, *val; yyjson_mut_obj_iter iter = yyjson_mut_obj_iter_with(obj); while ((key = yyjson_mut_obj_iter_next(&iter))) { val = yyjson_mut_obj_iter_get_val(key); your_func(key, val); if (your_val_is_unused(key, val)) { yyjson_mut_obj_iter_remove(&iter); } } @endcode If the ordering of the keys is known at compile-time, you can use this method to speed up value lookups: @code // {"k1":1, "k2": 3, "k3": 3} yyjson_mut_val *key, *val; yyjson_mut_obj_iter iter = yyjson_mut_obj_iter_with(obj); yyjson_mut_val *v1 = yyjson_mut_obj_iter_get(&iter, "k1"); yyjson_mut_val *v3 = yyjson_mut_obj_iter_get(&iter, "k3"); @endcode @see `yyjson_mut_obj_iter_get()` and `yyjson_mut_obj_iter_getn()` */ typedef struct yyjson_mut_obj_iter { size_t idx; /**< next key's index */ size_t max; /**< maximum key index (obj.size) */ yyjson_mut_val *cur; /**< current key */ yyjson_mut_val *pre; /**< previous key */ yyjson_mut_val *obj; /**< the object being iterated */ } yyjson_mut_obj_iter; /** Initialize an iterator for this object. @param obj The object to be iterated over. If this parameter is NULL or not an array, `iter` will be set to empty. @param iter The iterator to be initialized. If this parameter is NULL, the function will fail and return false. @return true if the `iter` has been successfully initialized. @note The iterator does not need to be destroyed. */ yyjson_api_inline bool yyjson_mut_obj_iter_init(yyjson_mut_val *obj, yyjson_mut_obj_iter *iter); /** Create an iterator with an object, same as `yyjson_obj_iter_init()`. @param obj The object to be iterated over. If this parameter is NULL or not an object, an empty iterator will returned. @return A new iterator for the object. @note The iterator does not need to be destroyed. */ yyjson_api_inline yyjson_mut_obj_iter yyjson_mut_obj_iter_with( yyjson_mut_val *obj); /** Returns whether the iteration has more elements. If `iter` is NULL, this function will return false. */ yyjson_api_inline bool yyjson_mut_obj_iter_has_next( yyjson_mut_obj_iter *iter); /** Returns the next key in the iteration, or NULL on end. If `iter` is NULL, this function will return NULL. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_next( yyjson_mut_obj_iter *iter); /** Returns the value for key inside the iteration. If `iter` is NULL, this function will return NULL. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_get_val( yyjson_mut_val *key); /** Removes current key-value pair in the iteration, returns the removed value. If `iter` is NULL, this function will return NULL. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_remove( yyjson_mut_obj_iter *iter); /** Iterates to a specified key and returns the value. This function does the same thing as `yyjson_mut_obj_get()`, but is much faster if the ordering of the keys is known at compile-time and you are using the same order to look up the values. If the key exists in this object, then the iterator will stop at the next key, otherwise the iterator will not change and NULL is returned. @param iter The object iterator, should not be NULL. @param key The key, should be a UTF-8 string with null-terminator. @return The value to which the specified key is mapped. NULL if this object contains no mapping for the key or input is invalid. @warning This function takes a linear search time if the key is not nearby. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_get( yyjson_mut_obj_iter *iter, const char *key); /** Iterates to a specified key and returns the value. This function does the same thing as `yyjson_mut_obj_getn()` but is much faster if the ordering of the keys is known at compile-time and you are using the same order to look up the values. If the key exists in this object, then the iterator will stop at the next key, otherwise the iterator will not change and NULL is returned. @param iter The object iterator, should not be NULL. @param key The key, should be a UTF-8 string, null-terminator is not required. @param key_len The the length of `key`, in bytes. @return The value to which the specified key is mapped. NULL if this object contains no mapping for the key or input is invalid. @warning This function takes a linear search time if the key is not nearby. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_getn( yyjson_mut_obj_iter *iter, const char *key, size_t key_len); /** Macro for iterating over an object. It works like iterator, but with a more intuitive API. @warning You should not modify the object while iterating over it. @par Example @code size_t idx, max; yyjson_val *key, *val; yyjson_obj_foreach(obj, idx, max, key, val) { your_func(key, val); } @endcode */ #define yyjson_mut_obj_foreach(obj, idx, max, key, val) \ for ((idx) = 0, \ (max) = yyjson_mut_obj_size(obj), \ (key) = (max) ? ((yyjson_mut_val *)(obj)->uni.ptr)->next->next : NULL, \ (val) = (key) ? (key)->next : NULL; \ (idx) < (max); \ (idx)++, \ (key) = (val)->next, \ (val) = (key)->next) /*============================================================================== * Mutable JSON Object Creation API *============================================================================*/ /** Creates and returns a mutable object, returns NULL on error. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj(yyjson_mut_doc *doc); /** Creates and returns a mutable object with keys and values, returns NULL on error. The keys and values are not copied. The strings should be a null-terminated UTF-8 string. @warning The input string is not copied, you should keep this string unmodified for the lifetime of this JSON document. @par Example @code const char *keys[2] = { "id", "name" }; const char *vals[2] = { "01", "Harry" }; yyjson_mut_val *obj = yyjson_mut_obj_with_str(doc, keys, vals, 2); @endcode */ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_str(yyjson_mut_doc *doc, const char **keys, const char **vals, size_t count); /** Creates and returns a mutable object with key-value pairs and pair count, returns NULL on error. The keys and values are not copied. The strings should be a null-terminated UTF-8 string. @warning The input string is not copied, you should keep this string unmodified for the lifetime of this JSON document. @par Example @code const char *kv_pairs[4] = { "id", "01", "name", "Harry" }; yyjson_mut_val *obj = yyjson_mut_obj_with_kv(doc, kv_pairs, 2); @endcode */ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_kv(yyjson_mut_doc *doc, const char **kv_pairs, size_t pair_count); /*============================================================================== * Mutable JSON Object Modification API *============================================================================*/ /** Adds a key-value pair at the end of the object. This function allows duplicated key in one object. @param obj The object to which the new key-value pair is to be added. @param key The key, should be a string which is created by `yyjson_mut_str()`, `yyjson_mut_strn()`, `yyjson_mut_strcpy()` or `yyjson_mut_strncpy()`. @param val The value to add to the object. @return Whether successful. */ yyjson_api_inline bool yyjson_mut_obj_add(yyjson_mut_val *obj, yyjson_mut_val *key, yyjson_mut_val *val); /** Sets a key-value pair at the end of the object. This function may remove all key-value pairs for the given key before add. @param obj The object to which the new key-value pair is to be added. @param key The key, should be a string which is created by `yyjson_mut_str()`, `yyjson_mut_strn()`, `yyjson_mut_strcpy()` or `yyjson_mut_strncpy()`. @param val The value to add to the object. If this value is null, the behavior is same as `yyjson_mut_obj_remove()`. @return Whether successful. */ yyjson_api_inline bool yyjson_mut_obj_put(yyjson_mut_val *obj, yyjson_mut_val *key, yyjson_mut_val *val); /** Inserts a key-value pair to the object at the given position. This function allows duplicated key in one object. @param obj The object to which the new key-value pair is to be added. @param key The key, should be a string which is created by `yyjson_mut_str()`, `yyjson_mut_strn()`, `yyjson_mut_strcpy()` or `yyjson_mut_strncpy()`. @param val The value to add to the object. @param idx The index to which to insert the new pair. @return Whether successful. */ yyjson_api_inline bool yyjson_mut_obj_insert(yyjson_mut_val *obj, yyjson_mut_val *key, yyjson_mut_val *val, size_t idx); /** Removes all key-value pair from the object with given key. @param obj The object from which the key-value pair is to be removed. @param key The key, should be a string value. @return The first matched value, or NULL if no matched value. @warning This function takes a linear search time. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove(yyjson_mut_val *obj, yyjson_mut_val *key); /** Removes all key-value pair from the object with given key. @param obj The object from which the key-value pair is to be removed. @param key The key, should be a UTF-8 string with null-terminator. @return The first matched value, or NULL if no matched value. @warning This function takes a linear search time. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_key( yyjson_mut_val *obj, const char *key); /** Removes all key-value pair from the object with given key. @param obj The object from which the key-value pair is to be removed. @param key The key, should be a UTF-8 string, null-terminator is not required. @param key_len The length of the key. @return The first matched value, or NULL if no matched value. @warning This function takes a linear search time. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_keyn( yyjson_mut_val *obj, const char *key, size_t key_len); /** Removes all key-value pairs in this object. @param obj The object from which all of the values are to be removed. @return Whether successful. */ yyjson_api_inline bool yyjson_mut_obj_clear(yyjson_mut_val *obj); /** Replaces value from the object with given key. If the key is not exist, or the value is NULL, it will fail. @param obj The object to which the value is to be replaced. @param key The key, should be a string value. @param val The value to replace into the object. @return Whether successful. @warning This function takes a linear search time. */ yyjson_api_inline bool yyjson_mut_obj_replace(yyjson_mut_val *obj, yyjson_mut_val *key, yyjson_mut_val *val); /** Rotates key-value pairs in the object for the given number of times. For example: `{"a":1,"b":2,"c":3,"d":4}` rotate 1 is `{"b":2,"c":3,"d":4,"a":1}`. @param obj The object to be rotated. @param idx Index (or times) to rotate. @return Whether successful. @warning This function takes a linear search time. */ yyjson_api_inline bool yyjson_mut_obj_rotate(yyjson_mut_val *obj, size_t idx); /*============================================================================== * Mutable JSON Object Modification Convenience API *============================================================================*/ /** Adds a `null` value at the end of the object. The `key` should be a null-terminated UTF-8 string. This function allows duplicated key in one object. @warning The key string is not copied, you should keep the string unmodified for the lifetime of this JSON document. */ yyjson_api_inline bool yyjson_mut_obj_add_null(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key); /** Adds a `true` value at the end of the object. The `key` should be a null-terminated UTF-8 string. This function allows duplicated key in one object. @warning The key string is not copied, you should keep the string unmodified for the lifetime of this JSON document. */ yyjson_api_inline bool yyjson_mut_obj_add_true(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key); /** Adds a `false` value at the end of the object. The `key` should be a null-terminated UTF-8 string. This function allows duplicated key in one object. @warning The key string is not copied, you should keep the string unmodified for the lifetime of this JSON document. */ yyjson_api_inline bool yyjson_mut_obj_add_false(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key); /** Adds a bool value at the end of the object. The `key` should be a null-terminated UTF-8 string. This function allows duplicated key in one object. @warning The key string is not copied, you should keep the string unmodified for the lifetime of this JSON document. */ yyjson_api_inline bool yyjson_mut_obj_add_bool(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, bool val); /** Adds an unsigned integer value at the end of the object. The `key` should be a null-terminated UTF-8 string. This function allows duplicated key in one object. @warning The key string is not copied, you should keep the string unmodified for the lifetime of this JSON document. */ yyjson_api_inline bool yyjson_mut_obj_add_uint(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, uint64_t val); /** Adds a signed integer value at the end of the object. The `key` should be a null-terminated UTF-8 string. This function allows duplicated key in one object. @warning The key string is not copied, you should keep the string unmodified for the lifetime of this JSON document. */ yyjson_api_inline bool yyjson_mut_obj_add_sint(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, int64_t val); /** Adds an int value at the end of the object. The `key` should be a null-terminated UTF-8 string. This function allows duplicated key in one object. @warning The key string is not copied, you should keep the string unmodified for the lifetime of this JSON document. */ yyjson_api_inline bool yyjson_mut_obj_add_int(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, int64_t val); /** Adds a double value at the end of the object. The `key` should be a null-terminated UTF-8 string. This function allows duplicated key in one object. @warning The key string is not copied, you should keep the string unmodified for the lifetime of this JSON document. */ yyjson_api_inline bool yyjson_mut_obj_add_real(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, double val); /** Adds a string value at the end of the object. The `key` and `val` should be null-terminated UTF-8 strings. This function allows duplicated key in one object. @warning The key/value strings are not copied, you should keep these strings unmodified for the lifetime of this JSON document. */ yyjson_api_inline bool yyjson_mut_obj_add_str(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, const char *val); /** Adds a string value at the end of the object. The `key` should be a null-terminated UTF-8 string. The `val` should be a UTF-8 string, null-terminator is not required. The `len` should be the length of the `val`, in bytes. This function allows duplicated key in one object. @warning The key/value strings are not copied, you should keep these strings unmodified for the lifetime of this JSON document. */ yyjson_api_inline bool yyjson_mut_obj_add_strn(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, const char *val, size_t len); /** Adds a string value at the end of the object. The `key` and `val` should be null-terminated UTF-8 strings. The value string is copied. This function allows duplicated key in one object. @warning The key string is not copied, you should keep the string unmodified for the lifetime of this JSON document. */ yyjson_api_inline bool yyjson_mut_obj_add_strcpy(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, const char *val); /** Adds a string value at the end of the object. The `key` should be a null-terminated UTF-8 string. The `val` should be a UTF-8 string, null-terminator is not required. The `len` should be the length of the `val`, in bytes. This function allows duplicated key in one object. @warning The key strings are not copied, you should keep these strings unmodified for the lifetime of this JSON document. */ yyjson_api_inline bool yyjson_mut_obj_add_strncpy(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, const char *val, size_t len); /** Creates and adds a new array to the target object. The `key` should be a null-terminated UTF-8 string. This function allows duplicated key in one object. @warning The key string is not copied, you should keep these strings unmodified for the lifetime of this JSON document. @return The new array, or NULL on error. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_add_arr(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key); /** Creates and adds a new object to the target object. The `key` should be a null-terminated UTF-8 string. This function allows duplicated key in one object. @warning The key string is not copied, you should keep these strings unmodified for the lifetime of this JSON document. @return The new object, or NULL on error. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_add_obj(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key); /** Adds a JSON value at the end of the object. The `key` should be a null-terminated UTF-8 string. This function allows duplicated key in one object. @warning The key string is not copied, you should keep the string unmodified for the lifetime of this JSON document. */ yyjson_api_inline bool yyjson_mut_obj_add_val(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, yyjson_mut_val *val); /** Removes all key-value pairs for the given key. Returns the first value to which the specified key is mapped or NULL if this object contains no mapping for the key. The `key` should be a null-terminated UTF-8 string. @warning This function takes a linear search time. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_str( yyjson_mut_val *obj, const char *key); /** Removes all key-value pairs for the given key. Returns the first value to which the specified key is mapped or NULL if this object contains no mapping for the key. The `key` should be a UTF-8 string, null-terminator is not required. The `len` should be the length of the key, in bytes. @warning This function takes a linear search time. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_strn( yyjson_mut_val *obj, const char *key, size_t len); /** Replaces all matching keys with the new key. Returns true if at least one key was renamed. The `key` and `new_key` should be a null-terminated UTF-8 string. The `new_key` is copied and held by doc. @warning This function takes a linear search time. If `new_key` already exists, it will cause duplicate keys. */ yyjson_api_inline bool yyjson_mut_obj_rename_key(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, const char *new_key); /** Replaces all matching keys with the new key. Returns true if at least one key was renamed. The `key` and `new_key` should be a UTF-8 string, null-terminator is not required. The `new_key` is copied and held by doc. @warning This function takes a linear search time. If `new_key` already exists, it will cause duplicate keys. */ yyjson_api_inline bool yyjson_mut_obj_rename_keyn(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, size_t len, const char *new_key, size_t new_len); /*============================================================================== * JSON Pointer API (RFC 6901) * https://tools.ietf.org/html/rfc6901 *============================================================================*/ /** JSON Pointer error code. */ typedef uint32_t yyjson_ptr_code; /** No JSON pointer error. */ static const yyjson_ptr_code YYJSON_PTR_ERR_NONE = 0; /** Invalid input parameter, such as NULL input. */ static const yyjson_ptr_code YYJSON_PTR_ERR_PARAMETER = 1; /** JSON pointer syntax error, such as invalid escape, token no prefix. */ static const yyjson_ptr_code YYJSON_PTR_ERR_SYNTAX = 2; /** JSON pointer resolve failed, such as index out of range, key not found. */ static const yyjson_ptr_code YYJSON_PTR_ERR_RESOLVE = 3; /** Document's root is NULL, but it is required for the function call. */ static const yyjson_ptr_code YYJSON_PTR_ERR_NULL_ROOT = 4; /** Cannot set root as the target is not a document. */ static const yyjson_ptr_code YYJSON_PTR_ERR_SET_ROOT = 5; /** The memory allocation failed and a new value could not be created. */ static const yyjson_ptr_code YYJSON_PTR_ERR_MEMORY_ALLOCATION = 6; /** Error information for JSON pointer. */ typedef struct yyjson_ptr_err { /** Error code, see `yyjson_ptr_code` for all possible values. */ yyjson_ptr_code code; /** Error message, constant, no need to free (NULL if no error). */ const char *msg; /** Error byte position for input JSON pointer (0 if no error). */ size_t pos; } yyjson_ptr_err; /** A context for JSON pointer operation. This struct stores the context of JSON Pointer operation result. The struct can be used with three helper functions: `ctx_append()`, `ctx_replace()`, and `ctx_remove()`, which perform the corresponding operations on the container without re-parsing the JSON Pointer. For example: @code // doc before: {"a":[0,1,null]} // ptr: "/a/2" val = yyjson_mut_doc_ptr_getx(doc, ptr, strlen(ptr), &ctx, &err); if (yyjson_is_null(val)) { yyjson_ptr_ctx_remove(&ctx); } // doc after: {"a":[0,1]} @endcode */ typedef struct yyjson_ptr_ctx { /** The container (parent) of the target value. It can be either an array or an object. If the target location has no value, but all its parent containers exist, and the target location can be used to insert a new value, then `ctn` is the parent container of the target location. Otherwise, `ctn` is NULL. */ yyjson_mut_val *ctn; /** The previous sibling of the target value. It can be either a value in an array or a key in an object. As the container is a `circular linked list` of elements, `pre` is the previous node of the target value. If the operation is `add` or `set`, then `pre` is the previous node of the new value, not the original target value. If the target value does not exist, `pre` is NULL. */ yyjson_mut_val *pre; /** The removed value if the operation is `set`, `replace` or `remove`. It can be used to restore the original state of the document if needed. */ yyjson_mut_val *old; } yyjson_ptr_ctx; /** Get value by a JSON Pointer. @param doc The JSON document to be queried. @param ptr The JSON pointer string (UTF-8 with null-terminator). @return The value referenced by the JSON pointer. NULL if `doc` or `ptr` is NULL, or the JSON pointer cannot be resolved. */ yyjson_api_inline yyjson_val *yyjson_doc_ptr_get(yyjson_doc *doc, const char *ptr); /** Get value by a JSON Pointer. @param doc The JSON document to be queried. @param ptr The JSON pointer string (UTF-8, null-terminator is not required). @param len The length of `ptr` in bytes. @return The value referenced by the JSON pointer. NULL if `doc` or `ptr` is NULL, or the JSON pointer cannot be resolved. */ yyjson_api_inline yyjson_val *yyjson_doc_ptr_getn(yyjson_doc *doc, const char *ptr, size_t len); /** Get value by a JSON Pointer. @param doc The JSON document to be queried. @param ptr The JSON pointer string (UTF-8, null-terminator is not required). @param len The length of `ptr` in bytes. @param err A pointer to store the error information, or NULL if not needed. @return The value referenced by the JSON pointer. NULL if `doc` or `ptr` is NULL, or the JSON pointer cannot be resolved. */ yyjson_api_inline yyjson_val *yyjson_doc_ptr_getx(yyjson_doc *doc, const char *ptr, size_t len, yyjson_ptr_err *err); /** Get value by a JSON Pointer. @param val The JSON value to be queried. @param ptr The JSON pointer string (UTF-8 with null-terminator). @return The value referenced by the JSON pointer. NULL if `val` or `ptr` is NULL, or the JSON pointer cannot be resolved. */ yyjson_api_inline yyjson_val *yyjson_ptr_get(yyjson_val *val, const char *ptr); /** Get value by a JSON Pointer. @param val The JSON value to be queried. @param ptr The JSON pointer string (UTF-8, null-terminator is not required). @param len The length of `ptr` in bytes. @return The value referenced by the JSON pointer. NULL if `val` or `ptr` is NULL, or the JSON pointer cannot be resolved. */ yyjson_api_inline yyjson_val *yyjson_ptr_getn(yyjson_val *val, const char *ptr, size_t len); /** Get value by a JSON Pointer. @param val The JSON value to be queried. @param ptr The JSON pointer string (UTF-8, null-terminator is not required). @param len The length of `ptr` in bytes. @param err A pointer to store the error information, or NULL if not needed. @return The value referenced by the JSON pointer. NULL if `val` or `ptr` is NULL, or the JSON pointer cannot be resolved. */ yyjson_api_inline yyjson_val *yyjson_ptr_getx(yyjson_val *val, const char *ptr, size_t len, yyjson_ptr_err *err); /** Get value by a JSON Pointer. @param doc The JSON document to be queried. @param ptr The JSON pointer string (UTF-8 with null-terminator). @return The value referenced by the JSON pointer. NULL if `doc` or `ptr` is NULL, or the JSON pointer cannot be resolved. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_get(yyjson_mut_doc *doc, const char *ptr); /** Get value by a JSON Pointer. @param doc The JSON document to be queried. @param ptr The JSON pointer string (UTF-8, null-terminator is not required). @param len The length of `ptr` in bytes. @return The value referenced by the JSON pointer. NULL if `doc` or `ptr` is NULL, or the JSON pointer cannot be resolved. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_getn(yyjson_mut_doc *doc, const char *ptr, size_t len); /** Get value by a JSON Pointer. @param doc The JSON document to be queried. @param ptr The JSON pointer string (UTF-8, null-terminator is not required). @param len The length of `ptr` in bytes. @param ctx A pointer to store the result context, or NULL if not needed. @param err A pointer to store the error information, or NULL if not needed. @return The value referenced by the JSON pointer. NULL if `doc` or `ptr` is NULL, or the JSON pointer cannot be resolved. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_getx(yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); /** Get value by a JSON Pointer. @param val The JSON value to be queried. @param ptr The JSON pointer string (UTF-8 with null-terminator). @return The value referenced by the JSON pointer. NULL if `val` or `ptr` is NULL, or the JSON pointer cannot be resolved. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_get(yyjson_mut_val *val, const char *ptr); /** Get value by a JSON Pointer. @param val The JSON value to be queried. @param ptr The JSON pointer string (UTF-8, null-terminator is not required). @param len The length of `ptr` in bytes. @return The value referenced by the JSON pointer. NULL if `val` or `ptr` is NULL, or the JSON pointer cannot be resolved. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_getn(yyjson_mut_val *val, const char *ptr, size_t len); /** Get value by a JSON Pointer. @param val The JSON value to be queried. @param ptr The JSON pointer string (UTF-8, null-terminator is not required). @param len The length of `ptr` in bytes. @param ctx A pointer to store the result context, or NULL if not needed. @param err A pointer to store the error information, or NULL if not needed. @return The value referenced by the JSON pointer. NULL if `val` or `ptr` is NULL, or the JSON pointer cannot be resolved. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_getx(yyjson_mut_val *val, const char *ptr, size_t len, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); /** Add (insert) value by a JSON pointer. @param doc The target JSON document. @param ptr The JSON pointer string (UTF-8 with null-terminator). @param new_val The value to be added. @return true if JSON pointer is valid and new value is added, false otherwise. @note The parent nodes will be created if they do not exist. */ yyjson_api_inline bool yyjson_mut_doc_ptr_add(yyjson_mut_doc *doc, const char *ptr, yyjson_mut_val *new_val); /** Add (insert) value by a JSON pointer. @param doc The target JSON document. @param ptr The JSON pointer string (UTF-8, null-terminator is not required). @param len The length of `ptr` in bytes. @param new_val The value to be added. @return true if JSON pointer is valid and new value is added, false otherwise. @note The parent nodes will be created if they do not exist. */ yyjson_api_inline bool yyjson_mut_doc_ptr_addn(yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val); /** Add (insert) value by a JSON pointer. @param doc The target JSON document. @param ptr The JSON pointer string (UTF-8, null-terminator is not required). @param len The length of `ptr` in bytes. @param new_val The value to be added. @param create_parent Whether to create parent nodes if not exist. @param ctx A pointer to store the result context, or NULL if not needed. @param err A pointer to store the error information, or NULL if not needed. @return true if JSON pointer is valid and new value is added, false otherwise. */ yyjson_api_inline bool yyjson_mut_doc_ptr_addx(yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val, bool create_parent, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); /** Add (insert) value by a JSON pointer. @param val The target JSON value. @param ptr The JSON pointer string (UTF-8 with null-terminator). @param doc Only used to create new values when needed. @param new_val The value to be added. @return true if JSON pointer is valid and new value is added, false otherwise. @note The parent nodes will be created if they do not exist. */ yyjson_api_inline bool yyjson_mut_ptr_add(yyjson_mut_val *val, const char *ptr, yyjson_mut_val *new_val, yyjson_mut_doc *doc); /** Add (insert) value by a JSON pointer. @param val The target JSON value. @param ptr The JSON pointer string (UTF-8, null-terminator is not required). @param len The length of `ptr` in bytes. @param doc Only used to create new values when needed. @param new_val The value to be added. @return true if JSON pointer is valid and new value is added, false otherwise. @note The parent nodes will be created if they do not exist. */ yyjson_api_inline bool yyjson_mut_ptr_addn(yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, yyjson_mut_doc *doc); /** Add (insert) value by a JSON pointer. @param val The target JSON value. @param ptr The JSON pointer string (UTF-8, null-terminator is not required). @param len The length of `ptr` in bytes. @param doc Only used to create new values when needed. @param new_val The value to be added. @param create_parent Whether to create parent nodes if not exist. @param ctx A pointer to store the result context, or NULL if not needed. @param err A pointer to store the error information, or NULL if not needed. @return true if JSON pointer is valid and new value is added, false otherwise. */ yyjson_api_inline bool yyjson_mut_ptr_addx(yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, yyjson_mut_doc *doc, bool create_parent, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); /** Set value by a JSON pointer. @param doc The target JSON document. @param ptr The JSON pointer string (UTF-8 with null-terminator). @param new_val The value to be set, pass NULL to remove. @return true if JSON pointer is valid and new value is set, false otherwise. @note The parent nodes will be created if they do not exist. If the target value already exists, it will be replaced by the new value. */ yyjson_api_inline bool yyjson_mut_doc_ptr_set(yyjson_mut_doc *doc, const char *ptr, yyjson_mut_val *new_val); /** Set value by a JSON pointer. @param doc The target JSON document. @param ptr The JSON pointer string (UTF-8, null-terminator is not required). @param len The length of `ptr` in bytes. @param new_val The value to be set, pass NULL to remove. @return true if JSON pointer is valid and new value is set, false otherwise. @note The parent nodes will be created if they do not exist. If the target value already exists, it will be replaced by the new value. */ yyjson_api_inline bool yyjson_mut_doc_ptr_setn(yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val); /** Set value by a JSON pointer. @param doc The target JSON document. @param ptr The JSON pointer string (UTF-8, null-terminator is not required). @param len The length of `ptr` in bytes. @param new_val The value to be set, pass NULL to remove. @param create_parent Whether to create parent nodes if not exist. @param ctx A pointer to store the result context, or NULL if not needed. @param err A pointer to store the error information, or NULL if not needed. @return true if JSON pointer is valid and new value is set, false otherwise. @note If the target value already exists, it will be replaced by the new value. */ yyjson_api_inline bool yyjson_mut_doc_ptr_setx(yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val, bool create_parent, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); /** Set value by a JSON pointer. @param val The target JSON value. @param ptr The JSON pointer string (UTF-8 with null-terminator). @param new_val The value to be set, pass NULL to remove. @param doc Only used to create new values when needed. @return true if JSON pointer is valid and new value is set, false otherwise. @note The parent nodes will be created if they do not exist. If the target value already exists, it will be replaced by the new value. */ yyjson_api_inline bool yyjson_mut_ptr_set(yyjson_mut_val *val, const char *ptr, yyjson_mut_val *new_val, yyjson_mut_doc *doc); /** Set value by a JSON pointer. @param val The target JSON value. @param ptr The JSON pointer string (UTF-8, null-terminator is not required). @param len The length of `ptr` in bytes. @param new_val The value to be set, pass NULL to remove. @param doc Only used to create new values when needed. @return true if JSON pointer is valid and new value is set, false otherwise. @note The parent nodes will be created if they do not exist. If the target value already exists, it will be replaced by the new value. */ yyjson_api_inline bool yyjson_mut_ptr_setn(yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, yyjson_mut_doc *doc); /** Set value by a JSON pointer. @param val The target JSON value. @param ptr The JSON pointer string (UTF-8, null-terminator is not required). @param len The length of `ptr` in bytes. @param new_val The value to be set, pass NULL to remove. @param doc Only used to create new values when needed. @param create_parent Whether to create parent nodes if not exist. @param ctx A pointer to store the result context, or NULL if not needed. @param err A pointer to store the error information, or NULL if not needed. @return true if JSON pointer is valid and new value is set, false otherwise. @note If the target value already exists, it will be replaced by the new value. */ yyjson_api_inline bool yyjson_mut_ptr_setx(yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, yyjson_mut_doc *doc, bool create_parent, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); /** Replace value by a JSON pointer. @param doc The target JSON document. @param ptr The JSON pointer string (UTF-8 with null-terminator). @param new_val The new value to replace the old one. @return The old value that was replaced, or NULL if not found. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replace( yyjson_mut_doc *doc, const char *ptr, yyjson_mut_val *new_val); /** Replace value by a JSON pointer. @param doc The target JSON document. @param ptr The JSON pointer string (UTF-8, null-terminator is not required). @param len The length of `ptr` in bytes. @param new_val The new value to replace the old one. @return The old value that was replaced, or NULL if not found. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replacen( yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val); /** Replace value by a JSON pointer. @param doc The target JSON document. @param ptr The JSON pointer string (UTF-8, null-terminator is not required). @param len The length of `ptr` in bytes. @param new_val The new value to replace the old one. @param ctx A pointer to store the result context, or NULL if not needed. @param err A pointer to store the error information, or NULL if not needed. @return The old value that was replaced, or NULL if not found. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replacex( yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); /** Replace value by a JSON pointer. @param val The target JSON value. @param ptr The JSON pointer string (UTF-8 with null-terminator). @param new_val The new value to replace the old one. @return The old value that was replaced, or NULL if not found. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replace( yyjson_mut_val *val, const char *ptr, yyjson_mut_val *new_val); /** Replace value by a JSON pointer. @param val The target JSON value. @param ptr The JSON pointer string (UTF-8, null-terminator is not required). @param len The length of `ptr` in bytes. @param new_val The new value to replace the old one. @return The old value that was replaced, or NULL if not found. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replacen( yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val); /** Replace value by a JSON pointer. @param val The target JSON value. @param ptr The JSON pointer string (UTF-8, null-terminator is not required). @param len The length of `ptr` in bytes. @param new_val The new value to replace the old one. @param ctx A pointer to store the result context, or NULL if not needed. @param err A pointer to store the error information, or NULL if not needed. @return The old value that was replaced, or NULL if not found. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replacex( yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); /** Remove value by a JSON pointer. @param doc The target JSON document. @param ptr The JSON pointer string (UTF-8 with null-terminator). @return The removed value, or NULL on error. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_remove( yyjson_mut_doc *doc, const char *ptr); /** Remove value by a JSON pointer. @param doc The target JSON document. @param ptr The JSON pointer string (UTF-8, null-terminator is not required). @param len The length of `ptr` in bytes. @return The removed value, or NULL on error. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_removen( yyjson_mut_doc *doc, const char *ptr, size_t len); /** Remove value by a JSON pointer. @param doc The target JSON document. @param ptr The JSON pointer string (UTF-8, null-terminator is not required). @param len The length of `ptr` in bytes. @param ctx A pointer to store the result context, or NULL if not needed. @param err A pointer to store the error information, or NULL if not needed. @return The removed value, or NULL on error. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_removex( yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); /** Remove value by a JSON pointer. @param val The target JSON value. @param ptr The JSON pointer string (UTF-8 with null-terminator). @return The removed value, or NULL on error. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_remove(yyjson_mut_val *val, const char *ptr); /** Remove value by a JSON pointer. @param val The target JSON value. @param ptr The JSON pointer string (UTF-8, null-terminator is not required). @param len The length of `ptr` in bytes. @return The removed value, or NULL on error. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_removen(yyjson_mut_val *val, const char *ptr, size_t len); /** Remove value by a JSON pointer. @param val The target JSON value. @param ptr The JSON pointer string (UTF-8, null-terminator is not required). @param len The length of `ptr` in bytes. @param ctx A pointer to store the result context, or NULL if not needed. @param err A pointer to store the error information, or NULL if not needed. @return The removed value, or NULL on error. */ yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_removex(yyjson_mut_val *val, const char *ptr, size_t len, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); /** Append value by JSON pointer context. @param ctx The context from the `yyjson_mut_ptr_xxx()` calls. @param key New key if `ctx->ctn` is object, or NULL if `ctx->ctn` is array. @param val New value to be added. @return true on success or false on fail. */ yyjson_api_inline bool yyjson_ptr_ctx_append(yyjson_ptr_ctx *ctx, yyjson_mut_val *key, yyjson_mut_val *val); /** Replace value by JSON pointer context. @param ctx The context from the `yyjson_mut_ptr_xxx()` calls. @param val New value to be replaced. @return true on success or false on fail. @note If success, the old value will be returned via `ctx->old`. */ yyjson_api_inline bool yyjson_ptr_ctx_replace(yyjson_ptr_ctx *ctx, yyjson_mut_val *val); /** Remove value by JSON pointer context. @param ctx The context from the `yyjson_mut_ptr_xxx()` calls. @return true on success or false on fail. @note If success, the old value will be returned via `ctx->old`. */ yyjson_api_inline bool yyjson_ptr_ctx_remove(yyjson_ptr_ctx *ctx); /*============================================================================== * JSON Patch API (RFC 6902) * https://tools.ietf.org/html/rfc6902 *============================================================================*/ /** Result code for JSON patch. */ typedef uint32_t yyjson_patch_code; /** Success, no error. */ static const yyjson_patch_code YYJSON_PATCH_SUCCESS = 0; /** Invalid parameter, such as NULL input or non-array patch. */ static const yyjson_patch_code YYJSON_PATCH_ERROR_INVALID_PARAMETER = 1; /** Memory allocation failure occurs. */ static const yyjson_patch_code YYJSON_PATCH_ERROR_MEMORY_ALLOCATION = 2; /** JSON patch operation is not object type. */ static const yyjson_patch_code YYJSON_PATCH_ERROR_INVALID_OPERATION = 3; /** JSON patch operation is missing a required key. */ static const yyjson_patch_code YYJSON_PATCH_ERROR_MISSING_KEY = 4; /** JSON patch operation member is invalid. */ static const yyjson_patch_code YYJSON_PATCH_ERROR_INVALID_MEMBER = 5; /** JSON patch operation `test` not equal. */ static const yyjson_patch_code YYJSON_PATCH_ERROR_EQUAL = 6; /** JSON patch operation failed on JSON pointer. */ static const yyjson_patch_code YYJSON_PATCH_ERROR_POINTER = 7; /** Error information for JSON patch. */ typedef struct yyjson_patch_err { /** Error code, see `yyjson_patch_code` for all possible values. */ yyjson_patch_code code; /** Index of the error operation (0 if no error). */ size_t idx; /** Error message, constant, no need to free (NULL if no error). */ const char *msg; /** JSON pointer error if `code == YYJSON_PATCH_ERROR_POINTER`. */ yyjson_ptr_err ptr; } yyjson_patch_err; /** Creates and returns a patched JSON value (RFC 6902). The memory of the returned value is allocated by the `doc`. The `err` is used to receive error information, pass NULL if not needed. Returns NULL if the patch could not be applied. */ yyjson_api yyjson_mut_val *yyjson_patch(yyjson_mut_doc *doc, yyjson_val *orig, yyjson_val *patch, yyjson_patch_err *err); /** Creates and returns a patched JSON value (RFC 6902). The memory of the returned value is allocated by the `doc`. The `err` is used to receive error information, pass NULL if not needed. Returns NULL if the patch could not be applied. */ yyjson_api yyjson_mut_val *yyjson_mut_patch(yyjson_mut_doc *doc, yyjson_mut_val *orig, yyjson_mut_val *patch, yyjson_patch_err *err); /*============================================================================== * JSON Merge-Patch API (RFC 7386) * https://tools.ietf.org/html/rfc7386 *============================================================================*/ /** Creates and returns a merge-patched JSON value (RFC 7386). The memory of the returned value is allocated by the `doc`. Returns NULL if the patch could not be applied. @warning This function is recursive and may cause a stack overflow if the object level is too deep. */ yyjson_api yyjson_mut_val *yyjson_merge_patch(yyjson_mut_doc *doc, yyjson_val *orig, yyjson_val *patch); /** Creates and returns a merge-patched JSON value (RFC 7386). The memory of the returned value is allocated by the `doc`. Returns NULL if the patch could not be applied. @warning This function is recursive and may cause a stack overflow if the object level is too deep. */ yyjson_api yyjson_mut_val *yyjson_mut_merge_patch(yyjson_mut_doc *doc, yyjson_mut_val *orig, yyjson_mut_val *patch); /*============================================================================== * JSON Structure (Implementation) *============================================================================*/ /** Payload of a JSON value (8 bytes). */ typedef union yyjson_val_uni { uint64_t u64; int64_t i64; double f64; const char *str; void *ptr; size_t ofs; } yyjson_val_uni; /** Immutable JSON value, 16 bytes. */ struct yyjson_val { uint64_t tag; /**< type, subtype and length */ yyjson_val_uni uni; /**< payload */ }; struct yyjson_doc { /** Root value of the document (nonnull). */ yyjson_val *root; /** Allocator used by document (nonnull). */ yyjson_alc alc; /** The total number of bytes read when parsing JSON (nonzero). */ size_t dat_read; /** The total number of value read when parsing JSON (nonzero). */ size_t val_read; /** The string pool used by JSON values (nullable). */ char *str_pool; }; /*============================================================================== * Unsafe JSON Value API (Implementation) *============================================================================*/ /* Whether the string does not need to be escaped for serialization. This function is used to optimize the writing speed of small constant strings. This function works only if the compiler can evaluate it at compile time. Clang supports it since v8.0, earlier versions do not support constant_p(strlen) and return false. GCC supports it since at least v4.4, earlier versions may compile it as run-time instructions. ICC supports it since at least v16, earlier versions are uncertain. @param str The C string. @param len The returnd value from strlen(str). */ yyjson_api_inline bool unsafe_yyjson_is_str_noesc(const char *str, size_t len) { #if YYJSON_HAS_CONSTANT_P && \ (!YYJSON_IS_REAL_GCC || yyjson_gcc_available(4, 4, 0)) if (yyjson_constant_p(len) && len <= 32) { /* Same as the following loop: for (size_t i = 0; i < len; i++) { char c = str[i]; if (c < ' ' || c > '~' || c == '"' || c == '\\') return false; } GCC evaluates it at compile time only if the string length is within 17 and -O3 (which turns on the -fpeel-loops flag) is used. So the loop is unrolled for GCC. */ # define yyjson_repeat32_incr(x) \ x(0) x(1) x(2) x(3) x(4) x(5) x(6) x(7) \ x(8) x(9) x(10) x(11) x(12) x(13) x(14) x(15) \ x(16) x(17) x(18) x(19) x(20) x(21) x(22) x(23) \ x(24) x(25) x(26) x(27) x(28) x(29) x(30) x(31) # define yyjson_check_char_noesc(i) \ if (i < len) { \ char c = str[i]; \ if (c < ' ' || c > '~' || c == '"' || c == '\\') return false; } yyjson_repeat32_incr(yyjson_check_char_noesc) # undef yyjson_repeat32_incr # undef yyjson_check_char_noesc return true; } #else (void)str; (void)len; #endif return false; } yyjson_api_inline yyjson_type unsafe_yyjson_get_type(void *val) { uint8_t tag = (uint8_t)((yyjson_val *)val)->tag; return (yyjson_type)(tag & YYJSON_TYPE_MASK); } yyjson_api_inline yyjson_subtype unsafe_yyjson_get_subtype(void *val) { uint8_t tag = (uint8_t)((yyjson_val *)val)->tag; return (yyjson_subtype)(tag & YYJSON_SUBTYPE_MASK); } yyjson_api_inline uint8_t unsafe_yyjson_get_tag(void *val) { uint8_t tag = (uint8_t)((yyjson_val *)val)->tag; return (uint8_t)(tag & YYJSON_TAG_MASK); } yyjson_api_inline bool unsafe_yyjson_is_raw(void *val) { return unsafe_yyjson_get_type(val) == YYJSON_TYPE_RAW; } yyjson_api_inline bool unsafe_yyjson_is_null(void *val) { return unsafe_yyjson_get_type(val) == YYJSON_TYPE_NULL; } yyjson_api_inline bool unsafe_yyjson_is_bool(void *val) { return unsafe_yyjson_get_type(val) == YYJSON_TYPE_BOOL; } yyjson_api_inline bool unsafe_yyjson_is_num(void *val) { return unsafe_yyjson_get_type(val) == YYJSON_TYPE_NUM; } yyjson_api_inline bool unsafe_yyjson_is_str(void *val) { return unsafe_yyjson_get_type(val) == YYJSON_TYPE_STR; } yyjson_api_inline bool unsafe_yyjson_is_arr(void *val) { return unsafe_yyjson_get_type(val) == YYJSON_TYPE_ARR; } yyjson_api_inline bool unsafe_yyjson_is_obj(void *val) { return unsafe_yyjson_get_type(val) == YYJSON_TYPE_OBJ; } yyjson_api_inline bool unsafe_yyjson_is_ctn(void *val) { uint8_t mask = YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ; return (unsafe_yyjson_get_tag(val) & mask) == mask; } yyjson_api_inline bool unsafe_yyjson_is_uint(void *val) { const uint8_t patt = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; return unsafe_yyjson_get_tag(val) == patt; } yyjson_api_inline bool unsafe_yyjson_is_sint(void *val) { const uint8_t patt = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; return unsafe_yyjson_get_tag(val) == patt; } yyjson_api_inline bool unsafe_yyjson_is_int(void *val) { const uint8_t mask = YYJSON_TAG_MASK & (~YYJSON_SUBTYPE_SINT); const uint8_t patt = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; return (unsafe_yyjson_get_tag(val) & mask) == patt; } yyjson_api_inline bool unsafe_yyjson_is_real(void *val) { const uint8_t patt = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; return unsafe_yyjson_get_tag(val) == patt; } yyjson_api_inline bool unsafe_yyjson_is_true(void *val) { const uint8_t patt = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_TRUE; return unsafe_yyjson_get_tag(val) == patt; } yyjson_api_inline bool unsafe_yyjson_is_false(void *val) { const uint8_t patt = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_FALSE; return unsafe_yyjson_get_tag(val) == patt; } yyjson_api_inline bool unsafe_yyjson_arr_is_flat(yyjson_val *val) { size_t ofs = val->uni.ofs; size_t len = (size_t)(val->tag >> YYJSON_TAG_BIT); return len * sizeof(yyjson_val) + sizeof(yyjson_val) == ofs; } yyjson_api_inline const char *unsafe_yyjson_get_raw(void *val) { return ((yyjson_val *)val)->uni.str; } yyjson_api_inline bool unsafe_yyjson_get_bool(void *val) { uint8_t tag = unsafe_yyjson_get_tag(val); return (bool)((tag & YYJSON_SUBTYPE_MASK) >> YYJSON_TYPE_BIT); } yyjson_api_inline uint64_t unsafe_yyjson_get_uint(void *val) { return ((yyjson_val *)val)->uni.u64; } yyjson_api_inline int64_t unsafe_yyjson_get_sint(void *val) { return ((yyjson_val *)val)->uni.i64; } yyjson_api_inline int unsafe_yyjson_get_int(void *val) { return (int)((yyjson_val *)val)->uni.i64; } yyjson_api_inline double unsafe_yyjson_get_real(void *val) { return ((yyjson_val *)val)->uni.f64; } yyjson_api_inline double unsafe_yyjson_get_num(void *val) { uint8_t tag = unsafe_yyjson_get_tag(val); if (tag == (YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL)) { return ((yyjson_val *)val)->uni.f64; } else if (tag == (YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT)) { return (double)((yyjson_val *)val)->uni.i64; } else if (tag == (YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT)) { #if YYJSON_U64_TO_F64_NO_IMPL uint64_t msb = ((uint64_t)1) << 63; uint64_t num = ((yyjson_val *)val)->uni.u64; if ((num & msb) == 0) { return (double)(int64_t)num; } else { return ((double)(int64_t)((num >> 1) | (num & 1))) * (double)2.0; } #else return (double)((yyjson_val *)val)->uni.u64; #endif } return 0.0; } yyjson_api_inline const char *unsafe_yyjson_get_str(void *val) { return ((yyjson_val *)val)->uni.str; } yyjson_api_inline size_t unsafe_yyjson_get_len(void *val) { return (size_t)(((yyjson_val *)val)->tag >> YYJSON_TAG_BIT); } yyjson_api_inline yyjson_val *unsafe_yyjson_get_first(yyjson_val *ctn) { return ctn + 1; } yyjson_api_inline yyjson_val *unsafe_yyjson_get_next(yyjson_val *val) { bool is_ctn = unsafe_yyjson_is_ctn(val); size_t ctn_ofs = val->uni.ofs; size_t ofs = (is_ctn ? ctn_ofs : sizeof(yyjson_val)); return (yyjson_val *)(void *)((uint8_t *)val + ofs); } yyjson_api_inline bool unsafe_yyjson_equals_strn(void *val, const char *str, size_t len) { return unsafe_yyjson_get_len(val) == len && memcmp(((yyjson_val *)val)->uni.str, str, len) == 0; } yyjson_api_inline bool unsafe_yyjson_equals_str(void *val, const char *str) { return unsafe_yyjson_equals_strn(val, str, strlen(str)); } yyjson_api_inline void unsafe_yyjson_set_type(void *val, yyjson_type type, yyjson_subtype subtype) { uint8_t tag = (type | subtype); uint64_t new_tag = ((yyjson_val *)val)->tag; new_tag = (new_tag & (~(uint64_t)YYJSON_TAG_MASK)) | (uint64_t)tag; ((yyjson_val *)val)->tag = new_tag; } yyjson_api_inline void unsafe_yyjson_set_len(void *val, size_t len) { uint64_t tag = ((yyjson_val *)val)->tag & YYJSON_TAG_MASK; tag |= (uint64_t)len << YYJSON_TAG_BIT; ((yyjson_val *)val)->tag = tag; } yyjson_api_inline void unsafe_yyjson_inc_len(void *val) { uint64_t tag = ((yyjson_val *)val)->tag; tag += (uint64_t)(1 << YYJSON_TAG_BIT); ((yyjson_val *)val)->tag = tag; } yyjson_api_inline void unsafe_yyjson_set_raw(void *val, const char *raw, size_t len) { unsafe_yyjson_set_type(val, YYJSON_TYPE_RAW, YYJSON_SUBTYPE_NONE); unsafe_yyjson_set_len(val, len); ((yyjson_val *)val)->uni.str = raw; } yyjson_api_inline void unsafe_yyjson_set_null(void *val) { unsafe_yyjson_set_type(val, YYJSON_TYPE_NULL, YYJSON_SUBTYPE_NONE); unsafe_yyjson_set_len(val, 0); } yyjson_api_inline void unsafe_yyjson_set_bool(void *val, bool num) { yyjson_subtype subtype = num ? YYJSON_SUBTYPE_TRUE : YYJSON_SUBTYPE_FALSE; unsafe_yyjson_set_type(val, YYJSON_TYPE_BOOL, subtype); unsafe_yyjson_set_len(val, 0); } yyjson_api_inline void unsafe_yyjson_set_uint(void *val, uint64_t num) { unsafe_yyjson_set_type(val, YYJSON_TYPE_NUM, YYJSON_SUBTYPE_UINT); unsafe_yyjson_set_len(val, 0); ((yyjson_val *)val)->uni.u64 = num; } yyjson_api_inline void unsafe_yyjson_set_sint(void *val, int64_t num) { unsafe_yyjson_set_type(val, YYJSON_TYPE_NUM, YYJSON_SUBTYPE_SINT); unsafe_yyjson_set_len(val, 0); ((yyjson_val *)val)->uni.i64 = num; } yyjson_api_inline void unsafe_yyjson_set_real(void *val, double num) { unsafe_yyjson_set_type(val, YYJSON_TYPE_NUM, YYJSON_SUBTYPE_REAL); unsafe_yyjson_set_len(val, 0); ((yyjson_val *)val)->uni.f64 = num; } yyjson_api_inline void unsafe_yyjson_set_str(void *val, const char *str) { size_t len = strlen(str); bool noesc = unsafe_yyjson_is_str_noesc(str, len); yyjson_subtype sub = noesc ? YYJSON_SUBTYPE_NOESC : YYJSON_SUBTYPE_NONE; unsafe_yyjson_set_type(val, YYJSON_TYPE_STR, sub); unsafe_yyjson_set_len(val, len); ((yyjson_val *)val)->uni.str = str; } yyjson_api_inline void unsafe_yyjson_set_strn(void *val, const char *str, size_t len) { unsafe_yyjson_set_type(val, YYJSON_TYPE_STR, YYJSON_SUBTYPE_NONE); unsafe_yyjson_set_len(val, len); ((yyjson_val *)val)->uni.str = str; } yyjson_api_inline void unsafe_yyjson_set_arr(void *val, size_t size) { unsafe_yyjson_set_type(val, YYJSON_TYPE_ARR, YYJSON_SUBTYPE_NONE); unsafe_yyjson_set_len(val, size); } yyjson_api_inline void unsafe_yyjson_set_obj(void *val, size_t size) { unsafe_yyjson_set_type(val, YYJSON_TYPE_OBJ, YYJSON_SUBTYPE_NONE); unsafe_yyjson_set_len(val, size); } /*============================================================================== * JSON Document API (Implementation) *============================================================================*/ yyjson_api_inline yyjson_val *yyjson_doc_get_root(yyjson_doc *doc) { return doc ? doc->root : NULL; } yyjson_api_inline size_t yyjson_doc_get_read_size(yyjson_doc *doc) { return doc ? doc->dat_read : 0; } yyjson_api_inline size_t yyjson_doc_get_val_count(yyjson_doc *doc) { return doc ? doc->val_read : 0; } yyjson_api void yyjson_doc_free(yyjson_doc *doc) { if (doc) { yyjson_alc alc = doc->alc; memset(&doc->alc, 0, sizeof(alc)); if (doc->str_pool) alc.free(alc.ctx, doc->str_pool); alc.free(alc.ctx, doc); } } /*============================================================================== * JSON Value Type API (Implementation) *============================================================================*/ yyjson_api_inline bool yyjson_is_raw(yyjson_val *val) { return val ? unsafe_yyjson_is_raw(val) : false; } yyjson_api_inline bool yyjson_is_null(yyjson_val *val) { return val ? unsafe_yyjson_is_null(val) : false; } yyjson_api_inline bool yyjson_is_true(yyjson_val *val) { return val ? unsafe_yyjson_is_true(val) : false; } yyjson_api_inline bool yyjson_is_false(yyjson_val *val) { return val ? unsafe_yyjson_is_false(val) : false; } yyjson_api_inline bool yyjson_is_bool(yyjson_val *val) { return val ? unsafe_yyjson_is_bool(val) : false; } yyjson_api_inline bool yyjson_is_uint(yyjson_val *val) { return val ? unsafe_yyjson_is_uint(val) : false; } yyjson_api_inline bool yyjson_is_sint(yyjson_val *val) { return val ? unsafe_yyjson_is_sint(val) : false; } yyjson_api_inline bool yyjson_is_int(yyjson_val *val) { return val ? unsafe_yyjson_is_int(val) : false; } yyjson_api_inline bool yyjson_is_real(yyjson_val *val) { return val ? unsafe_yyjson_is_real(val) : false; } yyjson_api_inline bool yyjson_is_num(yyjson_val *val) { return val ? unsafe_yyjson_is_num(val) : false; } yyjson_api_inline bool yyjson_is_str(yyjson_val *val) { return val ? unsafe_yyjson_is_str(val) : false; } yyjson_api_inline bool yyjson_is_arr(yyjson_val *val) { return val ? unsafe_yyjson_is_arr(val) : false; } yyjson_api_inline bool yyjson_is_obj(yyjson_val *val) { return val ? unsafe_yyjson_is_obj(val) : false; } yyjson_api_inline bool yyjson_is_ctn(yyjson_val *val) { return val ? unsafe_yyjson_is_ctn(val) : false; } /*============================================================================== * JSON Value Content API (Implementation) *============================================================================*/ yyjson_api_inline yyjson_type yyjson_get_type(yyjson_val *val) { return val ? unsafe_yyjson_get_type(val) : YYJSON_TYPE_NONE; } yyjson_api_inline yyjson_subtype yyjson_get_subtype(yyjson_val *val) { return val ? unsafe_yyjson_get_subtype(val) : YYJSON_SUBTYPE_NONE; } yyjson_api_inline uint8_t yyjson_get_tag(yyjson_val *val) { return val ? unsafe_yyjson_get_tag(val) : 0; } yyjson_api_inline const char *yyjson_get_type_desc(yyjson_val *val) { switch (yyjson_get_tag(val)) { case YYJSON_TYPE_RAW | YYJSON_SUBTYPE_NONE: return "raw"; case YYJSON_TYPE_NULL | YYJSON_SUBTYPE_NONE: return "null"; case YYJSON_TYPE_STR | YYJSON_SUBTYPE_NONE: return "string"; case YYJSON_TYPE_STR | YYJSON_SUBTYPE_NOESC: return "string"; case YYJSON_TYPE_ARR | YYJSON_SUBTYPE_NONE: return "array"; case YYJSON_TYPE_OBJ | YYJSON_SUBTYPE_NONE: return "object"; case YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_TRUE: return "true"; case YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_FALSE: return "false"; case YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT: return "uint"; case YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT: return "sint"; case YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL: return "real"; default: return "unknown"; } } yyjson_api_inline const char *yyjson_get_raw(yyjson_val *val) { return yyjson_is_raw(val) ? unsafe_yyjson_get_raw(val) : NULL; } yyjson_api_inline bool yyjson_get_bool(yyjson_val *val) { return yyjson_is_bool(val) ? unsafe_yyjson_get_bool(val) : false; } yyjson_api_inline uint64_t yyjson_get_uint(yyjson_val *val) { return yyjson_is_int(val) ? unsafe_yyjson_get_uint(val) : 0; } yyjson_api_inline int64_t yyjson_get_sint(yyjson_val *val) { return yyjson_is_int(val) ? unsafe_yyjson_get_sint(val) : 0; } yyjson_api_inline int yyjson_get_int(yyjson_val *val) { return yyjson_is_int(val) ? unsafe_yyjson_get_int(val) : 0; } yyjson_api_inline double yyjson_get_real(yyjson_val *val) { return yyjson_is_real(val) ? unsafe_yyjson_get_real(val) : 0.0; } yyjson_api_inline double yyjson_get_num(yyjson_val *val) { return val ? unsafe_yyjson_get_num(val) : 0.0; } yyjson_api_inline const char *yyjson_get_str(yyjson_val *val) { return yyjson_is_str(val) ? unsafe_yyjson_get_str(val) : NULL; } yyjson_api_inline size_t yyjson_get_len(yyjson_val *val) { return val ? unsafe_yyjson_get_len(val) : 0; } yyjson_api_inline bool yyjson_equals_str(yyjson_val *val, const char *str) { if (yyjson_likely(val && str)) { return unsafe_yyjson_is_str(val) && unsafe_yyjson_equals_str(val, str); } return false; } yyjson_api_inline bool yyjson_equals_strn(yyjson_val *val, const char *str, size_t len) { if (yyjson_likely(val && str)) { return unsafe_yyjson_is_str(val) && unsafe_yyjson_equals_strn(val, str, len); } return false; } yyjson_api bool unsafe_yyjson_equals(yyjson_val *lhs, yyjson_val *rhs); yyjson_api_inline bool yyjson_equals(yyjson_val *lhs, yyjson_val *rhs) { if (yyjson_unlikely(!lhs || !rhs)) return false; return unsafe_yyjson_equals(lhs, rhs); } yyjson_api_inline bool yyjson_set_raw(yyjson_val *val, const char *raw, size_t len) { if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; unsafe_yyjson_set_raw(val, raw, len); return true; } yyjson_api_inline bool yyjson_set_null(yyjson_val *val) { if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; unsafe_yyjson_set_null(val); return true; } yyjson_api_inline bool yyjson_set_bool(yyjson_val *val, bool num) { if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; unsafe_yyjson_set_bool(val, num); return true; } yyjson_api_inline bool yyjson_set_uint(yyjson_val *val, uint64_t num) { if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; unsafe_yyjson_set_uint(val, num); return true; } yyjson_api_inline bool yyjson_set_sint(yyjson_val *val, int64_t num) { if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; unsafe_yyjson_set_sint(val, num); return true; } yyjson_api_inline bool yyjson_set_int(yyjson_val *val, int num) { if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; unsafe_yyjson_set_sint(val, (int64_t)num); return true; } yyjson_api_inline bool yyjson_set_real(yyjson_val *val, double num) { if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; unsafe_yyjson_set_real(val, num); return true; } yyjson_api_inline bool yyjson_set_str(yyjson_val *val, const char *str) { if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; if (yyjson_unlikely(!str)) return false; unsafe_yyjson_set_str(val, str); return true; } yyjson_api_inline bool yyjson_set_strn(yyjson_val *val, const char *str, size_t len) { if (yyjson_unlikely(!val || unsafe_yyjson_is_ctn(val))) return false; if (yyjson_unlikely(!str)) return false; unsafe_yyjson_set_strn(val, str, len); return true; } /*============================================================================== * JSON Array API (Implementation) *============================================================================*/ yyjson_api_inline size_t yyjson_arr_size(yyjson_val *arr) { return yyjson_is_arr(arr) ? unsafe_yyjson_get_len(arr) : 0; } yyjson_api_inline yyjson_val *yyjson_arr_get(yyjson_val *arr, size_t idx) { if (yyjson_likely(yyjson_is_arr(arr))) { if (yyjson_likely(unsafe_yyjson_get_len(arr) > idx)) { yyjson_val *val = unsafe_yyjson_get_first(arr); if (unsafe_yyjson_arr_is_flat(arr)) { return val + idx; } else { while (idx-- > 0) val = unsafe_yyjson_get_next(val); return val; } } } return NULL; } yyjson_api_inline yyjson_val *yyjson_arr_get_first(yyjson_val *arr) { if (yyjson_likely(yyjson_is_arr(arr))) { if (yyjson_likely(unsafe_yyjson_get_len(arr) > 0)) { return unsafe_yyjson_get_first(arr); } } return NULL; } yyjson_api_inline yyjson_val *yyjson_arr_get_last(yyjson_val *arr) { if (yyjson_likely(yyjson_is_arr(arr))) { size_t len = unsafe_yyjson_get_len(arr); if (yyjson_likely(len > 0)) { yyjson_val *val = unsafe_yyjson_get_first(arr); if (unsafe_yyjson_arr_is_flat(arr)) { return val + (len - 1); } else { while (len-- > 1) val = unsafe_yyjson_get_next(val); return val; } } } return NULL; } /*============================================================================== * JSON Array Iterator API (Implementation) *============================================================================*/ yyjson_api_inline bool yyjson_arr_iter_init(yyjson_val *arr, yyjson_arr_iter *iter) { if (yyjson_likely(yyjson_is_arr(arr) && iter)) { iter->idx = 0; iter->max = unsafe_yyjson_get_len(arr); iter->cur = unsafe_yyjson_get_first(arr); return true; } if (iter) memset(iter, 0, sizeof(yyjson_arr_iter)); return false; } yyjson_api_inline yyjson_arr_iter yyjson_arr_iter_with(yyjson_val *arr) { yyjson_arr_iter iter; yyjson_arr_iter_init(arr, &iter); return iter; } yyjson_api_inline bool yyjson_arr_iter_has_next(yyjson_arr_iter *iter) { return iter ? iter->idx < iter->max : false; } yyjson_api_inline yyjson_val *yyjson_arr_iter_next(yyjson_arr_iter *iter) { yyjson_val *val; if (iter && iter->idx < iter->max) { val = iter->cur; iter->cur = unsafe_yyjson_get_next(val); iter->idx++; return val; } return NULL; } /*============================================================================== * JSON Object API (Implementation) *============================================================================*/ yyjson_api_inline size_t yyjson_obj_size(yyjson_val *obj) { return yyjson_is_obj(obj) ? unsafe_yyjson_get_len(obj) : 0; } yyjson_api_inline yyjson_val *yyjson_obj_get(yyjson_val *obj, const char *key) { return yyjson_obj_getn(obj, key, key ? strlen(key) : 0); } yyjson_api_inline yyjson_val *yyjson_obj_getn(yyjson_val *obj, const char *_key, size_t key_len) { if (yyjson_likely(yyjson_is_obj(obj) && _key)) { size_t len = unsafe_yyjson_get_len(obj); yyjson_val *key = unsafe_yyjson_get_first(obj); while (len-- > 0) { if (unsafe_yyjson_equals_strn(key, _key, key_len)) return key + 1; key = unsafe_yyjson_get_next(key + 1); } } return NULL; } /*============================================================================== * JSON Object Iterator API (Implementation) *============================================================================*/ yyjson_api_inline bool yyjson_obj_iter_init(yyjson_val *obj, yyjson_obj_iter *iter) { if (yyjson_likely(yyjson_is_obj(obj) && iter)) { iter->idx = 0; iter->max = unsafe_yyjson_get_len(obj); iter->cur = unsafe_yyjson_get_first(obj); iter->obj = obj; return true; } if (iter) memset(iter, 0, sizeof(yyjson_obj_iter)); return false; } yyjson_api_inline yyjson_obj_iter yyjson_obj_iter_with(yyjson_val *obj) { yyjson_obj_iter iter; yyjson_obj_iter_init(obj, &iter); return iter; } yyjson_api_inline bool yyjson_obj_iter_has_next(yyjson_obj_iter *iter) { return iter ? iter->idx < iter->max : false; } yyjson_api_inline yyjson_val *yyjson_obj_iter_next(yyjson_obj_iter *iter) { if (iter && iter->idx < iter->max) { yyjson_val *key = iter->cur; iter->idx++; iter->cur = unsafe_yyjson_get_next(key + 1); return key; } return NULL; } yyjson_api_inline yyjson_val *yyjson_obj_iter_get_val(yyjson_val *key) { return key ? key + 1 : NULL; } yyjson_api_inline yyjson_val *yyjson_obj_iter_get(yyjson_obj_iter *iter, const char *key) { return yyjson_obj_iter_getn(iter, key, key ? strlen(key) : 0); } yyjson_api_inline yyjson_val *yyjson_obj_iter_getn(yyjson_obj_iter *iter, const char *key, size_t key_len) { if (iter && key) { size_t idx = iter->idx; size_t max = iter->max; yyjson_val *cur = iter->cur; if (yyjson_unlikely(idx == max)) { idx = 0; cur = unsafe_yyjson_get_first(iter->obj); } while (idx++ < max) { yyjson_val *next = unsafe_yyjson_get_next(cur + 1); if (unsafe_yyjson_equals_strn(cur, key, key_len)) { iter->idx = idx; iter->cur = next; return cur + 1; } cur = next; if (idx == iter->max && iter->idx < iter->max) { idx = 0; max = iter->idx; cur = unsafe_yyjson_get_first(iter->obj); } } } return NULL; } /*============================================================================== * Mutable JSON Structure (Implementation) *============================================================================*/ /** Mutable JSON value, 24 bytes. The 'tag' and 'uni' field is same as immutable value. The 'next' field links all elements inside the container to be a cycle. */ struct yyjson_mut_val { uint64_t tag; /**< type, subtype and length */ yyjson_val_uni uni; /**< payload */ yyjson_mut_val *next; /**< the next value in circular linked list */ }; /** A memory chunk in string memory pool. */ typedef struct yyjson_str_chunk { struct yyjson_str_chunk *next; /* next chunk linked list */ size_t chunk_size; /* chunk size in bytes */ /* char str[]; flexible array member */ } yyjson_str_chunk; /** A memory pool to hold all strings in a mutable document. */ typedef struct yyjson_str_pool { char *cur; /* cursor inside current chunk */ char *end; /* the end of current chunk */ size_t chunk_size; /* chunk size in bytes while creating new chunk */ size_t chunk_size_max; /* maximum chunk size in bytes */ yyjson_str_chunk *chunks; /* a linked list of chunks, nullable */ } yyjson_str_pool; /** A memory chunk in value memory pool. `sizeof(yyjson_val_chunk)` should not larger than `sizeof(yyjson_mut_val)`. */ typedef struct yyjson_val_chunk { struct yyjson_val_chunk *next; /* next chunk linked list */ size_t chunk_size; /* chunk size in bytes */ /* char pad[sizeof(yyjson_mut_val) - sizeof(yyjson_val_chunk)]; padding */ /* yyjson_mut_val vals[]; flexible array member */ } yyjson_val_chunk; /** A memory pool to hold all values in a mutable document. */ typedef struct yyjson_val_pool { yyjson_mut_val *cur; /* cursor inside current chunk */ yyjson_mut_val *end; /* the end of current chunk */ size_t chunk_size; /* chunk size in bytes while creating new chunk */ size_t chunk_size_max; /* maximum chunk size in bytes */ yyjson_val_chunk *chunks; /* a linked list of chunks, nullable */ } yyjson_val_pool; struct yyjson_mut_doc { yyjson_mut_val *root; /**< root value of the JSON document, nullable */ yyjson_alc alc; /**< a valid allocator, nonnull */ yyjson_str_pool str_pool; /**< string memory pool */ yyjson_val_pool val_pool; /**< value memory pool */ }; /* Ensures the capacity to at least equal to the specified byte length. */ yyjson_api bool unsafe_yyjson_str_pool_grow(yyjson_str_pool *pool, const yyjson_alc *alc, size_t len); /* Ensures the capacity to at least equal to the specified value count. */ yyjson_api bool unsafe_yyjson_val_pool_grow(yyjson_val_pool *pool, const yyjson_alc *alc, size_t count); /* Allocate memory for string. */ yyjson_api_inline char *unsafe_yyjson_mut_str_alc(yyjson_mut_doc *doc, size_t len) { char *mem; const yyjson_alc *alc = &doc->alc; yyjson_str_pool *pool = &doc->str_pool; if (yyjson_unlikely((size_t)(pool->end - pool->cur) <= len)) { if (yyjson_unlikely(!unsafe_yyjson_str_pool_grow(pool, alc, len + 1))) { return NULL; } } mem = pool->cur; pool->cur = mem + len + 1; return mem; } yyjson_api_inline char *unsafe_yyjson_mut_strncpy(yyjson_mut_doc *doc, const char *str, size_t len) { char *mem = unsafe_yyjson_mut_str_alc(doc, len); if (yyjson_unlikely(!mem)) return NULL; memcpy((void *)mem, (const void *)str, len); mem[len] = '\0'; return mem; } yyjson_api_inline yyjson_mut_val *unsafe_yyjson_mut_val(yyjson_mut_doc *doc, size_t count) { yyjson_mut_val *val; yyjson_alc *alc = &doc->alc; yyjson_val_pool *pool = &doc->val_pool; if (yyjson_unlikely((size_t)(pool->end - pool->cur) < count)) { if (yyjson_unlikely(!unsafe_yyjson_val_pool_grow(pool, alc, count))) { return NULL; } } val = pool->cur; pool->cur += count; return val; } /*============================================================================== * Mutable JSON Document API (Implementation) *============================================================================*/ yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_get_root(yyjson_mut_doc *doc) { return doc ? doc->root : NULL; } yyjson_api_inline void yyjson_mut_doc_set_root(yyjson_mut_doc *doc, yyjson_mut_val *root) { if (doc) doc->root = root; } /*============================================================================== * Mutable JSON Value Type API (Implementation) *============================================================================*/ yyjson_api_inline bool yyjson_mut_is_raw(yyjson_mut_val *val) { return val ? unsafe_yyjson_is_raw(val) : false; } yyjson_api_inline bool yyjson_mut_is_null(yyjson_mut_val *val) { return val ? unsafe_yyjson_is_null(val) : false; } yyjson_api_inline bool yyjson_mut_is_true(yyjson_mut_val *val) { return val ? unsafe_yyjson_is_true(val) : false; } yyjson_api_inline bool yyjson_mut_is_false(yyjson_mut_val *val) { return val ? unsafe_yyjson_is_false(val) : false; } yyjson_api_inline bool yyjson_mut_is_bool(yyjson_mut_val *val) { return val ? unsafe_yyjson_is_bool(val) : false; } yyjson_api_inline bool yyjson_mut_is_uint(yyjson_mut_val *val) { return val ? unsafe_yyjson_is_uint(val) : false; } yyjson_api_inline bool yyjson_mut_is_sint(yyjson_mut_val *val) { return val ? unsafe_yyjson_is_sint(val) : false; } yyjson_api_inline bool yyjson_mut_is_int(yyjson_mut_val *val) { return val ? unsafe_yyjson_is_int(val) : false; } yyjson_api_inline bool yyjson_mut_is_real(yyjson_mut_val *val) { return val ? unsafe_yyjson_is_real(val) : false; } yyjson_api_inline bool yyjson_mut_is_num(yyjson_mut_val *val) { return val ? unsafe_yyjson_is_num(val) : false; } yyjson_api_inline bool yyjson_mut_is_str(yyjson_mut_val *val) { return val ? unsafe_yyjson_is_str(val) : false; } yyjson_api_inline bool yyjson_mut_is_arr(yyjson_mut_val *val) { return val ? unsafe_yyjson_is_arr(val) : false; } yyjson_api_inline bool yyjson_mut_is_obj(yyjson_mut_val *val) { return val ? unsafe_yyjson_is_obj(val) : false; } yyjson_api_inline bool yyjson_mut_is_ctn(yyjson_mut_val *val) { return val ? unsafe_yyjson_is_ctn(val) : false; } /*============================================================================== * Mutable JSON Value Content API (Implementation) *============================================================================*/ yyjson_api_inline yyjson_type yyjson_mut_get_type(yyjson_mut_val *val) { return yyjson_get_type((yyjson_val *)val); } yyjson_api_inline yyjson_subtype yyjson_mut_get_subtype(yyjson_mut_val *val) { return yyjson_get_subtype((yyjson_val *)val); } yyjson_api_inline uint8_t yyjson_mut_get_tag(yyjson_mut_val *val) { return yyjson_get_tag((yyjson_val *)val); } yyjson_api_inline const char *yyjson_mut_get_type_desc(yyjson_mut_val *val) { return yyjson_get_type_desc((yyjson_val *)val); } yyjson_api_inline const char *yyjson_mut_get_raw(yyjson_mut_val *val) { return yyjson_get_raw((yyjson_val *)val); } yyjson_api_inline bool yyjson_mut_get_bool(yyjson_mut_val *val) { return yyjson_get_bool((yyjson_val *)val); } yyjson_api_inline uint64_t yyjson_mut_get_uint(yyjson_mut_val *val) { return yyjson_get_uint((yyjson_val *)val); } yyjson_api_inline int64_t yyjson_mut_get_sint(yyjson_mut_val *val) { return yyjson_get_sint((yyjson_val *)val); } yyjson_api_inline int yyjson_mut_get_int(yyjson_mut_val *val) { return yyjson_get_int((yyjson_val *)val); } yyjson_api_inline double yyjson_mut_get_real(yyjson_mut_val *val) { return yyjson_get_real((yyjson_val *)val); } yyjson_api_inline double yyjson_mut_get_num(yyjson_mut_val *val) { return yyjson_get_num((yyjson_val *)val); } yyjson_api_inline const char *yyjson_mut_get_str(yyjson_mut_val *val) { return yyjson_get_str((yyjson_val *)val); } yyjson_api_inline size_t yyjson_mut_get_len(yyjson_mut_val *val) { return yyjson_get_len((yyjson_val *)val); } yyjson_api_inline bool yyjson_mut_equals_str(yyjson_mut_val *val, const char *str) { return yyjson_equals_str((yyjson_val *)val, str); } yyjson_api_inline bool yyjson_mut_equals_strn(yyjson_mut_val *val, const char *str, size_t len) { return yyjson_equals_strn((yyjson_val *)val, str, len); } yyjson_api bool unsafe_yyjson_mut_equals(yyjson_mut_val *lhs, yyjson_mut_val *rhs); yyjson_api_inline bool yyjson_mut_equals(yyjson_mut_val *lhs, yyjson_mut_val *rhs) { if (yyjson_unlikely(!lhs || !rhs)) return false; return unsafe_yyjson_mut_equals(lhs, rhs); } yyjson_api_inline bool yyjson_mut_set_raw(yyjson_mut_val *val, const char *raw, size_t len) { if (yyjson_unlikely(!val || !raw)) return false; unsafe_yyjson_set_raw(val, raw, len); return true; } yyjson_api_inline bool yyjson_mut_set_null(yyjson_mut_val *val) { if (yyjson_unlikely(!val)) return false; unsafe_yyjson_set_null(val); return true; } yyjson_api_inline bool yyjson_mut_set_bool(yyjson_mut_val *val, bool num) { if (yyjson_unlikely(!val)) return false; unsafe_yyjson_set_bool(val, num); return true; } yyjson_api_inline bool yyjson_mut_set_uint(yyjson_mut_val *val, uint64_t num) { if (yyjson_unlikely(!val)) return false; unsafe_yyjson_set_uint(val, num); return true; } yyjson_api_inline bool yyjson_mut_set_sint(yyjson_mut_val *val, int64_t num) { if (yyjson_unlikely(!val)) return false; unsafe_yyjson_set_sint(val, num); return true; } yyjson_api_inline bool yyjson_mut_set_int(yyjson_mut_val *val, int num) { if (yyjson_unlikely(!val)) return false; unsafe_yyjson_set_sint(val, (int64_t)num); return true; } yyjson_api_inline bool yyjson_mut_set_real(yyjson_mut_val *val, double num) { if (yyjson_unlikely(!val)) return false; unsafe_yyjson_set_real(val, num); return true; } yyjson_api_inline bool yyjson_mut_set_str(yyjson_mut_val *val, const char *str) { if (yyjson_unlikely(!val || !str)) return false; unsafe_yyjson_set_str(val, str); return true; } yyjson_api_inline bool yyjson_mut_set_strn(yyjson_mut_val *val, const char *str, size_t len) { if (yyjson_unlikely(!val || !str)) return false; unsafe_yyjson_set_strn(val, str, len); return true; } yyjson_api_inline bool yyjson_mut_set_arr(yyjson_mut_val *val) { if (yyjson_unlikely(!val)) return false; unsafe_yyjson_set_arr(val, 0); return true; } yyjson_api_inline bool yyjson_mut_set_obj(yyjson_mut_val *val) { if (yyjson_unlikely(!val)) return false; unsafe_yyjson_set_obj(val, 0); return true; } /*============================================================================== * Mutable JSON Value Creation API (Implementation) *============================================================================*/ yyjson_api_inline yyjson_mut_val *yyjson_mut_raw(yyjson_mut_doc *doc, const char *str) { if (yyjson_likely(str)) return yyjson_mut_rawn(doc, str, strlen(str)); return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_rawn(yyjson_mut_doc *doc, const char *str, size_t len) { if (yyjson_likely(doc && str)) { yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); if (yyjson_likely(val)) { val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_RAW; val->uni.str = str; return val; } } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_rawcpy(yyjson_mut_doc *doc, const char *str) { if (yyjson_likely(str)) return yyjson_mut_rawncpy(doc, str, strlen(str)); return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_rawncpy(yyjson_mut_doc *doc, const char *str, size_t len) { if (yyjson_likely(doc && str)) { yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); char *new_str = unsafe_yyjson_mut_strncpy(doc, str, len); if (yyjson_likely(val && new_str)) { val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_RAW; val->uni.str = new_str; return val; } } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_null(yyjson_mut_doc *doc) { if (yyjson_likely(doc)) { yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); if (yyjson_likely(val)) { val->tag = YYJSON_TYPE_NULL | YYJSON_SUBTYPE_NONE; return val; } } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_true(yyjson_mut_doc *doc) { if (yyjson_likely(doc)) { yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); if (yyjson_likely(val)) { val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_TRUE; return val; } } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_false(yyjson_mut_doc *doc) { if (yyjson_likely(doc)) { yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); if (yyjson_likely(val)) { val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_FALSE; return val; } } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_bool(yyjson_mut_doc *doc, bool _val) { if (yyjson_likely(doc)) { yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); if (yyjson_likely(val)) { _val = !!_val; val->tag = YYJSON_TYPE_BOOL | (uint8_t)((uint8_t)_val << 3); return val; } } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_uint(yyjson_mut_doc *doc, uint64_t num) { if (yyjson_likely(doc)) { yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); if (yyjson_likely(val)) { val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; val->uni.u64 = num; return val; } } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_sint(yyjson_mut_doc *doc, int64_t num) { if (yyjson_likely(doc)) { yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); if (yyjson_likely(val)) { val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; val->uni.i64 = num; return val; } } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_int(yyjson_mut_doc *doc, int64_t num) { return yyjson_mut_sint(doc, num); } yyjson_api_inline yyjson_mut_val *yyjson_mut_real(yyjson_mut_doc *doc, double num) { if (yyjson_likely(doc)) { yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); if (yyjson_likely(val)) { val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; val->uni.f64 = num; return val; } } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_str(yyjson_mut_doc *doc, const char *str) { if (yyjson_likely(doc && str)) { size_t len = strlen(str); bool noesc = unsafe_yyjson_is_str_noesc(str, len); yyjson_subtype sub = noesc ? YYJSON_SUBTYPE_NOESC : YYJSON_SUBTYPE_NONE; yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); if (yyjson_likely(val)) { val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | (uint64_t)(YYJSON_TYPE_STR | sub); val->uni.str = str; return val; } } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_strn(yyjson_mut_doc *doc, const char *str, size_t len) { if (yyjson_likely(doc && str)) { yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); if (yyjson_likely(val)) { val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; val->uni.str = str; return val; } } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_strcpy(yyjson_mut_doc *doc, const char *str) { if (yyjson_likely(doc && str)) { size_t len = strlen(str); bool noesc = unsafe_yyjson_is_str_noesc(str, len); yyjson_subtype sub = noesc ? YYJSON_SUBTYPE_NOESC : YYJSON_SUBTYPE_NONE; yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); char *new_str = unsafe_yyjson_mut_strncpy(doc, str, len); if (yyjson_likely(val && new_str)) { val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | (uint64_t)(YYJSON_TYPE_STR | sub); val->uni.str = new_str; return val; } } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_strncpy(yyjson_mut_doc *doc, const char *str, size_t len) { if (yyjson_likely(doc && str)) { yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); char *new_str = unsafe_yyjson_mut_strncpy(doc, str, len); if (yyjson_likely(val && new_str)) { val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; val->uni.str = new_str; return val; } } return NULL; } /*============================================================================== * Mutable JSON Array API (Implementation) *============================================================================*/ yyjson_api_inline size_t yyjson_mut_arr_size(yyjson_mut_val *arr) { return yyjson_mut_is_arr(arr) ? unsafe_yyjson_get_len(arr) : 0; } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get(yyjson_mut_val *arr, size_t idx) { if (yyjson_likely(idx < yyjson_mut_arr_size(arr))) { yyjson_mut_val *val = (yyjson_mut_val *)arr->uni.ptr; while (idx-- > 0) val = val->next; return val->next; } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_first( yyjson_mut_val *arr) { if (yyjson_likely(yyjson_mut_arr_size(arr) > 0)) { return ((yyjson_mut_val *)arr->uni.ptr)->next; } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_get_last( yyjson_mut_val *arr) { if (yyjson_likely(yyjson_mut_arr_size(arr) > 0)) { return ((yyjson_mut_val *)arr->uni.ptr); } return NULL; } /*============================================================================== * Mutable JSON Array Iterator API (Implementation) *============================================================================*/ yyjson_api_inline bool yyjson_mut_arr_iter_init(yyjson_mut_val *arr, yyjson_mut_arr_iter *iter) { if (yyjson_likely(yyjson_mut_is_arr(arr) && iter)) { iter->idx = 0; iter->max = unsafe_yyjson_get_len(arr); iter->cur = iter->max ? (yyjson_mut_val *)arr->uni.ptr : NULL; iter->pre = NULL; iter->arr = arr; return true; } if (iter) memset(iter, 0, sizeof(yyjson_mut_arr_iter)); return false; } yyjson_api_inline yyjson_mut_arr_iter yyjson_mut_arr_iter_with( yyjson_mut_val *arr) { yyjson_mut_arr_iter iter; yyjson_mut_arr_iter_init(arr, &iter); return iter; } yyjson_api_inline bool yyjson_mut_arr_iter_has_next(yyjson_mut_arr_iter *iter) { return iter ? iter->idx < iter->max : false; } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_next( yyjson_mut_arr_iter *iter) { if (iter && iter->idx < iter->max) { yyjson_mut_val *val = iter->cur; iter->pre = val; iter->cur = val->next; iter->idx++; return iter->cur; } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_iter_remove( yyjson_mut_arr_iter *iter) { if (yyjson_likely(iter && 0 < iter->idx && iter->idx <= iter->max)) { yyjson_mut_val *prev = iter->pre; yyjson_mut_val *cur = iter->cur; yyjson_mut_val *next = cur->next; if (yyjson_unlikely(iter->idx == iter->max)) iter->arr->uni.ptr = prev; iter->idx--; iter->max--; unsafe_yyjson_set_len(iter->arr, iter->max); prev->next = next; iter->cur = next; return cur; } return NULL; } /*============================================================================== * Mutable JSON Array Creation API (Implementation) *============================================================================*/ yyjson_api_inline yyjson_mut_val *yyjson_mut_arr(yyjson_mut_doc *doc) { if (yyjson_likely(doc)) { yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); if (yyjson_likely(val)) { val->tag = YYJSON_TYPE_ARR | YYJSON_SUBTYPE_NONE; return val; } } return NULL; } #define yyjson_mut_arr_with_func(func) \ if (yyjson_likely(doc && ((0 < count && count < \ (~(size_t)0) / sizeof(yyjson_mut_val) && vals) || count == 0))) { \ yyjson_mut_val *arr = unsafe_yyjson_mut_val(doc, 1 + count); \ if (yyjson_likely(arr)) { \ arr->tag = ((uint64_t)count << YYJSON_TAG_BIT) | YYJSON_TYPE_ARR; \ if (count > 0) { \ size_t i; \ for (i = 0; i < count; i++) { \ yyjson_mut_val *val = arr + i + 1; \ func \ val->next = val + 1; \ } \ arr[count].next = arr + 1; \ arr->uni.ptr = arr + count; \ } \ return arr; \ } \ } \ return NULL yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_bool( yyjson_mut_doc *doc, const bool *vals, size_t count) { yyjson_mut_arr_with_func({ bool _val = !!vals[i]; val->tag = YYJSON_TYPE_BOOL | (uint8_t)((uint8_t)_val << 3); }); } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint( yyjson_mut_doc *doc, const int64_t *vals, size_t count) { return yyjson_mut_arr_with_sint64(doc, vals, count); } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint( yyjson_mut_doc *doc, const uint64_t *vals, size_t count) { return yyjson_mut_arr_with_uint64(doc, vals, count); } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_real( yyjson_mut_doc *doc, const double *vals, size_t count) { return yyjson_mut_arr_with_double(doc, vals, count); } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint8( yyjson_mut_doc *doc, const int8_t *vals, size_t count) { yyjson_mut_arr_with_func({ val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; val->uni.i64 = (int64_t)vals[i]; }); } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint16( yyjson_mut_doc *doc, const int16_t *vals, size_t count) { yyjson_mut_arr_with_func({ val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; val->uni.i64 = vals[i]; }); } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint32( yyjson_mut_doc *doc, const int32_t *vals, size_t count) { yyjson_mut_arr_with_func({ val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; val->uni.i64 = vals[i]; }); } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_sint64( yyjson_mut_doc *doc, const int64_t *vals, size_t count) { yyjson_mut_arr_with_func({ val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; val->uni.i64 = vals[i]; }); } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint8( yyjson_mut_doc *doc, const uint8_t *vals, size_t count) { yyjson_mut_arr_with_func({ val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; val->uni.u64 = vals[i]; }); } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint16( yyjson_mut_doc *doc, const uint16_t *vals, size_t count) { yyjson_mut_arr_with_func({ val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; val->uni.u64 = vals[i]; }); } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint32( yyjson_mut_doc *doc, const uint32_t *vals, size_t count) { yyjson_mut_arr_with_func({ val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; val->uni.u64 = vals[i]; }); } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_uint64( yyjson_mut_doc *doc, const uint64_t *vals, size_t count) { yyjson_mut_arr_with_func({ val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; val->uni.u64 = vals[i]; }); } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_float( yyjson_mut_doc *doc, const float *vals, size_t count) { yyjson_mut_arr_with_func({ val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; val->uni.f64 = (double)vals[i]; }); } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_double( yyjson_mut_doc *doc, const double *vals, size_t count) { yyjson_mut_arr_with_func({ val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; val->uni.f64 = vals[i]; }); } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_str( yyjson_mut_doc *doc, const char **vals, size_t count) { yyjson_mut_arr_with_func({ uint64_t len = (uint64_t)strlen(vals[i]); val->tag = (len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; val->uni.str = vals[i]; if (yyjson_unlikely(!val->uni.str)) return NULL; }); } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strn( yyjson_mut_doc *doc, const char **vals, const size_t *lens, size_t count) { if (yyjson_unlikely(count > 0 && !lens)) return NULL; yyjson_mut_arr_with_func({ val->tag = ((uint64_t)lens[i] << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; val->uni.str = vals[i]; if (yyjson_unlikely(!val->uni.str)) return NULL; }); } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strcpy( yyjson_mut_doc *doc, const char **vals, size_t count) { size_t len; const char *str; yyjson_mut_arr_with_func({ str = vals[i]; if (!str) return NULL; len = strlen(str); val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; val->uni.str = unsafe_yyjson_mut_strncpy(doc, str, len); if (yyjson_unlikely(!val->uni.str)) return NULL; }); } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_with_strncpy( yyjson_mut_doc *doc, const char **vals, const size_t *lens, size_t count) { size_t len; const char *str; if (yyjson_unlikely(count > 0 && !lens)) return NULL; yyjson_mut_arr_with_func({ str = vals[i]; len = lens[i]; val->tag = ((uint64_t)len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; val->uni.str = unsafe_yyjson_mut_strncpy(doc, str, len); if (yyjson_unlikely(!val->uni.str)) return NULL; }); } #undef yyjson_mut_arr_with_func /*============================================================================== * Mutable JSON Array Modification API (Implementation) *============================================================================*/ yyjson_api_inline bool yyjson_mut_arr_insert(yyjson_mut_val *arr, yyjson_mut_val *val, size_t idx) { if (yyjson_likely(yyjson_mut_is_arr(arr) && val)) { size_t len = unsafe_yyjson_get_len(arr); if (yyjson_likely(idx <= len)) { unsafe_yyjson_set_len(arr, len + 1); if (len == 0) { val->next = val; arr->uni.ptr = val; } else { yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); yyjson_mut_val *next = prev->next; if (idx == len) { prev->next = val; val->next = next; arr->uni.ptr = val; } else { while (idx-- > 0) { prev = next; next = next->next; } prev->next = val; val->next = next; } } return true; } } return false; } yyjson_api_inline bool yyjson_mut_arr_append(yyjson_mut_val *arr, yyjson_mut_val *val) { if (yyjson_likely(yyjson_mut_is_arr(arr) && val)) { size_t len = unsafe_yyjson_get_len(arr); unsafe_yyjson_set_len(arr, len + 1); if (len == 0) { val->next = val; } else { yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); yyjson_mut_val *next = prev->next; prev->next = val; val->next = next; } arr->uni.ptr = val; return true; } return false; } yyjson_api_inline bool yyjson_mut_arr_prepend(yyjson_mut_val *arr, yyjson_mut_val *val) { if (yyjson_likely(yyjson_mut_is_arr(arr) && val)) { size_t len = unsafe_yyjson_get_len(arr); unsafe_yyjson_set_len(arr, len + 1); if (len == 0) { val->next = val; arr->uni.ptr = val; } else { yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); yyjson_mut_val *next = prev->next; prev->next = val; val->next = next; } return true; } return false; } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_replace(yyjson_mut_val *arr, size_t idx, yyjson_mut_val *val) { if (yyjson_likely(yyjson_mut_is_arr(arr) && val)) { size_t len = unsafe_yyjson_get_len(arr); if (yyjson_likely(idx < len)) { if (yyjson_likely(len > 1)) { yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); yyjson_mut_val *next = prev->next; while (idx-- > 0) { prev = next; next = next->next; } prev->next = val; val->next = next->next; if ((void *)next == arr->uni.ptr) arr->uni.ptr = val; return next; } else { yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); val->next = val; arr->uni.ptr = val; return prev; } } } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove(yyjson_mut_val *arr, size_t idx) { if (yyjson_likely(yyjson_mut_is_arr(arr))) { size_t len = unsafe_yyjson_get_len(arr); if (yyjson_likely(idx < len)) { unsafe_yyjson_set_len(arr, len - 1); if (yyjson_likely(len > 1)) { yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); yyjson_mut_val *next = prev->next; while (idx-- > 0) { prev = next; next = next->next; } prev->next = next->next; if ((void *)next == arr->uni.ptr) arr->uni.ptr = prev; return next; } else { return ((yyjson_mut_val *)arr->uni.ptr); } } } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove_first( yyjson_mut_val *arr) { if (yyjson_likely(yyjson_mut_is_arr(arr))) { size_t len = unsafe_yyjson_get_len(arr); if (len > 1) { yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); yyjson_mut_val *next = prev->next; prev->next = next->next; unsafe_yyjson_set_len(arr, len - 1); return next; } else if (len == 1) { yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); unsafe_yyjson_set_len(arr, 0); return prev; } } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_remove_last( yyjson_mut_val *arr) { if (yyjson_likely(yyjson_mut_is_arr(arr))) { size_t len = unsafe_yyjson_get_len(arr); if (yyjson_likely(len > 1)) { yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); yyjson_mut_val *next = prev->next; unsafe_yyjson_set_len(arr, len - 1); while (--len > 0) prev = prev->next; prev->next = next; next = (yyjson_mut_val *)arr->uni.ptr; arr->uni.ptr = prev; return next; } else if (len == 1) { yyjson_mut_val *prev = ((yyjson_mut_val *)arr->uni.ptr); unsafe_yyjson_set_len(arr, 0); return prev; } } return NULL; } yyjson_api_inline bool yyjson_mut_arr_remove_range(yyjson_mut_val *arr, size_t _idx, size_t _len) { if (yyjson_likely(yyjson_mut_is_arr(arr))) { yyjson_mut_val *prev, *next; bool tail_removed; size_t len = unsafe_yyjson_get_len(arr); if (yyjson_unlikely(_idx + _len > len)) return false; if (yyjson_unlikely(_len == 0)) return true; unsafe_yyjson_set_len(arr, len - _len); if (yyjson_unlikely(len == _len)) return true; tail_removed = (_idx + _len == len); prev = ((yyjson_mut_val *)arr->uni.ptr); while (_idx-- > 0) prev = prev->next; next = prev->next; while (_len-- > 0) next = next->next; prev->next = next; if (yyjson_unlikely(tail_removed)) arr->uni.ptr = prev; return true; } return false; } yyjson_api_inline bool yyjson_mut_arr_clear(yyjson_mut_val *arr) { if (yyjson_likely(yyjson_mut_is_arr(arr))) { unsafe_yyjson_set_len(arr, 0); return true; } return false; } yyjson_api_inline bool yyjson_mut_arr_rotate(yyjson_mut_val *arr, size_t idx) { if (yyjson_likely(yyjson_mut_is_arr(arr) && unsafe_yyjson_get_len(arr) > idx)) { yyjson_mut_val *val = (yyjson_mut_val *)arr->uni.ptr; while (idx-- > 0) val = val->next; arr->uni.ptr = (void *)val; return true; } return false; } /*============================================================================== * Mutable JSON Array Modification Convenience API (Implementation) *============================================================================*/ yyjson_api_inline bool yyjson_mut_arr_add_val(yyjson_mut_val *arr, yyjson_mut_val *val) { return yyjson_mut_arr_append(arr, val); } yyjson_api_inline bool yyjson_mut_arr_add_null(yyjson_mut_doc *doc, yyjson_mut_val *arr) { if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { yyjson_mut_val *val = yyjson_mut_null(doc); return yyjson_mut_arr_append(arr, val); } return false; } yyjson_api_inline bool yyjson_mut_arr_add_true(yyjson_mut_doc *doc, yyjson_mut_val *arr) { if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { yyjson_mut_val *val = yyjson_mut_true(doc); return yyjson_mut_arr_append(arr, val); } return false; } yyjson_api_inline bool yyjson_mut_arr_add_false(yyjson_mut_doc *doc, yyjson_mut_val *arr) { if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { yyjson_mut_val *val = yyjson_mut_false(doc); return yyjson_mut_arr_append(arr, val); } return false; } yyjson_api_inline bool yyjson_mut_arr_add_bool(yyjson_mut_doc *doc, yyjson_mut_val *arr, bool _val) { if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { yyjson_mut_val *val = yyjson_mut_bool(doc, _val); return yyjson_mut_arr_append(arr, val); } return false; } yyjson_api_inline bool yyjson_mut_arr_add_uint(yyjson_mut_doc *doc, yyjson_mut_val *arr, uint64_t num) { if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { yyjson_mut_val *val = yyjson_mut_uint(doc, num); return yyjson_mut_arr_append(arr, val); } return false; } yyjson_api_inline bool yyjson_mut_arr_add_sint(yyjson_mut_doc *doc, yyjson_mut_val *arr, int64_t num) { if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { yyjson_mut_val *val = yyjson_mut_sint(doc, num); return yyjson_mut_arr_append(arr, val); } return false; } yyjson_api_inline bool yyjson_mut_arr_add_int(yyjson_mut_doc *doc, yyjson_mut_val *arr, int64_t num) { if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { yyjson_mut_val *val = yyjson_mut_sint(doc, num); return yyjson_mut_arr_append(arr, val); } return false; } yyjson_api_inline bool yyjson_mut_arr_add_real(yyjson_mut_doc *doc, yyjson_mut_val *arr, double num) { if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { yyjson_mut_val *val = yyjson_mut_real(doc, num); return yyjson_mut_arr_append(arr, val); } return false; } yyjson_api_inline bool yyjson_mut_arr_add_str(yyjson_mut_doc *doc, yyjson_mut_val *arr, const char *str) { if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { yyjson_mut_val *val = yyjson_mut_str(doc, str); return yyjson_mut_arr_append(arr, val); } return false; } yyjson_api_inline bool yyjson_mut_arr_add_strn(yyjson_mut_doc *doc, yyjson_mut_val *arr, const char *str, size_t len) { if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { yyjson_mut_val *val = yyjson_mut_strn(doc, str, len); return yyjson_mut_arr_append(arr, val); } return false; } yyjson_api_inline bool yyjson_mut_arr_add_strcpy(yyjson_mut_doc *doc, yyjson_mut_val *arr, const char *str) { if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { yyjson_mut_val *val = yyjson_mut_strcpy(doc, str); return yyjson_mut_arr_append(arr, val); } return false; } yyjson_api_inline bool yyjson_mut_arr_add_strncpy(yyjson_mut_doc *doc, yyjson_mut_val *arr, const char *str, size_t len) { if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { yyjson_mut_val *val = yyjson_mut_strncpy(doc, str, len); return yyjson_mut_arr_append(arr, val); } return false; } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_add_arr(yyjson_mut_doc *doc, yyjson_mut_val *arr) { if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { yyjson_mut_val *val = yyjson_mut_arr(doc); return yyjson_mut_arr_append(arr, val) ? val : NULL; } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_arr_add_obj(yyjson_mut_doc *doc, yyjson_mut_val *arr) { if (yyjson_likely(doc && yyjson_mut_is_arr(arr))) { yyjson_mut_val *val = yyjson_mut_obj(doc); return yyjson_mut_arr_append(arr, val) ? val : NULL; } return NULL; } /*============================================================================== * Mutable JSON Object API (Implementation) *============================================================================*/ yyjson_api_inline size_t yyjson_mut_obj_size(yyjson_mut_val *obj) { return yyjson_mut_is_obj(obj) ? unsafe_yyjson_get_len(obj) : 0; } yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_get(yyjson_mut_val *obj, const char *key) { return yyjson_mut_obj_getn(obj, key, key ? strlen(key) : 0); } yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_getn(yyjson_mut_val *obj, const char *_key, size_t key_len) { size_t len = yyjson_mut_obj_size(obj); if (yyjson_likely(len && _key)) { yyjson_mut_val *key = ((yyjson_mut_val *)obj->uni.ptr)->next->next; while (len-- > 0) { if (unsafe_yyjson_equals_strn(key, _key, key_len)) return key->next; key = key->next->next; } } return NULL; } /*============================================================================== * Mutable JSON Object Iterator API (Implementation) *============================================================================*/ yyjson_api_inline bool yyjson_mut_obj_iter_init(yyjson_mut_val *obj, yyjson_mut_obj_iter *iter) { if (yyjson_likely(yyjson_mut_is_obj(obj) && iter)) { iter->idx = 0; iter->max = unsafe_yyjson_get_len(obj); iter->cur = iter->max ? (yyjson_mut_val *)obj->uni.ptr : NULL; iter->pre = NULL; iter->obj = obj; return true; } if (iter) memset(iter, 0, sizeof(yyjson_mut_obj_iter)); return false; } yyjson_api_inline yyjson_mut_obj_iter yyjson_mut_obj_iter_with( yyjson_mut_val *obj) { yyjson_mut_obj_iter iter; yyjson_mut_obj_iter_init(obj, &iter); return iter; } yyjson_api_inline bool yyjson_mut_obj_iter_has_next(yyjson_mut_obj_iter *iter) { return iter ? iter->idx < iter->max : false; } yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_next( yyjson_mut_obj_iter *iter) { if (iter && iter->idx < iter->max) { yyjson_mut_val *key = iter->cur; iter->pre = key; iter->cur = key->next->next; iter->idx++; return iter->cur; } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_get_val( yyjson_mut_val *key) { return key ? key->next : NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_remove( yyjson_mut_obj_iter *iter) { if (yyjson_likely(iter && 0 < iter->idx && iter->idx <= iter->max)) { yyjson_mut_val *prev = iter->pre; yyjson_mut_val *cur = iter->cur; yyjson_mut_val *next = cur->next->next; if (yyjson_unlikely(iter->idx == iter->max)) iter->obj->uni.ptr = prev; iter->idx--; iter->max--; unsafe_yyjson_set_len(iter->obj, iter->max); prev->next->next = next; iter->cur = prev; return cur->next; } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_get( yyjson_mut_obj_iter *iter, const char *key) { return yyjson_mut_obj_iter_getn(iter, key, key ? strlen(key) : 0); } yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_iter_getn( yyjson_mut_obj_iter *iter, const char *key, size_t key_len) { if (iter && key) { size_t idx = 0; size_t max = iter->max; yyjson_mut_val *pre, *cur = iter->cur; while (idx++ < max) { pre = cur; cur = cur->next->next; if (unsafe_yyjson_equals_strn(cur, key, key_len)) { iter->idx += idx; if (iter->idx > max) iter->idx -= max + 1; iter->pre = pre; iter->cur = cur; return cur->next; } } } return NULL; } /*============================================================================== * Mutable JSON Object Creation API (Implementation) *============================================================================*/ yyjson_api_inline yyjson_mut_val *yyjson_mut_obj(yyjson_mut_doc *doc) { if (yyjson_likely(doc)) { yyjson_mut_val *val = unsafe_yyjson_mut_val(doc, 1); if (yyjson_likely(val)) { val->tag = YYJSON_TYPE_OBJ | YYJSON_SUBTYPE_NONE; return val; } } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_str(yyjson_mut_doc *doc, const char **keys, const char **vals, size_t count) { if (yyjson_likely(doc && ((count > 0 && keys && vals) || (count == 0)))) { yyjson_mut_val *obj = unsafe_yyjson_mut_val(doc, 1 + count * 2); if (yyjson_likely(obj)) { obj->tag = ((uint64_t)count << YYJSON_TAG_BIT) | YYJSON_TYPE_OBJ; if (count > 0) { size_t i; for (i = 0; i < count; i++) { yyjson_mut_val *key = obj + (i * 2 + 1); yyjson_mut_val *val = obj + (i * 2 + 2); uint64_t key_len = (uint64_t)strlen(keys[i]); uint64_t val_len = (uint64_t)strlen(vals[i]); key->tag = (key_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; val->tag = (val_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; key->uni.str = keys[i]; val->uni.str = vals[i]; key->next = val; val->next = val + 1; } obj[count * 2].next = obj + 1; obj->uni.ptr = obj + (count * 2 - 1); } return obj; } } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_with_kv(yyjson_mut_doc *doc, const char **pairs, size_t count) { if (yyjson_likely(doc && ((count > 0 && pairs) || (count == 0)))) { yyjson_mut_val *obj = unsafe_yyjson_mut_val(doc, 1 + count * 2); if (yyjson_likely(obj)) { obj->tag = ((uint64_t)count << YYJSON_TAG_BIT) | YYJSON_TYPE_OBJ; if (count > 0) { size_t i; for (i = 0; i < count; i++) { yyjson_mut_val *key = obj + (i * 2 + 1); yyjson_mut_val *val = obj + (i * 2 + 2); const char *key_str = pairs[i * 2 + 0]; const char *val_str = pairs[i * 2 + 1]; uint64_t key_len = (uint64_t)strlen(key_str); uint64_t val_len = (uint64_t)strlen(val_str); key->tag = (key_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; val->tag = (val_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; key->uni.str = key_str; val->uni.str = val_str; key->next = val; val->next = val + 1; } obj[count * 2].next = obj + 1; obj->uni.ptr = obj + (count * 2 - 1); } return obj; } } return NULL; } /*============================================================================== * Mutable JSON Object Modification API (Implementation) *============================================================================*/ yyjson_api_inline void unsafe_yyjson_mut_obj_add(yyjson_mut_val *obj, yyjson_mut_val *key, yyjson_mut_val *val, size_t len) { if (yyjson_likely(len)) { yyjson_mut_val *prev_val = ((yyjson_mut_val *)obj->uni.ptr)->next; yyjson_mut_val *next_key = prev_val->next; prev_val->next = key; val->next = next_key; } else { val->next = key; } key->next = val; obj->uni.ptr = (void *)key; unsafe_yyjson_set_len(obj, len + 1); } yyjson_api_inline yyjson_mut_val *unsafe_yyjson_mut_obj_remove( yyjson_mut_val *obj, const char *key, size_t key_len) { size_t obj_len = unsafe_yyjson_get_len(obj); if (obj_len) { yyjson_mut_val *pre_key = (yyjson_mut_val *)obj->uni.ptr; yyjson_mut_val *cur_key = pre_key->next->next; yyjson_mut_val *removed_item = NULL; size_t i; for (i = 0; i < obj_len; i++) { if (unsafe_yyjson_equals_strn(cur_key, key, key_len)) { if (!removed_item) removed_item = cur_key->next; cur_key = cur_key->next->next; pre_key->next->next = cur_key; if (i + 1 == obj_len) obj->uni.ptr = pre_key; i--; obj_len--; } else { pre_key = cur_key; cur_key = cur_key->next->next; } } unsafe_yyjson_set_len(obj, obj_len); return removed_item; } else { return NULL; } } yyjson_api_inline bool unsafe_yyjson_mut_obj_replace(yyjson_mut_val *obj, yyjson_mut_val *key, yyjson_mut_val *val) { size_t key_len = unsafe_yyjson_get_len(key); size_t obj_len = unsafe_yyjson_get_len(obj); if (obj_len) { yyjson_mut_val *pre_key = (yyjson_mut_val *)obj->uni.ptr; yyjson_mut_val *cur_key = pre_key->next->next; size_t i; for (i = 0; i < obj_len; i++) { if (unsafe_yyjson_equals_strn(cur_key, key->uni.str, key_len)) { cur_key->next->tag = val->tag; cur_key->next->uni.u64 = val->uni.u64; return true; } else { cur_key = cur_key->next->next; } } } return false; } yyjson_api_inline void unsafe_yyjson_mut_obj_rotate(yyjson_mut_val *obj, size_t idx) { yyjson_mut_val *key = (yyjson_mut_val *)obj->uni.ptr; while (idx-- > 0) key = key->next->next; obj->uni.ptr = (void *)key; } yyjson_api_inline bool yyjson_mut_obj_add(yyjson_mut_val *obj, yyjson_mut_val *key, yyjson_mut_val *val) { if (yyjson_likely(yyjson_mut_is_obj(obj) && yyjson_mut_is_str(key) && val)) { unsafe_yyjson_mut_obj_add(obj, key, val, unsafe_yyjson_get_len(obj)); return true; } return false; } yyjson_api_inline bool yyjson_mut_obj_put(yyjson_mut_val *obj, yyjson_mut_val *key, yyjson_mut_val *val) { bool replaced = false; size_t key_len; yyjson_mut_obj_iter iter; yyjson_mut_val *cur_key; if (yyjson_unlikely(!yyjson_mut_is_obj(obj) || !yyjson_mut_is_str(key))) return false; key_len = unsafe_yyjson_get_len(key); yyjson_mut_obj_iter_init(obj, &iter); while ((cur_key = yyjson_mut_obj_iter_next(&iter)) != 0) { if (unsafe_yyjson_equals_strn(cur_key, key->uni.str, key_len)) { if (!replaced && val) { replaced = true; val->next = cur_key->next->next; cur_key->next = val; } else { yyjson_mut_obj_iter_remove(&iter); } } } if (!replaced && val) unsafe_yyjson_mut_obj_add(obj, key, val, iter.max); return true; } yyjson_api_inline bool yyjson_mut_obj_insert(yyjson_mut_val *obj, yyjson_mut_val *key, yyjson_mut_val *val, size_t idx) { if (yyjson_likely(yyjson_mut_is_obj(obj) && yyjson_mut_is_str(key) && val)) { size_t len = unsafe_yyjson_get_len(obj); if (yyjson_likely(len >= idx)) { if (len > idx) { void *ptr = obj->uni.ptr; unsafe_yyjson_mut_obj_rotate(obj, idx); unsafe_yyjson_mut_obj_add(obj, key, val, len); obj->uni.ptr = ptr; } else { unsafe_yyjson_mut_obj_add(obj, key, val, len); } return true; } } return false; } yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove(yyjson_mut_val *obj, yyjson_mut_val *key) { if (yyjson_likely(yyjson_mut_is_obj(obj) && yyjson_mut_is_str(key))) { return unsafe_yyjson_mut_obj_remove(obj, key->uni.str, unsafe_yyjson_get_len(key)); } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_key( yyjson_mut_val *obj, const char *key) { if (yyjson_likely(yyjson_mut_is_obj(obj) && key)) { size_t key_len = strlen(key); return unsafe_yyjson_mut_obj_remove(obj, key, key_len); } return NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_keyn( yyjson_mut_val *obj, const char *key, size_t key_len) { if (yyjson_likely(yyjson_mut_is_obj(obj) && key)) { return unsafe_yyjson_mut_obj_remove(obj, key, key_len); } return NULL; } yyjson_api_inline bool yyjson_mut_obj_clear(yyjson_mut_val *obj) { if (yyjson_likely(yyjson_mut_is_obj(obj))) { unsafe_yyjson_set_len(obj, 0); return true; } return false; } yyjson_api_inline bool yyjson_mut_obj_replace(yyjson_mut_val *obj, yyjson_mut_val *key, yyjson_mut_val *val) { if (yyjson_likely(yyjson_mut_is_obj(obj) && yyjson_mut_is_str(key) && val)) { return unsafe_yyjson_mut_obj_replace(obj, key, val); } return false; } yyjson_api_inline bool yyjson_mut_obj_rotate(yyjson_mut_val *obj, size_t idx) { if (yyjson_likely(yyjson_mut_is_obj(obj) && unsafe_yyjson_get_len(obj) > idx)) { unsafe_yyjson_mut_obj_rotate(obj, idx); return true; } return false; } /*============================================================================== * Mutable JSON Object Modification Convenience API (Implementation) *============================================================================*/ #define yyjson_mut_obj_add_func(func) \ if (yyjson_likely(doc && yyjson_mut_is_obj(obj) && _key)) { \ yyjson_mut_val *key = unsafe_yyjson_mut_val(doc, 2); \ if (yyjson_likely(key)) { \ size_t len = unsafe_yyjson_get_len(obj); \ yyjson_mut_val *val = key + 1; \ size_t key_len = strlen(_key); \ bool noesc = unsafe_yyjson_is_str_noesc(_key, key_len); \ key->tag = YYJSON_TYPE_STR; \ key->tag |= noesc ? YYJSON_SUBTYPE_NOESC : YYJSON_SUBTYPE_NONE; \ key->tag |= (uint64_t)strlen(_key) << YYJSON_TAG_BIT; \ key->uni.str = _key; \ func \ unsafe_yyjson_mut_obj_add(obj, key, val, len); \ return true; \ } \ } \ return false yyjson_api_inline bool yyjson_mut_obj_add_null(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *_key) { yyjson_mut_obj_add_func({ val->tag = YYJSON_TYPE_NULL | YYJSON_SUBTYPE_NONE; }); } yyjson_api_inline bool yyjson_mut_obj_add_true(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *_key) { yyjson_mut_obj_add_func({ val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_TRUE; }); } yyjson_api_inline bool yyjson_mut_obj_add_false(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *_key) { yyjson_mut_obj_add_func({ val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_FALSE; }); } yyjson_api_inline bool yyjson_mut_obj_add_bool(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *_key, bool _val) { yyjson_mut_obj_add_func({ _val = !!_val; val->tag = YYJSON_TYPE_BOOL | (uint8_t)((uint8_t)(_val) << 3); }); } yyjson_api_inline bool yyjson_mut_obj_add_uint(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *_key, uint64_t _val) { yyjson_mut_obj_add_func({ val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_UINT; val->uni.u64 = _val; }); } yyjson_api_inline bool yyjson_mut_obj_add_sint(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *_key, int64_t _val) { yyjson_mut_obj_add_func({ val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; val->uni.i64 = _val; }); } yyjson_api_inline bool yyjson_mut_obj_add_int(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *_key, int64_t _val) { yyjson_mut_obj_add_func({ val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_SINT; val->uni.i64 = _val; }); } yyjson_api_inline bool yyjson_mut_obj_add_real(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *_key, double _val) { yyjson_mut_obj_add_func({ val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; val->uni.f64 = _val; }); } yyjson_api_inline bool yyjson_mut_obj_add_str(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *_key, const char *_val) { if (yyjson_unlikely(!_val)) return false; yyjson_mut_obj_add_func({ size_t val_len = strlen(_val); bool val_noesc = unsafe_yyjson_is_str_noesc(_val, val_len); val->tag = ((uint64_t)strlen(_val) << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; val->tag |= val_noesc ? YYJSON_SUBTYPE_NOESC : YYJSON_SUBTYPE_NONE; val->uni.str = _val; }); } yyjson_api_inline bool yyjson_mut_obj_add_strn(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *_key, const char *_val, size_t _len) { if (yyjson_unlikely(!_val)) return false; yyjson_mut_obj_add_func({ val->tag = ((uint64_t)_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; val->uni.str = _val; }); } yyjson_api_inline bool yyjson_mut_obj_add_strcpy(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *_key, const char *_val) { if (yyjson_unlikely(!_val)) return false; yyjson_mut_obj_add_func({ size_t _len = strlen(_val); val->uni.str = unsafe_yyjson_mut_strncpy(doc, _val, _len); if (yyjson_unlikely(!val->uni.str)) return false; val->tag = ((uint64_t)_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; }); } yyjson_api_inline bool yyjson_mut_obj_add_strncpy(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *_key, const char *_val, size_t _len) { if (yyjson_unlikely(!_val)) return false; yyjson_mut_obj_add_func({ val->uni.str = unsafe_yyjson_mut_strncpy(doc, _val, _len); if (yyjson_unlikely(!val->uni.str)) return false; val->tag = ((uint64_t)_len << YYJSON_TAG_BIT) | YYJSON_TYPE_STR; }); } yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_add_arr(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *_key) { yyjson_mut_val *key = yyjson_mut_str(doc, _key); yyjson_mut_val *val = yyjson_mut_arr(doc); return yyjson_mut_obj_add(obj, key, val) ? val : NULL; } yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_add_obj(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *_key) { yyjson_mut_val *key = yyjson_mut_str(doc, _key); yyjson_mut_val *val = yyjson_mut_obj(doc); return yyjson_mut_obj_add(obj, key, val) ? val : NULL; } yyjson_api_inline bool yyjson_mut_obj_add_val(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *_key, yyjson_mut_val *_val) { if (yyjson_unlikely(!_val)) return false; yyjson_mut_obj_add_func({ val = _val; }); } yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_str(yyjson_mut_val *obj, const char *key) { return yyjson_mut_obj_remove_strn(obj, key, key ? strlen(key) : 0); } yyjson_api_inline yyjson_mut_val *yyjson_mut_obj_remove_strn( yyjson_mut_val *obj, const char *_key, size_t _len) { if (yyjson_likely(yyjson_mut_is_obj(obj) && _key)) { yyjson_mut_val *key; yyjson_mut_obj_iter iter; yyjson_mut_val *val_removed = NULL; yyjson_mut_obj_iter_init(obj, &iter); while ((key = yyjson_mut_obj_iter_next(&iter)) != NULL) { if (unsafe_yyjson_equals_strn(key, _key, _len)) { if (!val_removed) val_removed = key->next; yyjson_mut_obj_iter_remove(&iter); } } return val_removed; } return NULL; } yyjson_api_inline bool yyjson_mut_obj_rename_key(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, const char *new_key) { if (!key || !new_key) return false; return yyjson_mut_obj_rename_keyn(doc, obj, key, strlen(key), new_key, strlen(new_key)); } yyjson_api_inline bool yyjson_mut_obj_rename_keyn(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key, size_t len, const char *new_key, size_t new_len) { char *cpy_key = NULL; yyjson_mut_val *old_key; yyjson_mut_obj_iter iter; if (!doc || !obj || !key || !new_key) return false; yyjson_mut_obj_iter_init(obj, &iter); while ((old_key = yyjson_mut_obj_iter_next(&iter))) { if (unsafe_yyjson_equals_strn((void *)old_key, key, len)) { if (!cpy_key) { cpy_key = unsafe_yyjson_mut_strncpy(doc, new_key, new_len); if (!cpy_key) return false; } yyjson_mut_set_strn(old_key, cpy_key, new_len); } } return cpy_key != NULL; } /*============================================================================== * JSON Pointer API (Implementation) *============================================================================*/ #define yyjson_ptr_set_err(_code, _msg) do { \ if (err) { \ err->code = YYJSON_PTR_ERR_##_code; \ err->msg = _msg; \ err->pos = 0; \ } \ } while(false) /* require: val != NULL, *ptr == '/', len > 0 */ yyjson_api yyjson_val *unsafe_yyjson_ptr_getx(yyjson_val *val, const char *ptr, size_t len, yyjson_ptr_err *err); /* require: val != NULL, *ptr == '/', len > 0 */ yyjson_api yyjson_mut_val *unsafe_yyjson_mut_ptr_getx(yyjson_mut_val *val, const char *ptr, size_t len, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); /* require: val/new_val/doc != NULL, *ptr == '/', len > 0 */ yyjson_api bool unsafe_yyjson_mut_ptr_putx(yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, yyjson_mut_doc *doc, bool create_parent, bool insert_new, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); /* require: val/err != NULL, *ptr == '/', len > 0 */ yyjson_api yyjson_mut_val *unsafe_yyjson_mut_ptr_replacex( yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); /* require: val/err != NULL, *ptr == '/', len > 0 */ yyjson_api yyjson_mut_val *unsafe_yyjson_mut_ptr_removex(yyjson_mut_val *val, const char *ptr, size_t len, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err); yyjson_api_inline yyjson_val *yyjson_doc_ptr_get(yyjson_doc *doc, const char *ptr) { if (yyjson_unlikely(!ptr)) return NULL; return yyjson_doc_ptr_getn(doc, ptr, strlen(ptr)); } yyjson_api_inline yyjson_val *yyjson_doc_ptr_getn(yyjson_doc *doc, const char *ptr, size_t len) { return yyjson_doc_ptr_getx(doc, ptr, len, NULL); } yyjson_api_inline yyjson_val *yyjson_doc_ptr_getx(yyjson_doc *doc, const char *ptr, size_t len, yyjson_ptr_err *err) { yyjson_ptr_set_err(NONE, NULL); if (yyjson_unlikely(!doc || !ptr)) { yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); return NULL; } if (yyjson_unlikely(!doc->root)) { yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL"); return NULL; } if (yyjson_unlikely(len == 0)) { return doc->root; } if (yyjson_unlikely(*ptr != '/')) { yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); return NULL; } return unsafe_yyjson_ptr_getx(doc->root, ptr, len, err); } yyjson_api_inline yyjson_val *yyjson_ptr_get(yyjson_val *val, const char *ptr) { if (yyjson_unlikely(!ptr)) return NULL; return yyjson_ptr_getn(val, ptr, strlen(ptr)); } yyjson_api_inline yyjson_val *yyjson_ptr_getn(yyjson_val *val, const char *ptr, size_t len) { return yyjson_ptr_getx(val, ptr, len, NULL); } yyjson_api_inline yyjson_val *yyjson_ptr_getx(yyjson_val *val, const char *ptr, size_t len, yyjson_ptr_err *err) { yyjson_ptr_set_err(NONE, NULL); if (yyjson_unlikely(!val || !ptr)) { yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); return NULL; } if (yyjson_unlikely(len == 0)) { return val; } if (yyjson_unlikely(*ptr != '/')) { yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); return NULL; } return unsafe_yyjson_ptr_getx(val, ptr, len, err); } yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_get(yyjson_mut_doc *doc, const char *ptr) { if (!ptr) return NULL; return yyjson_mut_doc_ptr_getn(doc, ptr, strlen(ptr)); } yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_getn(yyjson_mut_doc *doc, const char *ptr, size_t len) { return yyjson_mut_doc_ptr_getx(doc, ptr, len, NULL, NULL); } yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_getx(yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { yyjson_ptr_set_err(NONE, NULL); if (ctx) memset(ctx, 0, sizeof(*ctx)); if (yyjson_unlikely(!doc || !ptr)) { yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); return NULL; } if (yyjson_unlikely(!doc->root)) { yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL"); return NULL; } if (yyjson_unlikely(len == 0)) { return doc->root; } if (yyjson_unlikely(*ptr != '/')) { yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); return NULL; } return unsafe_yyjson_mut_ptr_getx(doc->root, ptr, len, ctx, err); } yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_get(yyjson_mut_val *val, const char *ptr) { if (!ptr) return NULL; return yyjson_mut_ptr_getn(val, ptr, strlen(ptr)); } yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_getn(yyjson_mut_val *val, const char *ptr, size_t len) { return yyjson_mut_ptr_getx(val, ptr, len, NULL, NULL); } yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_getx(yyjson_mut_val *val, const char *ptr, size_t len, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { yyjson_ptr_set_err(NONE, NULL); if (ctx) memset(ctx, 0, sizeof(*ctx)); if (yyjson_unlikely(!val || !ptr)) { yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); return NULL; } if (yyjson_unlikely(len == 0)) { return val; } if (yyjson_unlikely(*ptr != '/')) { yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); return NULL; } return unsafe_yyjson_mut_ptr_getx(val, ptr, len, ctx, err); } yyjson_api_inline bool yyjson_mut_doc_ptr_add(yyjson_mut_doc *doc, const char *ptr, yyjson_mut_val *new_val) { if (yyjson_unlikely(!ptr)) return false; return yyjson_mut_doc_ptr_addn(doc, ptr, strlen(ptr), new_val); } yyjson_api_inline bool yyjson_mut_doc_ptr_addn(yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val) { return yyjson_mut_doc_ptr_addx(doc, ptr, len, new_val, true, NULL, NULL); } yyjson_api_inline bool yyjson_mut_doc_ptr_addx(yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val, bool create_parent, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { yyjson_ptr_set_err(NONE, NULL); if (ctx) memset(ctx, 0, sizeof(*ctx)); if (yyjson_unlikely(!doc || !ptr || !new_val)) { yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); return false; } if (yyjson_unlikely(len == 0)) { if (doc->root) { yyjson_ptr_set_err(SET_ROOT, "cannot set document's root"); return false; } else { doc->root = new_val; return true; } } if (yyjson_unlikely(*ptr != '/')) { yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); return false; } if (yyjson_unlikely(!doc->root && !create_parent)) { yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL"); return false; } if (yyjson_unlikely(!doc->root)) { yyjson_mut_val *root = yyjson_mut_obj(doc); if (yyjson_unlikely(!root)) { yyjson_ptr_set_err(MEMORY_ALLOCATION, "failed to create value"); return false; } if (unsafe_yyjson_mut_ptr_putx(root, ptr, len, new_val, doc, create_parent, true, ctx, err)) { doc->root = root; return true; } return false; } return unsafe_yyjson_mut_ptr_putx(doc->root, ptr, len, new_val, doc, create_parent, true, ctx, err); } yyjson_api_inline bool yyjson_mut_ptr_add(yyjson_mut_val *val, const char *ptr, yyjson_mut_val *new_val, yyjson_mut_doc *doc) { if (yyjson_unlikely(!ptr)) return false; return yyjson_mut_ptr_addn(val, ptr, strlen(ptr), new_val, doc); } yyjson_api_inline bool yyjson_mut_ptr_addn(yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, yyjson_mut_doc *doc) { return yyjson_mut_ptr_addx(val, ptr, len, new_val, doc, true, NULL, NULL); } yyjson_api_inline bool yyjson_mut_ptr_addx(yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, yyjson_mut_doc *doc, bool create_parent, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { yyjson_ptr_set_err(NONE, NULL); if (ctx) memset(ctx, 0, sizeof(*ctx)); if (yyjson_unlikely(!val || !ptr || !new_val || !doc)) { yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); return false; } if (yyjson_unlikely(len == 0)) { yyjson_ptr_set_err(SET_ROOT, "cannot set root"); return false; } if (yyjson_unlikely(*ptr != '/')) { yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); return false; } return unsafe_yyjson_mut_ptr_putx(val, ptr, len, new_val, doc, create_parent, true, ctx, err); } yyjson_api_inline bool yyjson_mut_doc_ptr_set(yyjson_mut_doc *doc, const char *ptr, yyjson_mut_val *new_val) { if (yyjson_unlikely(!ptr)) return false; return yyjson_mut_doc_ptr_setn(doc, ptr, strlen(ptr), new_val); } yyjson_api_inline bool yyjson_mut_doc_ptr_setn(yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val) { return yyjson_mut_doc_ptr_setx(doc, ptr, len, new_val, true, NULL, NULL); } yyjson_api_inline bool yyjson_mut_doc_ptr_setx(yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val, bool create_parent, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { yyjson_ptr_set_err(NONE, NULL); if (ctx) memset(ctx, 0, sizeof(*ctx)); if (yyjson_unlikely(!doc || !ptr)) { yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); return false; } if (yyjson_unlikely(len == 0)) { if (ctx) ctx->old = doc->root; doc->root = new_val; return true; } if (yyjson_unlikely(*ptr != '/')) { yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); return false; } if (!new_val) { if (!doc->root) { yyjson_ptr_set_err(RESOLVE, "JSON pointer cannot be resolved"); return false; } return !!unsafe_yyjson_mut_ptr_removex(doc->root, ptr, len, ctx, err); } if (yyjson_unlikely(!doc->root && !create_parent)) { yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL"); return false; } if (yyjson_unlikely(!doc->root)) { yyjson_mut_val *root = yyjson_mut_obj(doc); if (yyjson_unlikely(!root)) { yyjson_ptr_set_err(MEMORY_ALLOCATION, "failed to create value"); return false; } if (unsafe_yyjson_mut_ptr_putx(root, ptr, len, new_val, doc, create_parent, false, ctx, err)) { doc->root = root; return true; } return false; } return unsafe_yyjson_mut_ptr_putx(doc->root, ptr, len, new_val, doc, create_parent, false, ctx, err); } yyjson_api_inline bool yyjson_mut_ptr_set(yyjson_mut_val *val, const char *ptr, yyjson_mut_val *new_val, yyjson_mut_doc *doc) { if (yyjson_unlikely(!ptr)) return false; return yyjson_mut_ptr_setn(val, ptr, strlen(ptr), new_val, doc); } yyjson_api_inline bool yyjson_mut_ptr_setn(yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, yyjson_mut_doc *doc) { return yyjson_mut_ptr_setx(val, ptr, len, new_val, doc, true, NULL, NULL); } yyjson_api_inline bool yyjson_mut_ptr_setx(yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, yyjson_mut_doc *doc, bool create_parent, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { yyjson_ptr_set_err(NONE, NULL); if (ctx) memset(ctx, 0, sizeof(*ctx)); if (yyjson_unlikely(!val || !ptr || !doc)) { yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); return false; } if (yyjson_unlikely(len == 0)) { yyjson_ptr_set_err(SET_ROOT, "cannot set root"); return false; } if (yyjson_unlikely(*ptr != '/')) { yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); return false; } if (!new_val) { return !!unsafe_yyjson_mut_ptr_removex(val, ptr, len, ctx, err); } return unsafe_yyjson_mut_ptr_putx(val, ptr, len, new_val, doc, create_parent, false, ctx, err); } yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replace( yyjson_mut_doc *doc, const char *ptr, yyjson_mut_val *new_val) { if (!ptr) return NULL; return yyjson_mut_doc_ptr_replacen(doc, ptr, strlen(ptr), new_val); } yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replacen( yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val) { return yyjson_mut_doc_ptr_replacex(doc, ptr, len, new_val, NULL, NULL); } yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_replacex( yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_mut_val *new_val, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { yyjson_ptr_set_err(NONE, NULL); if (ctx) memset(ctx, 0, sizeof(*ctx)); if (yyjson_unlikely(!doc || !ptr || !new_val)) { yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); return NULL; } if (yyjson_unlikely(len == 0)) { yyjson_mut_val *root = doc->root; if (yyjson_unlikely(!root)) { yyjson_ptr_set_err(RESOLVE, "JSON pointer cannot be resolved"); return NULL; } if (ctx) ctx->old = root; doc->root = new_val; return root; } if (yyjson_unlikely(!doc->root)) { yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL"); return NULL; } if (yyjson_unlikely(*ptr != '/')) { yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); return NULL; } return unsafe_yyjson_mut_ptr_replacex(doc->root, ptr, len, new_val, ctx, err); } yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replace( yyjson_mut_val *val, const char *ptr, yyjson_mut_val *new_val) { if (!ptr) return NULL; return yyjson_mut_ptr_replacen(val, ptr, strlen(ptr), new_val); } yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replacen( yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val) { return yyjson_mut_ptr_replacex(val, ptr, len, new_val, NULL, NULL); } yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_replacex( yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { yyjson_ptr_set_err(NONE, NULL); if (ctx) memset(ctx, 0, sizeof(*ctx)); if (yyjson_unlikely(!val || !ptr || !new_val)) { yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); return NULL; } if (yyjson_unlikely(len == 0)) { yyjson_ptr_set_err(SET_ROOT, "cannot set root"); return NULL; } if (yyjson_unlikely(*ptr != '/')) { yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); return NULL; } return unsafe_yyjson_mut_ptr_replacex(val, ptr, len, new_val, ctx, err); } yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_remove( yyjson_mut_doc *doc, const char *ptr) { if (!ptr) return NULL; return yyjson_mut_doc_ptr_removen(doc, ptr, strlen(ptr)); } yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_removen( yyjson_mut_doc *doc, const char *ptr, size_t len) { return yyjson_mut_doc_ptr_removex(doc, ptr, len, NULL, NULL); } yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_ptr_removex( yyjson_mut_doc *doc, const char *ptr, size_t len, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { yyjson_ptr_set_err(NONE, NULL); if (ctx) memset(ctx, 0, sizeof(*ctx)); if (yyjson_unlikely(!doc || !ptr)) { yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); return NULL; } if (yyjson_unlikely(!doc->root)) { yyjson_ptr_set_err(NULL_ROOT, "document's root is NULL"); return NULL; } if (yyjson_unlikely(len == 0)) { yyjson_mut_val *root = doc->root; if (ctx) ctx->old = root; doc->root = NULL; return root; } if (yyjson_unlikely(*ptr != '/')) { yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); return NULL; } return unsafe_yyjson_mut_ptr_removex(doc->root, ptr, len, ctx, err); } yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_remove(yyjson_mut_val *val, const char *ptr) { if (!ptr) return NULL; return yyjson_mut_ptr_removen(val, ptr, strlen(ptr)); } yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_removen(yyjson_mut_val *val, const char *ptr, size_t len) { return yyjson_mut_ptr_removex(val, ptr, len, NULL, NULL); } yyjson_api_inline yyjson_mut_val *yyjson_mut_ptr_removex(yyjson_mut_val *val, const char *ptr, size_t len, yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) { yyjson_ptr_set_err(NONE, NULL); if (ctx) memset(ctx, 0, sizeof(*ctx)); if (yyjson_unlikely(!val || !ptr)) { yyjson_ptr_set_err(PARAMETER, "input parameter is NULL"); return NULL; } if (yyjson_unlikely(len == 0)) { yyjson_ptr_set_err(SET_ROOT, "cannot set root"); return NULL; } if (yyjson_unlikely(*ptr != '/')) { yyjson_ptr_set_err(SYNTAX, "no prefix '/'"); return NULL; } return unsafe_yyjson_mut_ptr_removex(val, ptr, len, ctx, err); } yyjson_api_inline bool yyjson_ptr_ctx_append(yyjson_ptr_ctx *ctx, yyjson_mut_val *key, yyjson_mut_val *val) { yyjson_mut_val *ctn, *pre_key, *pre_val, *cur_key, *cur_val; if (!ctx || !ctx->ctn || !val) return false; ctn = ctx->ctn; if (yyjson_mut_is_obj(ctn)) { if (!key) return false; key->next = val; pre_key = ctx->pre; if (unsafe_yyjson_get_len(ctn) == 0) { val->next = key; ctn->uni.ptr = key; ctx->pre = key; } else if (!pre_key) { pre_key = (yyjson_mut_val *)ctn->uni.ptr; pre_val = pre_key->next; val->next = pre_val->next; pre_val->next = key; ctn->uni.ptr = key; ctx->pre = pre_key; } else { cur_key = pre_key->next->next; cur_val = cur_key->next; val->next = cur_val->next; cur_val->next = key; if (ctn->uni.ptr == cur_key) ctn->uni.ptr = key; ctx->pre = cur_key; } } else { pre_val = ctx->pre; if (unsafe_yyjson_get_len(ctn) == 0) { val->next = val; ctn->uni.ptr = val; ctx->pre = val; } else if (!pre_val) { pre_val = (yyjson_mut_val *)ctn->uni.ptr; val->next = pre_val->next; pre_val->next = val; ctn->uni.ptr = val; ctx->pre = pre_val; } else { cur_val = pre_val->next; val->next = cur_val->next; cur_val->next = val; if (ctn->uni.ptr == cur_val) ctn->uni.ptr = val; ctx->pre = cur_val; } } unsafe_yyjson_inc_len(ctn); return true; } yyjson_api_inline bool yyjson_ptr_ctx_replace(yyjson_ptr_ctx *ctx, yyjson_mut_val *val) { yyjson_mut_val *ctn, *pre_key, *cur_key, *pre_val, *cur_val; if (!ctx || !ctx->ctn || !ctx->pre || !val) return false; ctn = ctx->ctn; if (yyjson_mut_is_obj(ctn)) { pre_key = ctx->pre; pre_val = pre_key->next; cur_key = pre_val->next; cur_val = cur_key->next; /* replace current value */ cur_key->next = val; val->next = cur_val->next; ctx->old = cur_val; } else { pre_val = ctx->pre; cur_val = pre_val->next; /* replace current value */ if (pre_val != cur_val) { val->next = cur_val->next; pre_val->next = val; if (ctn->uni.ptr == cur_val) ctn->uni.ptr = val; } else { val->next = val; ctn->uni.ptr = val; ctx->pre = val; } ctx->old = cur_val; } return true; } yyjson_api_inline bool yyjson_ptr_ctx_remove(yyjson_ptr_ctx *ctx) { yyjson_mut_val *ctn, *pre_key, *pre_val, *cur_key, *cur_val; size_t len; if (!ctx || !ctx->ctn || !ctx->pre) return false; ctn = ctx->ctn; if (yyjson_mut_is_obj(ctn)) { pre_key = ctx->pre; pre_val = pre_key->next; cur_key = pre_val->next; cur_val = cur_key->next; /* remove current key-value */ pre_val->next = cur_val->next; if (ctn->uni.ptr == cur_key) ctn->uni.ptr = pre_key; ctx->pre = NULL; ctx->old = cur_val; } else { pre_val = ctx->pre; cur_val = pre_val->next; /* remove current key-value */ pre_val->next = cur_val->next; if (ctn->uni.ptr == cur_val) ctn->uni.ptr = pre_val; ctx->pre = NULL; ctx->old = cur_val; } len = unsafe_yyjson_get_len(ctn) - 1; if (len == 0) ctn->uni.ptr = NULL; unsafe_yyjson_set_len(ctn, len); return true; } #undef yyjson_ptr_set_err /*============================================================================== * JSON Value at Pointer API (Implementation) *============================================================================*/ /** Set provided `value` if the JSON Pointer (RFC 6901) exists and is type bool. Returns true if value at `ptr` exists and is the correct type, otherwise false. */ yyjson_api_inline bool yyjson_ptr_get_bool( yyjson_val *root, const char *ptr, bool *value) { yyjson_val *val = yyjson_ptr_get(root, ptr); if (value && yyjson_is_bool(val)) { *value = unsafe_yyjson_get_bool(val); return true; } else { return false; } } /** Set provided `value` if the JSON Pointer (RFC 6901) exists and is an integer that fits in `uint64_t`. Returns true if successful, otherwise false. */ yyjson_api_inline bool yyjson_ptr_get_uint( yyjson_val *root, const char *ptr, uint64_t *value) { yyjson_val *val = yyjson_ptr_get(root, ptr); if (value && val) { uint64_t ret = val->uni.u64; if (unsafe_yyjson_is_uint(val) || (unsafe_yyjson_is_sint(val) && !(ret >> 63))) { *value = ret; return true; } } return false; } /** Set provided `value` if the JSON Pointer (RFC 6901) exists and is an integer that fits in `int64_t`. Returns true if successful, otherwise false. */ yyjson_api_inline bool yyjson_ptr_get_sint( yyjson_val *root, const char *ptr, int64_t *value) { yyjson_val *val = yyjson_ptr_get(root, ptr); if (value && val) { int64_t ret = val->uni.i64; if (unsafe_yyjson_is_sint(val) || (unsafe_yyjson_is_uint(val) && ret >= 0)) { *value = ret; return true; } } return false; } /** Set provided `value` if the JSON Pointer (RFC 6901) exists and is type real. Returns true if value at `ptr` exists and is the correct type, otherwise false. */ yyjson_api_inline bool yyjson_ptr_get_real( yyjson_val *root, const char *ptr, double *value) { yyjson_val *val = yyjson_ptr_get(root, ptr); if (value && yyjson_is_real(val)) { *value = unsafe_yyjson_get_real(val); return true; } else { return false; } } /** Set provided `value` if the JSON Pointer (RFC 6901) exists and is type sint, uint or real. Returns true if value at `ptr` exists and is the correct type, otherwise false. */ yyjson_api_inline bool yyjson_ptr_get_num( yyjson_val *root, const char *ptr, double *value) { yyjson_val *val = yyjson_ptr_get(root, ptr); if (value && yyjson_is_num(val)) { *value = unsafe_yyjson_get_num(val); return true; } else { return false; } } /** Set provided `value` if the JSON Pointer (RFC 6901) exists and is type string. Returns true if value at `ptr` exists and is the correct type, otherwise false. */ yyjson_api_inline bool yyjson_ptr_get_str( yyjson_val *root, const char *ptr, const char **value) { yyjson_val *val = yyjson_ptr_get(root, ptr); if (value && yyjson_is_str(val)) { *value = unsafe_yyjson_get_str(val); return true; } else { return false; } } /*============================================================================== * Deprecated *============================================================================*/ /** @deprecated renamed to `yyjson_doc_ptr_get` */ yyjson_deprecated("renamed to yyjson_doc_ptr_get") yyjson_api_inline yyjson_val *yyjson_doc_get_pointer(yyjson_doc *doc, const char *ptr) { return yyjson_doc_ptr_get(doc, ptr); } /** @deprecated renamed to `yyjson_doc_ptr_getn` */ yyjson_deprecated("renamed to yyjson_doc_ptr_getn") yyjson_api_inline yyjson_val *yyjson_doc_get_pointern(yyjson_doc *doc, const char *ptr, size_t len) { return yyjson_doc_ptr_getn(doc, ptr, len); } /** @deprecated renamed to `yyjson_mut_doc_ptr_get` */ yyjson_deprecated("renamed to yyjson_mut_doc_ptr_get") yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_get_pointer( yyjson_mut_doc *doc, const char *ptr) { return yyjson_mut_doc_ptr_get(doc, ptr); } /** @deprecated renamed to `yyjson_mut_doc_ptr_getn` */ yyjson_deprecated("renamed to yyjson_mut_doc_ptr_getn") yyjson_api_inline yyjson_mut_val *yyjson_mut_doc_get_pointern( yyjson_mut_doc *doc, const char *ptr, size_t len) { return yyjson_mut_doc_ptr_getn(doc, ptr, len); } /** @deprecated renamed to `yyjson_ptr_get` */ yyjson_deprecated("renamed to yyjson_ptr_get") yyjson_api_inline yyjson_val *yyjson_get_pointer(yyjson_val *val, const char *ptr) { return yyjson_ptr_get(val, ptr); } /** @deprecated renamed to `yyjson_ptr_getn` */ yyjson_deprecated("renamed to yyjson_ptr_getn") yyjson_api_inline yyjson_val *yyjson_get_pointern(yyjson_val *val, const char *ptr, size_t len) { return yyjson_ptr_getn(val, ptr, len); } /** @deprecated renamed to `yyjson_mut_ptr_get` */ yyjson_deprecated("renamed to yyjson_mut_ptr_get") yyjson_api_inline yyjson_mut_val *yyjson_mut_get_pointer(yyjson_mut_val *val, const char *ptr) { return yyjson_mut_ptr_get(val, ptr); } /** @deprecated renamed to `yyjson_mut_ptr_getn` */ yyjson_deprecated("renamed to yyjson_mut_ptr_getn") yyjson_api_inline yyjson_mut_val *yyjson_mut_get_pointern(yyjson_mut_val *val, const char *ptr, size_t len) { return yyjson_mut_ptr_getn(val, ptr, len); } /** @deprecated renamed to `yyjson_mut_ptr_getn` */ yyjson_deprecated("renamed to unsafe_yyjson_ptr_getn") yyjson_api_inline yyjson_val *unsafe_yyjson_get_pointer(yyjson_val *val, const char *ptr, size_t len) { yyjson_ptr_err err; return unsafe_yyjson_ptr_getx(val, ptr, len, &err); } /** @deprecated renamed to `unsafe_yyjson_mut_ptr_getx` */ yyjson_deprecated("renamed to unsafe_yyjson_mut_ptr_getx") yyjson_api_inline yyjson_mut_val *unsafe_yyjson_mut_get_pointer( yyjson_mut_val *val, const char *ptr, size_t len) { yyjson_ptr_err err; return unsafe_yyjson_mut_ptr_getx(val, ptr, len, NULL, &err); } /*============================================================================== * Compiler Hint End *============================================================================*/ #if defined(__clang__) # pragma clang diagnostic pop #elif defined(__GNUC__) # if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) # pragma GCC diagnostic pop # endif #elif defined(_MSC_VER) # pragma warning(pop) #endif /* warning suppress end */ #ifdef __cplusplus } #endif /* extern "C" end */ #endif /* YYJSON_H */ ================================================ FILE: integration/client ================================================ #!/usr/bin/env python3 # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2020-2023) import asyncio import sys import time import httpx port = sys.argv[1] url = f"http://127.0.0.1:{port}" timeout = httpx.Timeout(5.0) client = httpx.AsyncClient(timeout=timeout) stop_time = time.time() + 5 TEST_MESSAGE = "http test running..." async def main(): sys.stdout.write(TEST_MESSAGE) sys.stdout.flush() count = 0 while time.time() < stop_time: res = await client.get(url) count += 1 sys.stdout.write(f"\r{TEST_MESSAGE} ok, {count} requests made\n") loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) asyncio.run(main()) loop.close() ================================================ FILE: integration/http ================================================ #!/usr/bin/env bash set -e _dir="$(dirname "${BASH_SOURCE[0]}")" PYTHONPATH=${_dir} gunicorn --preload --bind localhost:8001 --workers 2 "$@" wsgi:app ================================================ FILE: integration/init ================================================ #!/usr/bin/env python3 # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2023-2025) import multiprocessing.pool import sys import orjson NUM_PROC = 16 TEST_MESSAGE = "parallel import of orjson running..." class Custom: pass def default(_): return None def func(_): orjson.dumps(Custom(), option=orjson.OPT_SERIALIZE_NUMPY, default=default) orjson.loads(b'{"a":1,"b":2,"c":3}') def main(): sys.stdout.write(TEST_MESSAGE) sys.stdout.flush() with multiprocessing.pool.ThreadPool(processes=NUM_PROC) as pool: pool.map(func, (i for i in range(NUM_PROC))) sys.stdout.write(f"\r{TEST_MESSAGE} ok\n") sys.stdout.flush() if __name__ == "__main__": main() ================================================ FILE: integration/requirements.txt ================================================ flask;sys_platform!="win" gunicorn;sys_platform!="win" httpx==0.28.1;sys_platform!="win" ================================================ FILE: integration/run ================================================ #!/usr/bin/env bash # SPDX-License-Identifier: (Apache-2.0 OR MIT) # Copyright ijl (2018-2023), Eric Jolibois (2022) set -eou pipefail _dir="$(dirname "${BASH_SOURCE[0]}")" to_run="${@:-thread http init}" export PYTHONMALLOC="debug" if [[ $to_run == *"thread"* ]]; then "${_dir}"/thread fi if [[ $to_run == *"http"* ]]; then "${_dir}"/http --daemon sleep 2 "${_dir}"/client 8001 set +e pkill -f 'wsgi:app' # pkill not present on all CI envs set -e fi if [[ $to_run == *"typestubs"* ]]; then python "${_dir}"/typestubs.py mypy "${_dir}"/typestubs.py fi if [[ $to_run == *"init"* ]]; then "${_dir}"/init fi ================================================ FILE: integration/thread ================================================ #!/usr/bin/env python3 # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2018-2025) import sys import traceback from concurrent.futures import ThreadPoolExecutor from operator import itemgetter from threading import get_ident import orjson DATA = sorted( [ { "id": i, "name": "90as90ji0123ioj2390as90as90", "body": "哈哈89asu89as😊89as9as90jas-😋0apjzxiojzx89hq23n", "score": 901290129.1, "bool": True, "int": 9832, "none": None, } for i in range(10) ], key=itemgetter("id"), ) STATUS = 0 TEST_MESSAGE = "thread test running..." sys.stdout.write(TEST_MESSAGE) sys.stdout.flush() def test_func(n): try: assert sorted(orjson.loads(orjson.dumps(DATA)), key=itemgetter("id")) == DATA except Exception: traceback.print_exc() print(f"thread {get_ident()}: {n} dumps, loads ERROR") with ThreadPoolExecutor(max_workers=4) as executor: executor.map(test_func, range(50000), chunksize=1000) executor.shutdown(wait=True) if STATUS == 0: sys.stdout.write(f"\r{TEST_MESSAGE} ok\n") else: sys.stdout.write(f"\r{TEST_MESSAGE} error\n") sys.exit(STATUS) ================================================ FILE: integration/typestubs.py ================================================ # SPDX-License-Identifier: (Apache-2.0 OR MIT) # Copyright Eric Jolibois (2022), ijl (2023) import orjson orjson.JSONDecodeError(msg="the_msg", doc="the_doc", pos=1) orjson.dumps(orjson.Fragment(b"{}")) ================================================ FILE: integration/wsgi.py ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2018-2025) from datetime import datetime, timezone from uuid import uuid4 from flask import Flask import orjson app = Flask(__name__) NOW = datetime.now(timezone.utc) @app.route("/") def root(): data = { "uuid": uuid4(), "updated_at": NOW, "data": [1, 2.2, None, True, False, orjson.Fragment(b"{}")], } payload = orjson.dumps( data, option=orjson.OPT_NAIVE_UTC | orjson.OPT_OMIT_MICROSECONDS, ) return app.response_class( response=payload, status=200, mimetype="application/json; charset=utf-8", ) ================================================ FILE: pyproject.toml ================================================ [project] name = "orjson" version = "3.11.7" repository = "https://github.com/ijl/orjson" description = "Fast, correct Python JSON library supporting dataclasses, datetimes, and numpy" requires-python = ">=3.10" classifiers = [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: Apache Software License", "License :: OSI Approved :: MIT License", "License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)", "Operating System :: MacOS", "Operating System :: Microsoft :: Windows", "Operating System :: POSIX :: Linux", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", "Programming Language :: Python :: 3.13", "Programming Language :: Python :: 3.14", "Programming Language :: Python :: 3.15", "Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python", "Programming Language :: Rust", "Typing :: Typed", ] readme = "README.md" license = "MPL-2.0 AND (Apache-2.0 OR MIT)" [project.urls] source = "https://github.com/ijl/orjson" documentation = "https://github.com/ijl/orjson" changelog = "https://github.com/ijl/orjson/blob/master/CHANGELOG.md" [build-system] build-backend = "maturin" requires = ["maturin>=1,<2"] [tool.maturin] python-source = "pysrc" include = [ { format = "sdist", path = ".cargo/*" }, { format = "sdist", path = "build.rs" }, { format = "sdist", path = "Cargo.lock" }, { format = "sdist", path = "include/cargo/**/*" }, { format = "sdist", path = "include/yyjson/**/*" }, ] [tool.ruff] line-length = 88 target-version = "py310" [tool.ruff.lint] select = [ "A", "ASYNC", "B", "COM", "DTZ", "E", "EXE", "F", "FLY", "I", "ISC", "PIE", "PLC", "PLE", "PLR", "PLW", "RUF", "TCH", "TID", "UP", "W", ] ignore = [ "B005", # Using `.strip()` with multi-character strings is misleading "DTZ001", # `datetime.datetime()` called without a `tzinfo` argument "DTZ005", # `datetime.datetime.now()` called without a `tz` argument "E402", # Module level import not at top of file "E501", # line too long "F601", # Dictionary key literal ... repeated "PIE810", # Call `startswith` once with a `tuple` "PLR2004", # Magic value used in comparison "UP012", # Unnecessary call to encode as UTF-8 ] [tool.pytest] minversion = "9.0" strict = true [tool.ruff.lint.isort] known-first-party = ["orjson"] [tool.mypy] python_version = "3.10" [[tool.mypy.overrides]] module = ["dateutil", "pytz"] ignore_missing_imports = true ================================================ FILE: pysrc/orjson/__init__.py ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2023) from .orjson import * from .orjson import __version__ __all__ = ( "__version__", "dumps", "Fragment", "JSONDecodeError", "JSONEncodeError", "loads", "OPT_APPEND_NEWLINE", "OPT_INDENT_2", "OPT_NAIVE_UTC", "OPT_NON_STR_KEYS", "OPT_OMIT_MICROSECONDS", "OPT_PASSTHROUGH_DATACLASS", "OPT_PASSTHROUGH_DATETIME", "OPT_PASSTHROUGH_SUBCLASS", "OPT_SERIALIZE_DATACLASS", "OPT_SERIALIZE_NUMPY", "OPT_SERIALIZE_UUID", "OPT_SORT_KEYS", "OPT_STRICT_INTEGER", "OPT_UTC_Z", ) ================================================ FILE: pysrc/orjson/__init__.pyi ================================================ # SPDX-License-Identifier: (Apache-2.0 OR MIT) # Copyright ijl (2019-2026), Eric Jolibois (2022), Anders Kaseorg (2020) import json from collections.abc import Callable from typing import Any __version__: str def dumps( __obj: Any, default: Callable[[Any], Any] | None = ..., option: int | None = ..., ) -> bytes: ... def loads(__obj: bytes | bytearray | memoryview | str) -> Any: ... class JSONDecodeError(json.JSONDecodeError): ... class JSONEncodeError(TypeError): ... class Fragment(tuple): contents: bytes | str OPT_APPEND_NEWLINE: int OPT_INDENT_2: int OPT_NAIVE_UTC: int OPT_NON_STR_KEYS: int OPT_OMIT_MICROSECONDS: int OPT_PASSTHROUGH_DATACLASS: int OPT_PASSTHROUGH_DATETIME: int OPT_PASSTHROUGH_SUBCLASS: int OPT_SERIALIZE_DATACLASS: int OPT_SERIALIZE_NUMPY: int OPT_SERIALIZE_UUID: int OPT_SORT_KEYS: int OPT_STRICT_INTEGER: int OPT_UTC_Z: int ================================================ FILE: pysrc/orjson/py.typed ================================================ ================================================ FILE: requirements-lint.txt ================================================ mypy==1.18.2 ruff>=0.14,<0.15 ================================================ FILE: requirements.txt ================================================ -r bench/requirements.txt -r integration/requirements.txt -r requirements-lint.txt -r test/requirements.txt maturin>=1.10,<2 ================================================ FILE: script/blame-to-header ================================================ #!/usr/bin/env python3 # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2026) import asyncio import datetime import re import subprocess from collections import defaultdict from pathlib import Path REPOSITORY = Path(__file__).parent.parent LICENSE_APACHE = "(Apache-2.0 OR MIT)" LICENSE_MPL2 = "MPL-2.0" SPACES = re.compile(r"[ ]{2,}") TO_INCLUDE = { ".github/**/*.yaml", "bench/*.py", "bench/run", "integration/*", "pysrc/orjson/*", "script/*", "src/**/*.rs", "test/**/*.py", } TO_EXCLUDE = { "integration/http", "script/cargo", "script/debug", "script/develop", "script/install-fedora", "script/lint", "script/profile", "script/pybench", "script/pytest", "script/valgrind", "src/ffi/atomiculong.rs", } def aggregate_files() -> list[Path]: files = [] files.append(REPOSITORY / Path("build.rs")) for pattern in TO_INCLUDE: files.extend(REPOSITORY.glob(pattern)) files = { each for each in files if not str(each).endswith(("py.typed", "__pycache__", ".txt")) } for filename in TO_EXCLUDE: files.remove(REPOSITORY / Path(filename)) return sorted(list(files)) SKIP_FRAGMENTS = ( "# Copyright", "# SPDX", "#!/usr", "// Copyright", "// SPDX", ) def get_contributor_and_date(line: str) -> tuple[str, datetime.date] | None: if not line or "Not Committed Yet" in line: return None end = line.index(")") diff = line[end + 2 :] diff = SPACES.sub(r" ", diff) # skip blank and ' };' etc if len(diff) <= 3: return None # skip headers and imports for fragment in SKIP_FRAGMENTS: if diff.startswith(fragment): return None line = line[line.index("(") + 1 : end] line = SPACES.sub(r" ", line) segments = line.split(" ")[0:-2] contributor = " ".join(segments[:-1]) date = datetime.date.fromtimestamp(int(segments[-1])).year return (contributor, date) def process_blame(filename: Path, blame: str) -> list[str, list[str]] | None: file_license = "(Apache-2.0 OR MIT)" contributors = defaultdict(list) document = blame.split("\n") for line in document: ret = get_contributor_and_date(line) if ret: contributors[ret[0]].append(ret[1]) overall_earliest = 9999 overall_latest = 0 file_credit = [] for contributor, dates in contributors.items(): earliest = min(dates) latest = max(dates) overall_latest = max((latest, overall_latest)) overall_earliest = min((latest, overall_earliest)) num_lines = len(dates) if earliest == latest: file_credit.append((num_lines, f"{contributor} ({earliest})")) else: file_credit.append((num_lines, f"{contributor} ({earliest}-{latest})")) if (len(contributors) == 1 and "ijl" in contributors) or overall_earliest == 2026: file_license = LICENSE_MPL2 file_credit.sort(reverse=True) return [file_license, file_credit] async def handle_file(filename: str): blame = await asyncio.create_subprocess_shell( f"git blame -C -C -C -M --date=raw {filename}", shell=True, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, ) header = process_blame(filename, (await blame.stdout.read()).decode("utf-8")) if header is None: print(f"{filename.relative_to(REPOSITORY)} skipping") return if not header[1] and str(filename).endswith("__init__.py"): return if str(filename).endswith(".rs"): prefix = "//" else: prefix = "#" spdx = f"{prefix} SPDX-License-Identifier: {header[0]}" credit = f"{prefix} Copyright {', '.join(each[1] for each in header[1])}" contents = filename.read_bytes().decode("utf-8").split("\n") if contents[0].startswith("#!"): start_idx = 1 else: start_idx = 0 if contents[start_idx].startswith(f"{prefix} SPDX-License-Identifier"): contents[start_idx] = spdx else: contents.insert(start_idx, spdx) if contents[start_idx + 1].startswith(f"{prefix} Copyright"): contents[start_idx + 1] = credit else: contents.insert(start_idx + 1, credit) # separate by blank line first_line = start_idx + 2 while contents[first_line].startswith(prefix): first_line += 1 if len(contents[first_line]) > 0: contents.insert(first_line, "") print(f"{filename.relative_to(REPOSITORY)} {spdx}") filename.write_bytes("\n".join(contents).encode("utf-8")) async def main(): async with asyncio.TaskGroup() as tg: for filename in aggregate_files(): tg.create_task(handle_file(filename)) if __name__ == "__main__": asyncio.run(main()) ================================================ FILE: script/cargo ================================================ #!/usr/bin/env bash set -eou pipefail export UNSAFE_PYO3_BUILD_FREE_THREADED=1 export UNSAFE_PYO3_SKIP_VERSION_CHECK=1 RUSTFLAGS="-Z unstable-options -C panic=immediate-abort -Z panic_abort_tests" cargo "$@" --target="${TARGET:-x86_64-unknown-linux-gnu}" ================================================ FILE: script/check-pypi ================================================ #!/usr/bin/env python3 # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2025) import sys from pathlib import Path import tomllib dist = sys.argv[1] pyproject_doc = tomllib.loads(Path("pyproject.toml").read_text(encoding="utf-8")) pyproject_version = pyproject_doc["project"]["version"] prefix = f"orjson-{pyproject_version}" abis = ( "cp310-cp310", "cp311-cp311", "cp312-cp312", "cp313-cp313", "cp314-cp314", ) per_abi_tags = ( "macosx_10_15_x86_64.macosx_11_0_arm64.macosx_10_15_universal2", "manylinux_2_17_aarch64.manylinux2014_aarch64", "manylinux_2_17_armv7l.manylinux2014_armv7l", "manylinux_2_17_ppc64le.manylinux2014_ppc64le", "manylinux_2_17_s390x.manylinux2014_s390x", "manylinux_2_17_x86_64.manylinux2014_x86_64", "manylinux_2_17_i686.manylinux2014_i686", "musllinux_1_2_aarch64", "musllinux_1_2_armv7l", "musllinux_1_2_i686", "musllinux_1_2_x86_64", "win32", "win_amd64", ) wheels_matrix = set() # orjson-3.10.15-cp310-cp310-macosx_10_15_x86_64.macosx_11_0_arm64.macosx_10_15_universal2.whl for abi in abis: for tag in per_abi_tags: wheels_matrix.add(f"{prefix}-{abi}-{tag}.whl") wheels_prerelease = set() wheels_unique = { f"{prefix}-cp311-cp311-macosx_15_0_arm64.whl", f"{prefix}-cp311-cp311-win_arm64.whl", f"{prefix}-cp312-cp312-macosx_15_0_arm64.whl", f"{prefix}-cp312-cp312-win_arm64.whl", f"{prefix}-cp313-cp313-macosx_15_0_arm64.whl", f"{prefix}-cp313-cp313-win_arm64.whl", f"{prefix}-cp314-cp314-macosx_15_0_arm64.whl", f"{prefix}-cp314-cp314-win_arm64.whl", } wheels_expected = wheels_matrix | wheels_unique | wheels_prerelease wheels_queued = set( str(each).replace(f"{dist}/", "") for each in Path(dist).glob("*.whl") ) exit_code = 0 # sdist sdist_path = Path(f"{dist}/{prefix}.tar.gz") if sdist_path.exists(): print("sdist present\n") else: exit_code = 1 print(f"Missing sdist:\n{sdist_path}\n") # whl if wheels_expected == wheels_queued: print(f"Wheels as expected, {len(wheels_queued)} total\n") else: exit_code = 1 missing = "\n".join(sorted(wheels_expected - wheels_queued)) if missing: print(f"Missing wheels:\n{missing}\n") additional = "\n".join(sorted(wheels_queued - wheels_expected)) if additional: print(f"Unexpected wheels:\n{additional}\n") sys.exit(exit_code) ================================================ FILE: script/check-version ================================================ #!/usr/bin/env python3 # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2025) from pathlib import Path import tomllib cargo_doc = tomllib.loads(Path("Cargo.toml").read_text(encoding="utf-8")) pyproject_doc = tomllib.loads(Path("pyproject.toml").read_text(encoding="utf-8")) cargo_version = cargo_doc["package"]["version"] pyproject_version = pyproject_doc["project"]["version"] print(f"Cargo.toml version: {cargo_version}") print(f"pyproject.toml version: {pyproject_version}") assert cargo_version == pyproject_version ================================================ FILE: script/debug ================================================ #!/usr/bin/env bash set -eou pipefail rm -rf .cargo rm -f ${CARGO_TARGET_DIR}/wheels/*.whl export UNSAFE_PYO3_BUILD_FREE_THREADED=1 export UNSAFE_PYO3_SKIP_VERSION_CHECK=1 export CC="${CC:-clang}" export LD="${LD:-lld}" export TARGET="${TARGET:-x86_64-unknown-linux-gnu}" export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-target}" export CFLAGS="-Os -fstrict-aliasing" export RUSTFLAGS="-C panic=unwind -C linker=${CC} -C link-arg=-fuse-ld=${LD}" maturin build --profile=dev --target="${TARGET}" uv pip install ${CARGO_TARGET_DIR}/wheels/*.whl pytest -v test mkdir .cargo cp ci/config.toml .cargo ================================================ FILE: script/develop ================================================ #!/bin/sh -e rm -f target/wheels/* export UNSAFE_PYO3_BUILD_FREE_THREADED=1 export UNSAFE_PYO3_SKIP_VERSION_CHECK=1 mkdir -p .cargo cp ci/config.toml .cargo/config.toml export CC="${CC:-clang}" export LD="${LD:-lld}" export TARGET="${TARGET:-x86_64-unknown-linux-gnu}" export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-target}" export ORJSON_COMPATIBILITY="${ORJSON_COMPATIBILITY:-manylinux_2_17}" echo "CC: ${CC}, LD: ${LD}, LD_LIBRARY_PATH: ${LD_LIBRARY_PATH}" export CFLAGS="-O2 -fstrict-aliasing -fno-plt -emit-llvm" export LDFLAGS="-fuse-ld=${LD} -Wl,-plugin-opt=also-emit-llvm -Wl,--as-needed -Wl,-zrelro,-znow" export RUSTFLAGS="-Z unstable-options -C panic=immediate-abort -C linker=${CC} -C link-arg=-fuse-ld=${LD} -C linker-plugin-lto -C link-arg=-Wl,-zrelro,-znow -Z mir-opt-level=4 -Z threads=8" rm -f ${CARGO_TARGET_DIR}/wheels/*.whl maturin build --target="${TARGET}" --features=no_panic --compatibility="${ORJSON_COMPATIBILITY}" "$@" uv pip install --link-mode=copy ${CARGO_TARGET_DIR}/wheels/*.whl ================================================ FILE: script/graph ================================================ #!/usr/bin/env python3 # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2018-2025) import collections import io import os import pandas as pd import seaborn as sns from matplotlib import pyplot as plt from tabulate import tabulate import orjson LIBRARIES = ("orjson", "json") def aggregate(): benchmarks_dir = os.path.join(".benchmarks", os.listdir(".benchmarks")[0]) res = collections.defaultdict(dict) for filename in os.listdir(benchmarks_dir): with open(os.path.join(benchmarks_dir, filename)) as fileh: data = orjson.loads(fileh.read()) for each in data["benchmarks"]: res[each["group"]][each["extra_info"]["lib"]] = { "data": [val * 1000 for val in each["stats"]["data"]], "median": each["stats"]["median"] * 1000, "ops": each["stats"]["ops"], "correct": each["extra_info"]["correct"], } return res def tab(obj): buf = io.StringIO() headers = ( "Library", "Median latency (milliseconds)", "Operations per second", "Relative (latency)", ) sns.set(rc={"figure.facecolor": (0, 0, 0, 0)}) sns.set_style("darkgrid") barplot_data = [] for group, val in sorted(obj.items(), reverse=True): buf.write("\n" + "#### " + group + "\n\n") table = [] for lib in LIBRARIES: correct = val[lib]["correct"] table.append( [ lib, val[lib]["median"] if correct else None, int(val[lib]["ops"]) if correct else None, 0, ], ) barplot_data.append( { "operation": "deserialization" if "deserialization" in group else "serialization", "group": group.strip("serialization") .strip("deserialization") .strip(), "library": lib, "latency": val[lib]["median"], "operations": int(val[lib]["ops"]) if correct else None, }, ) orjson_baseline = table[0][1] for each in table: each[3] = ( "%.1f" % (each[1] / orjson_baseline) if isinstance(each[1], float) else None ) if group.startswith("github"): each[1] = f"{each[1]:.2f}" if isinstance(each[1], float) else None else: each[1] = f"{each[1]:.1f}" if isinstance(each[1], float) else None buf.write(tabulate(table, headers, tablefmt="github") + "\n") for operation in ("deserialization", "serialization"): per_op_data = list( each for each in barplot_data if each["operation"] == operation ) if not per_op_data: continue max_y = 10 if operation == "serialization" else 5 json_baseline = {} for each in per_op_data: if each["group"] == "witter.json": each["group"] = "twitter.json" if each["library"] == "json": json_baseline[each["group"]] = each["operations"] for each in per_op_data: relative = each["operations"] / json_baseline[each["group"]] each["relative"] = min(max_y, relative) p = pd.DataFrame.from_dict(per_op_data) p.groupby("group") graph = sns.barplot( p, x="group", y="relative", orient="x", hue="library", errorbar="sd", legend="brief", ) graph.set_xlabel("Document") graph.set_ylabel("Operations/second relative to stdlib json") plt.title(operation) # ensure Y range plt.gca().set_yticks( list( {1, max_y}.union( set(int(y) for y in plt.gca().get_yticks() if int(y) <= max_y), ), ), ) # print Y as percent plt.gca().set_yticklabels([f"{x}x" for x in plt.gca().get_yticks()]) # reference for stdlib plt.axhline(y=1, color="#999", linestyle="dashed") plt.savefig(fname=f"doc/{operation}", dpi=300) plt.close() print(buf.getvalue()) tab(aggregate()) ================================================ FILE: script/install-fedora ================================================ #!/usr/bin/env bash set -eou pipefail export VENV="${VENV:-.venv}" export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-target}" rm /etc/yum.repos.d/fedora-cisco-openh264.repo || true dnf install --setopt=install_weak_deps=false -y rustup clang lld "${PYTHON_PACKAGE}" python3-uv rustup-init --default-toolchain "${RUST_TOOLCHAIN}-${TARGET}" --profile minimal --component rust-src -y source "${HOME}/.cargo/env" mkdir -p .cargo cp ci/config.toml .cargo/config.toml cargo fetch --target="${TARGET}" & rm -rf "${VENV}" uv venv --python "${PYTHON}" "${VENV}" source "${VENV}/bin/activate" uv pip install --upgrade "maturin>=1.10,<2" -r test/requirements.txt -r integration/requirements.txt ================================================ FILE: script/lint ================================================ #!/usr/bin/env bash set -eou pipefail to_lint="./bench/*.py ./pysrc/orjson/__init__.pyi ./test/*.py script/pydataclass script/pysort script/pynumpy script/pynonstr script/pycorrectness script/graph integration/init integration/wsgi.py integration/typestubs.py integration/thread script/check-version script/check-pypi" ruff format ${to_lint} ruff check ${to_lint} --fix mypy --ignore-missing-imports --check-untyped-defs ./bench/*.py ./pysrc/orjson/__init__.pyi ./test/*.py ================================================ FILE: script/profile ================================================ #!/bin/sh -e # usage: ./profile data/citm_catalog.json.xz loads perf record -g --delay 250 ./bench/run_func "$@" perf report --percent-limit 0.1 ================================================ FILE: script/pybench ================================================ #!/usr/bin/env bash set -eou pipefail pytest \ --verbose \ --benchmark-min-time=1 \ --benchmark-max-time=5 \ --benchmark-disable-gc \ --benchmark-autosave \ --benchmark-save-data \ --random-order \ "bench/benchmark_$1.py" ================================================ FILE: script/pycorrectness ================================================ #!/usr/bin/env python3 # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2019-2025) import collections import io import json import lzma import os from pathlib import Path from tabulate import tabulate import orjson dirname = os.path.join(os.path.dirname(__file__), "..", "data") LIBRARIES = ["orjson", "json"] LIBRARY_FUNC_MAP = { "orjson": orjson.loads, "json": json.loads, } def read_fixture_bytes(filename, subdir=None): if subdir is None: parts = (dirname, filename) else: parts = (dirname, subdir, filename) path = Path(*parts) if path.suffix == ".xz": contents = lzma.decompress(path.read_bytes()) else: contents = path.read_bytes() return contents PARSING = { filename: read_fixture_bytes(filename, "parsing") for filename in os.listdir("data/parsing") } JSONCHECKER = { filename: read_fixture_bytes(filename, "jsonchecker") for filename in os.listdir("data/jsonchecker") } RESULTS = collections.defaultdict(dict) def test_passed(libname, fixture): passed = [] loads = LIBRARY_FUNC_MAP[libname] try: passed.append(loads(fixture) == orjson.loads(fixture)) passed.append( loads(fixture.decode("utf-8")) == orjson.loads(fixture.decode("utf-8")), ) except Exception: passed.append(False) return all(passed) def test_failed(libname, fixture): rejected_as_bytes = False loads = LIBRARY_FUNC_MAP[libname] try: loads(fixture) except Exception: rejected_as_bytes = True rejected_as_str = False try: loads(fixture.decode("utf-8")) except Exception: rejected_as_str = True return rejected_as_bytes and rejected_as_str MISTAKEN_PASSES = {key: 0 for key in LIBRARIES} MISTAKEN_FAILS = {key: 0 for key in LIBRARIES} PASS_WHITELIST = ("fail01.json", "fail18.json") def should_pass(filename): return ( filename.startswith("y_") or filename.startswith("pass") or filename in PASS_WHITELIST ) def should_fail(filename): return ( filename.startswith("n_") or filename.startswith("i_string") or filename.startswith("i_object") or filename.startswith("fail") ) and filename not in PASS_WHITELIST for libname in LIBRARIES: for fixture_set in (PARSING, JSONCHECKER): for filename, fixture in fixture_set.items(): if should_pass(filename): res = test_passed(libname, fixture) RESULTS[filename][libname] = res if not res: MISTAKEN_PASSES[libname] += 1 elif should_fail(filename): res = test_failed(libname, fixture) RESULTS[filename][libname] = res if not res: MISTAKEN_FAILS[libname] += 1 elif filename.startswith("i_"): continue else: raise NotImplementedError FILENAMES = sorted(list(PARSING.keys()) + list(JSONCHECKER.keys())) tab_results = [] for filename in FILENAMES: entry = [ filename, ] for libname in LIBRARIES: try: entry.append("ok" if RESULTS[filename][libname] else "fail") except KeyError: continue tab_results.append(entry) buf = io.StringIO() buf.write(tabulate(tab_results, ["Fixture", *LIBRARIES], tablefmt="github")) buf.write("\n") print(buf.getvalue()) failure_results = [ [libname, MISTAKEN_FAILS[libname], MISTAKEN_PASSES[libname]] for libname in LIBRARIES ] buf = io.StringIO() buf.write( tabulate( failure_results, [ "Library", "Invalid JSON documents not rejected", "Valid JSON documents not deserialized", ], tablefmt="github", ), ) buf.write("\n") print(buf.getvalue()) num_results = len([each for each in tab_results if len(each) > 1]) print(f"{num_results} documents tested") ================================================ FILE: script/pydataclass ================================================ #!/usr/bin/env python3 # SPDX-License-Identifier: (Apache-2.0 OR MIT) # Copyright ijl (2019-2025), Aarni Koskela (2021) import dataclasses import io import json import os from timeit import timeit from tabulate import tabulate import orjson if hasattr(os, "sched_setaffinity"): os.sched_setaffinity(os.getpid(), {0, 1}) @dataclasses.dataclass class Member: id: int active: bool @dataclasses.dataclass class Object: id: int name: str members: list[Member] objects_as_dataclass = [ Object(i, str(i) * 3, [Member(j, True) for j in range(10)]) for i in range(100000, 102000) ] objects_as_dict = [dataclasses.asdict(each) for each in objects_as_dataclass] output_in_kib = len(orjson.dumps(objects_as_dict)) / 1024 print(f"{output_in_kib:,.0f}KiB output (orjson)") def default(__obj): if dataclasses.is_dataclass(__obj): return dataclasses.asdict(__obj) headers = ("Library", "dict (ms)", "dataclass (ms)", "vs. orjson") LIBRARIES = ("orjson", "json") ITERATIONS = 100 def per_iter_latency(val): if val is None: return None return (val * 1000) / ITERATIONS table = [] for lib_name in LIBRARIES: if lib_name == "json": as_dict = timeit( lambda: json.dumps(objects_as_dict).encode("utf-8"), number=ITERATIONS, ) as_dataclass = timeit( lambda: json.dumps(objects_as_dataclass, default=default).encode("utf-8"), number=ITERATIONS, ) elif lib_name == "orjson": as_dict = timeit(lambda: orjson.dumps(objects_as_dict), number=ITERATIONS) as_dataclass = timeit( lambda: orjson.dumps( objects_as_dataclass, None, orjson.OPT_SERIALIZE_DATACLASS, ), number=ITERATIONS, ) orjson_as_dataclass = per_iter_latency(as_dataclass) else: raise NotImplementedError as_dict = per_iter_latency(as_dict) as_dataclass = per_iter_latency(as_dataclass) if lib_name == "orjson": compared_to_orjson = 1 elif as_dataclass: compared_to_orjson = int(as_dataclass / orjson_as_dataclass) else: compared_to_orjson = None table.append( ( lib_name, f"{as_dict:,.2f}" if as_dict else "", f"{as_dataclass:,.2f}" if as_dataclass else "", f"{compared_to_orjson:d}" if compared_to_orjson else "", ), ) buf = io.StringIO() buf.write(tabulate(table, headers, tablefmt="github")) buf.write("\n") print(buf.getvalue()) ================================================ FILE: script/pyindent ================================================ #!/usr/bin/env python3 # SPDX-License-Identifier: (Apache-2.0 OR MIT) # Copyright ijl (2019-2024), Aarni Koskela (2021) import io import json import lzma import os import sys from pathlib import Path from timeit import timeit from tabulate import tabulate import orjson if hasattr(os, "sched_setaffinity"): os.sched_setaffinity(os.getpid(), {0, 1}) dirname = os.path.join(os.path.dirname(__file__), "..", "data") def read_fixture_obj(filename): path = Path(dirname, filename) if path.suffix == ".xz": contents = lzma.decompress(path.read_bytes()) else: contents = path.read_bytes() return orjson.loads(contents) filename = sys.argv[1] if len(sys.argv) >= 1 else "" data = read_fixture_obj(f"{filename}.json.xz") headers = ("Library", "compact (ms)", "pretty (ms)", "vs. orjson") LIBRARIES = ("orjson", "json") output_in_kib_compact = len(orjson.dumps(data)) / 1024 output_in_kib_pretty = len(orjson.dumps(data, option=orjson.OPT_INDENT_2)) / 1024 # minimum 2s runtime for orjson compact ITERATIONS = int(2 / (timeit(lambda: orjson.dumps(data), number=20) / 20)) print( f"{output_in_kib_compact:,.0f}KiB compact, {output_in_kib_pretty:,.0f}KiB pretty, {ITERATIONS} iterations" ) def per_iter_latency(val): if val is None: return None return (val * 1000) / ITERATIONS def test_correctness(serialized): return orjson.loads(serialized) == data table = [] for lib_name in LIBRARIES: print(f"{lib_name}...") if lib_name == "json": time_compact = timeit( lambda: json.dumps(data).encode("utf-8"), number=ITERATIONS, ) time_pretty = timeit( lambda: json.dumps(data, indent=2).encode("utf-8"), number=ITERATIONS, ) correct = test_correctness(json.dumps(data, indent=2).encode("utf-8")) elif lib_name == "orjson": time_compact = timeit(lambda: orjson.dumps(data), number=ITERATIONS) time_pretty = timeit( lambda: orjson.dumps(data, None, orjson.OPT_INDENT_2), number=ITERATIONS, ) correct = test_correctness(orjson.dumps(data, None, orjson.OPT_INDENT_2)) orjson_time_pretty = per_iter_latency(time_pretty) else: raise NotImplementedError time_compact = per_iter_latency(time_compact) if not correct: time_pretty = None else: time_pretty = per_iter_latency(time_pretty) if lib_name == "orjson": compared_to_orjson = 1 elif time_pretty: compared_to_orjson = time_pretty / orjson_time_pretty else: compared_to_orjson = None table.append( ( lib_name, f"{time_compact:,.2f}" if time_compact else "", f"{time_pretty:,.2f}" if time_pretty else "", f"{compared_to_orjson:,.1f}" if compared_to_orjson else "", ) ) buf = io.StringIO() buf.write(tabulate(table, headers, tablefmt="github")) buf.write("\n") print(buf.getvalue()) ================================================ FILE: script/pynonstr ================================================ #!/usr/bin/env python3 # SPDX-License-Identifier: (Apache-2.0 OR MIT) # Copyright ijl (2020-2025), Aarni Koskela (2021) import datetime import io import json import os import random from time import mktime from timeit import timeit from tabulate import tabulate import orjson if hasattr(os, "sched_setaffinity"): os.sched_setaffinity(os.getpid(), {0, 1}) data_as_obj = [] for year in range(1920, 2020): start = datetime.date(year, 1, 1) array = [ (int(mktime((start + datetime.timedelta(days=i)).timetuple())), i + 1) for i in range(365) ] array.append(("other", 0)) random.shuffle(array) data_as_obj.append(dict(array)) data_as_str = orjson.loads(orjson.dumps(data_as_obj, option=orjson.OPT_NON_STR_KEYS)) headers = ("Library", "str keys (ms)", "int keys (ms)", "int keys sorted (ms)") LIBRARIES = ("orjson", "json") ITERATIONS = 500 output_in_kib = len(orjson.dumps(data_as_str)) / 1024 print(f"{output_in_kib:,.0f}KiB output (orjson)") def per_iter_latency(val): if val is None: return None return (val * 1000) / ITERATIONS def test_correctness(serialized): return orjson.loads(serialized) == data_as_str table = [] for lib_name in LIBRARIES: print(f"{lib_name}...") if lib_name == "json": time_as_str = timeit( lambda: json.dumps(data_as_str).encode("utf-8"), number=ITERATIONS, ) time_as_obj = timeit( lambda: json.dumps(data_as_obj).encode("utf-8"), number=ITERATIONS, ) time_as_obj_sorted = ( None # TypeError: '<' not supported between instances of 'str' and 'int' ) correct = False elif lib_name == "orjson": time_as_str = timeit( lambda: orjson.dumps(data_as_str, None, orjson.OPT_NON_STR_KEYS), number=ITERATIONS, ) time_as_obj = timeit( lambda: orjson.dumps(data_as_obj, None, orjson.OPT_NON_STR_KEYS), number=ITERATIONS, ) time_as_obj_sorted = timeit( lambda: orjson.dumps( data_as_obj, None, orjson.OPT_NON_STR_KEYS | orjson.OPT_SORT_KEYS, ), number=ITERATIONS, ) correct = test_correctness( orjson.dumps( data_as_obj, None, orjson.OPT_NON_STR_KEYS | orjson.OPT_SORT_KEYS, ), ) else: raise NotImplementedError time_as_str = per_iter_latency(time_as_str) time_as_obj = per_iter_latency(time_as_obj) if not correct: time_as_obj_sorted = None else: time_as_obj_sorted = per_iter_latency(time_as_obj_sorted) table.append( ( lib_name, f"{time_as_str:,.2f}" if time_as_str else "", f"{time_as_obj:,.2f}" if time_as_obj else "", f"{time_as_obj_sorted:,.2f}" if time_as_obj_sorted else "", ), ) buf = io.StringIO() buf.write(tabulate(table, headers, tablefmt="github")) buf.write("\n") print(buf.getvalue()) ================================================ FILE: script/pynumpy ================================================ #!/usr/bin/env python3 # SPDX-License-Identifier: (Apache-2.0 OR MIT) # Copyright ijl (2020-2025), Nazar Kostetskyi (2022), Aarni Koskela (2021) import gc import io import json import os import sys import time from timeit import timeit import numpy as np import psutil from tabulate import tabulate import orjson if hasattr(os, "sched_setaffinity"): os.sched_setaffinity(os.getpid(), {0, 1}) kind = sys.argv[1] if len(sys.argv) >= 1 else "" if kind == "float16": dtype = np.float16 array = np.random.random(size=(50000, 100)).astype(dtype) elif kind == "float32": dtype = np.float32 array = np.random.random(size=(50000, 100)).astype(dtype) elif kind == "float64": dtype = np.float64 array = np.random.random(size=(50000, 100)) assert array.dtype == np.float64 elif kind == "bool": dtype = np.bool_ array = np.random.choice((True, False), size=(100000, 200)) elif kind == "int8": dtype = np.int8 array = np.random.randint(((2**7) - 1), size=(100000, 100), dtype=dtype) elif kind == "int16": dtype = np.int16 array = np.random.randint(((2**15) - 1), size=(100000, 100), dtype=dtype) elif kind == "int32": dtype = np.int32 array = np.random.randint(((2**31) - 1), size=(100000, 100), dtype=dtype) elif kind == "uint8": dtype = np.uint8 array = np.random.randint(((2**8) - 1), size=(100000, 100), dtype=dtype) elif kind == "uint16": dtype = np.uint16 array = np.random.randint(((2**16) - 1), size=(100000, 100), dtype=dtype) elif kind == "uint32": dtype = np.uint32 array = np.random.randint(((2**31) - 1), size=(100000, 100), dtype=dtype) else: print( "usage: pynumpy (bool|int16|int32|float16|float32|float64|int8|uint8|uint16|uint32)", ) sys.exit(1) proc = psutil.Process() def default(__obj): if isinstance(__obj, np.ndarray): return __obj.tolist() raise TypeError headers = ("Library", "Latency (ms)", "RSS diff (MiB)", "vs. orjson") LIBRARIES = ("orjson", "json") ITERATIONS = 10 def orjson_dumps(): return orjson.dumps(array, option=orjson.OPT_SERIALIZE_NUMPY) def json_dumps(): return json.dumps(array, default=default).encode("utf-8") output_in_mib = len(orjson_dumps()) / 1024 / 1024 print(f"{output_in_mib:,.1f}MiB {kind} output (orjson)") gc.collect() mem_before = proc.memory_full_info().rss / 1024 / 1024 def per_iter_latency(val): if val is None: return None return (val * 1000) / ITERATIONS def test_correctness(func): return np.array_equal(array, np.array(orjson.loads(func()), dtype=dtype)) table = [] for lib_name in LIBRARIES: gc.collect() print(f"{lib_name}...") func = locals()[f"{lib_name}_dumps"] if func is None: total_latency = None latency = None mem = None correct = False else: total_latency = timeit( func, number=ITERATIONS, ) latency = per_iter_latency(total_latency) time.sleep(1) mem = 0 # todo correct = test_correctness(func) if lib_name == "orjson": compared_to_orjson = 1 orjson_latency = latency elif latency: compared_to_orjson = latency / orjson_latency else: compared_to_orjson = None if not correct: latency = None mem = 0 mem_diff = mem - mem_before table.append( ( lib_name, f"{latency:,.0f}" if latency else "", f"{mem_diff:,.0f}" if mem else "", f"{compared_to_orjson:,.1f}" if (latency and compared_to_orjson) else "", ), ) buf = io.StringIO() buf.write(tabulate(table, headers, tablefmt="github")) buf.write("\n") print(buf.getvalue()) ================================================ FILE: script/pysort ================================================ #!/usr/bin/env python3 # SPDX-License-Identifier: (Apache-2.0 OR MIT) # Copyright ijl (2019-2024), Aarni Koskela (2021) import io import json import lzma import os from pathlib import Path from timeit import timeit from tabulate import tabulate import orjson if hasattr(os, "sched_setaffinity"): os.sched_setaffinity(os.getpid(), {0, 1}) dirname = os.path.join(os.path.dirname(__file__), "..", "data") def read_fixture_obj(filename): path = Path(dirname, filename) if path.suffix == ".xz": contents = lzma.decompress(path.read_bytes()) else: contents = path.read_bytes() return orjson.loads(contents) data = read_fixture_obj("twitter.json.xz") headers = ("Library", "unsorted (ms)", "sorted (ms)", "vs. orjson") LIBRARIES = ("orjson", "json") ITERATIONS = 500 def per_iter_latency(val): if val is None: return None return (val * 1000) / ITERATIONS table = [] for lib_name in LIBRARIES: if lib_name == "json": time_unsorted = timeit( lambda: json.dumps(data).encode("utf-8"), number=ITERATIONS, ) time_sorted = timeit( lambda: json.dumps(data, sort_keys=True).encode("utf-8"), number=ITERATIONS, ) elif lib_name == "orjson": time_unsorted = timeit(lambda: orjson.dumps(data), number=ITERATIONS) time_sorted = timeit( lambda: orjson.dumps(data, None, orjson.OPT_SORT_KEYS), number=ITERATIONS, ) orjson_time_sorted = per_iter_latency(time_sorted) else: raise NotImplementedError time_unsorted = per_iter_latency(time_unsorted) time_sorted = per_iter_latency(time_sorted) if lib_name == "orjson": compared_to_orjson = 1 elif time_unsorted: compared_to_orjson = time_sorted / orjson_time_sorted else: compared_to_orjson = None table.append( ( lib_name, f"{time_unsorted:,.2f}" if time_unsorted else "", f"{time_sorted:,.2f}" if time_sorted else "", f"{compared_to_orjson:,.1f}" if compared_to_orjson else "", ), ) buf = io.StringIO() buf.write(tabulate(table, headers, tablefmt="github")) buf.write("\n") print(buf.getvalue()) ================================================ FILE: script/pytest ================================================ #!/bin/sh -e PYTHONMALLOC="debug" pytest -s test ================================================ FILE: script/valgrind ================================================ #!/usr/bin/env bash set -eou pipefail valgrind "$@" pytest -v --ignore=test/test_memory.py test ================================================ FILE: src/alloc.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2025) use crate::ffi::{PyMem_Free, PyMem_Malloc, PyMem_Realloc}; use core::alloc::{GlobalAlloc, Layout}; use core::ffi::c_void; struct PyMemAllocator {} #[global_allocator] static ALLOCATOR: PyMemAllocator = PyMemAllocator {}; unsafe impl Sync for PyMemAllocator {} unsafe impl GlobalAlloc for PyMemAllocator { #[inline] unsafe fn alloc(&self, layout: Layout) -> *mut u8 { unsafe { PyMem_Malloc(layout.size()).cast::() } } #[inline] unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { unsafe { PyMem_Free(ptr.cast::()) } } #[inline] unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { unsafe { let len = layout.size(); let ptr = PyMem_Malloc(len).cast::(); core::ptr::write_bytes(ptr, 0, len); ptr } } #[inline] unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, new_size: usize) -> *mut u8 { unsafe { PyMem_Realloc(ptr.cast::(), new_size).cast::() } } } ================================================ FILE: src/deserialize/backend/ffi.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2022-2025) #[repr(C)] pub(crate) struct yyjson_alc { pub malloc: ::core::option::Option< unsafe extern "C" fn( ctx: *mut ::core::ffi::c_void, size: usize, ) -> *mut ::core::ffi::c_void, >, pub realloc: ::core::option::Option< unsafe extern "C" fn( ctx: *mut ::core::ffi::c_void, ptr: *mut ::core::ffi::c_void, size: usize, ) -> *mut ::core::ffi::c_void, >, pub free: ::core::option::Option< unsafe extern "C" fn(ctx: *mut ::core::ffi::c_void, ptr: *mut ::core::ffi::c_void), >, pub ctx: *mut ::core::ffi::c_void, } pub(crate) type yyjson_read_code = u32; pub(crate) const YYJSON_READ_SUCCESS: yyjson_read_code = 0; #[repr(C)] pub(crate) struct yyjson_read_err { pub code: yyjson_read_code, pub msg: *const ::core::ffi::c_char, pub pos: usize, } #[repr(C)] pub(crate) union yyjson_val_uni { pub u64_: u64, pub i64_: i64, pub f64_: f64, pub str_: *const ::core::ffi::c_char, pub ptr: *mut ::core::ffi::c_void, pub ofs: usize, } #[repr(C)] pub(crate) struct yyjson_val { pub tag: u64, pub uni: yyjson_val_uni, } #[repr(C)] pub(crate) struct yyjson_doc { pub root: *mut yyjson_val, pub alc: yyjson_alc, pub dat_read: usize, pub val_read: usize, pub str_pool: *mut ::core::ffi::c_char, } unsafe extern "C" { pub fn yyjson_read_opts( dat: *mut ::core::ffi::c_char, len: usize, alc: *const yyjson_alc, err: *mut yyjson_read_err, ) -> *mut yyjson_doc; pub fn yyjson_alc_pool_init( alc: *mut yyjson_alc, buf: *mut ::core::ffi::c_void, size: usize, ) -> bool; } ================================================ FILE: src/deserialize/backend/mod.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2024-2025) mod ffi; mod yyjson; pub(crate) use yyjson::deserialize; ================================================ FILE: src/deserialize/backend/yyjson.rs ================================================ // SPDX-License-Identifier: (Apache-2.0 OR MIT) // Copyright ijl (2022-2026), Anders Kaseorg (2023) use super::ffi::{ YYJSON_READ_SUCCESS, yyjson_alc, yyjson_alc_pool_init, yyjson_doc, yyjson_read_err, yyjson_read_opts, yyjson_val, }; use crate::deserialize::DeserializeError; use crate::deserialize::pyobject::get_unicode_key; use crate::ffi::{PyBoolRef, PyDictRef, PyFloatRef, PyIntRef, PyListRef, PyNoneRef, PyStrRef}; use core::ffi::c_char; use core::ptr::{NonNull, null, null_mut}; use std::borrow::Cow; const YYJSON_TAG_BIT: u8 = 8; const YYJSON_VAL_SIZE: usize = core::mem::size_of::(); const TAG_ARRAY: u8 = 0b00000110; const TAG_DOUBLE: u8 = 0b00010100; const TAG_FALSE: u8 = 0b00000011; const TAG_INT64: u8 = 0b00001100; const TAG_NULL: u8 = 0b00000010; const TAG_OBJECT: u8 = 0b00000111; const TAG_STRING: u8 = 0b00000101; const TAG_TRUE: u8 = 0b00001011; const TAG_UINT64: u8 = 0b00000100; macro_rules! is_yyjson_tag { ($elem:expr, $tag:expr) => { unsafe { (*$elem).tag as u8 == $tag } }; } fn yyjson_doc_get_root(doc: *mut yyjson_doc) -> *mut yyjson_val { unsafe { (*doc).root } } fn unsafe_yyjson_get_len(val: *mut yyjson_val) -> usize { unsafe { ((*val).tag >> YYJSON_TAG_BIT) as usize } } fn unsafe_yyjson_get_first(ctn: *mut yyjson_val) -> *mut yyjson_val { unsafe { ctn.add(1) } } const MINIMUM_BUFFER_CAPACITY: usize = 4096; fn buffer_capacity_to_allocate(len: usize) -> usize { // The max memory size is (json_size / 2 * 16 * 1.5 + padding). (((len / 2) * 24) + 256 + (MINIMUM_BUFFER_CAPACITY - 1)) & !(MINIMUM_BUFFER_CAPACITY - 1) } fn unsafe_yyjson_is_ctn(val: *mut yyjson_val) -> bool { unsafe { (*val).tag as u8 & 0b00000110 == 0b00000110 } } #[allow(clippy::cast_ptr_alignment)] fn unsafe_yyjson_get_next_container(val: *mut yyjson_val) -> *mut yyjson_val { unsafe { (val.cast::().add((*val).uni.ofs)).cast::() } } #[allow(clippy::cast_ptr_alignment)] fn unsafe_yyjson_get_next_non_container(val: *mut yyjson_val) -> *mut yyjson_val { unsafe { (val.cast::().add(YYJSON_VAL_SIZE)).cast::() } } pub(crate) fn deserialize( data: &'static str, ) -> Result, DeserializeError<'static>> { assume!(!data.is_empty()); let buffer_capacity = buffer_capacity_to_allocate(data.len()); let buffer_ptr = ffi!(PyMem_Malloc(buffer_capacity)); if buffer_ptr.is_null() { return Err(DeserializeError::from_yyjson( Cow::Borrowed("Not enough memory to allocate buffer for parsing"), 0, data, )); } let mut alloc = yyjson_alc { malloc: None, realloc: None, free: None, ctx: null_mut(), }; unsafe { yyjson_alc_pool_init(&raw mut alloc, buffer_ptr, buffer_capacity); } let mut err = yyjson_read_err { code: YYJSON_READ_SUCCESS, msg: null(), pos: 0, }; let doc = unsafe { yyjson_read_opts( data.as_ptr().cast::().cast_mut(), data.len(), &raw const alloc, &raw mut err, ) }; if doc.is_null() { ffi!(PyMem_Free(buffer_ptr)); let msg: Cow = unsafe { core::ffi::CStr::from_ptr(err.msg).to_string_lossy() }; #[allow(clippy::cast_possible_wrap)] let pos = err.pos as i64; return Err(DeserializeError::from_yyjson(msg, pos, data)); } let val = yyjson_doc_get_root(doc); let pyval = { if !unsafe_yyjson_is_ctn(val) { cold_path!(); match ElementType::from_tag(val) { ElementType::String => parse_yy_string(val), ElementType::Uint64 => parse_yy_u64(val), ElementType::Int64 => parse_yy_i64(val), ElementType::Double => parse_yy_f64(val), ElementType::Null => PyNoneRef::none().as_non_null_ptr(), ElementType::True => PyBoolRef::pytrue().as_non_null_ptr(), ElementType::False => PyBoolRef::pyfalse().as_non_null_ptr(), ElementType::Array | ElementType::Object => unreachable_unchecked!(), } } else if is_yyjson_tag!(val, TAG_ARRAY) { let pyval = PyListRef::with_capacity(unsafe_yyjson_get_len(val)); if unsafe_yyjson_get_len(val) > 0 { populate_yy_array(pyval.clone(), val); } pyval.as_non_null_ptr() } else { let pyval = PyDictRef::with_capacity(unsafe_yyjson_get_len(val)); if unsafe_yyjson_get_len(val) > 0 { populate_yy_object(pyval.clone(), val); } pyval.as_non_null_ptr() } }; ffi!(PyMem_Free(buffer_ptr)); Ok(pyval) } enum ElementType { String, Uint64, Int64, Double, Null, True, False, Array, Object, } impl ElementType { fn from_tag(elem: *mut yyjson_val) -> Self { match unsafe { (*elem).tag as u8 } { TAG_STRING => Self::String, TAG_UINT64 => Self::Uint64, TAG_INT64 => Self::Int64, TAG_DOUBLE => Self::Double, TAG_NULL => Self::Null, TAG_TRUE => Self::True, TAG_FALSE => Self::False, TAG_ARRAY => Self::Array, TAG_OBJECT => Self::Object, _ => unreachable_unchecked!(), } } } #[inline(always)] fn parse_yy_string(elem: *mut yyjson_val) -> NonNull { PyStrRef::from_str(str_from_slice!( (*elem).uni.str_.cast::(), unsafe_yyjson_get_len(elem) )) .as_non_null_ptr() } #[inline(always)] fn parse_yy_u64(elem: *mut yyjson_val) -> NonNull { PyIntRef::from_u64(unsafe { (*elem).uni.u64_ }).as_non_null_ptr() } #[inline(always)] fn parse_yy_i64(elem: *mut yyjson_val) -> NonNull { PyIntRef::from_i64(unsafe { (*elem).uni.i64_ }).as_non_null_ptr() } #[inline(always)] fn parse_yy_f64(elem: *mut yyjson_val) -> NonNull { PyFloatRef::from_f64(unsafe { (*elem).uni.f64_ }).as_non_null_ptr() } #[inline(never)] fn populate_yy_array(mut list: PyListRef, elem: *mut yyjson_val) { unsafe { let len = unsafe_yyjson_get_len(elem); assume!(len >= 1); let mut next = unsafe_yyjson_get_first(elem); for idx in 0..len { let val = next; if unsafe_yyjson_is_ctn(val) { cold_path!(); next = unsafe_yyjson_get_next_container(val); if is_yyjson_tag!(val, TAG_ARRAY) { let pyval = PyListRef::with_capacity(unsafe_yyjson_get_len(val)); list.set(idx, pyval.as_ptr()); if unsafe_yyjson_get_len(val) > 0 { populate_yy_array(pyval.clone(), val); } } else { let pyval = PyDictRef::with_capacity(unsafe_yyjson_get_len(val)); list.set(idx, pyval.as_ptr()); if unsafe_yyjson_get_len(val) > 0 { populate_yy_object(pyval.clone(), val); } } } else { next = unsafe_yyjson_get_next_non_container(val); let pyval = match ElementType::from_tag(val) { ElementType::String => parse_yy_string(val), ElementType::Uint64 => parse_yy_u64(val), ElementType::Int64 => parse_yy_i64(val), ElementType::Double => parse_yy_f64(val), ElementType::Null => PyNoneRef::none().as_non_null_ptr(), ElementType::True => PyBoolRef::pytrue().as_non_null_ptr(), ElementType::False => PyBoolRef::pyfalse().as_non_null_ptr(), ElementType::Array | ElementType::Object => unreachable_unchecked!(), }; list.set(idx, pyval.as_ptr()); } } } } #[inline(never)] fn populate_yy_object(mut dict: PyDictRef, elem: *mut yyjson_val) { unsafe { let len = unsafe_yyjson_get_len(elem); assume!(len >= 1); let mut next_key = unsafe_yyjson_get_first(elem); let mut next_val = next_key.add(1); for _ in 0..len { let val = next_val; let pykey = { let key_str = str_from_slice!( (*next_key).uni.str_.cast::(), unsafe_yyjson_get_len(next_key) ); get_unicode_key(key_str) }; if unsafe_yyjson_is_ctn(val) { cold_path!(); next_key = unsafe_yyjson_get_next_container(val); next_val = next_key.add(1); if is_yyjson_tag!(val, TAG_ARRAY) { let pyval = PyListRef::with_capacity(unsafe_yyjson_get_len(val)); dict.set(pykey, pyval.as_ptr()); if unsafe_yyjson_get_len(val) > 0 { populate_yy_array(pyval, val); } } else { let pyval = PyDictRef::with_capacity(unsafe_yyjson_get_len(val)); dict.set(pykey, pyval.as_ptr()); if unsafe_yyjson_get_len(val) > 0 { populate_yy_object(pyval.clone(), val); } } } else { next_key = unsafe_yyjson_get_next_non_container(val); next_val = next_key.add(1); let pyval = match ElementType::from_tag(val) { ElementType::String => parse_yy_string(val), ElementType::Uint64 => parse_yy_u64(val), ElementType::Int64 => parse_yy_i64(val), ElementType::Double => parse_yy_f64(val), ElementType::Null => PyNoneRef::none().as_non_null_ptr(), ElementType::True => PyBoolRef::pytrue().as_non_null_ptr(), ElementType::False => PyBoolRef::pyfalse().as_non_null_ptr(), ElementType::Array | ElementType::Object => unreachable_unchecked!(), }; dict.set(pykey, pyval.as_ptr()); } } } } ================================================ FILE: src/deserialize/cache.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2019-2026) use crate::ffi::{Py_DECREF, Py_INCREF, PyStrRef}; use associative_cache::{AssociativeCache, Capacity2048, HashDirectMapped, RoundRobinReplacement}; use core::cell::OnceCell; #[repr(transparent)] pub(crate) struct CachedKey { ptr: PyStrRef, } unsafe impl Send for CachedKey {} unsafe impl Sync for CachedKey {} impl CachedKey { pub const fn new(ptr: PyStrRef) -> CachedKey { CachedKey { ptr: ptr } } pub fn get(&mut self) -> PyStrRef { let ptr = self.ptr.as_ptr(); unsafe { Py_INCREF(ptr) }; self.ptr.clone() } } impl Drop for CachedKey { fn drop(&mut self) { unsafe { Py_DECREF(self.ptr.as_ptr().cast::()) }; } } pub(crate) type KeyMap = AssociativeCache; pub(crate) static mut KEY_MAP: OnceCell = OnceCell::new(); ================================================ FILE: src/deserialize/deserializer.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2024-2026) use super::DeserializeError; use super::input::Utf8Buffer; use crate::ffi::PyStrRef; use core::ptr::NonNull; #[repr(transparent)] pub struct Deserializer { buffer: Utf8Buffer, } impl Deserializer { #[inline] pub fn from_pyobject( ptr: *mut crate::ffi::PyObject, ) -> Result> { let buffer = Utf8Buffer::from_pyobject(ptr)?; debug_assert!(!buffer.as_str().is_empty()); Ok(Self { buffer: buffer }) } #[inline] pub fn deserialize(&self) -> Result, DeserializeError<'static>> { if self.buffer.len() == 2 { cold_path!(); match self.buffer.as_bytes() { b"[]" => { return Ok(nonnull!(ffi!(PyList_New(0)))); } b"{}" => { return Ok(nonnull!(unsafe { crate::ffi::PyDict_New(0) })); } b"\"\"" => { return Ok(PyStrRef::empty().as_non_null_ptr()); } _ => {} } } crate::deserialize::backend::deserialize(self.buffer.as_str()) } } pub(crate) fn deserialize( ptr: *mut crate::ffi::PyObject, ) -> Result, DeserializeError<'static>> { let deserializer = Deserializer::from_pyobject(ptr)?; deserializer.deserialize() } ================================================ FILE: src/deserialize/error.rs ================================================ // SPDX-License-Identifier: (Apache-2.0 OR MIT) // Copyright ijl (2022-2026), Eric Jolibois (2021) use std::borrow::Cow; pub(crate) struct DeserializeError<'a> { pub message: Cow<'a, str>, pub data: Option<&'a str>, pub pos: i64, } impl<'a> DeserializeError<'a> { #[cold] pub fn invalid(message: Cow<'a, str>) -> Self { DeserializeError { message: message, data: None, pos: 0, } } #[cold] pub fn from_yyjson(message: Cow<'a, str>, pos: i64, data: &'a str) -> Self { DeserializeError { message: message, data: Some(data), pos: pos, } } /// Return position of the error in the deserialized data #[cold] #[cfg_attr(feature = "optimize", optimize(size))] pub fn pos(&self) -> i64 { match self.data { Some(as_str) => { #[allow(clippy::cast_sign_loss)] let pos = self.pos as usize; #[allow(clippy::cast_possible_wrap)] let res = as_str[0..pos].chars().count() as i64; // stmt_expr_attributes res } None => 0, } } } ================================================ FILE: src/deserialize/input.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2026) use crate::deserialize::DeserializeError; #[cfg(all(CPython, not(Py_GIL_DISABLED)))] use crate::ffi::{PyByteArrayRef, PyMemoryViewRef}; use crate::ffi::{PyBytesRef, PyStrRef}; use crate::util::INVALID_STR; use std::borrow::Cow; #[cfg(all(CPython, not(Py_GIL_DISABLED)))] const INPUT_TYPE_MESSAGE: &str = "Input must be bytes, bytearray, memoryview, or str"; #[cfg(all(CPython, Py_GIL_DISABLED))] const INPUT_TYPE_MESSAGE: &str = "Input must be bytes or str"; #[cfg(not(CPython))] const INPUT_TYPE_MESSAGE: &str = "Input must be bytes, bytearray, or str"; #[cfg_attr(not(Py_GIL_DISABLED), repr(transparent))] pub struct Utf8Buffer { buffer: &'static str, } impl Utf8Buffer { #[cfg(all(CPython, not(Py_GIL_DISABLED)))] fn buffer_from_ptr( ptr: *mut crate::ffi::PyObject, ) -> Result, DeserializeError<'static>> { if let Ok(ob) = PyBytesRef::from_ptr(ptr) { Ok(ob.as_str()) } else if let Ok(ob) = PyStrRef::from_ptr(ptr) { Ok(ob.as_str()) } else if let Ok(ob) = PyByteArrayRef::from_ptr(ptr) { Ok(ob.as_str()) } else if let Ok(ob) = PyMemoryViewRef::from_ptr(ptr) { Ok(ob.as_str()) } else { Err(DeserializeError::invalid(Cow::Borrowed(INPUT_TYPE_MESSAGE))) } } #[cfg(any(not(CPython), Py_GIL_DISABLED))] fn buffer_from_ptr( ptr: *mut crate::ffi::PyObject, ) -> Result, DeserializeError<'static>> { if let Ok(ob) = PyBytesRef::from_ptr(ptr) { Ok(ob.as_str()) } else if let Ok(ob) = PyStrRef::from_ptr(ptr) { Ok(ob.as_str()) } else { Err(DeserializeError::invalid(Cow::Borrowed(INPUT_TYPE_MESSAGE))) } } pub fn from_pyobject( ptr: *mut crate::ffi::PyObject, ) -> Result> { debug_assert!(!ptr.is_null()); match Utf8Buffer::buffer_from_ptr(ptr) { Ok(Some(as_str)) => { if as_str.is_empty() { cold_path!(); Err(DeserializeError::invalid(Cow::Borrowed( "Input is a zero-length, empty document", ))) } else { Ok(Self { buffer: as_str }) } } Ok(None) => { cold_path!(); Err(DeserializeError::invalid(Cow::Borrowed(INVALID_STR))) } Err(_) => Err(DeserializeError::invalid(Cow::Borrowed(INPUT_TYPE_MESSAGE))), } } pub fn as_str(&self) -> &'static str { self.buffer } pub fn as_bytes(&self) -> &'static [u8] { self.buffer.as_bytes() } pub fn len(&self) -> usize { self.buffer.len() } } ================================================ FILE: src/deserialize/mod.rs ================================================ // SPDX-License-Identifier: (Apache-2.0 OR MIT) // Copyright ijl (2020-2026), Eric Jolibois (2021) mod backend; #[cfg(not(Py_GIL_DISABLED))] mod cache; mod deserializer; mod error; mod input; mod pyobject; #[cfg(not(Py_GIL_DISABLED))] pub(crate) use cache::{KEY_MAP, KeyMap}; pub(crate) use deserializer::deserialize; pub(crate) use error::DeserializeError; ================================================ FILE: src/deserialize/pyobject.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2022-2026) use crate::ffi::PyStrRef; #[cfg(all(CPython, not(Py_GIL_DISABLED)))] #[inline(always)] pub(crate) fn get_unicode_key(key_str: &str) -> PyStrRef { if key_str.len() > 64 { cold_path!(); PyStrRef::from_str_with_hash(key_str) } else { assume!(key_str.len() <= 64); let hash = xxhash_rust::xxh3::xxh3_64(key_str.as_bytes()); unsafe { let entry = crate::deserialize::cache::KEY_MAP .get_mut() .unwrap_or_else(|| unreachable_unchecked!()) .entry(&hash) .or_insert_with( || hash, || { crate::deserialize::cache::CachedKey::new(PyStrRef::from_str_with_hash( key_str, )) }, ); entry.get() } } } #[cfg(all(CPython, Py_GIL_DISABLED))] #[inline(always)] pub(crate) fn get_unicode_key(key_str: &str) -> PyStrRef { PyStrRef::from_str_with_hash(key_str) } #[cfg(not(CPython))] #[inline(always)] pub(crate) fn get_unicode_key(key_str: &str) -> PyStrRef { PyStrRef::from_str(key_str) } ================================================ FILE: src/exception.rs ================================================ // SPDX-License-Identifier: (Apache-2.0 OR MIT) // Copyright ijl (2020-2026), Jack Amadeo (2023) use core::ptr::null_mut; use crate::deserialize::DeserializeError; use crate::ffi::{Py_DECREF, PyErr_SetObject, PyIntRef, PyObject, PyStrRef, PyTupleRef}; use crate::typeref::{JsonDecodeError, JsonEncodeError}; #[cold] #[inline(never)] #[cfg_attr(feature = "optimize", optimize(size))] pub(crate) fn raise_loads_exception(err: DeserializeError) -> *mut PyObject { unsafe { let err_pos = PyIntRef::from_i64(err.pos()); let err_msg = PyStrRef::from_str(&err.message); let doc = match err.data { Some(as_str) => PyStrRef::from_str(as_str), None => PyStrRef::empty(), }; let mut args = PyTupleRef::with_capacity(3); args.set(0, err_msg.as_ptr()); args.set(1, doc.as_ptr()); args.set(2, err_pos.as_ptr()); PyErr_SetObject(JsonDecodeError, args.as_ptr()); Py_DECREF(args.as_ptr()); } null_mut() } #[cold] #[inline(never)] #[cfg_attr(feature = "optimize", optimize(size))] pub(crate) fn raise_dumps_exception_fixed(msg: &str) -> *mut PyObject { unsafe { let err_msg = PyStrRef::from_str(msg); PyErr_SetObject(JsonEncodeError, err_msg.as_ptr()); Py_DECREF(err_msg.as_ptr()); } null_mut() } #[cold] #[inline(never)] #[cfg_attr(feature = "optimize", optimize(size))] #[cfg(Py_3_12)] pub(crate) fn raise_dumps_exception_dynamic(err: &str) -> *mut PyObject { unsafe { let cause_exc: *mut PyObject = crate::ffi::PyErr_GetRaisedException(); let err_msg = PyStrRef::from_str(err); PyErr_SetObject(JsonEncodeError, err_msg.as_ptr()); Py_DECREF(err_msg.as_ptr()); if !cause_exc.is_null() { let exc: *mut PyObject = crate::ffi::PyErr_GetRaisedException(); crate::ffi::PyException_SetCause(exc, cause_exc); crate::ffi::PyErr_SetRaisedException(exc); } } null_mut() } #[cold] #[inline(never)] #[cfg_attr(feature = "optimize", optimize(size))] #[cfg(not(Py_3_12))] pub(crate) fn raise_dumps_exception_dynamic(err: &str) -> *mut PyObject { unsafe { let mut cause_tp: *mut PyObject = null_mut(); let mut cause_val: *mut PyObject = null_mut(); let mut cause_traceback: *mut PyObject = null_mut(); crate::ffi::PyErr_Fetch(&mut cause_tp, &mut cause_val, &mut cause_traceback); let err_msg = PyStrRef::from_str(err); PyErr_SetObject(JsonEncodeError, err_msg.as_ptr()); Py_DECREF(err_msg.as_ptr()); let mut tp: *mut PyObject = null_mut(); let mut val: *mut PyObject = null_mut(); let mut traceback: *mut PyObject = null_mut(); crate::ffi::PyErr_Fetch(&mut tp, &mut val, &mut traceback); crate::ffi::PyErr_NormalizeException(&mut tp, &mut val, &mut traceback); if !cause_tp.is_null() { crate::ffi::PyErr_NormalizeException( &mut cause_tp, &mut cause_val, &mut cause_traceback, ); crate::ffi::PyException_SetCause(val, cause_val); Py_DECREF(cause_tp); } if !cause_traceback.is_null() { Py_DECREF(cause_traceback); } crate::ffi::PyErr_Restore(tp, val, traceback); } null_mut() } ================================================ FILE: src/ffi/atomiculong.rs ================================================ // SPDX-License-Identifier: (Apache-2.0 OR MIT) // pyo3-ffi/src/impl_/mod.rs at 0.27.1 mod atomic_c_ulong { pub struct GetAtomicCULong(); pub trait AtomicCULongType { type Type; } impl AtomicCULongType for GetAtomicCULong<32> { type Type = core::sync::atomic::AtomicU32; } impl AtomicCULongType for GetAtomicCULong<64> { type Type = core::sync::atomic::AtomicU64; } pub type TYPE = () * 8 }> as AtomicCULongType>::Type; } pub type AtomicCULong = atomic_c_ulong::TYPE; ================================================ FILE: src/ffi/buffer.rs ================================================ // SPDX-License-Identifier: (Apache-2.0 OR MIT) // Copyright ijl (2021-2026), Baul (2020) use crate::ffi::{Py_buffer, Py_hash_t, Py_ssize_t, PyObject, PyVarObject}; use core::ffi::c_int; #[repr(C)] pub(crate) struct _PyManagedBufferObject { pub ob_base: *mut PyObject, pub flags: c_int, pub exports: Py_ssize_t, pub master: *mut Py_buffer, } #[repr(C)] pub(crate) struct PyMemoryViewObject { pub ob_base: PyVarObject, pub mbuf: *mut _PyManagedBufferObject, pub hash: Py_hash_t, pub flags: c_int, pub exports: Py_ssize_t, pub view: Py_buffer, pub weakreflist: *mut PyObject, pub ob_array: [Py_ssize_t; 1], } #[allow(non_snake_case)] #[inline(always)] pub(crate) unsafe fn PyMemoryView_GET_BUFFER(op: *mut PyObject) -> *const Py_buffer { unsafe { &raw const (*op.cast::()).view } } ================================================ FILE: src/ffi/bytes.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2022-2026) use crate::ffi::{Py_ssize_t, PyObject}; use core::ffi::c_char; pub(crate) use pyo3_ffi::PyBytesObject; #[cfg(CPython)] #[allow(non_snake_case)] #[inline(always)] pub(crate) unsafe fn PyBytes_AS_STRING(op: *mut PyObject) -> *const c_char { unsafe { (&raw const (*op.cast::()).ob_sval).cast::() } } #[cfg(not(CPython))] #[allow(non_snake_case)] #[inline(always)] pub(crate) unsafe fn PyBytes_AS_STRING(op: *mut PyObject) -> *const c_char { unsafe { pyo3_ffi::PyBytes_AsString(op) } } #[allow(non_snake_case)] #[inline(always)] pub(crate) unsafe fn PyBytes_GET_SIZE(op: *mut PyObject) -> Py_ssize_t { unsafe { super::compat::Py_SIZE(op) } } ================================================ FILE: src/ffi/compat.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2024-2026) #[cfg(Py_GIL_DISABLED)] #[allow(non_upper_case_globals)] pub(crate) const _Py_IMMORTAL_REFCNT_LOCAL: u32 = u32::MAX; #[cfg(all(Py_3_14, target_pointer_width = "32"))] #[allow(non_upper_case_globals)] pub(crate) const _Py_IMMORTAL_MINIMUM_REFCNT: pyo3_ffi::Py_ssize_t = ((1 as core::ffi::c_long) << (30 as core::ffi::c_long)) as pyo3_ffi::Py_ssize_t; #[cfg(all(Py_3_12, not(Py_3_14)))] #[allow(non_upper_case_globals)] pub(crate) const _Py_IMMORTAL_REFCNT: pyo3_ffi::Py_ssize_t = { if cfg!(target_pointer_width = "64") { core::ffi::c_uint::MAX as pyo3_ffi::Py_ssize_t } else { // for 32-bit systems, use the lower 30 bits (see comment in CPython's object.h) (core::ffi::c_uint::MAX >> 2) as pyo3_ffi::Py_ssize_t } }; #[cfg(all(Py_3_12, not(Py_GIL_DISABLED)))] #[inline(always)] #[allow(non_snake_case)] pub(crate) unsafe fn _Py_IsImmortal(op: *mut pyo3_ffi::PyObject) -> core::ffi::c_int { unsafe { #[cfg(all(target_pointer_width = "64", not(Py_GIL_DISABLED)))] { (((*op).ob_refcnt.ob_refcnt as pyo3_ffi::PY_INT32_T) < 0) as core::ffi::c_int } #[cfg(all(target_pointer_width = "32", not(Py_GIL_DISABLED)))] { #[cfg(not(Py_3_14))] { ((*op).ob_refcnt.ob_refcnt == _Py_IMMORTAL_REFCNT) as core::ffi::c_int } #[cfg(Py_3_14)] { ((*op).ob_refcnt.ob_refcnt >= _Py_IMMORTAL_MINIMUM_REFCNT) as core::ffi::c_int } } #[cfg(Py_GIL_DISABLED)] { ((*op) .ob_ref_local .load(core::sync::atomic::Ordering::Relaxed) == _Py_IMMORTAL_REFCNT_LOCAL) as core::ffi::c_int } } } #[cfg(CPython)] #[inline(always)] #[allow(non_snake_case)] pub(crate) unsafe fn PyDict_New(len: isize) -> *mut pyo3_ffi::PyObject { unsafe { if len > 8 { _PyDict_NewPresized(len) } else { pyo3_ffi::PyDict_New() } } } #[cfg(not(CPython))] #[inline(always)] #[allow(non_snake_case)] pub(crate) unsafe fn PyDict_New(_len: isize) -> *mut pyo3_ffi::PyObject { unsafe { pyo3_ffi::PyDict_New() } } #[cfg(all(CPython, Py_3_14))] #[inline(always)] #[allow(non_snake_case)] pub(crate) unsafe fn Py_HashBuffer( ptr: *const core::ffi::c_void, len: pyo3_ffi::Py_ssize_t, ) -> pyo3_ffi::Py_hash_t { unsafe { pyo3_ffi::Py_HashBuffer(ptr, len) } } #[cfg(all(CPython, not(Py_3_14)))] #[inline(always)] #[allow(non_snake_case)] pub(crate) unsafe fn Py_HashBuffer( ptr: *const core::ffi::c_void, len: pyo3_ffi::Py_ssize_t, ) -> pyo3_ffi::Py_hash_t { unsafe { pyo3_ffi::_Py_HashBytes(ptr, len) } } #[cfg(not(Py_3_13))] #[inline(always)] #[allow(non_snake_case)] pub(crate) unsafe fn PyLong_AsByteArray( v: *mut pyo3_ffi::PyLongObject, bytes: *mut core::ffi::c_uchar, n: pyo3_ffi::Py_ssize_t, little_endian: core::ffi::c_int, is_signed: core::ffi::c_int, ) -> core::ffi::c_int { unsafe { _PyLong_AsByteArray(v, bytes, n, little_endian, is_signed) } } #[cfg(Py_3_13)] #[inline(always)] #[allow(non_snake_case)] pub(crate) unsafe fn PyLong_AsByteArray( v: *mut pyo3_ffi::PyLongObject, bytes: *mut core::ffi::c_uchar, n: pyo3_ffi::Py_ssize_t, little_endian: core::ffi::c_int, is_signed: core::ffi::c_int, ) -> core::ffi::c_int { unsafe { _PyLong_AsByteArray(v, bytes, n, little_endian, is_signed, 0) } } #[cfg(CPython)] #[inline(always)] #[allow(non_snake_case)] pub(crate) unsafe fn Py_SIZE(op: *mut pyo3_ffi::PyObject) -> pyo3_ffi::Py_ssize_t { unsafe { (*op.cast::()).ob_size } } #[cfg(not(CPython))] #[inline(always)] #[allow(non_snake_case)] pub(crate) unsafe fn Py_SIZE(op: *mut pyo3_ffi::PyObject) -> pyo3_ffi::Py_ssize_t { unsafe { pyo3_ffi::Py_SIZE(op) } } #[allow(unused)] #[cfg(any(CPython, PyPy))] #[inline(always)] #[allow(non_snake_case)] pub(crate) unsafe fn Py_SET_SIZE(op: *mut pyo3_ffi::PyVarObject, size: pyo3_ffi::Py_ssize_t) { unsafe { (*op).ob_size = size } } #[allow(unused)] #[cfg(GraalPy)] #[inline(always)] #[allow(non_snake_case)] pub(crate) unsafe fn Py_SET_SIZE(op: *mut pyo3_ffi::PyVarObject, size: pyo3_ffi::Py_ssize_t) { unsafe { (*op)._ob_size_graalpy = size } } #[cfg(CPython)] #[inline(always)] #[allow(non_snake_case)] pub(crate) unsafe fn PyTuple_GET_ITEM( op: *mut pyo3_ffi::PyObject, i: pyo3_ffi::Py_ssize_t, ) -> *mut pyo3_ffi::PyObject { unsafe { *(*op.cast::()) .ob_item .as_ptr() .offset(i) } } #[cfg(not(CPython))] #[inline(always)] #[allow(non_snake_case)] pub(crate) unsafe fn PyTuple_GET_ITEM( op: *mut pyo3_ffi::PyObject, i: pyo3_ffi::Py_ssize_t, ) -> *mut pyo3_ffi::PyObject { unsafe { pyo3_ffi::PyTuple_GetItem(op, i) } } #[cfg(CPython)] #[inline(always)] #[allow(non_snake_case)] pub(crate) unsafe fn PyTuple_SET_ITEM( op: *mut pyo3_ffi::PyObject, i: pyo3_ffi::Py_ssize_t, v: *mut pyo3_ffi::PyObject, ) { unsafe { *(*(op.cast::())) .ob_item .as_mut_ptr() .offset(i) = v; } } #[cfg(not(CPython))] #[inline(always)] #[allow(non_snake_case)] pub(crate) unsafe fn PyTuple_SET_ITEM( op: *mut pyo3_ffi::PyObject, i: pyo3_ffi::Py_ssize_t, v: *mut pyo3_ffi::PyObject, ) { unsafe { pyo3_ffi::PyTuple_SetItem(op, i, v); } } unsafe extern "C" { #[cfg(CPython)] pub fn _PyBytes_Resize( pv: *mut *mut pyo3_ffi::PyObject, newsize: pyo3_ffi::Py_ssize_t, ) -> core::ffi::c_int; #[cfg(CPython)] pub fn _PyDict_Next( mp: *mut pyo3_ffi::PyObject, pos: *mut pyo3_ffi::Py_ssize_t, key: *mut *mut pyo3_ffi::PyObject, value: *mut *mut pyo3_ffi::PyObject, hash: *mut pyo3_ffi::Py_hash_t, ) -> core::ffi::c_int; #[cfg(CPython)] pub fn _PyDict_NewPresized(minused: pyo3_ffi::Py_ssize_t) -> *mut pyo3_ffi::PyObject; #[cfg(CPython)] pub fn _PyDict_Contains_KnownHash( op: *mut pyo3_ffi::PyObject, key: *mut pyo3_ffi::PyObject, hash: pyo3_ffi::Py_hash_t, ) -> core::ffi::c_int; #[cfg(all(CPython, not(Py_3_13)))] pub(crate) fn _PyDict_SetItem_KnownHash( mp: *mut pyo3_ffi::PyObject, name: *mut pyo3_ffi::PyObject, value: *mut pyo3_ffi::PyObject, hash: pyo3_ffi::Py_hash_t, ) -> core::ffi::c_int; #[cfg(all(CPython, Py_3_13))] pub(crate) fn _PyDict_SetItem_KnownHash_LockHeld( mp: *mut pyo3_ffi::PyDictObject, name: *mut pyo3_ffi::PyObject, value: *mut pyo3_ffi::PyObject, hash: pyo3_ffi::Py_hash_t, ) -> core::ffi::c_int; #[cfg(Py_3_13)] #[cfg_attr(PyPy, link_name = "_PyPyLong_AsByteArrayO")] pub(crate) fn _PyLong_AsByteArray( v: *mut pyo3_ffi::PyLongObject, bytes: *mut core::ffi::c_uchar, n: pyo3_ffi::Py_ssize_t, little_endian: core::ffi::c_int, is_signed: core::ffi::c_int, with_exceptions: core::ffi::c_int, ) -> core::ffi::c_int; #[cfg(not(Py_3_13))] #[cfg_attr(PyPy, link_name = "_PyPyLong_AsByteArrayO")] pub(crate) fn _PyLong_AsByteArray( v: *mut pyo3_ffi::PyLongObject, bytes: *mut core::ffi::c_uchar, n: pyo3_ffi::Py_ssize_t, little_endian: core::ffi::c_int, is_signed: core::ffi::c_int, ) -> core::ffi::c_int; } ================================================ FILE: src/ffi/fragment.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2020-2026) use core::ffi::c_char; use crate::ffi::{ Py_DECREF, Py_INCREF, Py_TPFLAGS_DEFAULT, PyErr_SetObject, PyExc_TypeError, PyObject, PyTupleRef, PyType_Ready, PyType_Type, PyTypeObject, PyUnicode_FromStringAndSize, PyVarObject, }; use core::ptr::null_mut; #[cfg(Py_GIL_DISABLED)] use super::atomiculong::AtomicCULong; #[cfg(Py_GIL_DISABLED)] use core::sync::atomic::{AtomicIsize, AtomicU32}; #[cfg(Py_GIL_DISABLED)] macro_rules! pymutex_new { () => { unsafe { core::mem::zeroed() } }; } #[repr(C)] pub(crate) struct Fragment { #[cfg(Py_GIL_DISABLED)] pub ob_tid: usize, #[cfg(all(Py_GIL_DISABLED, Py_3_14))] pub ob_flags: u16, #[cfg(all(Py_GIL_DISABLED, not(Py_3_14)))] pub _padding: u16, #[cfg(Py_GIL_DISABLED)] pub ob_mutex: pyo3_ffi::PyMutex, #[cfg(Py_GIL_DISABLED)] pub ob_gc_bits: u8, #[cfg(Py_GIL_DISABLED)] pub ob_ref_local: AtomicU32, #[cfg(Py_GIL_DISABLED)] pub ob_ref_shared: AtomicIsize, #[cfg(not(Py_GIL_DISABLED))] pub ob_refcnt: pyo3_ffi::Py_ssize_t, #[cfg(PyPy)] pub ob_pypy_link: pyo3_ffi::Py_ssize_t, pub ob_type: *mut pyo3_ffi::PyTypeObject, pub contents: *mut pyo3_ffi::PyObject, } #[cold] #[inline(never)] #[cfg_attr(feature = "optimize", optimize(size))] fn raise_args_exception() { unsafe { let msg = "orjson.Fragment() takes exactly 1 positional argument"; let err_msg = PyUnicode_FromStringAndSize(msg.as_ptr().cast::(), msg.len().cast_signed()); PyErr_SetObject(PyExc_TypeError, err_msg); Py_DECREF(err_msg); }; } #[unsafe(no_mangle)] #[cold] #[cfg_attr(feature = "optimize", optimize(size))] pub(crate) unsafe extern "C" fn orjson_fragment_tp_new( _subtype: *mut PyTypeObject, args: *mut PyObject, kwds: *mut PyObject, ) -> *mut PyObject { unsafe { let argsob = PyTupleRef::from_ptr_unchecked(args); if argsob.len() != 1 || !kwds.is_null() { raise_args_exception(); null_mut() } else { let contents = argsob.get(0); Py_INCREF(contents); let obj = Box::new(Fragment { #[cfg(Py_GIL_DISABLED)] ob_tid: 0, #[cfg(all(Py_GIL_DISABLED, Py_3_14))] ob_flags: 0, #[cfg(all(Py_GIL_DISABLED, not(Py_3_14)))] _padding: 0, #[cfg(Py_GIL_DISABLED)] ob_mutex: pymutex_new!(), #[cfg(Py_GIL_DISABLED)] ob_gc_bits: 0, #[cfg(Py_GIL_DISABLED)] ob_ref_local: AtomicU32::new(0), #[cfg(Py_GIL_DISABLED)] ob_ref_shared: AtomicIsize::new(0), #[cfg(not(Py_GIL_DISABLED))] ob_refcnt: 1, #[cfg(PyPy)] ob_pypy_link: 0, ob_type: crate::typeref::FRAGMENT_TYPE, contents: contents, }); Box::into_raw(obj).cast::() } } } #[unsafe(no_mangle)] #[cold] #[cfg_attr(feature = "optimize", optimize(size))] pub(crate) unsafe extern "C" fn orjson_fragment_dealloc(object: *mut PyObject) { unsafe { Py_DECREF((*object.cast::()).contents); crate::ffi::PyMem_Free(object.cast::()); } } #[unsafe(no_mangle)] #[cold] #[cfg_attr(feature = "optimize", optimize(size))] pub(crate) unsafe extern "C" fn orjson_fragmenttype_new() -> *mut PyTypeObject { unsafe { #[cfg(Py_GIL_DISABLED)] let tp_flags: AtomicCULong = AtomicCULong::new(Py_TPFLAGS_DEFAULT | pyo3_ffi::Py_TPFLAGS_IMMUTABLETYPE); #[cfg(not(Py_GIL_DISABLED))] let tp_flags: core::ffi::c_ulong = Py_TPFLAGS_DEFAULT | pyo3_ffi::Py_TPFLAGS_IMMUTABLETYPE; let ob = Box::new(PyTypeObject { ob_base: PyVarObject { ob_base: PyObject { #[cfg(Py_GIL_DISABLED)] ob_tid: 0, #[cfg(all(Py_GIL_DISABLED, Py_3_14))] ob_flags: 0, #[cfg(all(Py_GIL_DISABLED, not(Py_3_14)))] _padding: 0, #[cfg(Py_GIL_DISABLED)] ob_mutex: pymutex_new!(), #[cfg(Py_GIL_DISABLED)] ob_gc_bits: 0, #[cfg(Py_GIL_DISABLED)] ob_ref_local: AtomicU32::new(crate::ffi::compat::_Py_IMMORTAL_REFCNT_LOCAL), #[cfg(Py_GIL_DISABLED)] ob_ref_shared: AtomicIsize::new(0), #[cfg(all(Py_3_12, not(Py_GIL_DISABLED)))] ob_refcnt: pyo3_ffi::PyObjectObRefcnt { ob_refcnt: 0 }, #[cfg(not(Py_3_12))] ob_refcnt: 0, #[cfg(PyPy)] ob_pypy_link: 0, ob_type: &raw mut PyType_Type, }, #[cfg(not(GraalPy))] ob_size: 0, #[cfg(GraalPy)] _ob_size_graalpy: 0, }, tp_name: c"orjson.Fragment".as_ptr(), tp_basicsize: core::mem::size_of::().cast_signed(), tp_itemsize: 0, tp_dealloc: Some(orjson_fragment_dealloc), tp_init: None, tp_new: Some(orjson_fragment_tp_new), tp_flags: tp_flags, // ... tp_bases: null_mut(), tp_cache: null_mut(), tp_del: None, tp_finalize: None, tp_free: None, tp_is_gc: None, tp_mro: null_mut(), tp_subclasses: null_mut(), tp_vectorcall: None, tp_version_tag: 0, tp_weaklist: null_mut(), tp_vectorcall_offset: 0, tp_getattr: None, tp_setattr: None, tp_as_async: null_mut(), tp_repr: None, tp_as_number: null_mut(), tp_as_sequence: null_mut(), tp_as_mapping: null_mut(), tp_hash: None, tp_call: None, tp_str: None, tp_getattro: None, tp_setattro: None, tp_as_buffer: null_mut(), tp_doc: core::ptr::null_mut(), tp_traverse: None, tp_clear: None, tp_richcompare: None, tp_weaklistoffset: 0, tp_iter: None, tp_iternext: None, tp_methods: null_mut(), tp_members: null_mut(), tp_getset: null_mut(), tp_base: null_mut(), tp_dict: null_mut(), tp_descr_get: None, tp_descr_set: None, tp_dictoffset: 0, tp_alloc: None, #[cfg(Py_3_12)] tp_watched: 0, }); let ob_ptr = Box::into_raw(ob); PyType_Ready(ob_ptr); ob_ptr } } ================================================ FILE: src/ffi/mod.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2022-2026) #[cfg(Py_GIL_DISABLED)] mod atomiculong; #[cfg(all(CPython, not(Py_GIL_DISABLED)))] mod buffer; mod bytes; pub(crate) mod compat; mod fragment; mod pyboolref; #[cfg(all(CPython, not(Py_GIL_DISABLED)))] mod pybytearrayref; mod pybytesref; mod pydictref; mod pyfloatref; mod pyfragmentref; mod pyintref; mod pylistref; #[cfg(all(CPython, not(Py_GIL_DISABLED)))] mod pymemoryview; mod pynoneref; mod pystrref; mod pytupleref; mod pyuuidref; mod utf8; pub(crate) use compat::*; #[allow(unused_imports)] pub(crate) use { bytes::{PyBytes_AS_STRING, PyBytes_GET_SIZE, PyBytesObject}, fragment::{Fragment, orjson_fragmenttype_new}, pyboolref::PyBoolRef, pybytesref::{PyBytesRef, PyBytesRefError}, pydictref::PyDictRef, pyfloatref::PyFloatRef, pyfragmentref::{PyFragmentRef, PyFragmentRefError}, pyintref::{PyIntError, PyIntKind, PyIntRef}, pylistref::PyListRef, pynoneref::PyNoneRef, pystrref::{PyStrRef, PyStrSubclassRef, set_str_create_fn}, pytupleref::PyTupleRef, pyuuidref::PyUuidRef, }; #[allow(unused_imports)] #[cfg(all(CPython, not(Py_GIL_DISABLED)))] pub(crate) use { pybytearrayref::{PyByteArrayRef, PyByteArrayRefError}, pymemoryview::{PyMemoryViewRef, PyMemoryViewRefError}, }; #[allow(unused_imports)] pub(crate) use pyo3_ffi::{ METH_FASTCALL, METH_KEYWORDS, METH_O, Py_DECREF, Py_False, Py_INCREF, Py_None, Py_REFCNT, Py_TPFLAGS_DEFAULT, Py_TPFLAGS_DICT_SUBCLASS, Py_TPFLAGS_LIST_SUBCLASS, Py_TPFLAGS_LONG_SUBCLASS, Py_TPFLAGS_TUPLE_SUBCLASS, Py_TPFLAGS_UNICODE_SUBCLASS, Py_TYPE, Py_True, Py_XDECREF, Py_buffer, Py_hash_t, Py_intptr_t, Py_mod_exec, Py_ssize_t, PyASCIIObject, PyBool_Type, PyBuffer_IsContiguous, PyByteArray_AsString, PyByteArray_Size, PyByteArray_Type, PyBytes_FromStringAndSize, PyBytes_Type, PyCFunction_NewEx, PyCapsule_Import, PyCompactUnicodeObject, PyDateTime_CAPI, PyDateTime_DATE_GET_HOUR, PyDateTime_DATE_GET_MICROSECOND, PyDateTime_DATE_GET_MINUTE, PyDateTime_DATE_GET_SECOND, PyDateTime_DATE_GET_TZINFO, PyDateTime_DELTA_GET_DAYS, PyDateTime_DELTA_GET_SECONDS, PyDateTime_DateTime, PyDateTime_GET_DAY, PyDateTime_GET_MONTH, PyDateTime_GET_YEAR, PyDateTime_IMPORT, PyDateTime_TIME_GET_HOUR, PyDateTime_TIME_GET_MICROSECOND, PyDateTime_TIME_GET_MINUTE, PyDateTime_TIME_GET_SECOND, PyDateTime_Time, PyDict_Contains, PyDict_Next, PyDict_SetItem, PyDict_Type, PyDictObject, PyErr_Clear, PyErr_NewException, PyErr_Occurred, PyErr_SetObject, PyExc_TypeError, PyException_SetCause, PyFloat_AS_DOUBLE, PyFloat_FromDouble, PyFloat_Type, PyImport_ImportModule, PyList_GET_ITEM, PyList_New, PyList_SET_ITEM, PyList_Type, PyListObject, PyLong_AsLong, PyLong_AsLongLong, PyLong_AsUnsignedLongLong, PyLong_FromLongLong, PyLong_FromUnsignedLongLong, PyLong_Type, PyLongObject, PyMapping_GetItemString, PyMem_Free, PyMem_Malloc, PyMem_Realloc, PyMemoryView_Type, PyMethodDef, PyMethodDefPointer, PyModule_AddIntConstant, PyModule_AddObject, PyModuleDef, PyModuleDef_HEAD_INIT, PyModuleDef_Init, PyModuleDef_Slot, PyObject, PyObject_CallFunctionObjArgs, PyObject_CallMethodObjArgs, PyObject_GenericGetDict, PyObject_GetAttr, PyObject_HasAttr, PyObject_Hash, PyObject_Vectorcall, PyTuple_New, PyTuple_Type, PyTupleObject, PyType_Ready, PyType_Type, PyTypeObject, PyUnicode_AsUTF8AndSize, PyUnicode_FromStringAndSize, PyUnicode_InternFromString, PyUnicode_New, PyUnicode_Type, PyVarObject, PyVectorcall_NARGS, }; #[allow(unused_imports, deprecated)] pub(crate) use pyo3_ffi::PyErr_Restore; #[cfg(CPython)] pub(crate) use pyo3_ffi::{PyObject_CallMethodNoArgs, PyObject_CallMethodOneArg}; #[cfg(all(CPython, not(Py_GIL_DISABLED)))] pub(crate) use buffer::PyMemoryView_GET_BUFFER; #[cfg(not(feature = "inline_str"))] pub(crate) use pyo3_ffi::{PyUnicode_DATA, PyUnicode_KIND}; #[cfg(Py_3_12)] #[allow(unused_imports)] pub(crate) use pyo3_ffi::{ Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED, Py_mod_multiple_interpreters, PyErr_GetRaisedException, PyErr_SetRaisedException, PyType_GetDict, }; #[cfg(not(Py_3_12))] #[allow(unused_imports)] pub(crate) use pyo3_ffi::{PyErr_Fetch, PyErr_NormalizeException}; #[cfg(not(Py_3_13))] #[allow(unused_imports)] pub(crate) use pyo3_ffi::PyModule_AddObjectRef; #[cfg(Py_3_13)] #[allow(unused_imports)] pub(crate) use pyo3_ffi::PyModule_Add; #[cfg(Py_3_13)] #[allow(unused_imports)] pub(crate) use pyo3_ffi::{Py_MOD_GIL_USED, Py_mod_gil}; ================================================ FILE: src/ffi/pyboolref.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2026) #[derive(Clone)] #[repr(transparent)] pub(crate) struct PyBoolRef { ptr: core::ptr::NonNull, } unsafe impl Send for PyBoolRef {} unsafe impl Sync for PyBoolRef {} impl PartialEq for PyBoolRef { fn eq(&self, other: &Self) -> bool { self.ptr == other.ptr } } impl PyBoolRef { #[inline] pub(crate) unsafe fn from_ptr_unchecked(ptr: *mut pyo3_ffi::PyObject) -> Self { unsafe { debug_assert!(!ptr.is_null()); debug_assert!(ob_type!(ptr) == crate::typeref::BOOL_TYPE); Self { ptr: core::ptr::NonNull::new_unchecked(ptr), } } } #[inline] #[allow(unused)] pub const fn as_ptr(&self) -> *mut pyo3_ffi::PyObject { self.ptr.as_ptr() } #[inline] #[allow(unused)] pub const fn as_non_null_ptr(&self) -> core::ptr::NonNull { self.ptr } #[inline] pub fn pytrue() -> Self { Self { ptr: unsafe { core::ptr::NonNull::new_unchecked(use_immortal!(crate::typeref::TRUE)) }, } } #[inline] pub fn pyfalse() -> Self { Self { ptr: unsafe { core::ptr::NonNull::new_unchecked(use_immortal!(crate::typeref::FALSE)) }, } } } ================================================ FILE: src/ffi/pybytearrayref.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2026) pub(crate) enum PyByteArrayRefError { NotType, } #[derive(Clone)] #[repr(transparent)] pub(crate) struct PyByteArrayRef { ptr: core::ptr::NonNull, } unsafe impl Send for PyByteArrayRef {} unsafe impl Sync for PyByteArrayRef {} impl PartialEq for PyByteArrayRef { fn eq(&self, other: &Self) -> bool { self.ptr == other.ptr } } impl PyByteArrayRef { #[inline] pub fn from_ptr(ptr: *mut pyo3_ffi::PyObject) -> Result { unsafe { debug_assert!(!ptr.is_null()); if ob_type!(ptr) == &raw mut crate::ffi::PyByteArray_Type { Ok(Self { ptr: core::ptr::NonNull::new_unchecked(ptr), }) } else { Err(PyByteArrayRefError::NotType) } } } #[inline] pub fn as_ptr(&self) -> *mut pyo3_ffi::PyObject { self.ptr.as_ptr() } #[allow(unused)] #[inline] pub fn as_non_null_ptr(&self) -> core::ptr::NonNull { self.ptr } #[inline] pub fn as_bytes(&self) -> &'static [u8] { unsafe { core::slice::from_raw_parts( crate::ffi::PyByteArray_AsString(self.as_ptr()) .cast::() .cast_const(), crate::util::isize_to_usize(crate::ffi::PyByteArray_Size(self.as_ptr())), ) } } #[inline] pub fn as_str(&self) -> Option<&'static str> { let buffer = self.as_bytes(); if !crate::ffi::utf8::is_valid_utf8(buffer) { cold_path!(); None } else { unsafe { Some(core::str::from_utf8_unchecked(buffer)) } } } } ================================================ FILE: src/ffi/pybytesref.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2026) pub(crate) enum PyBytesRefError { NotType, } #[derive(Clone)] #[repr(transparent)] pub(crate) struct PyBytesRef { ptr: core::ptr::NonNull, } unsafe impl Send for PyBytesRef {} unsafe impl Sync for PyBytesRef {} impl PartialEq for PyBytesRef { fn eq(&self, other: &Self) -> bool { self.ptr == other.ptr } } impl PyBytesRef { #[inline] pub fn from_ptr(ptr: *mut pyo3_ffi::PyObject) -> Result { unsafe { debug_assert!(!ptr.is_null()); if ob_type!(ptr) == crate::typeref::BYTES_TYPE { Ok(Self { ptr: core::ptr::NonNull::new_unchecked(ptr), }) } else { Err(PyBytesRefError::NotType) } } } #[inline] pub fn as_ptr(&self) -> *mut pyo3_ffi::PyObject { self.ptr.as_ptr() } #[allow(unused)] #[inline] pub fn as_non_null_ptr(&self) -> core::ptr::NonNull { self.ptr } #[inline] pub fn as_bytes(&self) -> &'static [u8] { unsafe { core::slice::from_raw_parts( crate::ffi::PyBytes_AS_STRING(self.as_ptr()).cast::(), crate::util::isize_to_usize(crate::ffi::PyBytes_GET_SIZE(self.as_ptr())), ) } } #[inline] pub fn as_str(&self) -> Option<&'static str> { let buffer = self.as_bytes(); if !crate::ffi::utf8::is_valid_utf8(buffer) { cold_path!(); None } else { unsafe { Some(core::str::from_utf8_unchecked(buffer)) } } } } ================================================ FILE: src/ffi/pydictref.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2025-2026) #[allow(unused)] use super::Py_TPFLAGS_DICT_SUBCLASS; #[derive(Clone)] #[repr(transparent)] pub(crate) struct PyDictRef { ptr: core::ptr::NonNull, } unsafe impl Send for PyDictRef {} unsafe impl Sync for PyDictRef {} impl PartialEq for PyDictRef { fn eq(&self, other: &Self) -> bool { self.ptr == other.ptr } } impl PyDictRef { #[cfg(CPython)] #[inline] pub fn with_capacity(cap: usize) -> Self { unsafe { let ptr = crate::ffi::PyDict_New(crate::util::usize_to_isize(cap)); debug_assert!(!ptr.is_null()); Self { ptr: nonnull!(ptr) } } } #[cfg(not(CPython))] #[allow(unused)] #[inline] pub fn with_capacity(_cap: usize) -> Self { Self::new() } #[allow(unused)] #[inline] pub fn new() -> Self { unsafe { let ptr = crate::ffi::PyDict_New(0); debug_assert!(!ptr.is_null()); Self { ptr: nonnull!(ptr) } } } #[inline] pub(crate) unsafe fn from_ptr_unchecked(ptr: *mut pyo3_ffi::PyObject) -> Self { unsafe { debug_assert!(!ptr.is_null()); debug_assert!( ob_type!(ptr) == crate::typeref::DICT_TYPE || is_subclass_by_flag!(tp_flags!(ob_type!(ptr)), Py_TPFLAGS_DICT_SUBCLASS) ); Self { ptr: core::ptr::NonNull::new_unchecked(ptr), } } } #[inline] #[allow(unused)] pub fn as_ptr(&self) -> *mut pyo3_ffi::PyObject { self.ptr.as_ptr() } #[inline] #[allow(unused)] pub fn as_non_null_ptr(&self) -> core::ptr::NonNull { self.ptr } #[inline] pub fn len(&self) -> usize { unsafe { crate::util::isize_to_usize(super::Py_SIZE(self.as_ptr())) } } #[cfg(CPython)] #[inline] pub fn set(&mut self, key: crate::ffi::PyStrRef, value: *mut crate::ffi::PyObject) { debug_assert!(ffi!(Py_REFCNT(self.as_ptr())) == 1); debug_assert!(key.hash() != -1); #[cfg(not(Py_3_13))] unsafe { let _ = crate::ffi::_PyDict_SetItem_KnownHash( self.as_ptr(), key.as_ptr(), value, key.hash(), ); } #[cfg(Py_3_13)] unsafe { let _ = crate::ffi::_PyDict_SetItem_KnownHash_LockHeld( self.as_ptr().cast::(), key.as_ptr(), value, key.hash(), ); } #[cfg(not(Py_GIL_DISABLED))] reverse_pydict_incref!(key.as_ptr()); reverse_pydict_incref!(value); } #[cfg(not(CPython))] #[inline] pub fn set(&mut self, key: crate::ffi::PyStrRef, value: *mut crate::ffi::PyObject) { unsafe { let _ = crate::ffi::PyDict_SetItem(self.as_ptr(), key.as_ptr(), value); } #[cfg(not(Py_GIL_DISABLED))] reverse_pydict_incref!(key.as_ptr()); reverse_pydict_incref!(value); } } ================================================ FILE: src/ffi/pyfloatref.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2026) #[derive(Clone)] #[repr(transparent)] pub(crate) struct PyFloatRef { ptr: core::ptr::NonNull, } unsafe impl Send for PyFloatRef {} unsafe impl Sync for PyFloatRef {} impl PartialEq for PyFloatRef { fn eq(&self, other: &Self) -> bool { self.ptr == other.ptr } } impl PyFloatRef { #[inline] pub(crate) unsafe fn from_ptr_unchecked(ptr: *mut pyo3_ffi::PyObject) -> Self { unsafe { debug_assert!(!ptr.is_null()); debug_assert!(ob_type!(ptr) == crate::typeref::FLOAT_TYPE); Self { ptr: core::ptr::NonNull::new_unchecked(ptr), } } } #[inline] pub fn as_non_null_ptr(&self) -> core::ptr::NonNull { self.ptr } #[inline] pub fn value(&self) -> f64 { unsafe { super::PyFloat_AS_DOUBLE(self.ptr.as_ptr()) } } #[inline] pub fn from_f64(value: f64) -> Self { unsafe { let ptr = super::PyFloat_FromDouble(value); Self::from_ptr_unchecked(ptr) } } } ================================================ FILE: src/ffi/pyfragmentref.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2026) use crate::ffi::{Fragment, PyBytesRef, PyStrRef}; pub(crate) enum PyFragmentRefError { InvalidStr, InvalidFragment, } #[repr(transparent)] pub(crate) struct PyFragmentRef { ptr: core::ptr::NonNull, } unsafe impl Send for PyFragmentRef {} unsafe impl Sync for PyFragmentRef {} impl PartialEq for PyFragmentRef { fn eq(&self, other: &Self) -> bool { self.ptr == other.ptr } } impl PyFragmentRef { #[inline] pub(crate) unsafe fn from_ptr_unchecked(ptr: *mut pyo3_ffi::PyObject) -> Self { unsafe { debug_assert!(!ptr.is_null()); debug_assert!(ob_type!(ptr) == crate::typeref::FRAGMENT_TYPE); Self { ptr: core::ptr::NonNull::new_unchecked(ptr), } } } #[inline] #[allow(unused)] pub fn as_ptr(&self) -> *mut pyo3_ffi::PyObject { self.ptr.as_ptr() } #[inline] #[allow(unused)] pub fn as_non_null_ptr(&self) -> core::ptr::NonNull { self.ptr } #[cold] pub fn value(&self) -> Result<&[u8], PyFragmentRefError> { let buffer: &[u8]; unsafe { let contents: *mut pyo3_ffi::PyObject = (*self.ptr.as_ptr().cast::()).contents; if let Ok(ob) = PyBytesRef::from_ptr(contents) { buffer = ob.as_bytes(); } else if let Ok(ob) = PyStrRef::from_ptr(contents) { match ob.as_str() { Some(ob) => buffer = ob.as_bytes(), None => return Err(PyFragmentRefError::InvalidStr), } } else { return Err(PyFragmentRefError::InvalidFragment); } Ok(buffer) } } } ================================================ FILE: src/ffi/pyintref.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2023-2026) #[allow(unused)] use super::Py_TPFLAGS_LONG_SUBCLASS; use super::{PyLong_FromLongLong, PyLong_FromUnsignedLongLong, PyObject}; use crate::opt::{MAX_OPT, Opt}; // longintrepr.h, _longobject, _PyLongValue #[allow(dead_code)] #[cfg(Py_3_12)] #[allow(non_upper_case_globals)] const SIGN_MASK: usize = 3; #[cfg(all(Py_3_12, feature = "inline_int"))] #[allow(non_upper_case_globals)] const NON_SIZE_BITS: usize = 3; #[cfg(Py_3_12)] #[repr(C)] pub(crate) struct _PyLongValue { pub lv_tag: usize, pub ob_digit: u32, } #[cfg(all(Py_3_12, feature = "inline_int"))] #[repr(C)] pub(crate) struct PyLongObject { pub ob_base: super::PyObject, pub long_value: _PyLongValue, } #[allow(dead_code)] #[cfg(all(not(Py_3_12), feature = "inline_int"))] #[repr(C)] pub(crate) struct PyLongObject { pub ob_base: super::PyVarObject, pub ob_digit: u32, } pub(crate) enum PyIntError { NotType, #[cfg(not(feature = "inline_int"))] NotSigned, Exceeds64Bit, } pub(crate) enum PyIntOptConversionError { InvalidRange, } #[allow(unused)] #[derive(PartialEq)] pub(crate) enum PyIntKind { U32, I32, U64, I64, } #[derive(Clone)] #[repr(transparent)] pub(crate) struct PyIntRef { ptr: core::ptr::NonNull, } unsafe impl Send for PyIntRef {} unsafe impl Sync for PyIntRef {} impl PartialEq for PyIntRef { fn eq(&self, other: &Self) -> bool { self.ptr == other.ptr } } impl PyIntRef { #[inline] pub fn from_ptr(ptr: *mut pyo3_ffi::PyObject) -> Result { unsafe { debug_assert!(!ptr.is_null()); if ob_type!(ptr) == crate::typeref::INT_TYPE { Ok(Self { ptr: core::ptr::NonNull::new_unchecked(ptr), }) } else { Err(PyIntError::NotType) } } } #[inline] pub unsafe fn from_ptr_unchecked(ptr: *mut PyObject) -> Self { unsafe { debug_assert!(!ptr.is_null()); debug_assert!( ob_type!(ptr) == crate::typeref::INT_TYPE || is_subclass_by_flag!(tp_flags!(ob_type!(ptr)), Py_TPFLAGS_LONG_SUBCLASS) ); Self { ptr: core::ptr::NonNull::new_unchecked(ptr), } } } #[inline] pub fn as_ptr(&self) -> *mut PyObject { self.ptr.as_ptr() } #[inline] pub fn as_non_null_ptr(&self) -> core::ptr::NonNull { self.ptr } #[cfg(feature = "inline_int")] #[inline] pub fn kind(&self) -> PyIntKind { match (self.is_signed(), self.fits_in_i32()) { (true, true) => PyIntKind::I32, (true, false) => PyIntKind::I64, (false, true) => PyIntKind::U32, (false, false) => PyIntKind::U64, } } #[cfg(all(Py_3_12, feature = "inline_int"))] #[inline] pub fn is_signed(&self) -> bool { unsafe { (*self.as_ptr().cast::()).long_value.lv_tag & SIGN_MASK != 0 } } #[cfg(all(not(Py_3_12), feature = "inline_int"))] #[inline] pub fn is_signed(&self) -> bool { unsafe { (*self.as_ptr().cast::()).ob_size < 0 } } #[cfg(all(Py_3_12, feature = "inline_int"))] #[inline] pub fn fits_in_i32(&self) -> bool { unsafe { (*self.as_ptr().cast::()).long_value.lv_tag < (2 << NON_SIZE_BITS) } } #[cfg(all(not(Py_3_12), feature = "inline_int"))] #[inline] pub fn fits_in_i32(&self) -> bool { unsafe { isize::abs(super::Py_SIZE(self.as_ptr())) == 1 } } #[cfg(all(Py_3_12, feature = "inline_int"))] #[inline] fn get_inline_value(&self) -> u32 { unsafe { (*self.as_ptr().cast::()).long_value.ob_digit } } #[cfg(all(not(Py_3_12), feature = "inline_int"))] #[inline] fn get_inline_value(&self) -> u32 { unsafe { (*self.as_ptr().cast::()).ob_digit } } #[cfg(feature = "inline_int")] #[inline] pub unsafe fn as_u32(&self) -> u32 { debug_assert!(self.kind() == PyIntKind::U32); self.get_inline_value() } #[cfg(feature = "inline_int")] #[inline] pub unsafe fn as_i32(&self) -> i32 { debug_assert!(self.kind() == PyIntKind::I32); -(self.get_inline_value().cast_signed()) } #[cfg(feature = "inline_int")] #[inline] fn get_64bit_value(&self) -> Result<[u8; 8], PyIntError> { unsafe { let mut buffer: [u8; 8] = [0; 8]; let ret = crate::ffi::PyLong_AsByteArray( self.as_ptr().cast::(), buffer.as_mut_ptr().cast::(), 8, 1, i32::from(self.is_signed()), ); if ret == -1 { cold_path!(); #[cfg(not(Py_3_13))] ffi!(PyErr_Clear()); Err(PyIntError::Exceeds64Bit) } else { Ok(buffer) } } } #[cfg(feature = "inline_int")] #[inline] pub unsafe fn as_i64(&self) -> Result { debug_assert!(self.kind() == PyIntKind::I64); let val = self.get_64bit_value()?; #[allow(unnecessary_transmutes)] unsafe { Ok(core::mem::transmute::<[u8; 8], i64>(val)) } } #[cfg(feature = "inline_int")] #[inline] pub unsafe fn as_u64(&self) -> Result { debug_assert!(self.kind() == PyIntKind::U64); let val = self.get_64bit_value()?; #[allow(unnecessary_transmutes)] unsafe { Ok(core::mem::transmute::<[u8; 8], u64>(val)) } } #[cfg(not(feature = "inline_int"))] #[inline] pub unsafe fn as_i64(&self) -> Result { let ival = ffi!(PyLong_AsLongLong(self.as_ptr())); if ival == -1 && !ffi!(PyErr_Occurred()).is_null() { cold_path!(); ffi!(PyErr_Clear()); Err(PyIntError::NotSigned) } else { Ok(ival) } } #[cfg(not(feature = "inline_int"))] #[inline] pub unsafe fn as_u64(&self) -> Result { let uval = ffi!(PyLong_AsUnsignedLongLong(self.as_ptr())); if uval == u64::MAX && !ffi!(PyErr_Occurred()).is_null() { cold_path!(); Err(PyIntError::Exceeds64Bit) } else { Ok(uval) } } #[cfg(feature = "inline_int")] pub fn as_opt(&self) -> Result { let val = self.get_inline_value(); if val == 0 { Ok(val as Opt) } else { match self.kind() { PyIntKind::U32 => { if !(0..=MAX_OPT as u32).contains(&val) { Err(PyIntOptConversionError::InvalidRange) } else { Ok(val as Opt) } } _ => Err(PyIntOptConversionError::InvalidRange), } } } #[cfg(not(feature = "inline_int"))] pub fn as_opt(&self) -> Result { match unsafe { self.as_u64() } { Ok(val) => { if !(0..=MAX_OPT as u64).contains(&val) { Err(PyIntOptConversionError::InvalidRange) } else { Ok(val as Opt) } } Err(_) => Err(PyIntOptConversionError::InvalidRange), } } #[inline] pub fn from_i64(value: i64) -> Self { unsafe { let ptr = PyLong_FromLongLong(value); debug_assert!(!ptr.is_null()); Self::from_ptr_unchecked(ptr) } } #[inline] pub fn from_u64(value: u64) -> Self { unsafe { let ptr = PyLong_FromUnsignedLongLong(value); debug_assert!(!ptr.is_null()); Self::from_ptr_unchecked(ptr) } } } ================================================ FILE: src/ffi/pylistref.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2026) #[derive(Clone)] #[repr(transparent)] pub(crate) struct PyListRef { ptr: core::ptr::NonNull, } unsafe impl Send for PyListRef {} unsafe impl Sync for PyListRef {} impl PartialEq for PyListRef { fn eq(&self, other: &Self) -> bool { self.ptr == other.ptr } } impl PyListRef { #[inline] pub unsafe fn from_ptr_unchecked(ptr: *mut pyo3_ffi::PyObject) -> Self { unsafe { debug_assert!(!ptr.is_null()); debug_assert!( is_type!(ob_type!(ptr), crate::typeref::LIST_TYPE) || is_subclass_by_flag!(tp_flags!(ob_type!(ptr)), Py_TPFLAGS_LIST_SUBCLASS) ); Self { ptr: core::ptr::NonNull::new_unchecked(ptr), } } } #[inline] pub fn with_capacity(cap: usize) -> Self { unsafe { let list = super::PyList_New(crate::util::usize_to_isize(cap)); debug_assert!(!list.is_null()); Self { ptr: nonnull!(list), } } } #[inline] #[allow(unused)] pub fn as_ptr(&self) -> *mut pyo3_ffi::PyObject { self.ptr.as_ptr() } #[inline] #[allow(unused)] pub fn as_non_null_ptr(&self) -> core::ptr::NonNull { self.ptr } #[cfg(CPython)] #[inline] pub fn data_ptr(&self) -> *const *mut pyo3_ffi::PyObject { unsafe { (*self.ptr.as_ptr().cast::()).ob_item } } #[cfg(CPython)] #[inline] pub fn get(&mut self, i: usize) -> *mut pyo3_ffi::PyObject { unsafe { *((self.data_ptr()).add(i)) } } #[cfg(not(CPython))] #[inline] pub fn get(&mut self, i: usize) -> *mut pyo3_ffi::PyObject { unsafe { pyo3_ffi::PyList_GetItem(self.ptr.as_ptr(), crate::util::usize_to_isize(i)) } } #[cfg(CPython)] #[inline] pub fn set(&mut self, i: usize, val: *mut pyo3_ffi::PyObject) { unsafe { core::ptr::write(self.data_ptr().cast_mut().add(i), val); } } #[cfg(not(CPython))] pub fn set(&mut self, i: usize, val: *mut pyo3_ffi::PyObject) { unsafe { pyo3_ffi::PyList_SetItem(self.ptr.as_ptr(), crate::util::usize_to_isize(i), val) }; } #[inline] pub fn len(&self) -> usize { unsafe { crate::util::isize_to_usize(super::Py_SIZE(self.ptr.as_ptr())) } } } ================================================ FILE: src/ffi/pymemoryview.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2024-2026) #[allow(clippy::enum_variant_names)] #[allow(unused)] pub(crate) enum PyMemoryViewRefError { NotType, NotCContiguous, NotSupported, } #[derive(Clone)] #[repr(transparent)] pub(crate) struct PyMemoryViewRef { ptr: core::ptr::NonNull, } unsafe impl Send for PyMemoryViewRef {} unsafe impl Sync for PyMemoryViewRef {} impl PartialEq for PyMemoryViewRef { fn eq(&self, other: &Self) -> bool { self.ptr == other.ptr } } impl PyMemoryViewRef { #[cfg(CPython)] #[inline] pub fn from_ptr(ptr: *mut pyo3_ffi::PyObject) -> Result { unsafe { debug_assert!(!ptr.is_null()); if ob_type!(ptr) == &raw mut crate::ffi::PyMemoryView_Type { let membuf = unsafe { crate::ffi::PyMemoryView_GET_BUFFER(ptr) }; #[allow(clippy::cast_possible_wrap)] if unsafe { crate::ffi::PyBuffer_IsContiguous(membuf, b'C' as core::ffi::c_char) == 0 } { return Err(PyMemoryViewRefError::NotCContiguous); } Ok(Self { ptr: core::ptr::NonNull::new_unchecked(ptr), }) } else { Err(PyMemoryViewRefError::NotType) } } } #[cfg(not(CPython))] #[inline] pub fn from_ptr(ptr: *mut pyo3_ffi::PyObject) -> Result { unsafe { debug_assert!(!ptr.is_null()); if ob_type!(ptr) == &raw mut crate::ffi::PyMemoryView_Type { Err(PyMemoryViewRefError::NotSupported) } else { Err(PyMemoryViewRefError::NotType) } } } #[allow(unused)] #[inline] pub fn as_ptr(&self) -> *mut pyo3_ffi::PyObject { self.ptr.as_ptr() } #[allow(unused)] #[inline] pub fn as_non_null_ptr(&self) -> core::ptr::NonNull { self.ptr } #[cfg(CPython)] #[inline] pub fn as_bytes(&self) -> &'static [u8] { unsafe { let membuf = crate::ffi::PyMemoryView_GET_BUFFER(self.as_ptr()); core::slice::from_raw_parts( (*membuf).buf.cast::().cast_const(), crate::util::isize_to_usize((*membuf).len), ) } } #[cfg(CPython)] #[inline] pub fn as_str(&self) -> Option<&'static str> { let buffer = self.as_bytes(); if !crate::ffi::utf8::is_valid_utf8(buffer) { cold_path!(); None } else { unsafe { Some(core::str::from_utf8_unchecked(buffer)) } } } #[cfg(not(CPython))] #[inline] pub fn as_str(&self) -> Option<&'static str> { None } } ================================================ FILE: src/ffi/pynoneref.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2026) #[derive(Clone)] #[repr(transparent)] pub(crate) struct PyNoneRef { ptr: core::ptr::NonNull, } unsafe impl Send for PyNoneRef {} unsafe impl Sync for PyNoneRef {} impl PartialEq for PyNoneRef { fn eq(&self, other: &Self) -> bool { self.ptr == other.ptr } } impl PyNoneRef { #[inline] #[allow(unused)] pub(crate) unsafe fn from_ptr_unchecked(ptr: *mut pyo3_ffi::PyObject) -> Self { unsafe { debug_assert!(!ptr.is_null()); debug_assert!(ob_type!(ptr) == crate::typeref::NONE_TYPE); Self { ptr: core::ptr::NonNull::new_unchecked(ptr), } } } #[inline] #[allow(unused)] pub fn as_ptr(&self) -> *mut pyo3_ffi::PyObject { self.ptr.as_ptr() } #[inline] #[allow(unused)] pub fn as_non_null_ptr(&self) -> core::ptr::NonNull { self.ptr } #[inline] pub fn none() -> Self { Self { ptr: unsafe { core::ptr::NonNull::new_unchecked(use_immortal!(crate::typeref::NONE)) }, } } } ================================================ FILE: src/ffi/pystrref/avx512.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2024-2026) use super::pyunicode_new::*; use core::arch::x86_64::{ _mm512_and_si512, _mm512_cmpgt_epu8_mask, _mm512_cmpneq_epi8_mask, _mm512_loadu_epi8, _mm512_mask_cmpneq_epi8_mask, _mm512_maskz_loadu_epi8, _mm512_max_epu8, _mm512_set1_epi8, }; #[inline(never)] #[target_feature(enable = "avx512f,avx512bw,avx512vl,bmi2")] pub(crate) unsafe fn create_str_impl_avx512vl(buf: &str) -> *mut crate::ffi::PyObject { unsafe { const STRIDE: usize = 64; let buf_ptr = buf.as_bytes().as_ptr().cast::(); let buf_len = buf.len(); assume!(buf_len > 0); let num_loops = buf_len / STRIDE; let remainder = buf_len % STRIDE; let remainder_mask: u64 = !(u64::MAX << remainder); let mut str_vec = _mm512_maskz_loadu_epi8(remainder_mask, buf_ptr); let sptr = buf_ptr.add(remainder); for i in 0..num_loops { str_vec = _mm512_max_epu8( str_vec, _mm512_loadu_epi8(sptr.add(STRIDE * i).cast::()), ); } #[allow(overflowing_literals)] let vec_128 = _mm512_set1_epi8(0b10000000i8); if _mm512_cmpgt_epu8_mask(str_vec, vec_128) == 0 { pyunicode_ascii(buf.as_bytes().as_ptr(), buf_len) } else { #[allow(overflowing_literals)] let is_four = _mm512_cmpgt_epu8_mask(str_vec, _mm512_set1_epi8(239i8)) != 0; #[allow(overflowing_literals)] let is_not_latin = _mm512_cmpgt_epu8_mask(str_vec, _mm512_set1_epi8(195i8)) != 0; #[allow(overflowing_literals)] let multibyte = _mm512_set1_epi8(0b11000000i8); let mut num_chars = _mm512_mask_cmpneq_epi8_mask( remainder_mask, _mm512_and_si512(_mm512_maskz_loadu_epi8(remainder_mask, buf_ptr), multibyte), vec_128, ) .count_ones() as usize; for i in 0..num_loops { num_chars += _mm512_cmpneq_epi8_mask( _mm512_and_si512( _mm512_loadu_epi8(sptr.add(STRIDE * i).cast::()), multibyte, ), vec_128, ) .count_ones() as usize; } if is_four { pyunicode_fourbyte(buf, num_chars) } else if is_not_latin { pyunicode_twobyte(buf, num_chars) } else { pyunicode_onebyte(buf, num_chars) } } } } ================================================ FILE: src/ffi/pystrref/mod.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2026) #[cfg(feature = "avx512")] #[cfg(CPython)] mod avx512; mod object; #[cfg(CPython)] mod pyunicode_new; #[cfg(CPython)] mod scalar; pub(crate) use object::{PyStrRef, PyStrSubclassRef, set_str_create_fn}; ================================================ FILE: src/ffi/pystrref/object.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2025-2026) #[allow(unused)] use crate::ffi::{Py_HashBuffer, Py_ssize_t, PyASCIIObject, PyCompactUnicodeObject, PyObject}; #[cfg(all(CPython, not(feature = "inline_str")))] use crate::ffi::{PyUnicode_DATA, PyUnicode_KIND}; use crate::typeref::{EMPTY_UNICODE, STR_TYPE}; use core::ptr::NonNull; fn to_str_via_ffi(op: *mut PyObject) -> Option<&'static str> { let mut str_size: Py_ssize_t = 0; let ptr = ffi!(PyUnicode_AsUTF8AndSize(op, &raw mut str_size)).cast::(); if ptr.is_null() { cold_path!(); None } else { #[allow(clippy::cast_sign_loss)] let str_usize = str_size as usize; Some(str_from_slice!(ptr, str_usize)) } } #[cfg(all(CPython, feature = "avx512"))] pub type StrDeserializer = unsafe fn(&str) -> *mut PyObject; #[cfg(all(CPython, feature = "avx512"))] static mut STR_CREATE_FN: StrDeserializer = super::scalar::str_impl_kind_scalar; pub fn set_str_create_fn() { unsafe { #[cfg(all(CPython, feature = "avx512"))] if std::is_x86_feature_detected!("avx512vl") { STR_CREATE_FN = super::avx512::create_str_impl_avx512vl; } } } #[cfg(all(feature = "inline_str", Py_3_14, Py_GIL_DISABLED))] const STATE_KIND_SHIFT: usize = 8; #[cfg(all(feature = "inline_str", not(all(Py_3_14, Py_GIL_DISABLED))))] const STATE_KIND_SHIFT: usize = 2; #[cfg(feature = "inline_str")] const STATE_KIND_MASK: u32 = 7 << STATE_KIND_SHIFT; #[cfg(feature = "inline_str")] const STATE_COMPACT_ASCII: u32 = 1 << STATE_KIND_SHIFT | 1 << (STATE_KIND_SHIFT + 3) | 1 << (STATE_KIND_SHIFT + 4); pub(crate) enum PyStrRefError { NotStrType, } #[derive(Clone)] #[repr(transparent)] pub(crate) struct PyStrRef { ptr: core::ptr::NonNull, } unsafe impl Send for PyStrRef {} unsafe impl Sync for PyStrRef {} impl PartialEq for PyStrRef { fn eq(&self, other: &Self) -> bool { self.ptr == other.ptr } } impl PyStrRef { #[inline] pub fn from_ptr(ptr: *mut pyo3_ffi::PyObject) -> Result { unsafe { debug_assert!(!ptr.is_null()); if ob_type!(ptr) == crate::typeref::STR_TYPE { Ok(Self { ptr: core::ptr::NonNull::new_unchecked(ptr), }) } else { cold_path!(); Err(PyStrRefError::NotStrType) } } } #[inline] pub unsafe fn from_ptr_unchecked(ptr: *mut pyo3_ffi::PyObject) -> Self { unsafe { debug_assert!(!ptr.is_null()); debug_assert!(ob_type!(ptr) == crate::typeref::STR_TYPE); Self { ptr: core::ptr::NonNull::new_unchecked(ptr), } } } #[inline] pub fn empty() -> Self { unsafe { Self { ptr: nonnull!(use_immortal!(EMPTY_UNICODE)), } } } #[inline] pub fn as_ptr(&self) -> *mut pyo3_ffi::PyObject { self.ptr.as_ptr() } pub fn as_non_null_ptr(&self) -> NonNull { nonnull!(self.as_ptr()) } #[cfg(CPython)] #[inline(always)] pub fn from_str_with_hash(buf: &str) -> Self { let mut obj = PyStrRef::from_str(buf); obj.set_hash(); obj } #[cfg(CPython)] #[inline(always)] pub fn from_str(buf: &str) -> Self { if buf.is_empty() { cold_path!(); return Self::empty(); } #[cfg(not(feature = "avx512"))] let str_ptr = unsafe { super::scalar::str_impl_kind_scalar(buf) }; #[cfg(feature = "avx512")] let str_ptr = unsafe { STR_CREATE_FN(buf) }; debug_assert!(!str_ptr.is_null()); Self { ptr: nonnull!(str_ptr), } } #[cfg(not(CPython))] #[inline(always)] pub fn from_str(buf: &str) -> Self { if buf.is_empty() { cold_path!(); return Self::empty(); } let str_ptr = unsafe { crate::ffi::PyUnicode_FromStringAndSize( buf.as_ptr().cast::(), crate::util::usize_to_isize(buf.len()), ) }; debug_assert!(!str_ptr.is_null()); Self { ptr: nonnull!(str_ptr), } } #[cfg(CPython)] #[allow(unused)] pub fn hash(&self) -> crate::ffi::Py_hash_t { unsafe { debug_assert!((*self.as_ptr().cast::()).hash != -1); (*self.as_ptr().cast::()).hash } } #[cfg(feature = "inline_str")] fn set_hash(&mut self) { unsafe { let ptr = self.as_ptr().cast::(); let data_ptr: *mut core::ffi::c_void = if (*ptr).state & STATE_COMPACT_ASCII == STATE_COMPACT_ASCII { ptr.offset(1).cast::() } else { ptr.cast::() .offset(1) .cast::() }; #[allow(clippy::cast_possible_wrap)] let num_bytes = (*ptr).length * (((*ptr).state & STATE_KIND_MASK) >> STATE_KIND_SHIFT) as isize; let hash = Py_HashBuffer(data_ptr, num_bytes); (*ptr).hash = hash; debug_assert!((*ptr).hash != -1); } } #[cfg(not(feature = "inline_str"))] fn set_hash(&mut self) { unsafe { let data_ptr = PyUnicode_DATA(self.as_ptr()); #[allow(clippy::cast_possible_wrap)] let num_bytes = PyUnicode_KIND(self.as_ptr()) as isize * ffi!(Py_SIZE(self.as_ptr())); let hash = Py_HashBuffer(data_ptr, num_bytes); (*self.as_ptr().cast::()).hash = hash; debug_assert!(self.hash() != -1); } } #[inline(always)] #[cfg(feature = "inline_str")] pub fn as_str(&self) -> Option<&'static str> { unsafe { let op = self.as_ptr(); if (*op.cast::()).state & STATE_COMPACT_ASCII == STATE_COMPACT_ASCII { let ptr = op.cast::().offset(1).cast::(); let len = crate::util::isize_to_usize((*op.cast::()).length); Some(str_from_slice!(ptr, len)) } else if (*op.cast::()).state & STATE_COMPACT_ASCII == 0 { cold_path!(); to_str_via_ffi(op) } else if (*op.cast::()).utf8_length != 0 { let ptr = ((*op.cast::()).utf8).cast::(); let len = crate::util::isize_to_usize((*op.cast::()).utf8_length); Some(str_from_slice!(ptr, len)) } else { to_str_via_ffi(op) } } } #[inline(always)] #[cfg(not(feature = "inline_str"))] pub fn as_str(&self) -> Option<&'static str> { to_str_via_ffi(self.as_ptr()) } } #[repr(transparent)] pub(crate) struct PyStrSubclassRef { ptr: NonNull, } impl PyStrSubclassRef { pub unsafe fn from_ptr_unchecked(ptr: *mut PyObject) -> PyStrSubclassRef { let ob_type = ob_type!(ptr); let tp_flags = tp_flags!(ob_type); debug_assert!(!ptr.is_null()); debug_assert!(!is_class_by_type!(ob_type, STR_TYPE)); debug_assert!(is_subclass_by_flag!(tp_flags, Py_TPFLAGS_UNICODE_SUBCLASS)); PyStrSubclassRef { ptr: nonnull!(ptr) } } #[inline] pub fn as_ptr(&self) -> *mut pyo3_ffi::PyObject { self.ptr.as_ptr() } #[inline(always)] pub fn as_str(&self) -> Option<&'static str> { to_str_via_ffi(self.as_ptr()) } } ================================================ FILE: src/ffi/pystrref/pyunicode_new.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2023-2026) use crate::ffi::{PyASCIIObject, PyCompactUnicodeObject, PyObject, PyUnicode_New}; use crate::util::usize_to_isize; macro_rules! validate_str { ($ptr:expr) => { #[cfg(CPython)] unsafe { debug_assert!(pyo3_ffi::_PyUnicode_CheckConsistency($ptr.cast::(), 1) == 1) }; }; } #[inline(never)] pub(crate) fn pyunicode_ascii(buf: *const u8, num_chars: usize) -> *mut PyObject { unsafe { let ptr = PyUnicode_New(usize_to_isize(num_chars), 127); let data_ptr = ptr.cast::().offset(1).cast::(); core::ptr::copy_nonoverlapping(buf, data_ptr, num_chars); core::ptr::write(data_ptr.add(num_chars), 0); validate_str!(ptr); ptr.cast::() } } #[cold] #[inline(never)] pub(crate) fn pyunicode_onebyte(buf: &str, num_chars: usize) -> *mut PyObject { unsafe { let ptr = PyUnicode_New(usize_to_isize(num_chars), 255); let mut data_ptr = ptr.cast::().offset(1).cast::(); for each in buf.chars().fuse() { core::ptr::write(data_ptr, each as u8); data_ptr = data_ptr.offset(1); } core::ptr::write(data_ptr, 0); validate_str!(ptr); ptr.cast::() } } #[inline(never)] pub(crate) fn pyunicode_twobyte(buf: &str, num_chars: usize) -> *mut PyObject { unsafe { let ptr = PyUnicode_New(usize_to_isize(num_chars), 65535); let mut data_ptr = ptr.cast::().offset(1).cast::(); for each in buf.chars().fuse() { core::ptr::write(data_ptr, each as u16); data_ptr = data_ptr.offset(1); } core::ptr::write(data_ptr, 0); validate_str!(ptr); ptr.cast::() } } #[inline(never)] pub(crate) fn pyunicode_fourbyte(buf: &str, num_chars: usize) -> *mut PyObject { unsafe { let ptr = PyUnicode_New(usize_to_isize(num_chars), 1114111); let mut data_ptr = ptr.cast::().offset(1).cast::(); for each in buf.chars().fuse() { core::ptr::write(data_ptr, each as u32); data_ptr = data_ptr.offset(1); } core::ptr::write(data_ptr, 0); validate_str!(ptr); ptr.cast::() } } ================================================ FILE: src/ffi/pystrref/scalar.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2024-2026) use super::pyunicode_new::{ pyunicode_ascii, pyunicode_fourbyte, pyunicode_onebyte, pyunicode_twobyte, }; #[inline(never)] pub(crate) unsafe fn str_impl_kind_scalar(buf: &str) -> *mut crate::ffi::PyObject { let num_chars = bytecount::num_chars(buf.as_bytes()); if buf.len() == num_chars { return pyunicode_ascii(buf.as_ptr(), num_chars); } unsafe { let len = buf.len(); assume!(len > 0); if *(buf.as_bytes().as_ptr()) > 239 { cold_path!(); return pyunicode_fourbyte(buf, num_chars); } let sptr = buf.as_bytes().as_ptr(); let mut is_four = false; let mut not_latin = false; for i in 0..len { is_four |= *sptr.add(i) > 239; not_latin |= *sptr.add(i) > 195; } if is_four { pyunicode_fourbyte(buf, num_chars) } else if not_latin { pyunicode_twobyte(buf, num_chars) } else { pyunicode_onebyte(buf, num_chars) } } } ================================================ FILE: src/ffi/pytupleref.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2026) #[derive(Clone)] #[repr(transparent)] pub(crate) struct PyTupleRef { ptr: core::ptr::NonNull, } unsafe impl Send for PyTupleRef {} unsafe impl Sync for PyTupleRef {} impl PartialEq for PyTupleRef { fn eq(&self, other: &Self) -> bool { self.ptr == other.ptr } } impl PyTupleRef { #[inline] pub fn with_capacity(cap: usize) -> Self { unsafe { let ptr = crate::ffi::PyTuple_New(crate::util::usize_to_isize(cap)); debug_assert!(!ptr.is_null()); Self { ptr: nonnull!(ptr) } } } #[inline] pub unsafe fn from_ptr_unchecked(ptr: *mut pyo3_ffi::PyObject) -> Self { unsafe { debug_assert!(!ptr.is_null()); debug_assert!( is_type!(ob_type!(ptr), crate::typeref::TUPLE_TYPE) || is_subclass_by_flag!(tp_flags!(ob_type!(ptr)), Py_TPFLAGS_TUPLE_SUBCLASS) ); Self { ptr: core::ptr::NonNull::new_unchecked(ptr), } } } #[inline] #[allow(unused)] pub fn as_ptr(&self) -> *mut pyo3_ffi::PyObject { self.ptr.as_ptr() } #[inline] #[allow(unused)] pub fn as_non_null_ptr(&self) -> core::ptr::NonNull { self.ptr } pub fn get(&self, i: usize) -> *mut pyo3_ffi::PyObject { unsafe { crate::ffi::PyTuple_GET_ITEM(self.ptr.as_ptr(), crate::util::usize_to_isize(i)) } } #[inline] pub fn set(&mut self, i: usize, val: *mut pyo3_ffi::PyObject) { unsafe { crate::ffi::PyTuple_SET_ITEM(self.as_ptr(), crate::util::usize_to_isize(i), val); } } #[inline] pub fn len(&self) -> usize { unsafe { crate::util::isize_to_usize(super::Py_SIZE(self.ptr.as_ptr())) } } } ================================================ FILE: src/ffi/pyuuidref.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2026) use core::ffi::c_uchar; #[derive(Clone)] #[repr(transparent)] pub(crate) struct PyUuidRef { ptr: core::ptr::NonNull, } unsafe impl Send for PyUuidRef {} unsafe impl Sync for PyUuidRef {} impl PartialEq for PyUuidRef { fn eq(&self, other: &Self) -> bool { self.ptr == other.ptr } } impl PyUuidRef { #[inline] pub(crate) unsafe fn from_ptr_unchecked(ptr: *mut pyo3_ffi::PyObject) -> Self { unsafe { debug_assert!(!ptr.is_null()); debug_assert!(ob_type!(ptr) == crate::typeref::UUID_TYPE); Self { ptr: core::ptr::NonNull::new_unchecked(ptr), } } } #[inline(never)] pub(crate) fn value(&self) -> u128 { unsafe { { // test_uuid_immutable, test_uuid_int let py_int = crate::ffi::PyObject_GetAttr(self.ptr.as_ptr(), crate::typeref::INT_ATTR_STR); ffi!(Py_DECREF(py_int)); let mut buffer: [c_uchar; 16] = [0; 16]; unsafe { // test_uuid_overflow crate::ffi::PyLong_AsByteArray( py_int.cast::(), buffer.as_mut_ptr(), 16, 1, // little_endian 0, // is_signed ); }; u128::from_le_bytes(buffer) } } } } ================================================ FILE: src/ffi/utf8.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2021-2026) #[cfg(all(target_arch = "x86_64", not(target_feature = "avx2")))] pub(crate) fn is_valid_utf8(buf: &[u8]) -> bool { if std::is_x86_feature_detected!("avx2") { unsafe { simdutf8::basic::imp::x86::avx2::validate_utf8(buf).is_ok() } } else { encoding_rs::Encoding::utf8_valid_up_to(buf) == buf.len() } } #[cfg(all(target_arch = "x86_64", target_feature = "avx2"))] pub(crate) fn is_valid_utf8(buf: &[u8]) -> bool { simdutf8::basic::from_utf8(buf).is_ok() } #[cfg(target_arch = "aarch64")] pub(crate) fn is_valid_utf8(buf: &[u8]) -> bool { unsafe { simdutf8::basic::imp::aarch64::neon::validate_utf8(buf).is_ok() } } #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))] pub(crate) fn is_valid_utf8(buf: &[u8]) -> bool { std::str::from_utf8(buf).is_ok() } ================================================ FILE: src/lib.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2018-2026) #![cfg_attr(feature = "optimize", feature(optimize_attribute))] #![cfg_attr(feature = "generic_simd", feature(portable_simd))] #![cfg_attr(feature = "cold_path", feature(cold_path))] #![allow(non_camel_case_types)] #![allow(static_mut_refs)] #![allow(unused_unsafe)] #![warn(clippy::correctness)] #![warn(clippy::suspicious)] #![warn(clippy::complexity)] #![warn(clippy::perf)] #![warn(clippy::style)] #![allow(clippy::inline_always)] #![allow(clippy::explicit_iter_loop)] #![allow(clippy::redundant_field_names)] #![allow(clippy::upper_case_acronyms)] #![allow(clippy::zero_prefixed_literal)] #![warn(clippy::borrow_as_ptr)] #![warn(clippy::cast_possible_wrap)] #![warn(clippy::cast_ptr_alignment)] #![warn(clippy::cast_sign_loss)] #![warn(clippy::elidable_lifetime_names)] #![warn(clippy::ptr_arg)] #![warn(clippy::ptr_as_ptr)] #![warn(clippy::ptr_cast_constness)] #![warn(clippy::ptr_eq)] #![warn(clippy::redundant_allocation)] #![warn(clippy::redundant_clone)] #![warn(clippy::redundant_locals)] #![warn(clippy::redundant_slicing)] #![warn(clippy::semicolon_inside_block)] #![warn(clippy::size_of_ref)] #![warn(clippy::std_instead_of_core)] #![warn(clippy::trivially_copy_pass_by_ref)] #![warn(clippy::unnecessary_semicolon)] #![warn(clippy::unnecessary_wraps)] #![warn(clippy::zero_ptr)] #[cfg(feature = "unwind")] extern crate unwinding; #[macro_use] mod util; mod alloc; mod deserialize; mod exception; mod ffi; mod opt; mod serialize; mod typeref; use core::ffi::{c_char, c_int, c_void}; use core::ptr::{NonNull, null, null_mut}; use crate::deserialize::deserialize; use crate::exception::{ raise_dumps_exception_dynamic, raise_dumps_exception_fixed, raise_loads_exception, }; use crate::ffi::{ METH_KEYWORDS, METH_O, Py_SIZE, Py_ssize_t, PyCFunction_NewEx, PyIntRef, PyMethodDef, PyMethodDefPointer, PyModuleDef, PyModuleDef_HEAD_INIT, PyModuleDef_Slot, PyNoneRef, PyObject, PyTupleRef, PyUnicode_FromStringAndSize, PyUnicode_InternFromString, PyVectorcall_NARGS, }; use crate::serialize::serialize; use crate::util::{isize_to_usize, usize_to_isize}; #[cfg(Py_3_13)] macro_rules! add { ($mptr:expr, $name:expr, $obj:expr) => { crate::ffi::PyModule_Add($mptr, $name.as_ptr(), $obj); }; } #[cfg(not(Py_3_13))] macro_rules! add { ($mptr:expr, $name:expr, $obj:expr) => { crate::ffi::PyModule_AddObjectRef($mptr, $name.as_ptr(), $obj); }; } macro_rules! opt { ($mptr:expr, $name:expr, $opt:expr) => { #[cfg(all(not(target_os = "windows"), target_pointer_width = "64"))] crate::ffi::PyModule_AddIntConstant($mptr, $name.as_ptr(), i64::from($opt)); #[cfg(all(not(target_os = "windows"), target_pointer_width = "32"))] crate::ffi::PyModule_AddIntConstant($mptr, $name.as_ptr(), $opt as i32); #[cfg(target_os = "windows")] crate::ffi::PyModule_AddIntConstant($mptr, $name.as_ptr(), $opt as i32); }; } #[allow(non_snake_case)] #[unsafe(no_mangle)] #[cold] #[cfg_attr(feature = "optimize", optimize(size))] pub(crate) unsafe extern "C" fn orjson_init_exec(mptr: *mut PyObject) -> c_int { unsafe { typeref::init_typerefs(); { let version = env!("CARGO_PKG_VERSION"); let pyversion = PyUnicode_FromStringAndSize( version.as_ptr().cast::(), usize_to_isize(version.len()), ); add!(mptr, c"__version__", pyversion); } { let dumps_doc = c"dumps(obj, /, default=None, option=None)\n--\n\nSerialize Python objects to JSON."; let wrapped_dumps = Box::new(PyMethodDef { ml_name: c"dumps".as_ptr(), ml_meth: PyMethodDefPointer { PyCFunctionFastWithKeywords: dumps, }, ml_flags: crate::ffi::METH_FASTCALL | METH_KEYWORDS, ml_doc: dumps_doc.as_ptr(), }); let func = PyCFunction_NewEx( Box::into_raw(wrapped_dumps), null_mut(), PyUnicode_InternFromString(c"orjson".as_ptr()), ); add!(mptr, c"dumps", func); } { let loads_doc = c"loads(obj, /)\n--\n\nDeserialize JSON to Python objects."; let wrapped_loads = Box::new(PyMethodDef { ml_name: c"loads".as_ptr(), ml_meth: PyMethodDefPointer { PyCFunction: loads }, ml_flags: METH_O, ml_doc: loads_doc.as_ptr(), }); let func = PyCFunction_NewEx( Box::into_raw(wrapped_loads), null_mut(), PyUnicode_InternFromString(c"orjson".as_ptr()), ); add!(mptr, c"loads", func); } add!(mptr, c"Fragment", typeref::FRAGMENT_TYPE.cast::()); opt!(mptr, c"OPT_APPEND_NEWLINE", opt::APPEND_NEWLINE); opt!(mptr, c"OPT_INDENT_2", opt::INDENT_2); opt!(mptr, c"OPT_NAIVE_UTC", opt::NAIVE_UTC); opt!(mptr, c"OPT_NON_STR_KEYS", opt::NON_STR_KEYS); opt!(mptr, c"OPT_OMIT_MICROSECONDS", opt::OMIT_MICROSECONDS); opt!( mptr, c"OPT_PASSTHROUGH_DATACLASS", opt::PASSTHROUGH_DATACLASS ); opt!(mptr, c"OPT_PASSTHROUGH_DATETIME", opt::PASSTHROUGH_DATETIME); opt!(mptr, c"OPT_PASSTHROUGH_SUBCLASS", opt::PASSTHROUGH_SUBCLASS); opt!(mptr, c"OPT_SERIALIZE_DATACLASS", opt::SERIALIZE_DATACLASS); opt!(mptr, c"OPT_SERIALIZE_NUMPY", opt::SERIALIZE_NUMPY); opt!(mptr, c"OPT_SERIALIZE_UUID", opt::SERIALIZE_UUID); opt!(mptr, c"OPT_SORT_KEYS", opt::SORT_KEYS); opt!(mptr, c"OPT_STRICT_INTEGER", opt::STRICT_INTEGER); opt!(mptr, c"OPT_UTC_Z", opt::UTC_Z); add!(mptr, c"JSONDecodeError", typeref::JsonDecodeError); add!(mptr, c"JSONEncodeError", typeref::JsonEncodeError); 0 } } #[allow(non_snake_case)] #[unsafe(no_mangle)] #[cold] #[cfg_attr(feature = "optimize", optimize(size))] pub(crate) unsafe extern "C" fn PyInit_orjson() -> *mut PyModuleDef { #[cfg(not(Py_3_12))] const PYMODULEDEF_LEN: usize = 2; #[cfg(all(Py_3_12, not(Py_3_13)))] const PYMODULEDEF_LEN: usize = 3; #[cfg(Py_3_13)] const PYMODULEDEF_LEN: usize = 4; unsafe { let mod_slots: Box<[PyModuleDef_Slot; PYMODULEDEF_LEN]> = Box::new([ PyModuleDef_Slot { slot: crate::ffi::Py_mod_exec, #[allow(clippy::fn_to_numeric_cast_any, clippy::as_conversions)] value: orjson_init_exec as *mut c_void, }, #[cfg(Py_3_12)] PyModuleDef_Slot { slot: crate::ffi::Py_mod_multiple_interpreters, value: crate::ffi::Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED, }, #[cfg(Py_3_13)] PyModuleDef_Slot { slot: crate::ffi::Py_mod_gil, value: crate::ffi::Py_MOD_GIL_USED, }, PyModuleDef_Slot { slot: 0, value: null_mut(), }, ]); let init = Box::new(PyModuleDef { m_base: PyModuleDef_HEAD_INIT, m_name: c"orjson".as_ptr(), m_doc: null(), m_size: 0, m_methods: null_mut(), m_slots: Box::into_raw(mod_slots).cast::(), m_traverse: None, m_clear: None, m_free: None, }); let init_ptr = Box::into_raw(init); ffi!(PyModuleDef_Init(init_ptr)); init_ptr } } #[unsafe(no_mangle)] pub(crate) unsafe extern "C" fn loads(_self: *mut PyObject, obj: *mut PyObject) -> *mut PyObject { deserialize(obj).map_or_else(raise_loads_exception, NonNull::as_ptr) } #[cfg(CPython)] macro_rules! matches_kwarg { ($val:expr, $ref:expr) => { unsafe { core::ptr::eq($val, $ref) } }; } #[cfg(not(CPython))] macro_rules! matches_kwarg { ($val:expr, $ref:expr) => { unsafe { crate::ffi::PyObject_Hash($val) == crate::ffi::PyObject_Hash($ref) } }; } #[unsafe(no_mangle)] pub(crate) unsafe extern "C" fn dumps( _self: *mut PyObject, args: *const *mut PyObject, nargs: Py_ssize_t, kwnames: *mut PyObject, ) -> *mut PyObject { unsafe { let mut default: Option> = None; let mut optsptr: Option> = None; let num_args = PyVectorcall_NARGS(isize_to_usize(nargs)); if num_args == 0 { cold_path!(); return raise_dumps_exception_fixed( "dumps() missing 1 required positional argument: 'obj'", ); } if num_args & 2 == 2 { default = Some(NonNull::new_unchecked(*args.offset(1))); } if num_args & 3 == 3 { optsptr = Some(NonNull::new_unchecked(*args.offset(2))); } if !kwnames.is_null() { cold_path!(); let kwob = PyTupleRef::from_ptr_unchecked(kwnames); for i in 0..=Py_SIZE(kwnames).saturating_sub(1) { let arg = kwob.get(i.cast_unsigned()); if matches_kwarg!(arg, typeref::OPTION) { if num_args & 3 == 3 { cold_path!(); return raise_dumps_exception_fixed( "dumps() got multiple values for argument: 'option'", ); } optsptr = Some(NonNull::new_unchecked(*args.offset(num_args + i))); } else if matches_kwarg!(arg, typeref::DEFAULT) { if num_args & 2 == 2 { cold_path!(); return raise_dumps_exception_fixed( "dumps() got multiple values for argument: 'default'", ); } default = Some(NonNull::new_unchecked(*args.offset(num_args + i))); } else { return raise_dumps_exception_fixed( "dumps() got an unexpected keyword argument", ); } } } let mut opts = 0 as opt::Opt; if let Some(tmp) = optsptr { cold_path!(); match PyIntRef::from_ptr(tmp.as_ptr()) { Ok(val) => match val.as_opt() { Ok(opt) => { opts = opt; } Err(_) => { return raise_dumps_exception_fixed("Invalid opts"); } }, Err(_) => { if !core::ptr::eq(tmp.as_ptr(), PyNoneRef::none().as_ptr()) { cold_path!(); return raise_dumps_exception_fixed("Invalid opts"); } } } } serialize(*args, default, opts).map_or_else( |err| raise_dumps_exception_dynamic(err.as_str()), NonNull::as_ptr, ) } } ================================================ FILE: src/opt.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2020-2025) pub(crate) type Opt = u32; pub(crate) const INDENT_2: Opt = 1; pub(crate) const NAIVE_UTC: Opt = 1 << 1; pub(crate) const NON_STR_KEYS: Opt = 1 << 2; pub(crate) const OMIT_MICROSECONDS: Opt = 1 << 3; pub(crate) const SERIALIZE_NUMPY: Opt = 1 << 4; pub(crate) const SORT_KEYS: Opt = 1 << 5; pub(crate) const STRICT_INTEGER: Opt = 1 << 6; pub(crate) const UTC_Z: Opt = 1 << 7; pub(crate) const PASSTHROUGH_SUBCLASS: Opt = 1 << 8; pub(crate) const PASSTHROUGH_DATETIME: Opt = 1 << 9; pub(crate) const APPEND_NEWLINE: Opt = 1 << 10; pub(crate) const PASSTHROUGH_DATACLASS: Opt = 1 << 11; // deprecated pub(crate) const SERIALIZE_DATACLASS: Opt = 0; pub(crate) const SERIALIZE_UUID: Opt = 0; pub(crate) const SORT_OR_NON_STR_KEYS: Opt = SORT_KEYS | NON_STR_KEYS; pub(crate) const NOT_PASSTHROUGH: Opt = !(PASSTHROUGH_DATETIME | PASSTHROUGH_DATACLASS | PASSTHROUGH_SUBCLASS); #[allow(clippy::cast_possible_wrap)] pub(crate) const MAX_OPT: i32 = (APPEND_NEWLINE | INDENT_2 | NAIVE_UTC | NON_STR_KEYS | OMIT_MICROSECONDS | PASSTHROUGH_DATETIME | PASSTHROUGH_DATACLASS | PASSTHROUGH_SUBCLASS | SERIALIZE_DATACLASS | SERIALIZE_NUMPY | SERIALIZE_UUID | SORT_KEYS | STRICT_INTEGER | UTC_Z) as i32; ================================================ FILE: src/serialize/buffer.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2024-2025) use bytes::{BufMut, buf::UninitSlice}; use core::mem::MaybeUninit; const BUFFER_LENGTH: usize = 64 - core::mem::size_of::(); /// For use to serialize fixed-size UUIDs and DateTime. #[repr(align(64))] pub(crate) struct SmallFixedBuffer { idx: usize, bytes: [MaybeUninit; BUFFER_LENGTH], } impl SmallFixedBuffer { #[inline] pub fn new() -> Self { Self { idx: 0, bytes: [MaybeUninit::::uninit(); BUFFER_LENGTH], } } #[inline] pub fn as_ptr(&self) -> *const u8 { (&raw const self.bytes).cast::() } #[inline] pub fn len(&self) -> usize { self.idx } } unsafe impl BufMut for SmallFixedBuffer { #[inline] unsafe fn advance_mut(&mut self, cnt: usize) { self.idx += cnt; } #[inline] fn chunk_mut(&mut self) -> &mut UninitSlice { UninitSlice::uninit(&mut self.bytes) } #[inline] fn remaining_mut(&self) -> usize { BUFFER_LENGTH - self.idx } #[inline] fn put_u8(&mut self, value: u8) { debug_assert!(self.remaining_mut() > 1); unsafe { core::ptr::write((&raw mut self.bytes).cast::().add(self.idx), value); self.advance_mut(1); }; } #[inline] fn put_slice(&mut self, src: &[u8]) { debug_assert!(self.remaining_mut() > src.len()); unsafe { core::ptr::copy_nonoverlapping( src.as_ptr(), (&raw mut self.bytes).cast::().add(self.idx), src.len(), ); self.advance_mut(src.len()); } } } ================================================ FILE: src/serialize/error.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2021-2025) use core::ffi::CStr; use core::ptr::NonNull; pub(crate) enum SerializeError { DatetimeLibraryUnsupported, DefaultRecursionLimit, Integer53Bits, Integer64Bits, InvalidStr, InvalidFragment, KeyMustBeStr, RecursionLimit, TimeHasTzinfo, DictIntegerKey64Bit, DictKeyInvalidType, NumpyMalformed, NumpyNotCContiguous, NumpyNotNativeEndian, NumpyUnsupportedDatatype, UnsupportedType(NonNull), } impl core::fmt::Display for SerializeError { #[cold] #[cfg_attr(feature = "optimize", optimize(size))] fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { match *self { SerializeError::DatetimeLibraryUnsupported => write!( f, "datetime's timezone library is not supported: use datetime.timezone.utc, pendulum, pytz, or dateutil" ), SerializeError::DefaultRecursionLimit => { write!(f, "default serializer exceeds recursion limit") } SerializeError::Integer53Bits => write!(f, "Integer exceeds 53-bit range"), SerializeError::Integer64Bits => write!(f, "Integer exceeds 64-bit range"), SerializeError::InvalidStr => write!(f, "{}", crate::util::INVALID_STR), SerializeError::InvalidFragment => { write!(f, "orjson.Fragment's content is not of type bytes or str") } SerializeError::KeyMustBeStr => write!(f, "Dict key must be str"), SerializeError::RecursionLimit => write!(f, "Recursion limit reached"), SerializeError::TimeHasTzinfo => write!(f, "datetime.time must not have tzinfo set"), SerializeError::DictIntegerKey64Bit => { write!(f, "Dict integer key must be within 64-bit range") } SerializeError::DictKeyInvalidType => { write!(f, "Dict key must a type serializable with OPT_NON_STR_KEYS") } SerializeError::NumpyMalformed => write!(f, "numpy array is malformed"), SerializeError::NumpyNotCContiguous => write!( f, "numpy array is not C contiguous; use ndarray.tolist() in default" ), SerializeError::NumpyNotNativeEndian => { write!(f, "numpy array is not native-endianness") } SerializeError::NumpyUnsupportedDatatype => { write!(f, "unsupported datatype in numpy array") } SerializeError::UnsupportedType(ptr) => { let name = unsafe { CStr::from_ptr((*ob_type!(ptr.as_ptr())).tp_name).to_string_lossy() }; write!(f, "Type is not JSON serializable: {name}") } } } } ================================================ FILE: src/serialize/mod.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2021-2025) mod buffer; mod error; mod obtype; mod per_type; mod serializer; mod state; pub(crate) mod writer; pub(crate) use serializer::serialize; ================================================ FILE: src/serialize/obtype.rs ================================================ // SPDX-License-Identifier: (Apache-2.0 OR MIT) // Copyright ijl (2020-2025), Aviram Hassan (2020) use crate::opt::{ Opt, PASSTHROUGH_DATACLASS, PASSTHROUGH_DATETIME, PASSTHROUGH_SUBCLASS, SERIALIZE_NUMPY, }; use crate::serialize::per_type::{is_numpy_array, is_numpy_scalar}; use crate::typeref::{ BOOL_TYPE, DATACLASS_FIELDS_STR, DATE_TYPE, DATETIME_TYPE, DICT_TYPE, ENUM_TYPE, FLOAT_TYPE, FRAGMENT_TYPE, INT_TYPE, LIST_TYPE, NONE_TYPE, STR_TYPE, TIME_TYPE, TUPLE_TYPE, UUID_TYPE, }; #[repr(u32)] pub(crate) enum ObType { Str, Int, Bool, None, Float, List, Dict, Datetime, Date, Time, Tuple, Uuid, Dataclass, NumpyScalar, NumpyArray, Enum, StrSubclass, Fragment, Unknown, } pub(crate) fn pyobject_to_obtype(obj: *mut crate::ffi::PyObject, opts: Opt) -> ObType { let ob_type = ob_type!(obj); if is_class_by_type!(ob_type, STR_TYPE) { ObType::Str } else if is_class_by_type!(ob_type, INT_TYPE) { ObType::Int } else if is_class_by_type!(ob_type, BOOL_TYPE) { ObType::Bool } else if is_class_by_type!(ob_type, NONE_TYPE) { ObType::None } else if is_class_by_type!(ob_type, FLOAT_TYPE) { ObType::Float } else if is_class_by_type!(ob_type, LIST_TYPE) { ObType::List } else if is_class_by_type!(ob_type, DICT_TYPE) { ObType::Dict } else if is_class_by_type!(ob_type, DATETIME_TYPE) && opt_disabled!(opts, PASSTHROUGH_DATETIME) { ObType::Datetime } else { pyobject_to_obtype_unlikely(ob_type, opts) } } #[cfg_attr(feature = "optimize", optimize(size))] #[inline(never)] pub(crate) fn pyobject_to_obtype_unlikely( ob_type: *mut crate::ffi::PyTypeObject, opts: Opt, ) -> ObType { if is_class_by_type!(ob_type, UUID_TYPE) { return ObType::Uuid; } else if is_class_by_type!(ob_type, TUPLE_TYPE) { return ObType::Tuple; } else if is_class_by_type!(ob_type, FRAGMENT_TYPE) { return ObType::Fragment; } if opt_disabled!(opts, PASSTHROUGH_DATETIME) { if is_class_by_type!(ob_type, DATE_TYPE) { return ObType::Date; } else if is_class_by_type!(ob_type, TIME_TYPE) { return ObType::Time; } } let tp_flags = tp_flags!(ob_type); if opt_disabled!(opts, PASSTHROUGH_SUBCLASS) { if is_subclass_by_flag!(tp_flags, Py_TPFLAGS_UNICODE_SUBCLASS) { return ObType::StrSubclass; } else if is_subclass_by_flag!(tp_flags, Py_TPFLAGS_LONG_SUBCLASS) { return ObType::Int; } else if is_subclass_by_flag!(tp_flags, Py_TPFLAGS_LIST_SUBCLASS) { return ObType::List; } else if is_subclass_by_flag!(tp_flags, Py_TPFLAGS_DICT_SUBCLASS) { return ObType::Dict; } } if is_subclass_by_type!(ob_type, ENUM_TYPE) { return ObType::Enum; } if opt_disabled!(opts, PASSTHROUGH_DATACLASS) && pydict_contains!(ob_type, DATACLASS_FIELDS_STR) { return ObType::Dataclass; } if opt_enabled!(opts, SERIALIZE_NUMPY) { cold_path!(); if is_numpy_scalar(ob_type) { return ObType::NumpyScalar; } else if is_numpy_array(ob_type) { return ObType::NumpyArray; } } ObType::Unknown } ================================================ FILE: src/serialize/per_type/dataclass.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2018-2026) use crate::ffi::PyStrRef; use crate::serialize::error::SerializeError; use crate::serialize::per_type::dict::ZeroDictSerializer; use crate::serialize::serializer::PyObjectSerializer; use crate::serialize::state::SerializerState; use crate::typeref::{ DATACLASS_FIELDS_STR, DICT_STR, FIELD_TYPE, FIELD_TYPE_STR, SLOTS_STR, STR_TYPE, }; use crate::util::isize_to_usize; use serde::ser::{Serialize, SerializeMap, Serializer}; use core::ptr::NonNull; #[repr(transparent)] pub(crate) struct DataclassGenericSerializer<'a> { previous: &'a PyObjectSerializer, } impl<'a> DataclassGenericSerializer<'a> { pub fn new(previous: &'a PyObjectSerializer) -> Self { Self { previous: previous } } } impl Serialize for DataclassGenericSerializer<'_> { #[inline(never)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { if self.previous.state.recursion_limit() { err!(SerializeError::RecursionLimit) } let dict = ffi!(PyObject_GetAttr(self.previous.ptr, DICT_STR)); let ob_type = ob_type!(self.previous.ptr); if dict.is_null() { cold_path!(); ffi!(PyErr_Clear()); DataclassFallbackSerializer::new( self.previous.ptr, self.previous.state, self.previous.default, ) .serialize(serializer) } else if pydict_contains!(ob_type, SLOTS_STR) { let ret = DataclassFallbackSerializer::new( self.previous.ptr, self.previous.state, self.previous.default, ) .serialize(serializer); ffi!(Py_DECREF(dict)); ret } else { let ret = DataclassFastSerializer::new(dict, self.previous.state, self.previous.default) .serialize(serializer); ffi!(Py_DECREF(dict)); ret } } } pub(crate) struct DataclassFastSerializer { ptr: *mut crate::ffi::PyObject, state: SerializerState, default: Option>, } impl DataclassFastSerializer { pub fn new( ptr: *mut crate::ffi::PyObject, state: SerializerState, default: Option>, ) -> Self { DataclassFastSerializer { ptr: ptr, state: state.copy_for_recursive_call(), default: default, } } } impl Serialize for DataclassFastSerializer { #[inline(never)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let len = isize_to_usize(ffi!(Py_SIZE(self.ptr))); if len == 0 { cold_path!(); return ZeroDictSerializer::new().serialize(serializer); } let mut map = serializer.serialize_map(None).unwrap(); let mut pos = 0; let mut next_key: *mut crate::ffi::PyObject = core::ptr::null_mut(); let mut next_value: *mut crate::ffi::PyObject = core::ptr::null_mut(); pydict_next!( self.ptr, &raw mut pos, &raw mut next_key, &raw mut next_value ); for _ in 0..len { let key = next_key; let value = next_value; pydict_next!( self.ptr, &raw mut pos, &raw mut next_key, &raw mut next_value ); let key_as_str = { let key_ob_type = ob_type!(key); if !is_class_by_type!(key_ob_type, STR_TYPE) { cold_path!(); err!(SerializeError::KeyMustBeStr) } match unsafe { PyStrRef::from_ptr_unchecked(key).as_str() } { Some(uni) => uni, None => err!(SerializeError::InvalidStr), } }; if key_as_str.as_bytes()[0] == b'_' { cold_path!(); continue; } let pyvalue = PyObjectSerializer::new(value, self.state, self.default); map.serialize_key(key_as_str).unwrap(); map.serialize_value(&pyvalue)?; } map.end() } } pub(crate) struct DataclassFallbackSerializer { ptr: *mut crate::ffi::PyObject, state: SerializerState, default: Option>, } impl DataclassFallbackSerializer { pub fn new( ptr: *mut crate::ffi::PyObject, state: SerializerState, default: Option>, ) -> Self { DataclassFallbackSerializer { ptr: ptr, state: state.copy_for_recursive_call(), default: default, } } } impl Serialize for DataclassFallbackSerializer { #[cold] #[inline(never)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let fields = ffi!(PyObject_GetAttr(self.ptr, DATACLASS_FIELDS_STR)); debug_assert!(ffi!(Py_REFCNT(fields)) >= 2); ffi!(Py_DECREF(fields)); let len = isize_to_usize(ffi!(Py_SIZE(fields))); if len == 0 { cold_path!(); return ZeroDictSerializer::new().serialize(serializer); } let mut map = serializer.serialize_map(None).unwrap(); let mut pos = 0; let mut next_key: *mut crate::ffi::PyObject = core::ptr::null_mut(); let mut next_value: *mut crate::ffi::PyObject = core::ptr::null_mut(); pydict_next!(fields, &raw mut pos, &raw mut next_key, &raw mut next_value); for _ in 0..len { let attr = next_key; let field = next_value; pydict_next!(fields, &raw mut pos, &raw mut next_key, &raw mut next_value); let field_type = ffi!(PyObject_GetAttr(field, FIELD_TYPE_STR)); debug_assert!(ffi!(Py_REFCNT(field_type)) >= 2); ffi!(Py_DECREF(field_type)); if unsafe { !core::ptr::eq(field_type.cast::(), FIELD_TYPE) } { continue; } let key_as_str = match unsafe { PyStrRef::from_ptr_unchecked(attr).as_str() } { Some(uni) => uni, None => err!(SerializeError::InvalidStr), }; if key_as_str.as_bytes()[0] == b'_' { cold_path!(); continue; } let value = ffi!(PyObject_GetAttr(self.ptr, attr)); debug_assert!(ffi!(Py_REFCNT(value)) >= 2); ffi!(Py_DECREF(value)); let pyvalue = PyObjectSerializer::new(value, self.state, self.default); map.serialize_key(key_as_str).unwrap(); map.serialize_value(&pyvalue)?; } map.end() } } ================================================ FILE: src/serialize/per_type/datetime.rs ================================================ // SPDX-License-Identifier: (Apache-2.0 OR MIT) // Copyright ijl (2018-2026), Ben Sully (2021) use crate::opt::{OMIT_MICROSECONDS, Opt}; use crate::serialize::buffer::SmallFixedBuffer; use crate::serialize::error::SerializeError; use crate::serialize::per_type::datetimelike::{DateTimeError, DateTimeLike, Offset}; use crate::typeref::{ CONVERT_METHOD_STR, DST_STR, NORMALIZE_METHOD_STR, UTCOFFSET_METHOD_STR, ZONEINFO_TYPE, }; use serde::ser::{Serialize, Serializer}; macro_rules! write_double_digit { ($buf:ident, $value:ident) => { if $value < 10 { $buf.put_u8(b'0'); } $buf.put_slice(itoa::Buffer::new().format($value).as_bytes()); }; } macro_rules! write_microsecond { ($buf:ident, $microsecond:ident) => { if $microsecond != 0 { let mut buf = itoa::Buffer::new(); let formatted = buf.format($microsecond); $buf.put_slice(&[b'.', b'0', b'0', b'0', b'0', b'0', b'0'][..(7 - formatted.len())]); $buf.put_slice(formatted.as_bytes()); } }; } #[repr(transparent)] pub(crate) struct Date { ptr: *mut crate::ffi::PyObject, } impl Date { pub fn new(ptr: *mut crate::ffi::PyObject) -> Self { Date { ptr: ptr } } #[inline(never)] pub fn write_buf(&self, buf: &mut B) where B: bytes::BufMut, { { let year = ffi!(PyDateTime_GET_YEAR(self.ptr)); let mut yearbuf = itoa::Buffer::new(); let formatted = yearbuf.format(year); if year < 1000 { cold_path!(); // date-fullyear = 4DIGIT buf.put_slice(&[b'0', b'0', b'0', b'0'][..(4 - formatted.len())]); } buf.put_slice(formatted.as_bytes()); } buf.put_u8(b'-'); { let val_py = ffi!(PyDateTime_GET_MONTH(self.ptr)); debug_assert!(val_py >= 0); let val = val_py.cast_unsigned(); write_double_digit!(buf, val); } buf.put_u8(b'-'); { let val_py = ffi!(PyDateTime_GET_DAY(self.ptr)); debug_assert!(val_py >= 0); let val = val_py.cast_unsigned(); write_double_digit!(buf, val); } } } impl Serialize for Date { fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut buf = SmallFixedBuffer::new(); self.write_buf(&mut buf); serializer.serialize_unit_struct(str_from_slice!(buf.as_ptr(), buf.len())) } } pub(crate) enum TimeError { HasTimezone, } pub(crate) struct Time { ptr: *mut crate::ffi::PyObject, opts: Opt, } impl Time { pub fn new(ptr: *mut crate::ffi::PyObject, opts: Opt) -> Self { Time { ptr: ptr, opts: opts, } } #[inline(never)] pub fn write_buf(&self, buf: &mut B) -> Result<(), TimeError> where B: bytes::BufMut, { if unsafe { (*self.ptr.cast::()).hastzinfo == 1 } { return Err(TimeError::HasTimezone); } let hour = ffi!(PyDateTime_TIME_GET_HOUR(self.ptr)).cast_unsigned(); write_double_digit!(buf, hour); buf.put_u8(b':'); let minute = ffi!(PyDateTime_TIME_GET_MINUTE(self.ptr)).cast_unsigned(); write_double_digit!(buf, minute); buf.put_u8(b':'); let second = ffi!(PyDateTime_TIME_GET_SECOND(self.ptr)).cast_unsigned(); write_double_digit!(buf, second); if opt_disabled!(self.opts, OMIT_MICROSECONDS) { let microsecond = ffi!(PyDateTime_TIME_GET_MICROSECOND(self.ptr)).cast_unsigned(); write_microsecond!(buf, microsecond); } Ok(()) } } impl Serialize for Time { fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut buf = SmallFixedBuffer::new(); if self.write_buf(&mut buf).is_err() { err!(SerializeError::DatetimeLibraryUnsupported) } serializer.serialize_unit_struct(str_from_slice!(buf.as_ptr(), buf.len())) } } pub(crate) struct DateTime { ptr: *mut crate::ffi::PyObject, opts: Opt, } impl DateTime { pub fn new(ptr: *mut crate::ffi::PyObject, opts: Opt) -> Self { DateTime { ptr: ptr, opts: opts, } } } macro_rules! pydatetime_get { ($fn: ident, $pyfn: ident, $ty: ident) => { fn $fn(&self) -> $ty { let ret = ffi!($pyfn(self.ptr)); debug_assert!(ret >= 0); #[allow(clippy::cast_sign_loss)] let ret2 = ret as $ty; // stmt_expr_attributes ret2 } }; } impl DateTimeLike for DateTime { pydatetime_get!(year, PyDateTime_GET_YEAR, i32); pydatetime_get!(month, PyDateTime_GET_MONTH, u8); pydatetime_get!(day, PyDateTime_GET_DAY, u8); pydatetime_get!(hour, PyDateTime_DATE_GET_HOUR, u8); pydatetime_get!(minute, PyDateTime_DATE_GET_MINUTE, u8); pydatetime_get!(second, PyDateTime_DATE_GET_SECOND, u8); pydatetime_get!(microsecond, PyDateTime_DATE_GET_MICROSECOND, u32); fn nanosecond(&self) -> u32 { self.microsecond() * 1_000 } fn has_tz(&self) -> bool { unsafe { (*(self.ptr.cast::())).hastzinfo == 1 } } #[inline(never)] fn slow_offset(&self) -> Result { let tzinfo = ffi!(PyDateTime_DATE_GET_TZINFO(self.ptr)); if ffi!(PyObject_HasAttr(tzinfo, CONVERT_METHOD_STR)) == 1 { // pendulum let py_offset = call_method!(self.ptr, UTCOFFSET_METHOD_STR); let offset = Offset { second: ffi!(PyDateTime_DELTA_GET_SECONDS(py_offset)), day: ffi!(PyDateTime_DELTA_GET_DAYS(py_offset)), }; ffi!(Py_DECREF(py_offset)); Ok(offset) } else if ffi!(PyObject_HasAttr(tzinfo, NORMALIZE_METHOD_STR)) == 1 { // pytz let method_ptr = call_method!(tzinfo, NORMALIZE_METHOD_STR, self.ptr); let py_offset = call_method!(method_ptr, UTCOFFSET_METHOD_STR); ffi!(Py_DECREF(method_ptr)); let offset = Offset { second: ffi!(PyDateTime_DELTA_GET_SECONDS(py_offset)), day: ffi!(PyDateTime_DELTA_GET_DAYS(py_offset)), }; ffi!(Py_DECREF(py_offset)); Ok(offset) } else if ffi!(PyObject_HasAttr(tzinfo, DST_STR)) == 1 { // dateutil/arrow, datetime.timezone.utc let py_offset = call_method!(tzinfo, UTCOFFSET_METHOD_STR, self.ptr); let offset = Offset { second: ffi!(PyDateTime_DELTA_GET_SECONDS(py_offset)), day: ffi!(PyDateTime_DELTA_GET_DAYS(py_offset)), }; ffi!(Py_DECREF(py_offset)); Ok(offset) } else { Err(DateTimeError::LibraryUnsupported) } } #[inline] fn offset(&self) -> Result { if !self.has_tz() { Ok(Offset::default()) } else { let tzinfo = ffi!(PyDateTime_DATE_GET_TZINFO(self.ptr)); if unsafe { core::ptr::eq(ob_type!(tzinfo), ZONEINFO_TYPE) } { // zoneinfo let py_offset = call_method!(tzinfo, UTCOFFSET_METHOD_STR, self.ptr); let offset = Offset { second: ffi!(PyDateTime_DELTA_GET_SECONDS(py_offset)), day: ffi!(PyDateTime_DELTA_GET_DAYS(py_offset)), }; ffi!(Py_DECREF(py_offset)); Ok(offset) } else { self.slow_offset() } } } } impl Serialize for DateTime { fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut buf = SmallFixedBuffer::new(); if self.write_buf(&mut buf, self.opts).is_err() { err!(SerializeError::DatetimeLibraryUnsupported) } serializer.serialize_unit_struct(str_from_slice!(buf.as_ptr(), buf.len())) } } ================================================ FILE: src/serialize/per_type/datetimelike.rs ================================================ // SPDX-License-Identifier: (Apache-2.0 OR MIT) // Copyright ijl (2020-2025), Ben Sully (2021) use crate::opt::{NAIVE_UTC, OMIT_MICROSECONDS, Opt, UTC_Z}; pub(crate) enum DateTimeError { LibraryUnsupported, } macro_rules! write_double_digit { ($buf:ident, $value:expr) => { if $value < 10 { $buf.put_u8(b'0'); } $buf.put_slice(itoa::Buffer::new().format($value).as_bytes()); }; } macro_rules! write_triple_digit { ($buf:ident, $value:expr) => { if $value < 100 { $buf.put_u8(b'0'); } if $value < 10 { $buf.put_u8(b'0'); } $buf.put_slice(itoa::Buffer::new().format($value).as_bytes()); }; } #[derive(Default)] pub(crate) struct Offset { pub day: i32, pub second: i32, } /// Trait providing a method to write a datetime-like object to a buffer in an RFC3339-compatible format. /// /// The provided `write_buf` method does not allocate, and is faster /// than writing to a heap-allocated string. pub(crate) trait DateTimeLike { /// Returns the year component of the datetime. fn year(&self) -> i32; /// Returns the month component of the datetime. fn month(&self) -> u8; /// Returns the day component of the datetime. fn day(&self) -> u8; /// Returns the hour component of the datetime. fn hour(&self) -> u8; /// Returns the minute component of the datetime. fn minute(&self) -> u8; /// Returns the second component of the datetime. fn second(&self) -> u8; /// Returns the number of microseconds since the whole non-leap second. fn microsecond(&self) -> u32; /// Returns the number of nanoseconds since the whole non-leap second. fn nanosecond(&self) -> u32; /// Is the object time-zone aware? fn has_tz(&self) -> bool; //// Non-zoneinfo implementation of offset() fn slow_offset(&self) -> Result; /// The offset of the timezone. fn offset(&self) -> Result; /// Write `self` to a buffer in RFC3339 format, using `opts` to /// customise if desired. #[inline(never)] fn write_buf(&self, buf: &mut B, opts: Opt) -> Result<(), DateTimeError> where B: bytes::BufMut, { { let year = self.year(); let mut yearbuf = itoa::Buffer::new(); let formatted = yearbuf.format(year); if year < 1000 { cold_path!(); // date-fullyear = 4DIGIT buf.put_slice(&[b'0', b'0', b'0', b'0'][..(4 - formatted.len())]); } buf.put_slice(formatted.as_bytes()); } buf.put_u8(b'-'); write_double_digit!(buf, self.month()); buf.put_u8(b'-'); write_double_digit!(buf, self.day()); buf.put_u8(b'T'); write_double_digit!(buf, self.hour()); buf.put_u8(b':'); write_double_digit!(buf, self.minute()); buf.put_u8(b':'); write_double_digit!(buf, self.second()); if opt_disabled!(opts, OMIT_MICROSECONDS) { let microsecond = self.microsecond(); if microsecond != 0 { buf.put_u8(b'.'); write_triple_digit!(buf, microsecond / 1_000); write_triple_digit!(buf, microsecond % 1_000); // Don't support writing nanoseconds for now. // If requested, something like the following should work, // and `SmallFixedBuffer` needs at least length 35. // let nanosecond = self.nanosecond(); // if nanosecond % 1_000 != 0 { // write_triple_digit!(buf, nanosecond % 1_000); // } } } if self.has_tz() || opt_enabled!(opts, NAIVE_UTC) { let offset = self.offset()?; let mut offset_second = offset.second; if offset_second == 0 { if opt_enabled!(opts, UTC_Z) { buf.put_u8(b'Z'); } else { buf.put_slice(b"+00:00"); } } else { // This branch is only really hit by the Python datetime implementation, // since numpy datetimes are all converted to UTC. if offset.day == -1 { // datetime.timedelta(days=-1, seconds=68400) -> -05:00 buf.put_u8(b'-'); offset_second = 86400 - offset_second; } else { // datetime.timedelta(seconds=37800) -> +10:30 buf.put_u8(b'+'); } let offset_minute = offset_second / 60; let offset_hour = offset_minute / 60; write_double_digit!(buf, offset_hour); buf.put_u8(b':'); let mut offset_minute_print = offset_minute % 60; // https://tools.ietf.org/html/rfc3339#section-5.8 // "exactly 19 minutes and 32.13 seconds ahead of UTC" // "closest representable UTC offset" // "+20:00" let offset_excess_second = offset_second - (offset_minute_print * 60 + offset_hour * 3600); if offset_excess_second >= 30 { offset_minute_print += 1; } write_double_digit!(buf, offset_minute_print); } } Ok(()) } } ================================================ FILE: src/serialize/per_type/default.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2018-2026) use crate::serialize::error::SerializeError; use crate::serialize::serializer::PyObjectSerializer; use serde::ser::{Serialize, Serializer}; #[repr(transparent)] pub(crate) struct DefaultSerializer<'a> { previous: &'a PyObjectSerializer, } impl<'a> DefaultSerializer<'a> { pub fn new(previous: &'a PyObjectSerializer) -> Self { Self { previous: previous } } } impl Serialize for DefaultSerializer<'_> { #[cold] #[inline(never)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { match self.previous.default { Some(callable) => { if self.previous.state.default_calls_limit() { cold_path!(); err!(SerializeError::DefaultRecursionLimit) } let nargs = ffi!(PyVectorcall_NARGS(1)).cast_unsigned() as usize; let default_obj = unsafe { crate::ffi::PyObject_Vectorcall( callable.as_ptr(), &raw const self.previous.ptr, nargs, core::ptr::null_mut(), ) }; if default_obj.is_null() { err!(SerializeError::UnsupportedType(nonnull!(self.previous.ptr))) } else { let res = PyObjectSerializer::new( default_obj, self.previous.state.copy_for_default_call(), self.previous.default, ) .serialize(serializer); ffi!(Py_DECREF(default_obj)); res } } None => err!(SerializeError::UnsupportedType(nonnull!(self.previous.ptr))), } } } ================================================ FILE: src/serialize/per_type/dict.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2018-2026) use crate::ffi::{ PyBoolRef, PyDictRef, PyFloatRef, PyFragmentRef, PyIntRef, PyListRef, PyStrRef, PyStrSubclassRef, PyUuidRef, }; use crate::opt::{NON_STR_KEYS, NOT_PASSTHROUGH, SORT_KEYS, SORT_OR_NON_STR_KEYS}; use crate::serialize::buffer::SmallFixedBuffer; use crate::serialize::error::SerializeError; use crate::serialize::obtype::{ObType, pyobject_to_obtype}; use crate::serialize::per_type::datetimelike::DateTimeLike; use crate::serialize::per_type::{ BoolSerializer, DataclassGenericSerializer, Date, DateTime, DefaultSerializer, EnumSerializer, FloatSerializer, FragmentSerializer, IntSerializer, ListTupleSerializer, NoneSerializer, NumpyScalar, NumpySerializer, StrSerializer, StrSubclassSerializer, Time, UUID, ZeroListSerializer, }; use crate::serialize::serializer::PyObjectSerializer; use crate::serialize::state::SerializerState; use crate::typeref::{STR_TYPE, TRUE, VALUE_STR}; use core::ptr::NonNull; use serde::ser::{Serialize, SerializeMap, Serializer}; use smallvec::SmallVec; pub(crate) struct ZeroDictSerializer; impl ZeroDictSerializer { pub const fn new() -> Self { Self {} } } impl Serialize for ZeroDictSerializer { #[inline(always)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_bytes(b"{}") } } pub(crate) struct DictGenericSerializer { dict: PyDictRef, state: SerializerState, #[allow(dead_code)] default: Option>, } impl DictGenericSerializer { pub fn new( dict: PyDictRef, state: SerializerState, default: Option>, ) -> Self { DictGenericSerializer { dict: dict, state: state.copy_for_recursive_call(), default: default, } } } impl Serialize for DictGenericSerializer { #[inline(always)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { if self.state.recursion_limit() { cold_path!(); err!(SerializeError::RecursionLimit) } if self.dict.len() == 0 { cold_path!(); ZeroDictSerializer::new().serialize(serializer) } else if opt_disabled!(self.state.opts(), SORT_OR_NON_STR_KEYS) { unsafe { (*(core::ptr::from_ref::(self)).cast::()) .serialize(serializer) } } else if opt_enabled!(self.state.opts(), NON_STR_KEYS) { unsafe { (*(core::ptr::from_ref::(self)).cast::()) .serialize(serializer) } } else { unsafe { (*(core::ptr::from_ref::(self)).cast::()) .serialize(serializer) } } } } macro_rules! impl_serialize_entry { ($map:expr, $self:expr, $key:expr, $value:expr) => { match pyobject_to_obtype($value, $self.state.opts()) { ObType::Str => { $map.serialize_key($key).unwrap(); $map.serialize_value(&StrSerializer::new(unsafe { PyStrRef::from_ptr_unchecked($value) }))?; } ObType::StrSubclass => { $map.serialize_key($key).unwrap(); $map.serialize_value(&StrSubclassSerializer::new(unsafe { PyStrSubclassRef::from_ptr_unchecked($value) }))?; } ObType::Int => { $map.serialize_key($key).unwrap(); $map.serialize_value(&IntSerializer::new( unsafe { PyIntRef::from_ptr_unchecked($value) }, $self.state.opts(), ))?; } ObType::None => { $map.serialize_key($key).unwrap(); $map.serialize_value(&NoneSerializer::new()).unwrap(); } ObType::Float => { $map.serialize_key($key).unwrap(); $map.serialize_value(&FloatSerializer::new(unsafe { PyFloatRef::from_ptr_unchecked($value) }))?; } ObType::Bool => { $map.serialize_key($key).unwrap(); $map.serialize_value(&BoolSerializer::new(unsafe { PyBoolRef::from_ptr_unchecked($value) })) .unwrap(); } ObType::Datetime => { $map.serialize_key($key).unwrap(); $map.serialize_value(&DateTime::new($value, $self.state.opts()))?; } ObType::Date => { $map.serialize_key($key).unwrap(); $map.serialize_value(&Date::new($value))?; } ObType::Time => { $map.serialize_key($key).unwrap(); $map.serialize_value(&Time::new($value, $self.state.opts()))?; } ObType::Uuid => { $map.serialize_key($key).unwrap(); $map.serialize_value(&UUID::new(unsafe { PyUuidRef::from_ptr_unchecked($value) })) .unwrap(); } ObType::Dict => { let pyvalue = DictGenericSerializer::new( unsafe { PyDictRef::from_ptr_unchecked($value) }, $self.state, $self.default, ); $map.serialize_key($key).unwrap(); $map.serialize_value(&pyvalue)?; } ObType::List => { if ffi!(Py_SIZE($value)) == 0 { $map.serialize_key($key).unwrap(); $map.serialize_value(&ZeroListSerializer::new()).unwrap(); } else { let pyvalue = ListTupleSerializer::from_list( unsafe { PyListRef::from_ptr_unchecked($value) }, $self.state, $self.default, ); $map.serialize_key($key).unwrap(); $map.serialize_value(&pyvalue)?; } } ObType::Tuple => { if ffi!(Py_SIZE($value)) == 0 { $map.serialize_key($key).unwrap(); $map.serialize_value(&ZeroListSerializer::new()).unwrap(); } else { let pyvalue = ListTupleSerializer::from_tuple($value, $self.state, $self.default); $map.serialize_key($key).unwrap(); $map.serialize_value(&pyvalue)?; } } ObType::Dataclass => { $map.serialize_key($key).unwrap(); $map.serialize_value(&DataclassGenericSerializer::new(&PyObjectSerializer::new( $value, $self.state, $self.default, )))?; } ObType::Enum => { $map.serialize_key($key).unwrap(); $map.serialize_value(&EnumSerializer::new(&PyObjectSerializer::new( $value, $self.state, $self.default, )))?; } ObType::NumpyArray => { $map.serialize_key($key).unwrap(); $map.serialize_value(&NumpySerializer::new(&PyObjectSerializer::new( $value, $self.state, $self.default, )))?; } ObType::NumpyScalar => { $map.serialize_key($key).unwrap(); $map.serialize_value(&NumpyScalar::new($value, $self.state.opts()))?; } ObType::Fragment => { $map.serialize_key($key).unwrap(); $map.serialize_value(&FragmentSerializer::new(unsafe { PyFragmentRef::from_ptr_unchecked($value) }))?; } ObType::Unknown => { $map.serialize_key($key).unwrap(); $map.serialize_value(&DefaultSerializer::new(&PyObjectSerializer::new( $value, $self.state, $self.default, )))?; } } }; } pub(crate) struct Dict { dict: PyDictRef, state: SerializerState, default: Option>, } impl Serialize for Dict { #[inline(never)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut pos = 0; let mut next_key: *mut crate::ffi::PyObject = core::ptr::null_mut(); let mut next_value: *mut crate::ffi::PyObject = core::ptr::null_mut(); pydict_next!( self.dict.as_ptr(), &raw mut pos, &raw mut next_key, &raw mut next_value ); let mut map = serializer.serialize_map(None).unwrap(); let len = self.dict.len(); assume!(len > 0); for _ in 0..len { let key = next_key; let value = next_value; pydict_next!( self.dict.as_ptr(), &raw mut pos, &raw mut next_key, &raw mut next_value ); // key let uni = PyStrRef::from_ptr(key) .map_err(|_| serde::ser::Error::custom(SerializeError::KeyMustBeStr))? .as_str(); if uni.is_none() { cold_path!(); err!(SerializeError::InvalidStr); } // value impl_serialize_entry!(map, self, uni.unwrap(), value); } map.end() } } pub(crate) struct DictSortedKey { dict: PyDictRef, state: SerializerState, default: Option>, } impl Serialize for DictSortedKey { #[inline(never)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut pos = 0; let mut next_key: *mut crate::ffi::PyObject = core::ptr::null_mut(); let mut next_value: *mut crate::ffi::PyObject = core::ptr::null_mut(); pydict_next!( self.dict.as_ptr(), &raw mut pos, &raw mut next_key, &raw mut next_value ); let len = self.dict.len(); assume!(len > 0); let mut items: SmallVec<[(&str, *mut crate::ffi::PyObject); 8]> = SmallVec::with_capacity(len); for _ in 0..len { let key = next_key; let value = next_value; pydict_next!( self.dict.as_ptr(), &raw mut pos, &raw mut next_key, &raw mut next_value ); if unsafe { !core::ptr::eq(ob_type!(key), STR_TYPE) } { err!(SerializeError::KeyMustBeStr) } let pystr = unsafe { PyStrRef::from_ptr_unchecked(key) }; let uni = pystr.as_str(); if uni.is_none() { err!(SerializeError::InvalidStr) } let key_as_str = uni.unwrap(); items.push((key_as_str, value)); } sort_dict_items(&mut items); let mut map = serializer.serialize_map(None).unwrap(); for (key, val) in items.iter() { let pyvalue = PyObjectSerializer::new(*val, self.state, self.default); map.serialize_key(key).unwrap(); map.serialize_value(&pyvalue)?; } map.end() } } #[cold] #[inline(never)] fn non_str_str(key: PyStrRef) -> Result { // because of ObType::Enum match key.as_str() { Some(uni) => Ok(String::from(uni)), None => { cold_path!(); Err(SerializeError::InvalidStr) } } } #[cold] #[inline(never)] fn non_str_str_subclass(key: PyStrSubclassRef) -> Result { match key.as_str() { Some(uni) => Ok(String::from(uni)), None => { cold_path!(); Err(SerializeError::InvalidStr) } } } #[allow(clippy::unnecessary_wraps)] #[inline(never)] fn non_str_date(key: *mut crate::ffi::PyObject) -> Result { let mut buf = SmallFixedBuffer::new(); Date::new(key).write_buf(&mut buf); let key_as_str = str_from_slice!(buf.as_ptr(), buf.len()); Ok(String::from(key_as_str)) } #[inline(never)] fn non_str_datetime( key: *mut crate::ffi::PyObject, opts: crate::opt::Opt, ) -> Result { let mut buf = SmallFixedBuffer::new(); let dt = DateTime::new(key, opts); if dt.write_buf(&mut buf, opts).is_err() { return Err(SerializeError::DatetimeLibraryUnsupported); } let key_as_str = str_from_slice!(buf.as_ptr(), buf.len()); Ok(String::from(key_as_str)) } #[cold] #[inline(never)] fn non_str_time( key: *mut crate::ffi::PyObject, opts: crate::opt::Opt, ) -> Result { let mut buf = SmallFixedBuffer::new(); let time = Time::new(key, opts); if time.write_buf(&mut buf).is_err() { return Err(SerializeError::TimeHasTzinfo); } let key_as_str = str_from_slice!(buf.as_ptr(), buf.len()); Ok(String::from(key_as_str)) } #[allow(clippy::unnecessary_wraps)] #[inline(never)] fn non_str_uuid(key: PyUuidRef) -> Result { let mut buf = SmallFixedBuffer::new(); UUID::new(key).write_buf(&mut buf); let key_as_str = str_from_slice!(buf.as_ptr(), buf.len()); Ok(String::from(key_as_str)) } #[allow(clippy::unnecessary_wraps)] #[cold] #[inline(never)] fn non_str_float(key: *mut crate::ffi::PyObject) -> Result { let val = ffi!(PyFloat_AS_DOUBLE(key)); if !val.is_finite() { Ok(String::from("null")) } else { Ok(String::from(zmij::Buffer::new().format_finite(val))) } } #[allow(clippy::unnecessary_wraps)] #[inline(never)] fn non_str_int(key: *mut crate::ffi::PyObject) -> Result { let ival = ffi!(PyLong_AsLongLong(key)); if ival == -1 && !ffi!(PyErr_Occurred()).is_null() { cold_path!(); ffi!(PyErr_Clear()); let uval = ffi!(PyLong_AsUnsignedLongLong(key)); if uval == u64::MAX && !ffi!(PyErr_Occurred()).is_null() { return Err(SerializeError::DictIntegerKey64Bit); } Ok(String::from(itoa::Buffer::new().format(uval))) } else { Ok(String::from(itoa::Buffer::new().format(ival))) } } #[inline(never)] fn sort_dict_items(items: &mut SmallVec<[(&str, *mut crate::ffi::PyObject); 8]>) { items.sort_unstable_by(|a, b| a.0.cmp(b.0)); } pub(crate) struct DictNonStrKey { dict: PyDictRef, state: SerializerState, default: Option>, } impl DictNonStrKey { fn pyobject_to_string( key: *mut crate::ffi::PyObject, opts: crate::opt::Opt, ) -> Result { unsafe { match pyobject_to_obtype(key, opts) { ObType::None => Ok(String::from("null")), ObType::Bool => { if unsafe { core::ptr::eq(key, TRUE) } { Ok(String::from("true")) } else { Ok(String::from("false")) } } ObType::Int => non_str_int(key), ObType::Float => non_str_float(key), ObType::Datetime => non_str_datetime(key, opts), ObType::Date => non_str_date(key), ObType::Time => non_str_time(key, opts), ObType::Uuid => non_str_uuid(PyUuidRef::from_ptr_unchecked(key)), ObType::Enum => { let value = ffi!(PyObject_GetAttr(key, VALUE_STR)); debug_assert!(ffi!(Py_REFCNT(value)) >= 2); let ret = Self::pyobject_to_string(value, opts); ffi!(Py_DECREF(value)); ret } ObType::Str => non_str_str(PyStrRef::from_ptr_unchecked(key)), ObType::StrSubclass => { non_str_str_subclass(PyStrSubclassRef::from_ptr_unchecked(key)) } ObType::Tuple | ObType::NumpyScalar | ObType::NumpyArray | ObType::Dict | ObType::List | ObType::Dataclass | ObType::Fragment | ObType::Unknown => Err(SerializeError::DictKeyInvalidType), } } } } impl Serialize for DictNonStrKey { #[inline(never)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut pos = 0; let mut next_key: *mut crate::ffi::PyObject = core::ptr::null_mut(); let mut next_value: *mut crate::ffi::PyObject = core::ptr::null_mut(); pydict_next!( self.dict.as_ptr(), &raw mut pos, &raw mut next_key, &raw mut next_value ); let opts = self.state.opts() & NOT_PASSTHROUGH; let len = self.dict.len(); assume!(len > 0); let mut items: SmallVec<[(String, *mut crate::ffi::PyObject); 8]> = SmallVec::with_capacity(len); for _ in 0..len { let key = next_key; let value = next_value; pydict_next!( self.dict.as_ptr(), &raw mut pos, &raw mut next_key, &raw mut next_value ); match PyStrRef::from_ptr(key) { Ok(pystr) => match pystr.as_str() { Some(uni) => { items.push((String::from(uni), value)); } None => err!(SerializeError::InvalidStr), }, Err(_) => match Self::pyobject_to_string(key, opts) { Ok(key_as_str) => items.push((key_as_str, value)), Err(err) => err!(err), }, } } let mut items_as_str: SmallVec<[(&str, *mut crate::ffi::PyObject); 8]> = SmallVec::with_capacity(len); items .iter() .for_each(|(key, val)| items_as_str.push(((*key).as_str(), *val))); if opt_enabled!(opts, SORT_KEYS) { sort_dict_items(&mut items_as_str); } let mut map = serializer.serialize_map(None).unwrap(); for (key, val) in items_as_str.iter() { let pyvalue = PyObjectSerializer::new(*val, self.state, self.default); map.serialize_key(key).unwrap(); map.serialize_value(&pyvalue)?; } map.end() } } ================================================ FILE: src/serialize/per_type/float.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2018-2026) use crate::ffi::PyFloatRef; use serde::ser::{Serialize, Serializer}; #[repr(transparent)] pub(crate) struct FloatSerializer { ob: PyFloatRef, } impl FloatSerializer { pub fn new(ptr: PyFloatRef) -> Self { FloatSerializer { ob: ptr } } } impl Serialize for FloatSerializer { #[inline(always)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_f64(self.ob.value()) } } ================================================ FILE: src/serialize/per_type/fragment.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2018-2026) use crate::ffi::{PyFragmentRef, PyFragmentRefError}; use crate::serialize::error::SerializeError; use serde::ser::{Serialize, Serializer}; #[repr(transparent)] pub(crate) struct FragmentSerializer { ob: PyFragmentRef, } impl FragmentSerializer { pub fn new(ob: PyFragmentRef) -> Self { FragmentSerializer { ob: ob } } } impl Serialize for FragmentSerializer { #[cold] #[inline(never)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { match self.ob.value() { Ok(buffer) => serializer.serialize_bytes(buffer), Err(PyFragmentRefError::InvalidStr) => err!(SerializeError::InvalidStr), Err(PyFragmentRefError::InvalidFragment) => err!(SerializeError::InvalidFragment), } } } ================================================ FILE: src/serialize/per_type/int.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2018-2026) use crate::ffi::PyIntRef; use crate::opt::{Opt, STRICT_INTEGER}; use crate::serialize::error::SerializeError; use serde::ser::{Serialize, Serializer}; // https://tools.ietf.org/html/rfc7159#section-6 // "[-(2**53)+1, (2**53)-1]" const STRICT_INT_MIN: i64 = -9007199254740991; const STRICT_INT_MAX: i64 = 9007199254740991; pub(crate) struct IntSerializer { ob: PyIntRef, opts: Opt, } impl IntSerializer { pub fn new(ob: PyIntRef, opts: Opt) -> Self { IntSerializer { ob: ob, opts: opts } } } impl Serialize for IntSerializer { #[inline(always)] #[cfg(feature = "inline_int")] fn serialize(&self, serializer: S) -> Result where S: Serializer, { unsafe { match self.ob.kind() { crate::ffi::PyIntKind::I32 => serializer.serialize_i32(self.ob.as_i32()), crate::ffi::PyIntKind::U32 => serializer.serialize_u32(self.ob.as_u32()), crate::ffi::PyIntKind::I64 => { let value = self .ob .as_i64() .map_err(|_| serde::ser::Error::custom(SerializeError::Integer64Bits))?; if opt_enabled!(self.opts, STRICT_INTEGER) && !(STRICT_INT_MIN..=STRICT_INT_MAX).contains(&value) { cold_path!(); err!(SerializeError::Integer53Bits); } serializer.serialize_i64(value) } crate::ffi::PyIntKind::U64 => { let value = self .ob .as_u64() .map_err(|_| serde::ser::Error::custom(SerializeError::Integer64Bits))?; if opt_enabled!(self.opts, STRICT_INTEGER) && value > STRICT_INT_MAX as u64 { cold_path!(); err!(SerializeError::Integer53Bits); } serializer.serialize_u64(value) } } } } #[inline(always)] #[cfg(not(feature = "inline_int"))] fn serialize(&self, serializer: S) -> Result where S: Serializer, { unsafe { match self.ob.as_i64() { Ok(value) => { if opt_enabled!(self.opts, STRICT_INTEGER) && !(STRICT_INT_MIN..=STRICT_INT_MAX).contains(&value) { cold_path!(); err!(SerializeError::Integer53Bits); } serializer.serialize_i64(value) } Err(_) => match self.ob.as_u64() { Ok(value) => { if opt_enabled!(self.opts, STRICT_INTEGER) && value > STRICT_INT_MAX as u64 { cold_path!(); err!(SerializeError::Integer53Bits); } serializer.serialize_u64(value) } Err(_) => err!(SerializeError::Integer64Bits), }, } } } } ================================================ FILE: src/serialize/per_type/list.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2018-2026) use crate::ffi::{ PyBoolRef, PyDictRef, PyFloatRef, PyFragmentRef, PyIntRef, PyListRef, PyStrRef, PyStrSubclassRef, PyUuidRef, }; use crate::serialize::error::SerializeError; use crate::serialize::obtype::{ObType, pyobject_to_obtype}; use crate::serialize::per_type::{ BoolSerializer, DataclassGenericSerializer, Date, DateTime, DefaultSerializer, DictGenericSerializer, EnumSerializer, FloatSerializer, FragmentSerializer, IntSerializer, NoneSerializer, NumpyScalar, NumpySerializer, StrSerializer, StrSubclassSerializer, Time, UUID, }; use crate::serialize::serializer::PyObjectSerializer; use crate::serialize::state::SerializerState; use crate::typeref::TUPLE_TYPE; use crate::util::isize_to_usize; use core::ptr::NonNull; use serde::ser::{Serialize, SerializeSeq, Serializer}; pub(crate) struct ZeroListSerializer; impl ZeroListSerializer { pub const fn new() -> Self { Self {} } } impl Serialize for ZeroListSerializer { #[inline(always)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_bytes(b"[]") } } pub(crate) struct ListTupleSerializer { data_ptr: *const *mut crate::ffi::PyObject, state: SerializerState, default: Option>, len: usize, } impl ListTupleSerializer { pub fn from_list( ob: PyListRef, state: SerializerState, default: Option>, ) -> Self { Self { data_ptr: ob.data_ptr(), len: ob.len(), state: state.copy_for_recursive_call(), default: default, } } pub fn from_tuple( ptr: *mut crate::ffi::PyObject, state: SerializerState, default: Option>, ) -> Self { debug_assert!( is_type!(ob_type!(ptr), TUPLE_TYPE) || is_subclass_by_flag!(tp_flags!(ob_type!(ptr)), Py_TPFLAGS_TUPLE_SUBCLASS) ); let data_ptr = unsafe { (*ptr.cast::()).ob_item.as_ptr() }; let len = isize_to_usize(ffi!(Py_SIZE(ptr))); Self { data_ptr: data_ptr, len: len, state: state.copy_for_recursive_call(), default: default, } } } impl Serialize for ListTupleSerializer { #[inline(never)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { if self.state.recursion_limit() { cold_path!(); err!(SerializeError::RecursionLimit) } debug_assert!(self.len >= 1); let mut seq = serializer.serialize_seq(None).unwrap(); for idx in 0..self.len { let value = unsafe { *((self.data_ptr).add(idx)) }; match pyobject_to_obtype(value, self.state.opts()) { ObType::Str => { seq.serialize_element(&StrSerializer::new(unsafe { PyStrRef::from_ptr_unchecked(value) }))?; } ObType::StrSubclass => { seq.serialize_element(&StrSubclassSerializer::new(unsafe { PyStrSubclassRef::from_ptr_unchecked(value) }))?; } ObType::Int => { seq.serialize_element(&IntSerializer::new( unsafe { PyIntRef::from_ptr_unchecked(value) }, self.state.opts(), ))?; } ObType::None => { seq.serialize_element(&NoneSerializer::new()).unwrap(); } ObType::Float => { seq.serialize_element(&FloatSerializer::new(unsafe { PyFloatRef::from_ptr_unchecked(value) }))?; } ObType::Bool => { seq.serialize_element(&BoolSerializer::new(unsafe { PyBoolRef::from_ptr_unchecked(value) })) .unwrap(); } ObType::Datetime => { seq.serialize_element(&DateTime::new(value, self.state.opts()))?; } ObType::Date => { seq.serialize_element(&Date::new(value))?; } ObType::Time => { seq.serialize_element(&Time::new(value, self.state.opts()))?; } ObType::Uuid => { seq.serialize_element(&UUID::new(unsafe { PyUuidRef::from_ptr_unchecked(value) })) .unwrap(); } ObType::Dict => { let pyvalue = DictGenericSerializer::new( unsafe { PyDictRef::from_ptr_unchecked(value) }, self.state, self.default, ); seq.serialize_element(&pyvalue)?; } ObType::List => { if ffi!(Py_SIZE(value)) == 0 { seq.serialize_element(&ZeroListSerializer::new()).unwrap(); } else { let pyvalue = ListTupleSerializer::from_list( unsafe { PyListRef::from_ptr_unchecked(value) }, self.state, self.default, ); seq.serialize_element(&pyvalue)?; } } ObType::Tuple => { if ffi!(Py_SIZE(value)) == 0 { seq.serialize_element(&ZeroListSerializer::new()).unwrap(); } else { let pyvalue = ListTupleSerializer::from_tuple(value, self.state, self.default); seq.serialize_element(&pyvalue)?; } } ObType::Dataclass => { seq.serialize_element(&DataclassGenericSerializer::new( &PyObjectSerializer::new(value, self.state, self.default), ))?; } ObType::Enum => { seq.serialize_element(&EnumSerializer::new(&PyObjectSerializer::new( value, self.state, self.default, )))?; } ObType::NumpyArray => { seq.serialize_element(&NumpySerializer::new(&PyObjectSerializer::new( value, self.state, self.default, )))?; } ObType::NumpyScalar => { seq.serialize_element(&NumpyScalar::new(value, self.state.opts()))?; } ObType::Fragment => { seq.serialize_element(&FragmentSerializer::new(unsafe { PyFragmentRef::from_ptr_unchecked(value) }))?; } ObType::Unknown => { seq.serialize_element(&DefaultSerializer::new(&PyObjectSerializer::new( value, self.state, self.default, )))?; } } } seq.end() } } ================================================ FILE: src/serialize/per_type/mod.rs ================================================ // SPDX-License-Identifier: (Apache-2.0 OR MIT) // Copyright ijl (2020-2025), Ben Sully (2021) mod dataclass; mod datetime; mod pybool; #[macro_use] mod datetimelike; mod default; mod dict; mod float; mod fragment; mod int; mod list; mod none; mod numpy; mod pyenum; mod unicode; mod uuid; pub(crate) use dataclass::DataclassGenericSerializer; pub(crate) use datetime::{Date, DateTime, Time}; pub(crate) use datetimelike::{DateTimeError, DateTimeLike, Offset}; pub(crate) use default::DefaultSerializer; pub(crate) use dict::DictGenericSerializer; pub(crate) use float::FloatSerializer; pub(crate) use fragment::FragmentSerializer; pub(crate) use int::IntSerializer; pub(crate) use list::{ListTupleSerializer, ZeroListSerializer}; pub(crate) use none::NoneSerializer; pub(crate) use numpy::{NumpyScalar, NumpySerializer, is_numpy_array, is_numpy_scalar}; pub(crate) use pybool::BoolSerializer; pub(crate) use pyenum::EnumSerializer; pub(crate) use unicode::{StrSerializer, StrSubclassSerializer}; pub(crate) use uuid::UUID; ================================================ FILE: src/serialize/per_type/none.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2018-2025) use serde::ser::{Serialize, Serializer}; pub(crate) struct NoneSerializer; impl NoneSerializer { pub const fn new() -> Self { Self {} } } impl Serialize for NoneSerializer { #[inline] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_unit() } } ================================================ FILE: src/serialize/per_type/numpy.rs ================================================ // SPDX-License-Identifier: (Apache-2.0 OR MIT) // Copyright ijl (2018-2026), Ben Sully (2021), Nazar Kostetskyi (2022), Aviram Hassan (2020-2021) use crate::ffi::{ Py_intptr_t, Py_ssize_t, PyListRef, PyObject, PyStrRef, PyTupleRef, PyTypeObject, }; use crate::opt::Opt; use crate::serialize::buffer::SmallFixedBuffer; use crate::serialize::error::SerializeError; use crate::serialize::per_type::{ DateTimeError, DateTimeLike, DefaultSerializer, Offset, ZeroListSerializer, }; use crate::serialize::serializer::PyObjectSerializer; use crate::typeref::{ARRAY_STRUCT_STR, DESCR_STR, DTYPE_STR, NUMPY_TYPES, load_numpy_types}; use crate::util::isize_to_usize; use core::ffi::{c_char, c_int, c_void}; use jiff::Timestamp; use jiff::civil::DateTime; use serde::ser::{self, Serialize, SerializeSeq, Serializer}; use std::fmt; #[repr(transparent)] pub(crate) struct NumpySerializer<'a> { previous: &'a PyObjectSerializer, } impl<'a> NumpySerializer<'a> { pub fn new(previous: &'a PyObjectSerializer) -> Self { Self { previous: previous } } } impl Serialize for NumpySerializer<'_> { #[cold] #[inline(never)] #[cfg_attr(feature = "optimize", optimize(size))] fn serialize(&self, serializer: S) -> Result where S: Serializer, { match NumpyArray::new(self.previous.ptr, self.previous.state.opts()) { Ok(val) => val.serialize(serializer), Err(PyArrayError::Malformed) => err!(SerializeError::NumpyMalformed), Err(PyArrayError::NotContiguous | PyArrayError::UnsupportedDataType) if self.previous.default.is_some() => { DefaultSerializer::new(self.previous).serialize(serializer) } Err(PyArrayError::NotContiguous) => { err!(SerializeError::NumpyNotCContiguous) } Err(PyArrayError::NotNativeEndian) => { err!(SerializeError::NumpyNotNativeEndian) } Err(PyArrayError::UnsupportedDataType) => { err!(SerializeError::NumpyUnsupportedDatatype) } } } } macro_rules! slice { ($ptr:expr, $size:expr) => { unsafe { core::slice::from_raw_parts($ptr, $size) } }; } #[cold] pub(crate) fn is_numpy_scalar(ob_type: *mut PyTypeObject) -> bool { let numpy_types = unsafe { NUMPY_TYPES.get_or_init(load_numpy_types) }; if numpy_types.is_none() { false } else { let scalar_types = unsafe { numpy_types.unwrap().as_ref() }; core::ptr::eq(ob_type, scalar_types.float64) || core::ptr::eq(ob_type, scalar_types.float32) || core::ptr::eq(ob_type, scalar_types.float16) || core::ptr::eq(ob_type, scalar_types.int64) || core::ptr::eq(ob_type, scalar_types.int16) || core::ptr::eq(ob_type, scalar_types.int32) || core::ptr::eq(ob_type, scalar_types.int8) || core::ptr::eq(ob_type, scalar_types.uint64) || core::ptr::eq(ob_type, scalar_types.uint32) || core::ptr::eq(ob_type, scalar_types.uint8) || core::ptr::eq(ob_type, scalar_types.uint16) || core::ptr::eq(ob_type, scalar_types.bool_) || core::ptr::eq(ob_type, scalar_types.datetime64) } } #[cold] pub(crate) fn is_numpy_array(ob_type: *mut PyTypeObject) -> bool { let numpy_types = unsafe { NUMPY_TYPES.get_or_init(load_numpy_types) }; if numpy_types.is_none() { false } else { let scalar_types = unsafe { numpy_types.unwrap().as_ref() }; unsafe { core::ptr::eq(ob_type, scalar_types.array) } } } #[repr(C)] pub(crate) struct PyCapsule { pub ob_refcnt: Py_ssize_t, pub ob_type: *mut PyTypeObject, pub pointer: *mut c_void, pub name: *const c_char, pub context: *mut c_void, pub destructor: *mut c_void, // should be typedef void (*PyCapsule_Destructor)(PyObject *); } // https://docs.scipy.org/doc/numpy/reference/arrays.interface.html#c.__array_struct__ const NPY_ARRAY_C_CONTIGUOUS: c_int = 0x1; const NPY_ARRAY_NOTSWAPPED: c_int = 0x200; #[repr(C)] pub(crate) struct PyArrayInterface { pub two: c_int, pub nd: c_int, pub typekind: c_char, pub itemsize: c_int, pub flags: c_int, pub shape: *mut Py_intptr_t, pub strides: *mut Py_intptr_t, pub data: *mut c_void, pub descr: *mut PyObject, } #[derive(Clone, Copy)] pub(crate) enum ItemType { BOOL, DATETIME64(NumpyDatetimeUnit), F16, F32, F64, I8, I16, I32, I64, U8, U16, U32, U64, } impl ItemType { fn find(array: *mut PyArrayInterface, ptr: *mut PyObject) -> Option { match unsafe { ((*array).typekind, (*array).itemsize) } { (098, 1) => Some(ItemType::BOOL), (077, 8) => { let unit = NumpyDatetimeUnit::from_pyobject(ptr); Some(ItemType::DATETIME64(unit)) } (102, 2) => Some(ItemType::F16), (102, 4) => Some(ItemType::F32), (102, 8) => Some(ItemType::F64), (105, 1) => Some(ItemType::I8), (105, 2) => Some(ItemType::I16), (105, 4) => Some(ItemType::I32), (105, 8) => Some(ItemType::I64), (117, 1) => Some(ItemType::U8), (117, 2) => Some(ItemType::U16), (117, 4) => Some(ItemType::U32), (117, 8) => Some(ItemType::U64), _ => None, } } } pub(crate) enum PyArrayError { Malformed, NotContiguous, NotNativeEndian, UnsupportedDataType, } // >>> arr = numpy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], numpy.int32) // >>> arr.ndim // 3 // >>> arr.shape // (2, 2, 2) // >>> arr.strides // (16, 8, 4) pub(crate) struct NumpyArray { array: *mut PyArrayInterface, position: Vec, children: Vec, depth: usize, capsule: *mut PyCapsule, kind: ItemType, opts: Opt, } impl NumpyArray { #[cold] #[inline(never)] #[cfg_attr(feature = "optimize", optimize(size))] pub fn new(ptr: *mut PyObject, opts: Opt) -> Result { let capsule = ffi!(PyObject_GetAttr(ptr, ARRAY_STRUCT_STR)); debug_assert!(!capsule.is_null()); let array = unsafe { (*capsule.cast::()) .pointer .cast::() }; debug_assert!(!array.is_null()); if unsafe { (*array).two != 2 } { ffi!(Py_DECREF(capsule)); Err(PyArrayError::Malformed) } else if unsafe { (*array).flags } & NPY_ARRAY_C_CONTIGUOUS != NPY_ARRAY_C_CONTIGUOUS { ffi!(Py_DECREF(capsule)); Err(PyArrayError::NotContiguous) } else if unsafe { (*array).flags } & NPY_ARRAY_NOTSWAPPED != NPY_ARRAY_NOTSWAPPED { ffi!(Py_DECREF(capsule)); Err(PyArrayError::NotNativeEndian) } else { debug_assert!(unsafe { (*array).nd >= 0 }); let num_dimensions = unsafe { (*array).nd.cast_unsigned() as usize }; if num_dimensions == 0 { ffi!(Py_DECREF(capsule)); return Err(PyArrayError::UnsupportedDataType); } match ItemType::find(array, ptr) { None => { ffi!(Py_DECREF(capsule)); Err(PyArrayError::UnsupportedDataType) } Some(kind) => { let mut pyarray = NumpyArray { array: array, position: vec![0; num_dimensions], children: Vec::with_capacity(num_dimensions), depth: 0, capsule: capsule.cast::(), kind: kind, opts, }; if pyarray.dimensions() > 1 { pyarray.build(); } Ok(pyarray) } } } } #[cfg_attr(feature = "optimize", optimize(size))] fn child_from_parent(&self, position: Vec, num_children: usize) -> Self { let mut arr = NumpyArray { array: self.array, position: position, children: Vec::with_capacity(num_children), depth: self.depth + 1, capsule: self.capsule, kind: self.kind, opts: self.opts, }; arr.build(); arr } #[cfg_attr(feature = "optimize", optimize(size))] fn build(&mut self) { if self.depth < self.dimensions() - 1 { for i in 0..self.shape()[self.depth] { let mut position: Vec = self.position.clone(); position[self.depth] = i; let num_children: usize = if self.depth < self.dimensions() - 2 { isize_to_usize(self.shape()[self.depth + 1]) } else { 0 }; self.children .push(self.child_from_parent(position, num_children)); } } } #[inline(always)] fn data(&self) -> *const c_void { let offset = self .strides() .iter() .zip(self.position.iter().copied()) .take(self.depth) .map(|(a, b)| a * b) .sum::(); unsafe { (*self.array).data.offset(offset) } } fn num_items(&self) -> usize { isize_to_usize(self.shape()[self.shape().len() - 1]) } fn dimensions(&self) -> usize { unsafe { (*self.array).nd.cast_unsigned() as usize } } fn shape(&self) -> &[isize] { slice!((*self.array).shape.cast_const(), self.dimensions()) } fn strides(&self) -> &[isize] { slice!((*self.array).strides.cast_const(), self.dimensions()) } } impl Drop for NumpyArray { fn drop(&mut self) { if self.depth == 0 { ffi!(Py_DECREF(self.array.cast::())); ffi!(Py_DECREF(self.capsule.cast::())); } } } impl Serialize for NumpyArray { #[cold] #[inline(never)] #[cfg_attr(feature = "optimize", optimize(size))] fn serialize(&self, serializer: S) -> Result where S: Serializer, { if !(self.depth >= self.dimensions() || self.shape()[self.depth] != 0) { cold_path!(); ZeroListSerializer::new().serialize(serializer) } else if !self.children.is_empty() { cold_path!(); let mut seq = serializer.serialize_seq(None).unwrap(); for child in &self.children { seq.serialize_element(child).unwrap(); } seq.end() } else { match self.kind { ItemType::F64 => { NumpyF64Array::new(slice!(self.data().cast::(), self.num_items())) .serialize(serializer) } ItemType::F32 => { NumpyF32Array::new(slice!(self.data().cast::(), self.num_items())) .serialize(serializer) } ItemType::F16 => { NumpyF16Array::new(slice!(self.data().cast::(), self.num_items())) .serialize(serializer) } ItemType::U64 => { NumpyU64Array::new(slice!(self.data().cast::(), self.num_items())) .serialize(serializer) } ItemType::U32 => { NumpyU32Array::new(slice!(self.data().cast::(), self.num_items())) .serialize(serializer) } ItemType::U16 => { NumpyU16Array::new(slice!(self.data().cast::(), self.num_items())) .serialize(serializer) } ItemType::U8 => { NumpyU8Array::new(slice!(self.data().cast::(), self.num_items())) .serialize(serializer) } ItemType::I64 => { NumpyI64Array::new(slice!(self.data().cast::(), self.num_items())) .serialize(serializer) } ItemType::I32 => { NumpyI32Array::new(slice!(self.data().cast::(), self.num_items())) .serialize(serializer) } ItemType::I16 => { NumpyI16Array::new(slice!(self.data().cast::(), self.num_items())) .serialize(serializer) } ItemType::I8 => { NumpyI8Array::new(slice!(self.data().cast::(), self.num_items())) .serialize(serializer) } ItemType::BOOL => { NumpyBoolArray::new(slice!(self.data().cast::(), self.num_items())) .serialize(serializer) } ItemType::DATETIME64(unit) => NumpyDatetime64Array::new( slice!(self.data().cast::(), self.num_items()), unit, self.opts, ) .serialize(serializer), } } } } #[repr(transparent)] struct NumpyF64Array<'a> { data: &'a [f64], } impl<'a> NumpyF64Array<'a> { fn new(data: &'a [f64]) -> Self { Self { data } } } impl Serialize for NumpyF64Array<'_> { #[cold] #[inline(never)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut seq = serializer.serialize_seq(None).unwrap(); for &each in self.data.iter() { seq.serialize_element(&DataTypeF64 { obj: each }).unwrap(); } seq.end() } } #[repr(transparent)] pub(crate) struct DataTypeF64 { obj: f64, } impl Serialize for DataTypeF64 { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_f64(self.obj) } } #[repr(transparent)] struct NumpyF32Array<'a> { data: &'a [f32], } impl<'a> NumpyF32Array<'a> { fn new(data: &'a [f32]) -> Self { Self { data } } } impl Serialize for NumpyF32Array<'_> { #[cold] #[inline(never)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut seq = serializer.serialize_seq(None).unwrap(); for &each in self.data.iter() { seq.serialize_element(&DataTypeF32 { obj: each }).unwrap(); } seq.end() } } #[repr(transparent)] struct DataTypeF32 { obj: f32, } impl Serialize for DataTypeF32 { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_f32(self.obj) } } #[repr(transparent)] struct NumpyF16Array<'a> { data: &'a [u16], } impl<'a> NumpyF16Array<'a> { fn new(data: &'a [u16]) -> Self { Self { data } } } impl Serialize for NumpyF16Array<'_> { #[cold] #[inline(never)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut seq = serializer.serialize_seq(None).unwrap(); for &each in self.data.iter() { seq.serialize_element(&DataTypeF16 { obj: each }).unwrap(); } seq.end() } } #[repr(transparent)] struct DataTypeF16 { obj: u16, } impl Serialize for DataTypeF16 { #[cold] #[cfg_attr(feature = "optimize", optimize(size))] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let as_f16 = half::f16::from_bits(self.obj); serializer.serialize_f32(as_f16.to_f32()) } } #[repr(transparent)] struct NumpyU64Array<'a> { data: &'a [u64], } impl<'a> NumpyU64Array<'a> { fn new(data: &'a [u64]) -> Self { Self { data } } } impl Serialize for NumpyU64Array<'_> { #[cold] #[inline(never)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut seq = serializer.serialize_seq(None).unwrap(); for &each in self.data.iter() { seq.serialize_element(&DataTypeU64 { obj: each }).unwrap(); } seq.end() } } #[repr(transparent)] pub(crate) struct DataTypeU64 { obj: u64, } impl Serialize for DataTypeU64 { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_u64(self.obj) } } #[repr(transparent)] struct NumpyU32Array<'a> { data: &'a [u32], } impl<'a> NumpyU32Array<'a> { fn new(data: &'a [u32]) -> Self { Self { data } } } impl Serialize for NumpyU32Array<'_> { #[cold] #[inline(never)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut seq = serializer.serialize_seq(None).unwrap(); for &each in self.data.iter() { seq.serialize_element(&DataTypeU32 { obj: each }).unwrap(); } seq.end() } } #[repr(transparent)] pub(crate) struct DataTypeU32 { obj: u32, } impl Serialize for DataTypeU32 { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_u32(self.obj) } } #[repr(transparent)] struct NumpyU16Array<'a> { data: &'a [u16], } impl<'a> NumpyU16Array<'a> { fn new(data: &'a [u16]) -> Self { Self { data } } } impl Serialize for NumpyU16Array<'_> { #[cold] #[inline(never)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut seq = serializer.serialize_seq(None).unwrap(); for &each in self.data.iter() { seq.serialize_element(&DataTypeU16 { obj: each }).unwrap(); } seq.end() } } #[repr(transparent)] pub(crate) struct DataTypeU16 { obj: u16, } impl Serialize for DataTypeU16 { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_u32(u32::from(self.obj)) } } #[repr(transparent)] struct NumpyI64Array<'a> { data: &'a [i64], } impl<'a> NumpyI64Array<'a> { fn new(data: &'a [i64]) -> Self { Self { data } } } impl Serialize for NumpyI64Array<'_> { #[cold] #[inline(never)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut seq = serializer.serialize_seq(None).unwrap(); for &each in self.data.iter() { seq.serialize_element(&DataTypeI64 { obj: each }).unwrap(); } seq.end() } } #[repr(transparent)] pub(crate) struct DataTypeI64 { obj: i64, } impl Serialize for DataTypeI64 { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_i64(self.obj) } } #[repr(transparent)] struct NumpyI32Array<'a> { data: &'a [i32], } impl<'a> NumpyI32Array<'a> { fn new(data: &'a [i32]) -> Self { Self { data } } } impl Serialize for NumpyI32Array<'_> { #[cold] #[inline(never)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut seq = serializer.serialize_seq(None).unwrap(); for &each in self.data.iter() { seq.serialize_element(&DataTypeI32 { obj: each }).unwrap(); } seq.end() } } #[repr(transparent)] pub(crate) struct DataTypeI32 { obj: i32, } impl Serialize for DataTypeI32 { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_i32(self.obj) } } #[repr(transparent)] struct NumpyI16Array<'a> { data: &'a [i16], } impl<'a> NumpyI16Array<'a> { fn new(data: &'a [i16]) -> Self { Self { data } } } impl Serialize for NumpyI16Array<'_> { #[cold] #[inline(never)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut seq = serializer.serialize_seq(None).unwrap(); for &each in self.data.iter() { seq.serialize_element(&DataTypeI16 { obj: each }).unwrap(); } seq.end() } } #[repr(transparent)] pub(crate) struct DataTypeI16 { obj: i16, } impl Serialize for DataTypeI16 { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_i32(i32::from(self.obj)) } } #[repr(transparent)] struct NumpyI8Array<'a> { data: &'a [i8], } impl<'a> NumpyI8Array<'a> { fn new(data: &'a [i8]) -> Self { Self { data } } } impl Serialize for NumpyI8Array<'_> { #[cold] #[inline(never)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut seq = serializer.serialize_seq(None).unwrap(); for &each in self.data.iter() { seq.serialize_element(&DataTypeI8 { obj: each }).unwrap(); } seq.end() } } #[repr(transparent)] pub(crate) struct DataTypeI8 { obj: i8, } impl Serialize for DataTypeI8 { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_i32(i32::from(self.obj)) } } #[repr(transparent)] struct NumpyU8Array<'a> { data: &'a [u8], } impl<'a> NumpyU8Array<'a> { fn new(data: &'a [u8]) -> Self { Self { data } } } impl Serialize for NumpyU8Array<'_> { #[cold] #[inline(never)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut seq = serializer.serialize_seq(None).unwrap(); for &each in self.data.iter() { seq.serialize_element(&DataTypeU8 { obj: each }).unwrap(); } seq.end() } } #[repr(transparent)] pub(crate) struct DataTypeU8 { obj: u8, } impl Serialize for DataTypeU8 { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_u32(u32::from(self.obj)) } } #[repr(transparent)] struct NumpyBoolArray<'a> { data: &'a [u8], } impl<'a> NumpyBoolArray<'a> { fn new(data: &'a [u8]) -> Self { Self { data } } } impl Serialize for NumpyBoolArray<'_> { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut seq = serializer.serialize_seq(None).unwrap(); for &each in self.data.iter() { seq.serialize_element(&DataTypeBool { obj: each }).unwrap(); } seq.end() } } #[repr(transparent)] pub(crate) struct DataTypeBool { obj: u8, } impl Serialize for DataTypeBool { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_bool(self.obj == 1) } } pub(crate) struct NumpyScalar { ptr: *mut PyObject, opts: Opt, } impl NumpyScalar { pub fn new(ptr: *mut PyObject, opts: Opt) -> Self { NumpyScalar { ptr, opts } } } impl Serialize for NumpyScalar { #[cold] #[inline(never)] #[cfg_attr(feature = "optimize", optimize(size))] fn serialize(&self, serializer: S) -> Result where S: Serializer, { unsafe { let ob_type = ob_type!(self.ptr); let scalar_types = unsafe { NUMPY_TYPES.get_or_init(load_numpy_types).unwrap().as_ref() }; if core::ptr::eq(ob_type, scalar_types.float64) { (*(self.ptr.cast::())).serialize(serializer) } else if core::ptr::eq(ob_type, scalar_types.float32) { (*(self.ptr.cast::())).serialize(serializer) } else if core::ptr::eq(ob_type, scalar_types.float16) { (*(self.ptr.cast::())).serialize(serializer) } else if core::ptr::eq(ob_type, scalar_types.int64) { (*(self.ptr.cast::())).serialize(serializer) } else if core::ptr::eq(ob_type, scalar_types.int32) { (*(self.ptr.cast::())).serialize(serializer) } else if core::ptr::eq(ob_type, scalar_types.int16) { (*(self.ptr.cast::())).serialize(serializer) } else if core::ptr::eq(ob_type, scalar_types.int8) { (*(self.ptr.cast::())).serialize(serializer) } else if core::ptr::eq(ob_type, scalar_types.uint64) { (*(self.ptr.cast::())).serialize(serializer) } else if core::ptr::eq(ob_type, scalar_types.uint32) { (*(self.ptr.cast::())).serialize(serializer) } else if core::ptr::eq(ob_type, scalar_types.uint16) { (*(self.ptr.cast::())).serialize(serializer) } else if core::ptr::eq(ob_type, scalar_types.uint8) { (*(self.ptr.cast::())).serialize(serializer) } else if core::ptr::eq(ob_type, scalar_types.bool_) { (*(self.ptr.cast::())).serialize(serializer) } else if core::ptr::eq(ob_type, scalar_types.datetime64) { let unit = NumpyDatetimeUnit::from_pyobject(self.ptr); let obj = &*self.ptr.cast::(); let dt = unit .datetime(obj.value, self.opts) .map_err(NumpyDateTimeError::into_serde_err)?; dt.serialize(serializer) } else { unreachable!() } } } } #[repr(C)] pub(crate) struct NumpyInt8 { ob_refcnt: Py_ssize_t, ob_type: *mut PyTypeObject, value: i8, } impl Serialize for NumpyInt8 { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_i32(i32::from(self.value)) } } #[repr(C)] pub(crate) struct NumpyInt16 { pub ob_refcnt: Py_ssize_t, pub ob_type: *mut PyTypeObject, pub value: i16, } impl Serialize for NumpyInt16 { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_i32(i32::from(self.value)) } } #[repr(C)] pub(crate) struct NumpyInt32 { ob_refcnt: Py_ssize_t, ob_type: *mut PyTypeObject, value: i32, } impl Serialize for NumpyInt32 { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_i32(self.value) } } #[repr(C)] pub(crate) struct NumpyInt64 { ob_refcnt: Py_ssize_t, ob_type: *mut PyTypeObject, value: i64, } impl Serialize for NumpyInt64 { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_i64(self.value) } } #[repr(C)] pub(crate) struct NumpyUint8 { ob_refcnt: Py_ssize_t, ob_type: *mut PyTypeObject, value: u8, } impl Serialize for NumpyUint8 { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_u32(u32::from(self.value)) } } #[repr(C)] pub(crate) struct NumpyUint16 { pub ob_refcnt: Py_ssize_t, pub ob_type: *mut PyTypeObject, pub value: u16, } impl Serialize for NumpyUint16 { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_u32(u32::from(self.value)) } } #[repr(C)] pub(crate) struct NumpyUint32 { ob_refcnt: Py_ssize_t, ob_type: *mut PyTypeObject, value: u32, } impl Serialize for NumpyUint32 { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_u32(self.value) } } #[repr(C)] pub(crate) struct NumpyUint64 { ob_refcnt: Py_ssize_t, ob_type: *mut PyTypeObject, value: u64, } impl Serialize for NumpyUint64 { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_u64(self.value) } } #[repr(C)] pub(crate) struct NumpyFloat16 { ob_refcnt: Py_ssize_t, ob_type: *mut PyTypeObject, value: u16, } impl Serialize for NumpyFloat16 { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let as_f16 = half::f16::from_bits(self.value); serializer.serialize_f32(as_f16.to_f32()) } } #[repr(C)] pub(crate) struct NumpyFloat32 { ob_refcnt: Py_ssize_t, ob_type: *mut PyTypeObject, value: f32, } impl Serialize for NumpyFloat32 { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_f32(self.value) } } #[repr(C)] pub(crate) struct NumpyFloat64 { ob_refcnt: Py_ssize_t, ob_type: *mut PyTypeObject, value: f64, } impl Serialize for NumpyFloat64 { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_f64(self.value) } } #[repr(C)] pub(crate) struct NumpyBool { ob_refcnt: Py_ssize_t, ob_type: *mut PyTypeObject, value: bool, } impl Serialize for NumpyBool { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_bool(self.value) } } /// This mimicks the units supported by numpy's datetime64 type. /// /// See /// https://github.com/numpy/numpy/blob/fc8e3bbe419748ac5c6b7f3d0845e4bafa74644b/numpy/core/include/numpy/ndarraytypes.h#L268-L282. #[derive(Clone, Copy)] pub(crate) enum NumpyDatetimeUnit { NaT, Years, Months, Weeks, Days, Hours, Minutes, Seconds, Milliseconds, Microseconds, Nanoseconds, Picoseconds, Femtoseconds, Attoseconds, Generic, } impl fmt::Display for NumpyDatetimeUnit { #[cold] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let unit = match self { Self::NaT => "NaT", Self::Years => "years", Self::Months => "months", Self::Weeks => "weeks", Self::Days => "days", Self::Hours => "hours", Self::Minutes => "minutes", Self::Seconds => "seconds", Self::Milliseconds => "milliseconds", Self::Microseconds => "microseconds", Self::Nanoseconds => "nanoseconds", Self::Picoseconds => "picoseconds", Self::Femtoseconds => "femtoseconds", Self::Attoseconds => "attoseconds", Self::Generic => "generic", }; write!(f, "{unit}") } } #[derive(Clone, Copy)] enum NumpyDateTimeError { UnsupportedUnit(NumpyDatetimeUnit), Unrepresentable { unit: NumpyDatetimeUnit, val: i64 }, } impl NumpyDateTimeError { #[cold] fn into_serde_err(self) -> T { let err = match self { Self::UnsupportedUnit(unit) => format!("unsupported numpy.datetime64 unit: {unit}"), Self::Unrepresentable { unit, val } => { format!("unrepresentable numpy.datetime64: {val} {unit}") } }; ser::Error::custom(err) } } macro_rules! to_jiff_datetime { ($timestamp:expr, $self:expr, $val:expr) => { Ok( ($timestamp.map_err(|_| NumpyDateTimeError::Unrepresentable { unit: $self, val: $val, })?) .to_zoned(jiff::tz::TimeZone::UTC) .datetime(), ) }; } impl NumpyDatetimeUnit { /// Create a `NumpyDatetimeUnit` from a pointer to a Python object holding a /// numpy array. /// /// This function must only be called with pointers to numpy arrays. /// /// We need to look inside the `obj.dtype.descr` attribute of the Python /// object rather than using the `descr` field of the `__array_struct__` /// because that field isn't populated for datetime64 arrays; see /// https://github.com/numpy/numpy/issues/5350. #[cold] #[cfg_attr(feature = "optimize", optimize(size))] fn from_pyobject(ptr: *mut PyObject) -> Self { let dtype = ffi!(PyObject_GetAttr(ptr, DTYPE_STR)); let descr = ffi!(PyObject_GetAttr(dtype, DESCR_STR)); let el0 = unsafe { PyListRef::from_ptr_unchecked(descr).get(0) }; let descr_str = unsafe { PyTupleRef::from_ptr_unchecked(el0).get(1) }; match PyStrRef::from_ptr(descr_str) { Ok(uni) => { match uni.as_str() { Some(as_str) => { if as_str.len() < 5 { return Self::NaT; } // unit descriptions are found at // https://github.com/numpy/numpy/blob/b235f9e701e14ed6f6f6dcba885f7986a833743f/numpy/core/src/multiarray/datetime.c#L79-L96. let ret = match &as_str[4..as_str.len() - 1] { "Y" => Self::Years, "M" => Self::Months, "W" => Self::Weeks, "D" => Self::Days, "h" => Self::Hours, "m" => Self::Minutes, "s" => Self::Seconds, "ms" => Self::Milliseconds, "us" => Self::Microseconds, "ns" => Self::Nanoseconds, "ps" => Self::Picoseconds, "fs" => Self::Femtoseconds, "as" => Self::Attoseconds, "generic" => Self::Generic, _ => unreachable!(), }; ffi!(Py_DECREF(dtype)); ffi!(Py_DECREF(descr)); ret } None => Self::NaT, } } Err(_) => Self::NaT, } } /// Return a `NumpyDatetime64Repr` for a value in array with this unit. /// /// Returns an `Err(NumpyDateTimeError)` if the value is invalid for this unit. #[cold] #[cfg_attr(feature = "optimize", optimize(size))] fn datetime(self, val: i64, opts: Opt) -> Result { match self { Self::Years => Ok(DateTime::new( (val + 1970) .try_into() .map_err(|_| NumpyDateTimeError::Unrepresentable { unit: self, val })?, 1, 1, 0, 0, 0, 0, ) .unwrap()), Self::Months => Ok(DateTime::new( (val / 12 + 1970) .try_into() .map_err(|_| NumpyDateTimeError::Unrepresentable { unit: self, val })?, (val % 12 + 1) .try_into() .map_err(|_| NumpyDateTimeError::Unrepresentable { unit: self, val })?, 1, 0, 0, 0, 0, ) .unwrap()), Self::Weeks => { to_jiff_datetime!(Timestamp::from_second(val * 7 * 24 * 60 * 60), self, val) } Self::Days => to_jiff_datetime!(Timestamp::from_second(val * 24 * 60 * 60), self, val), Self::Hours => to_jiff_datetime!(Timestamp::from_second(val * 60 * 60), self, val), Self::Minutes => to_jiff_datetime!(Timestamp::from_second(val * 60), self, val), Self::Seconds => to_jiff_datetime!(Timestamp::from_second(val), self, val), Self::Milliseconds => to_jiff_datetime!(Timestamp::from_millisecond(val), self, val), Self::Microseconds => to_jiff_datetime!(Timestamp::from_microsecond(val), self, val), Self::Nanoseconds => { to_jiff_datetime!(Timestamp::from_nanosecond(i128::from(val)), self, val) } _ => Err(NumpyDateTimeError::UnsupportedUnit(self)), } .map(|dt| NumpyDatetime64Repr { dt, opts }) } } struct NumpyDatetime64Array<'a> { data: &'a [i64], unit: NumpyDatetimeUnit, opts: Opt, } impl<'a> NumpyDatetime64Array<'a> { fn new(data: &'a [i64], unit: NumpyDatetimeUnit, opts: Opt) -> Self { Self { data, unit, opts } } } impl Serialize for NumpyDatetime64Array<'_> { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut seq = serializer.serialize_seq(None).unwrap(); for &each in self.data.iter() { let dt = self .unit .datetime(each, self.opts) .map_err(NumpyDateTimeError::into_serde_err)?; seq.serialize_element(&dt).unwrap(); } seq.end() } } #[repr(C)] pub(crate) struct NumpyDatetime64 { ob_refcnt: Py_ssize_t, ob_type: *mut PyTypeObject, value: i64, } macro_rules! forward_inner { ($meth: ident, $ty: ident) => { fn $meth(&self) -> $ty { debug_assert!(self.dt.$meth() >= 0); #[allow(clippy::cast_sign_loss)] let ret = self.dt.$meth() as $ty; // stmt_expr_attributes ret } }; } struct NumpyDatetime64Repr { dt: DateTime, opts: Opt, } impl DateTimeLike for NumpyDatetime64Repr { forward_inner!(year, i32); forward_inner!(month, u8); forward_inner!(day, u8); forward_inner!(hour, u8); forward_inner!(minute, u8); forward_inner!(second, u8); fn nanosecond(&self) -> u32 { debug_assert!(self.dt.subsec_nanosecond() >= 0); self.dt.subsec_nanosecond().cast_unsigned() } fn microsecond(&self) -> u32 { self.nanosecond() / 1_000 } fn has_tz(&self) -> bool { false } fn slow_offset(&self) -> Result { unreachable!() } fn offset(&self) -> Result { Ok(Offset::default()) } } impl Serialize for NumpyDatetime64Repr { #[cold] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut buf = SmallFixedBuffer::new(); let _ = self.write_buf(&mut buf, self.opts); serializer.collect_str(str_from_slice!(buf.as_ptr(), buf.len())) } } ================================================ FILE: src/serialize/per_type/pybool.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2018-2026) use crate::ffi::PyBoolRef; use serde::ser::{Serialize, Serializer}; #[repr(transparent)] pub(crate) struct BoolSerializer { ob: PyBoolRef, } impl BoolSerializer { pub fn new(ob: PyBoolRef) -> Self { BoolSerializer { ob: ob } } } impl Serialize for BoolSerializer { #[inline] fn serialize(&self, serializer: S) -> Result where S: Serializer, { serializer.serialize_bool(unsafe { core::ptr::eq(self.ob.as_ptr(), crate::typeref::TRUE) }) } } ================================================ FILE: src/serialize/per_type/pyenum.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2018-2025) use crate::serialize::serializer::PyObjectSerializer; use crate::typeref::VALUE_STR; use serde::ser::{Serialize, Serializer}; #[repr(transparent)] pub(crate) struct EnumSerializer<'a> { previous: &'a PyObjectSerializer, } impl<'a> EnumSerializer<'a> { pub fn new(previous: &'a PyObjectSerializer) -> Self { Self { previous: previous } } } impl Serialize for EnumSerializer<'_> { #[inline(never)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let value = ffi!(PyObject_GetAttr(self.previous.ptr, VALUE_STR)); debug_assert!(ffi!(Py_REFCNT(value)) >= 2); let ret = PyObjectSerializer::new(value, self.previous.state, self.previous.default) .serialize(serializer); ffi!(Py_DECREF(value)); ret } } ================================================ FILE: src/serialize/per_type/unicode.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2018-2026) use crate::ffi::{PyStrRef, PyStrSubclassRef}; use crate::serialize::error::SerializeError; use serde::ser::{Serialize, Serializer}; #[repr(transparent)] pub(crate) struct StrSerializer { ob: PyStrRef, } impl StrSerializer { pub fn new(ptr: PyStrRef) -> Self { StrSerializer { ob: ptr } } } impl Serialize for StrSerializer { #[inline(always)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { match self.ob.clone().as_str() { Some(uni) => serializer.serialize_str(uni), None => { cold_path!(); err!(SerializeError::InvalidStr) } } } } #[repr(transparent)] pub(crate) struct StrSubclassSerializer { ob: PyStrSubclassRef, } impl StrSubclassSerializer { pub fn new(ptr: PyStrSubclassRef) -> Self { StrSubclassSerializer { ob: ptr } } } impl Serialize for StrSubclassSerializer { #[inline(never)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { match self.ob.as_str() { Some(uni) => serializer.serialize_str(uni), None => { cold_path!(); err!(SerializeError::InvalidStr) } } } } ================================================ FILE: src/serialize/per_type/uuid.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2018-2026) use crate::ffi::PyUuidRef; use crate::serialize::buffer::SmallFixedBuffer; use serde::ser::{Serialize, Serializer}; #[repr(transparent)] pub(crate) struct UUID { ob: PyUuidRef, } impl UUID { pub fn new(ptr: PyUuidRef) -> Self { UUID { ob: ptr } } #[inline(never)] pub fn write_buf(&self, buf: &mut B) where B: bytes::BufMut, { unsafe { let buffer_length: usize = 40; debug_assert!(buf.remaining_mut() >= buffer_length); let len = uuid::Uuid::from_u128(self.ob.value()) .hyphenated() .encode_lower(core::slice::from_raw_parts_mut( buf.chunk_mut().as_mut_ptr(), buffer_length, )) .len(); buf.advance_mut(len); } } } impl Serialize for UUID { #[inline(always)] fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut buf = SmallFixedBuffer::new(); self.write_buf(&mut buf); serializer.serialize_unit_struct(str_from_slice!(buf.as_ptr(), buf.len())) } } ================================================ FILE: src/serialize/serializer.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2018-2026) use crate::ffi::{ PyBoolRef, PyDictRef, PyFloatRef, PyFragmentRef, PyIntRef, PyListRef, PyStrRef, PyStrSubclassRef, PyUuidRef, }; use crate::opt::{APPEND_NEWLINE, INDENT_2, Opt}; use crate::serialize::obtype::{ObType, pyobject_to_obtype}; use crate::serialize::per_type::{ BoolSerializer, DataclassGenericSerializer, Date, DateTime, DefaultSerializer, DictGenericSerializer, EnumSerializer, FloatSerializer, FragmentSerializer, IntSerializer, ListTupleSerializer, NoneSerializer, NumpyScalar, NumpySerializer, StrSerializer, StrSubclassSerializer, Time, UUID, ZeroListSerializer, }; use crate::serialize::state::SerializerState; use crate::serialize::writer::{BytesWriter, to_writer, to_writer_pretty}; use core::ptr::NonNull; use serde::ser::{Serialize, Serializer}; pub(crate) fn serialize( ptr: *mut crate::ffi::PyObject, default: Option>, opts: Opt, ) -> Result, String> { let mut buf = BytesWriter::default(); let obj = PyObjectSerializer::new(ptr, SerializerState::new(opts), default); let res = if opt_disabled!(opts, INDENT_2) { to_writer(&mut buf, &obj) } else { to_writer_pretty(&mut buf, &obj) }; match res { Ok(()) => Ok(buf.finish(opt_enabled!(opts, APPEND_NEWLINE))), Err(err) => { buf.abort(); Err(err.to_string()) } } } pub(crate) struct PyObjectSerializer { pub ptr: *mut crate::ffi::PyObject, pub state: SerializerState, pub default: Option>, } impl PyObjectSerializer { pub fn new( ptr: *mut crate::ffi::PyObject, state: SerializerState, default: Option>, ) -> Self { PyObjectSerializer { ptr: ptr, state: state, default: default, } } } impl Serialize for PyObjectSerializer { fn serialize(&self, serializer: S) -> Result where S: Serializer, { unsafe { match pyobject_to_obtype(self.ptr, self.state.opts()) { ObType::Str => { StrSerializer::new(PyStrRef::from_ptr_unchecked(self.ptr)).serialize(serializer) } ObType::StrSubclass => { StrSubclassSerializer::new(PyStrSubclassRef::from_ptr_unchecked(self.ptr)) .serialize(serializer) } ObType::Int => IntSerializer::new( unsafe { PyIntRef::from_ptr_unchecked(self.ptr) }, self.state.opts(), ) .serialize(serializer), ObType::None => NoneSerializer::new().serialize(serializer), ObType::Float => FloatSerializer::new(PyFloatRef::from_ptr_unchecked(self.ptr)) .serialize(serializer), ObType::Bool => { BoolSerializer::new(unsafe { PyBoolRef::from_ptr_unchecked(self.ptr) }) .serialize(serializer) } ObType::Datetime => { DateTime::new(self.ptr, self.state.opts()).serialize(serializer) } ObType::Date => Date::new(self.ptr).serialize(serializer), ObType::Time => Time::new(self.ptr, self.state.opts()).serialize(serializer), ObType::Uuid => { UUID::new(PyUuidRef::from_ptr_unchecked(self.ptr)).serialize(serializer) } ObType::Dict => DictGenericSerializer::new( PyDictRef::from_ptr_unchecked(self.ptr), self.state, self.default, ) .serialize(serializer), ObType::List => { if ffi!(Py_SIZE(self.ptr)) == 0 { ZeroListSerializer::new().serialize(serializer) } else { ListTupleSerializer::from_list( PyListRef::from_ptr_unchecked(self.ptr), self.state, self.default, ) .serialize(serializer) } } ObType::Tuple => { if ffi!(Py_SIZE(self.ptr)) == 0 { ZeroListSerializer::new().serialize(serializer) } else { ListTupleSerializer::from_tuple(self.ptr, self.state, self.default) .serialize(serializer) } } ObType::Dataclass => DataclassGenericSerializer::new(self).serialize(serializer), ObType::Enum => EnumSerializer::new(self).serialize(serializer), ObType::NumpyArray => NumpySerializer::new(self).serialize(serializer), ObType::NumpyScalar => { NumpyScalar::new(self.ptr, self.state.opts()).serialize(serializer) } ObType::Fragment => { FragmentSerializer::new(unsafe { PyFragmentRef::from_ptr_unchecked(self.ptr) }) .serialize(serializer) } ObType::Unknown => DefaultSerializer::new(self).serialize(serializer), } } } } ================================================ FILE: src/serialize/state.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2024-2025) use crate::opt::Opt; const RECURSION_SHIFT: usize = 24; const RECURSION_MASK: u32 = 255 << RECURSION_SHIFT; const DEFAULT_SHIFT: usize = 16; const DEFAULT_MASK: u32 = 255 << DEFAULT_SHIFT; #[repr(transparent)] #[derive(Copy, Clone)] pub(crate) struct SerializerState { // recursion: u8, // default_calls: u8, // opts: u16, state: u32, } impl SerializerState { #[inline(always)] pub fn new(opts: Opt) -> Self { debug_assert!(opts < u32::from(u16::MAX)); Self { state: opts } } #[inline(always)] pub fn opts(self) -> u32 { self.state } #[inline(always)] pub fn recursion_limit(self) -> bool { self.state & RECURSION_MASK == RECURSION_MASK } #[inline(always)] pub fn default_calls_limit(self) -> bool { self.state & DEFAULT_MASK == DEFAULT_MASK } #[inline(always)] pub fn copy_for_recursive_call(self) -> Self { let opt = self.state & !RECURSION_MASK; let recursion = (((self.state & RECURSION_MASK) >> RECURSION_SHIFT) + 1) << RECURSION_SHIFT; Self { state: opt | recursion, } } #[inline(always)] pub fn copy_for_default_call(self) -> Self { let opt = self.state & !DEFAULT_MASK; let default_calls = (((self.state & DEFAULT_MASK) >> DEFAULT_SHIFT) + 1) << DEFAULT_SHIFT; Self { state: opt | default_calls, } } } ================================================ FILE: src/serialize/writer/byteswriter.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2020-2025) use crate::ffi::{PyBytes_FromStringAndSize, PyObject}; use crate::util::usize_to_isize; use bytes::{BufMut, buf::UninitSlice}; use core::mem::MaybeUninit; use core::ptr::NonNull; #[cfg(CPython)] const BUFFER_LENGTH: usize = 1024; #[cfg(not(CPython))] const BUFFER_LENGTH: usize = 4096; pub(crate) struct BytesWriter { cap: usize, len: usize, #[cfg(CPython)] bytes: *mut crate::ffi::PyBytesObject, #[cfg(not(CPython))] bytes: *mut u8, } impl BytesWriter { #[inline] pub fn default() -> Self { BytesWriter { cap: BUFFER_LENGTH, len: 0, #[cfg(CPython)] bytes: unsafe { PyBytes_FromStringAndSize(core::ptr::null_mut(), usize_to_isize(BUFFER_LENGTH)) .cast::() }, #[cfg(not(CPython))] bytes: unsafe { crate::ffi::PyMem_Malloc(BUFFER_LENGTH).cast::() }, } } #[cfg(CPython)] pub fn abort(&mut self) { ffi!(Py_DECREF(self.bytes.cast::())); } #[cfg(not(CPython))] pub fn abort(&mut self) { unsafe { crate::ffi::PyMem_Free(self.bytes.cast::()); } } fn append_and_terminate(&mut self, append: bool) { unsafe { if append { core::ptr::write(self.buffer_ptr(), b'\n'); self.len += 1; } #[cfg(CPython)] core::ptr::write(self.buffer_ptr(), 0); } } #[cfg(CPython)] #[inline] pub fn finish(&mut self, append: bool) -> NonNull { unsafe { self.append_and_terminate(append); crate::ffi::Py_SET_SIZE( self.bytes.cast::(), usize_to_isize(self.len), ); self.resize(self.len); NonNull::new_unchecked(self.bytes.cast::()) } } #[cfg(not(CPython))] #[inline] pub fn finish(&mut self, append: bool) -> NonNull { unsafe { self.append_and_terminate(append); let bytes = PyBytes_FromStringAndSize( self.bytes.cast::().cast_const(), usize_to_isize(self.len), ); debug_assert!(!bytes.is_null()); crate::ffi::PyMem_Free(self.bytes.cast::()); nonnull!(bytes) } } #[cfg(CPython)] #[inline] fn buffer_ptr(&self) -> *mut u8 { unsafe { (&raw mut (*self.bytes).ob_sval).cast::().add(self.len) } } #[cfg(not(CPython))] #[inline] fn buffer_ptr(&self) -> *mut u8 { debug_assert!(!self.bytes.is_null()); unsafe { self.bytes.add(self.len) } } #[cfg(CPython)] #[inline] pub fn resize(&mut self, len: usize) { self.cap = len; unsafe { crate::ffi::_PyBytes_Resize( (&raw mut self.bytes).cast::<*mut PyObject>(), usize_to_isize(len), ); } } #[cfg(not(CPython))] #[inline] pub fn resize(&mut self, len: usize) { self.cap = len; unsafe { self.bytes = crate::ffi::PyMem_Realloc(self.bytes.cast::(), len).cast::(); debug_assert!(!self.bytes.is_null()); } } #[cold] #[inline(never)] fn grow(&mut self, len: usize) { let mut cap = self.cap; while len >= cap { cap *= 2; } self.resize(cap); } } unsafe impl BufMut for BytesWriter { #[inline] unsafe fn advance_mut(&mut self, cnt: usize) { self.len += cnt; } #[inline] fn chunk_mut(&mut self) -> &mut UninitSlice { unsafe { UninitSlice::uninit(core::slice::from_raw_parts_mut( self.buffer_ptr().cast::>(), self.remaining_mut(), )) } } #[inline] fn remaining_mut(&self) -> usize { self.cap - self.len } #[inline] fn put_u8(&mut self, value: u8) { debug_assert!(self.remaining_mut() > 1); unsafe { core::ptr::write(self.buffer_ptr(), value); self.advance_mut(1); } } #[inline] fn put_bytes(&mut self, val: u8, cnt: usize) { debug_assert!(self.remaining_mut() > cnt); unsafe { core::ptr::write_bytes(self.buffer_ptr(), val, cnt); self.advance_mut(cnt); }; } #[inline] fn put_slice(&mut self, src: &[u8]) { debug_assert!(self.remaining_mut() > src.len()); unsafe { core::ptr::copy_nonoverlapping(src.as_ptr(), self.buffer_ptr(), src.len()); self.advance_mut(src.len()); } } } // hack based on saethlin's research and patch in https://github.com/serde-rs/json/issues/766 pub(crate) trait WriteExt { #[inline] fn as_mut_buffer_ptr(&mut self) -> *mut u8 { core::ptr::null_mut() } #[inline] fn reserve(&mut self, len: usize) { let _ = len; } } impl WriteExt for &mut BytesWriter { #[inline(always)] fn as_mut_buffer_ptr(&mut self) -> *mut u8 { self.buffer_ptr() } #[inline(always)] fn reserve(&mut self, len: usize) { let end_length = self.len + len; if end_length >= self.cap { cold_path!(); self.grow(end_length); } } } ================================================ FILE: src/serialize/writer/formatter.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2022-2026) // This is an adaptation of `src/value/ser.rs` from serde-json. use super::{ write_float32, write_float64, write_integer_i32, write_integer_i64, write_integer_u32, write_integer_u64, }; use crate::serialize::writer::WriteExt; use std::io; macro_rules! debug_assert_has_capacity { ($writer:expr) => { debug_assert!($writer.remaining_mut() > 4) }; } pub(crate) trait Formatter { #[inline] fn write_null(&mut self, writer: &mut W) -> io::Result<()> where W: ?Sized + WriteExt + bytes::BufMut, { unsafe { reserve_minimum!(writer); writer.put_slice(b"null"); Ok(()) } } #[inline] fn write_bool(&mut self, writer: &mut W, value: bool) -> io::Result<()> where W: ?Sized + WriteExt + bytes::BufMut, { reserve_minimum!(writer); unsafe { writer.put_slice(if value { b"true" } else { b"false" }); } Ok(()) } #[inline] fn write_i32(&mut self, writer: &mut W, value: i32) -> io::Result<()> where W: ?Sized + WriteExt + bytes::BufMut, { unsafe { reserve_minimum!(writer); write_integer_i32(writer, value); } Ok(()) } #[inline] fn write_i64(&mut self, writer: &mut W, value: i64) -> io::Result<()> where W: ?Sized + WriteExt + bytes::BufMut, { unsafe { reserve_minimum!(writer); write_integer_i64(writer, value); } Ok(()) } #[inline] fn write_u32(&mut self, writer: &mut W, value: u32) -> io::Result<()> where W: ?Sized + WriteExt + bytes::BufMut, { unsafe { reserve_minimum!(writer); write_integer_u32(writer, value); } Ok(()) } #[inline] fn write_u64(&mut self, writer: &mut W, value: u64) -> io::Result<()> where W: ?Sized + WriteExt + bytes::BufMut, { unsafe { reserve_minimum!(writer); write_integer_u64(writer, value); } Ok(()) } #[inline] fn write_f32(&mut self, writer: &mut W, value: f32) -> io::Result<()> where W: ?Sized + WriteExt + bytes::BufMut, { unsafe { reserve_minimum!(writer); write_float32(writer, value); } Ok(()) } #[inline] fn write_f64(&mut self, writer: &mut W, value: f64) -> io::Result<()> where W: ?Sized + WriteExt + bytes::BufMut, { unsafe { reserve_minimum!(writer); write_float64(writer, value); } Ok(()) } #[inline] fn begin_array(&mut self, writer: &mut W) -> io::Result<()> where W: ?Sized + WriteExt + bytes::BufMut, { reserve_minimum!(writer); unsafe { writer.put_u8(b'['); } Ok(()) } #[inline] fn end_array(&mut self, writer: &mut W) -> io::Result<()> where W: ?Sized + WriteExt + bytes::BufMut, { debug_assert_has_capacity!(writer); unsafe { writer.put_u8(b']'); } Ok(()) } #[inline] fn begin_array_value(&mut self, writer: &mut W, first: bool) -> io::Result<()> where W: ?Sized + WriteExt + bytes::BufMut, { debug_assert_has_capacity!(writer); if !first { unsafe { writer.put_u8(b',') } } Ok(()) } #[inline] fn end_array_value(&mut self, _writer: &mut W) -> io::Result<()> where W: ?Sized, { Ok(()) } #[inline] fn begin_object(&mut self, writer: &mut W) -> io::Result<()> where W: ?Sized + WriteExt + bytes::BufMut, { reserve_minimum!(writer); unsafe { writer.put_u8(b'{'); } Ok(()) } #[inline] fn end_object(&mut self, writer: &mut W) -> io::Result<()> where W: ?Sized + WriteExt + bytes::BufMut, { reserve_minimum!(writer); unsafe { writer.put_u8(b'}'); } Ok(()) } #[inline] fn begin_object_key(&mut self, writer: &mut W, first: bool) -> io::Result<()> where W: ?Sized + WriteExt + bytes::BufMut, { debug_assert_has_capacity!(writer); if !first { unsafe { writer.put_u8(b','); } } Ok(()) } #[inline] fn end_object_key(&mut self, _writer: &mut W) -> io::Result<()> where W: ?Sized, { Ok(()) } #[inline] fn begin_object_value(&mut self, writer: &mut W) -> io::Result<()> where W: ?Sized + WriteExt + bytes::BufMut, { debug_assert_has_capacity!(writer); unsafe { writer.put_u8(b':'); } Ok(()) } #[inline] fn end_object_value(&mut self, _writer: &mut W) -> io::Result<()> where W: ?Sized, { Ok(()) } } pub(crate) struct CompactFormatter; impl Formatter for CompactFormatter {} pub(crate) struct PrettyFormatter { current_indent: usize, has_value: bool, } impl PrettyFormatter { #[allow(clippy::new_without_default)] pub const fn new() -> Self { PrettyFormatter { current_indent: 0, has_value: false, } } } impl Formatter for PrettyFormatter { #[inline] fn begin_array(&mut self, writer: &mut W) -> io::Result<()> where W: ?Sized + WriteExt + bytes::BufMut, { self.current_indent += 1; self.has_value = false; reserve_minimum!(writer); unsafe { writer.put_u8(b'['); } Ok(()) } #[inline] fn end_array(&mut self, writer: &mut W) -> io::Result<()> where W: ?Sized + WriteExt + bytes::BufMut, { self.current_indent -= 1; let num_spaces = self.current_indent * 2; reserve_pretty!(writer, num_spaces); unsafe { if self.has_value { writer.put_u8(b'\n'); writer.put_bytes(b' ', num_spaces); } writer.put_u8(b']'); Ok(()) } } #[inline] fn begin_array_value(&mut self, writer: &mut W, first: bool) -> io::Result<()> where W: ?Sized + WriteExt + bytes::BufMut, { let num_spaces = self.current_indent * 2; reserve_pretty!(writer, num_spaces); unsafe { writer.put_slice(if first { b"\n" } else { b",\n" }); writer.put_bytes(b' ', num_spaces); }; Ok(()) } #[inline] fn end_array_value(&mut self, _writer: &mut W) -> io::Result<()> where W: ?Sized, { self.has_value = true; Ok(()) } #[inline] fn begin_object(&mut self, writer: &mut W) -> io::Result<()> where W: ?Sized + WriteExt + bytes::BufMut, { self.current_indent += 1; self.has_value = false; reserve_minimum!(writer); unsafe { writer.put_u8(b'{'); } Ok(()) } #[inline] fn end_object(&mut self, writer: &mut W) -> io::Result<()> where W: ?Sized + WriteExt + bytes::BufMut, { self.current_indent -= 1; let num_spaces = self.current_indent * 2; reserve_pretty!(writer, num_spaces); unsafe { if self.has_value { writer.put_u8(b'\n'); writer.put_bytes(b' ', num_spaces); } writer.put_u8(b'}'); Ok(()) } } #[inline] fn begin_object_key(&mut self, writer: &mut W, first: bool) -> io::Result<()> where W: ?Sized + WriteExt + bytes::BufMut, { let num_spaces = self.current_indent * 2; reserve_pretty!(writer, num_spaces); unsafe { writer.put_slice(if first { b"\n" } else { b",\n" }); writer.put_bytes(b' ', num_spaces); } Ok(()) } #[inline] fn begin_object_value(&mut self, writer: &mut W) -> io::Result<()> where W: ?Sized + WriteExt + bytes::BufMut, { reserve_minimum!(writer); unsafe { writer.put_slice(b": "); } Ok(()) } #[inline] fn end_object_value(&mut self, _writer: &mut W) -> io::Result<()> where W: ?Sized, { self.has_value = true; Ok(()) } } ================================================ FILE: src/serialize/writer/json.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2022-2025) // This is an adaptation of `src/value/ser.rs` from serde-json. use crate::serialize::writer::WriteExt; use crate::serialize::writer::formatter::{CompactFormatter, Formatter, PrettyFormatter}; use serde::ser::{self, Impossible, Serialize}; use serde_json::error::{Error, Result}; pub(crate) struct Serializer { writer: W, formatter: F, } impl Serializer where W: WriteExt + bytes::BufMut, { #[inline] pub fn new(writer: W) -> Self { Serializer::with_formatter(writer, CompactFormatter) } } impl Serializer where W: WriteExt + bytes::BufMut, { #[inline] pub fn pretty(writer: W) -> Self { Serializer::with_formatter(writer, PrettyFormatter::new()) } } impl Serializer where W: WriteExt + bytes::BufMut, F: Formatter, { #[inline] pub fn with_formatter(writer: W, formatter: F) -> Self { Serializer { writer, formatter } } } impl<'a, W, F> ser::Serializer for &'a mut Serializer where W: WriteExt + bytes::BufMut, F: Formatter, { type Ok = (); type Error = Error; type SerializeSeq = Compound<'a, W, F>; type SerializeTuple = Impossible<(), Error>; type SerializeTupleStruct = Impossible<(), Error>; type SerializeTupleVariant = Impossible<(), Error>; type SerializeMap = Compound<'a, W, F>; type SerializeStruct = Impossible<(), Error>; type SerializeStructVariant = Impossible<(), Error>; #[inline] fn serialize_bool(self, value: bool) -> Result<()> { self.formatter .write_bool(&mut self.writer, value) .map_err(Error::io) } fn serialize_i8(self, _value: i8) -> Result<()> { unreachable!(); } fn serialize_i16(self, _value: i16) -> Result<()> { unreachable!(); } #[inline] fn serialize_i32(self, value: i32) -> Result<()> { self.formatter .write_i32(&mut self.writer, value) .map_err(Error::io) } #[inline] fn serialize_i64(self, value: i64) -> Result<()> { self.formatter .write_i64(&mut self.writer, value) .map_err(Error::io) } fn serialize_i128(self, _value: i128) -> Result<()> { unreachable!(); } fn serialize_u8(self, _value: u8) -> Result<()> { unreachable!(); } fn serialize_u16(self, _value: u16) -> Result<()> { unreachable!(); } #[inline] fn serialize_u32(self, value: u32) -> Result<()> { self.formatter .write_u32(&mut self.writer, value) .map_err(Error::io) } #[inline] fn serialize_u64(self, value: u64) -> Result<()> { self.formatter .write_u64(&mut self.writer, value) .map_err(Error::io) } fn serialize_u128(self, _value: u128) -> Result<()> { unreachable!(); } #[inline] fn serialize_f32(self, value: f32) -> Result<()> { if value.is_infinite() || value.is_nan() { cold_path!(); self.serialize_unit() } else { self.formatter .write_f32(&mut self.writer, value) .map_err(Error::io) } } #[inline] fn serialize_f64(self, value: f64) -> Result<()> { if value.is_infinite() || value.is_nan() { cold_path!(); self.serialize_unit() } else { self.formatter .write_f64(&mut self.writer, value) .map_err(Error::io) } } fn serialize_char(self, _value: char) -> Result<()> { unreachable!(); } #[inline(always)] fn serialize_str(self, value: &str) -> Result<()> { format_escaped_str(&mut self.writer, value); Ok(()) } #[inline(always)] fn serialize_bytes(self, value: &[u8]) -> Result<()> { self.writer.reserve(value.len() + 32); unsafe { self.writer.put_slice(value); } Ok(()) } #[inline] fn serialize_unit(self) -> Result<()> { self.formatter .write_null(&mut self.writer) .map_err(Error::io) } #[inline(always)] fn serialize_unit_struct(self, name: &'static str) -> Result<()> { debug_assert!(name.len() <= 36); reserve_minimum!(self.writer); unsafe { self.writer.put_u8(b'"'); self.writer.put_slice(name.as_bytes()); self.writer.put_u8(b'"'); } Ok(()) } fn serialize_unit_variant( self, _name: &'static str, _variant_index: u32, _variant: &'static str, ) -> Result<()> { unreachable!(); } fn serialize_newtype_struct(self, _name: &'static str, _value: &T) -> Result<()> where T: ?Sized + Serialize, { unreachable!(); } fn serialize_newtype_variant( self, _name: &'static str, _variant_index: u32, _variant: &'static str, _value: &T, ) -> Result<()> where T: ?Sized + Serialize, { unreachable!(); } #[inline] fn serialize_none(self) -> Result<()> { self.serialize_unit() } #[inline] fn serialize_some(self, value: &T) -> Result<()> where T: ?Sized + Serialize, { value.serialize(self) } #[inline(always)] fn serialize_seq(self, _len: Option) -> Result { self.formatter .begin_array(&mut self.writer) .map_err(Error::io)?; Ok(Compound { ser: self, state: State::First, }) } fn serialize_tuple(self, _len: usize) -> Result { unreachable!(); } fn serialize_tuple_struct( self, _name: &'static str, _len: usize, ) -> Result { unreachable!(); } fn serialize_tuple_variant( self, _name: &'static str, _variant_index: u32, _variant: &'static str, _len: usize, ) -> Result { unreachable!(); } #[inline(always)] fn serialize_map(self, _len: Option) -> Result { self.formatter .begin_object(&mut self.writer) .map_err(Error::io)?; Ok(Compound { ser: self, state: State::First, }) } fn serialize_struct(self, _name: &'static str, _len: usize) -> Result { unreachable!(); } fn serialize_struct_variant( self, _name: &'static str, _variant_index: u32, _variant: &'static str, _len: usize, ) -> Result { unreachable!(); } } #[derive(Eq, PartialEq)] pub(crate) enum State { First, Rest, } pub(crate) struct Compound<'a, W: 'a, F: 'a> { ser: &'a mut Serializer, state: State, } impl ser::SerializeSeq for Compound<'_, W, F> where W: WriteExt + bytes::BufMut, F: Formatter, { type Ok = (); type Error = Error; #[inline] fn serialize_element(&mut self, value: &T) -> Result<()> where T: ?Sized + Serialize, { self.ser .formatter .begin_array_value(&mut self.ser.writer, self.state == State::First) .unwrap(); self.state = State::Rest; value.serialize(&mut *self.ser)?; self.ser .formatter .end_array_value(&mut self.ser.writer) .map_err(Error::io) .unwrap(); Ok(()) } #[inline] fn end(self) -> Result<()> { self.ser.formatter.end_array(&mut self.ser.writer).unwrap(); Ok(()) } } impl ser::SerializeMap for Compound<'_, W, F> where W: WriteExt + bytes::BufMut, F: Formatter, { type Ok = (); type Error = Error; fn serialize_entry(&mut self, _key: &K, _value: &V) -> Result<()> where K: ?Sized + Serialize, V: ?Sized + Serialize, { unreachable!() } #[inline] fn serialize_key(&mut self, key: &T) -> Result<()> where T: ?Sized + Serialize, { self.ser .formatter .begin_object_key(&mut self.ser.writer, self.state == State::First) .unwrap(); self.state = State::Rest; key.serialize(MapKeySerializer { ser: self.ser })?; self.ser .formatter .end_object_key(&mut self.ser.writer) .unwrap(); Ok(()) } #[inline] fn serialize_value(&mut self, value: &T) -> Result<()> where T: ?Sized + Serialize, { self.ser .formatter .begin_object_value(&mut self.ser.writer) .unwrap(); value.serialize(&mut *self.ser)?; self.ser .formatter .end_object_value(&mut self.ser.writer) .unwrap(); Ok(()) } #[inline] fn end(self) -> Result<()> { self.ser.formatter.end_object(&mut self.ser.writer).unwrap(); Ok(()) } } #[repr(transparent)] struct MapKeySerializer<'a, W: 'a, F: 'a> { ser: &'a mut Serializer, } impl ser::Serializer for MapKeySerializer<'_, W, F> where W: WriteExt + bytes::BufMut, F: Formatter, { type Ok = (); type Error = Error; type SerializeSeq = Impossible<(), Error>; type SerializeTuple = Impossible<(), Error>; type SerializeTupleStruct = Impossible<(), Error>; type SerializeTupleVariant = Impossible<(), Error>; type SerializeMap = Impossible<(), Error>; type SerializeStruct = Impossible<(), Error>; type SerializeStructVariant = Impossible<(), Error>; #[inline(always)] fn serialize_str(self, value: &str) -> Result<()> { self.ser.serialize_str(value) } fn serialize_unit_variant( self, _name: &'static str, _variant_index: u32, _variant: &'static str, ) -> Result<()> { unreachable!(); } fn serialize_newtype_struct(self, _name: &'static str, _value: &T) -> Result<()> where T: ?Sized + Serialize, { unreachable!(); } fn serialize_bool(self, _value: bool) -> Result<()> { unreachable!(); } fn serialize_i8(self, _value: i8) -> Result<()> { unreachable!(); } fn serialize_i16(self, _value: i16) -> Result<()> { unreachable!(); } fn serialize_i32(self, _value: i32) -> Result<()> { unreachable!(); } fn serialize_i64(self, _value: i64) -> Result<()> { unreachable!(); } fn serialize_i128(self, _value: i128) -> Result<()> { unreachable!(); } fn serialize_u8(self, _value: u8) -> Result<()> { unreachable!(); } fn serialize_u16(self, _value: u16) -> Result<()> { unreachable!(); } fn serialize_u32(self, _value: u32) -> Result<()> { unreachable!(); } fn serialize_u64(self, _value: u64) -> Result<()> { unreachable!(); } fn serialize_u128(self, _value: u128) -> Result<()> { unreachable!(); } fn serialize_f32(self, _value: f32) -> Result<()> { unreachable!(); } fn serialize_f64(self, _value: f64) -> Result<()> { unreachable!(); } fn serialize_char(self, _value: char) -> Result<()> { unreachable!(); } fn serialize_bytes(self, _value: &[u8]) -> Result<()> { unreachable!(); } fn serialize_unit(self) -> Result<()> { unreachable!(); } fn serialize_unit_struct(self, _name: &'static str) -> Result<()> { unreachable!(); } fn serialize_newtype_variant( self, _name: &'static str, _variant_index: u32, _variant: &'static str, _value: &T, ) -> Result<()> where T: ?Sized + Serialize, { unreachable!(); } fn serialize_none(self) -> Result<()> { unreachable!(); } fn serialize_some(self, _value: &T) -> Result<()> where T: ?Sized + Serialize, { unreachable!(); } fn serialize_seq(self, _len: Option) -> Result { unreachable!(); } fn serialize_tuple(self, _len: usize) -> Result { unreachable!(); } fn serialize_tuple_struct( self, _name: &'static str, _len: usize, ) -> Result { unreachable!(); } fn serialize_tuple_variant( self, _name: &'static str, _variant_index: u32, _variant: &'static str, _len: usize, ) -> Result { unreachable!(); } fn serialize_map(self, _len: Option) -> Result { unreachable!(); } fn serialize_struct(self, _name: &'static str, _len: usize) -> Result { unreachable!(); } fn serialize_struct_variant( self, _name: &'static str, _variant_index: u32, _variant: &'static str, _len: usize, ) -> Result { unreachable!(); } } macro_rules! reserve_str { ($writer:expr, $value:expr) => { $writer.reserve($value.len() * 8 + 32); }; } #[cfg(all(target_arch = "x86_64", feature = "avx512"))] type StrFormatter = unsafe fn(*mut u8, *const u8, usize) -> usize; #[cfg(all(target_arch = "x86_64", feature = "avx512"))] static mut STR_FORMATTER_FN: StrFormatter = crate::serialize::writer::str::format_escaped_str_impl_sse2_128; pub(crate) fn set_str_formatter_fn() { unsafe { #[cfg(all(target_arch = "x86_64", feature = "avx512"))] if std::is_x86_feature_detected!("avx512vl") { STR_FORMATTER_FN = crate::serialize::writer::str::format_escaped_str_impl_512vl; } } } #[cfg(all(target_arch = "x86_64", not(feature = "avx512")))] #[inline(always)] fn format_escaped_str(writer: &mut W, value: &str) where W: ?Sized + WriteExt + bytes::BufMut, { unsafe { reserve_str!(writer, value); let written = crate::serialize::writer::str::format_escaped_str_impl_sse2_128( writer.as_mut_buffer_ptr(), value.as_bytes().as_ptr(), value.len(), ); writer.advance_mut(written); } } #[cfg(all(target_arch = "x86_64", feature = "avx512"))] #[inline(always)] fn format_escaped_str(writer: &mut W, value: &str) where W: ?Sized + WriteExt + bytes::BufMut, { unsafe { reserve_str!(writer, value); let written = STR_FORMATTER_FN( writer.as_mut_buffer_ptr(), value.as_bytes().as_ptr(), value.len(), ); writer.advance_mut(written); } } #[cfg(all( not(target_arch = "x86_64"), not(feature = "avx512"), feature = "generic_simd" ))] #[inline(always)] fn format_escaped_str(writer: &mut W, value: &str) where W: ?Sized + WriteExt + bytes::BufMut, { unsafe { reserve_str!(writer, value); let written = crate::serialize::writer::str::format_escaped_str_impl_generic_128( writer.as_mut_buffer_ptr(), value.as_bytes().as_ptr(), value.len(), ); writer.advance_mut(written); } } #[cfg(all(not(target_arch = "x86_64"), not(feature = "generic_simd")))] #[inline(always)] fn format_escaped_str(writer: &mut W, value: &str) where W: ?Sized + WriteExt + bytes::BufMut, { unsafe { reserve_str!(writer, value); let written = crate::serialize::writer::str::format_escaped_str_scalar( writer.as_mut_buffer_ptr(), value.as_bytes().as_ptr(), value.len(), ); writer.advance_mut(written); } } #[inline] pub(crate) fn to_writer(writer: W, value: &T) -> Result<()> where W: WriteExt + bytes::BufMut, T: ?Sized + Serialize, { let mut ser = Serializer::new(writer); value.serialize(&mut ser) } #[inline] pub(crate) fn to_writer_pretty(writer: W, value: &T) -> Result<()> where W: WriteExt + bytes::BufMut, T: ?Sized + Serialize, { let mut ser = Serializer::pretty(writer); value.serialize(&mut ser) } ================================================ FILE: src/serialize/writer/mod.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2024-2026) mod byteswriter; mod formatter; mod json; mod num; mod str; pub(crate) use byteswriter::{BytesWriter, WriteExt}; pub(crate) use json::{set_str_formatter_fn, to_writer, to_writer_pretty}; pub(crate) use num::{ write_float32, write_float64, write_integer_i32, write_integer_i64, write_integer_u32, write_integer_u64, }; ================================================ FILE: src/serialize/writer/num.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2026) use crate::serialize::writer::WriteExt; use bytes::BufMut; #[inline] pub(crate) fn write_integer_u32(buf: &mut B, val: u32) where B: ?Sized + WriteExt + BufMut, { write_integer(buf, val) } #[inline] pub(crate) fn write_integer_i32(buf: &mut B, val: i32) where B: ?Sized + WriteExt + BufMut, { write_integer(buf, val) } #[inline] pub(crate) fn write_integer_u64(buf: &mut B, val: u64) where B: ?Sized + WriteExt + BufMut, { write_integer(buf, val) } #[inline] pub(crate) fn write_integer_i64(buf: &mut B, val: i64) where B: ?Sized + WriteExt + BufMut, { write_integer(buf, val) } #[inline] fn write_integer(buf: &mut B, val: V) where B: ?Sized + WriteExt + BufMut, { unsafe { debug_assert!(buf.remaining_mut() >= 20); let len = itoap::write_to_ptr(buf.as_mut_buffer_ptr(), val); buf.advance_mut(len); } } #[inline] pub(crate) fn write_float32(buf: &mut B, val: f32) where B: ?Sized + WriteExt + BufMut, { if val.is_infinite() || val.is_nan() { cold_path!(); buf.put_slice(b"null"); } else { write_finite_float(buf, val) } } #[inline] pub(crate) fn write_float64(buf: &mut B, val: f64) where B: ?Sized + WriteExt + BufMut, { if val.is_infinite() || val.is_nan() { cold_path!(); buf.put_slice(b"null"); } else { write_finite_float(buf, val) } } fn write_finite_float(buf: &mut B, val: F) where B: ?Sized + WriteExt + BufMut, { unsafe { debug_assert!(buf.remaining_mut() >= 40); let buffer = unsafe { core::mem::transmute::<*mut u8, &mut zmij::Buffer>(buf.as_mut_buffer_ptr()) }; let res = buffer.format_finite(val); buf.advance_mut(res.len()); } } ================================================ FILE: src/serialize/writer/str/avx512.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2024-2025) use core::arch::x86_64::{ _mm256_cmpeq_epu8_mask, _mm256_cmplt_epu8_mask, _mm256_loadu_epi8, _mm256_maskz_loadu_epi8, _mm256_set1_epi8, _mm256_storeu_epi8, }; #[inline(never)] #[target_feature(enable = "avx512f,avx512bw,avx512vl,bmi2")] pub(crate) unsafe fn format_escaped_str_impl_512vl( odst: *mut u8, value_ptr: *const u8, value_len: usize, ) -> usize { unsafe { const STRIDE: usize = 32; let mut dst = odst; let mut src = value_ptr; let mut nb: usize = value_len; let blash = _mm256_set1_epi8(0b01011100i8); let quote = _mm256_set1_epi8(0b00100010i8); let x20 = _mm256_set1_epi8(0b00100000i8); core::ptr::write(dst, b'"'); dst = dst.add(1); while nb >= STRIDE { let str_vec = _mm256_loadu_epi8(src.cast::()); _mm256_storeu_epi8(dst.cast::(), str_vec); let mask = _mm256_cmpeq_epu8_mask(str_vec, blash) | _mm256_cmpeq_epu8_mask(str_vec, quote) | _mm256_cmplt_epu8_mask(str_vec, x20); if mask != 0 { let cn = mask.trailing_zeros() as usize; src = src.add(cn); dst = dst.add(cn); nb -= cn; nb -= 1; write_escape!(*(src), dst); src = src.add(1); } else { nb -= STRIDE; dst = dst.add(STRIDE); src = src.add(STRIDE); } } loop { let remainder_mask = !(u32::MAX << nb); let str_vec = _mm256_maskz_loadu_epi8(remainder_mask, src.cast::()); _mm256_storeu_epi8(dst.cast::(), str_vec); let mask = (_mm256_cmpeq_epu8_mask(str_vec, blash) | _mm256_cmpeq_epu8_mask(str_vec, quote) | _mm256_cmplt_epu8_mask(str_vec, x20)) & remainder_mask; if mask != 0 { let cn = mask.trailing_zeros() as usize; src = src.add(cn); dst = dst.add(cn); nb -= cn; nb -= 1; write_escape!(*(src), dst); src = src.add(1); } else { dst = dst.add(nb); break; } } core::ptr::write(dst, b'"'); dst = dst.add(1); dst as usize - odst as usize } } ================================================ FILE: src/serialize/writer/str/escape.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2024-2026) // the constants and SIMD approach are adapted from cloudwego's sonic-rs #[cfg(feature = "inline_int")] macro_rules! write_escape { ($byte:expr, $dst:expr) => { debug_assert!($byte < 96); #[allow(unnecessary_transmutes)] let escape = core::mem::transmute::<[u8; 8], u64>( *crate::serialize::writer::str::escape::QUOTE_TAB.get_unchecked($byte as usize), ); #[allow(clippy::cast_ptr_alignment)] let _dst = $dst.cast::(); // stmt_expr_attributes core::ptr::write(_dst, escape); $dst = $dst.add((escape as usize) >> 56); }; } #[cfg(not(feature = "inline_int"))] macro_rules! write_escape { ($byte:expr, $dst:expr) => { debug_assert!($byte < 96); let escape = crate::serialize::writer::str::escape::QUOTE_TAB.get_unchecked($byte as usize); core::ptr::copy_nonoverlapping(escape.as_ptr(), $dst, 8); $dst = $dst.add(((*escape.as_ptr().add(7)) as usize)); }; } pub(crate) const NEED_ESCAPED: [u8; 256] = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]; pub(crate) const QUOTE_TAB: [[u8; 8]; 96] = [ [b'\\', b'u', b'0', b'0', b'0', b'0', 0, 6], [b'\\', b'u', b'0', b'0', b'0', b'1', 0, 6], [b'\\', b'u', b'0', b'0', b'0', b'2', 0, 6], [b'\\', b'u', b'0', b'0', b'0', b'3', 0, 6], [b'\\', b'u', b'0', b'0', b'0', b'4', 0, 6], [b'\\', b'u', b'0', b'0', b'0', b'5', 0, 6], [b'\\', b'u', b'0', b'0', b'0', b'6', 0, 6], [b'\\', b'u', b'0', b'0', b'0', b'7', 0, 6], [b'\\', b'b', b'0', b'0', b'0', b'0', 0, 2], [b'\\', b't', b'0', b'0', b'0', b'0', 0, 2], [b'\\', b'n', b'0', b'0', b'0', b'0', 0, 2], [b'\\', b'u', b'0', b'0', b'0', b'b', 0, 6], [b'\\', b'f', b'0', b'0', b'0', b'0', 0, 2], [b'\\', b'r', b'0', b'0', b'0', b'0', 0, 2], [b'\\', b'u', b'0', b'0', b'0', b'e', 0, 6], [b'\\', b'u', b'0', b'0', b'0', b'f', 0, 6], [b'\\', b'u', b'0', b'0', b'1', b'0', 0, 6], [b'\\', b'u', b'0', b'0', b'1', b'1', 0, 6], [b'\\', b'u', b'0', b'0', b'1', b'2', 0, 6], [b'\\', b'u', b'0', b'0', b'1', b'3', 0, 6], [b'\\', b'u', b'0', b'0', b'1', b'4', 0, 6], [b'\\', b'u', b'0', b'0', b'1', b'5', 0, 6], [b'\\', b'u', b'0', b'0', b'1', b'6', 0, 6], [b'\\', b'u', b'0', b'0', b'1', b'7', 0, 6], [b'\\', b'u', b'0', b'0', b'1', b'8', 0, 6], [b'\\', b'u', b'0', b'0', b'1', b'9', 0, 6], [b'\\', b'u', b'0', b'0', b'1', b'a', 0, 6], [b'\\', b'u', b'0', b'0', b'1', b'b', 0, 6], [b'\\', b'u', b'0', b'0', b'1', b'c', 0, 6], [b'\\', b'u', b'0', b'0', b'1', b'd', 0, 6], [b'\\', b'u', b'0', b'0', b'1', b'e', 0, 6], [b'\\', b'u', b'0', b'0', b'1', b'f', 0, 6], [0; 8], [0; 8], [b'\\', b'"', 0, 0, 0, 0, 0, 2], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [0; 8], [b'\\', b'\\', 0, 0, 0, 0, 0, 2], [0; 8], [0; 8], [0; 8], ]; ================================================ FILE: src/serialize/writer/str/generic.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2024-2025) use core::simd::cmp::{SimdPartialEq, SimdPartialOrd}; use core::simd::u8x16; #[inline(never)] #[cfg_attr(target_arch = "aarch64", target_feature(enable = "neon"))] pub(crate) unsafe fn format_escaped_str_impl_generic_128( odst: *mut u8, value_ptr: *const u8, value_len: usize, ) -> usize { unsafe { const STRIDE: usize = 16; let mut dst = odst; let mut src = value_ptr; core::ptr::write(dst, b'"'); dst = dst.add(1); if value_len < STRIDE { impl_format_scalar!(dst, src, value_len); } else { let blash = u8x16::splat(b'\\'); let quote = u8x16::splat(b'"'); let x20 = u8x16::splat(32); let last_stride_src = src.add(value_len).sub(STRIDE); let mut nb: usize = value_len; { while nb >= STRIDE { let v = u8x16::from_slice(core::slice::from_raw_parts(src, STRIDE)); let mask = (v.simd_eq(blash) | v.simd_eq(quote) | v.simd_lt(x20)).to_bitmask() as u32; v.copy_to_slice(core::slice::from_raw_parts_mut(dst, STRIDE)); if mask != 0 { let cn = mask.trailing_zeros() as usize; nb -= cn; dst = dst.add(cn); src = src.add(cn); nb -= 1; write_escape!(*(src), dst); src = src.add(1); } else { nb -= STRIDE; dst = dst.add(STRIDE); src = src.add(STRIDE); } } } let mut scratch: [u8; 32] = [b'a'; 32]; let mut v = u8x16::from_slice(core::slice::from_raw_parts(last_stride_src, STRIDE)); v.copy_to_slice(core::slice::from_raw_parts_mut( scratch.as_mut_ptr(), STRIDE, )); let mut scratch_ptr = scratch.as_mut_ptr().add(16 - nb); v = u8x16::from_slice(core::slice::from_raw_parts(scratch_ptr, STRIDE)); let mut mask = (v.simd_eq(blash) | v.simd_eq(quote) | v.simd_lt(x20)).to_bitmask() as u32; loop { v.copy_to_slice(core::slice::from_raw_parts_mut(dst, STRIDE)); if mask != 0 { let cn = mask.trailing_zeros() as usize; nb -= cn; dst = dst.add(cn); scratch_ptr = scratch_ptr.add(cn); nb -= 1; mask >>= cn + 1; write_escape!(*(scratch_ptr), dst); scratch_ptr = scratch_ptr.add(1); v = u8x16::from_slice(core::slice::from_raw_parts(scratch_ptr, STRIDE)); } else { dst = dst.add(nb); break; } } } core::ptr::write(dst, b'"'); dst = dst.add(1); dst as usize - odst as usize } } ================================================ FILE: src/serialize/writer/str/mod.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2024-2025) #[macro_use] mod escape; #[macro_use] mod scalar; #[cfg(all(feature = "generic_simd", not(target_arch = "x86_64")))] mod generic; #[cfg(target_arch = "x86_64")] mod sse2; #[cfg(all(target_arch = "x86_64", feature = "avx512"))] mod avx512; #[cfg(all(not(target_arch = "x86_64"), not(feature = "generic_simd")))] pub(crate) use scalar::format_escaped_str_scalar; #[cfg(all(target_arch = "x86_64", feature = "avx512"))] pub(crate) use avx512::format_escaped_str_impl_512vl; #[allow(unused_imports)] #[cfg(target_arch = "x86_64")] pub(crate) use sse2::format_escaped_str_impl_sse2_128; #[cfg(all(feature = "generic_simd", not(target_arch = "x86_64")))] pub(crate) use generic::format_escaped_str_impl_generic_128; ================================================ FILE: src/serialize/writer/str/scalar.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2024-2025) macro_rules! impl_format_scalar { ($dst:expr, $src:expr, $value_len:expr) => { unsafe { for _ in 0..$value_len { core::ptr::write($dst, *($src)); $src = $src.add(1); $dst = $dst.add(1); if *super::escape::NEED_ESCAPED.get_unchecked(*($src.sub(1)) as usize) != 0 { $dst = $dst.sub(1); write_escape!(*($src.sub(1)), $dst); } } } }; } #[inline(never)] #[cfg(all(not(target_arch = "x86_64"), not(feature = "generic_simd")))] pub(crate) unsafe fn format_escaped_str_scalar( odst: *mut u8, value_ptr: *const u8, value_len: usize, ) -> usize { unsafe { let mut dst = odst; let mut src = value_ptr; core::ptr::write(dst, b'"'); dst = dst.add(1); impl_format_scalar!(dst, src, value_len); core::ptr::write(dst, b'"'); dst = dst.add(1); dst as usize - odst as usize } } ================================================ FILE: src/serialize/writer/str/sse2.rs ================================================ // SPDX-License-Identifier: MPL-2.0 // Copyright ijl (2024-2025) use core::arch::x86_64::{ __m128i, _mm_cmpeq_epi8, _mm_loadu_si128, _mm_movemask_epi8, _mm_or_si128, _mm_set1_epi8, _mm_setzero_si128, _mm_storeu_si128, _mm_subs_epu8, }; #[allow(dead_code)] #[expect(clippy::cast_ptr_alignment)] #[inline(never)] pub(crate) unsafe fn format_escaped_str_impl_sse2_128( odst: *mut u8, value_ptr: *const u8, value_len: usize, ) -> usize { unsafe { const STRIDE: usize = 16; let mut dst = odst; let mut src = value_ptr; core::ptr::write(dst, b'"'); dst = dst.add(1); if value_len < STRIDE { impl_format_scalar!(dst, src, value_len); } else { let blash = _mm_set1_epi8(0b01011100i8); let quote = _mm_set1_epi8(0b00100010i8); let x20 = _mm_set1_epi8(0b00011111i8); let v0 = _mm_setzero_si128(); let last_stride_src = src.add(value_len).sub(STRIDE); let mut nb: usize = value_len; unsafe { while nb >= STRIDE { let str_vec = _mm_loadu_si128(src.cast::<__m128i>()); let mask = _mm_movemask_epi8(_mm_or_si128( _mm_or_si128( _mm_cmpeq_epi8(str_vec, blash), _mm_cmpeq_epi8(str_vec, quote), ), _mm_cmpeq_epi8(_mm_subs_epu8(str_vec, x20), v0), )); _mm_storeu_si128(dst.cast::<__m128i>(), str_vec); if mask != 0 { let cn = mask.trailing_zeros() as usize; nb -= cn; dst = dst.add(cn); src = src.add(cn); nb -= 1; write_escape!(*(src), dst); src = src.add(1); } else { nb -= STRIDE; dst = dst.add(STRIDE); src = src.add(STRIDE); } } let mut scratch: [u8; 32] = [b'a'; 32]; let mut str_vec = _mm_loadu_si128(last_stride_src.cast::<__m128i>()); _mm_storeu_si128(scratch.as_mut_ptr().cast::<__m128i>(), str_vec); let mut scratch_ptr = scratch.as_mut_ptr().add(16 - nb); str_vec = _mm_loadu_si128(scratch_ptr as *const __m128i); let mut mask = _mm_movemask_epi8(_mm_or_si128( _mm_or_si128( _mm_cmpeq_epi8(str_vec, blash), _mm_cmpeq_epi8(str_vec, quote), ), _mm_cmpeq_epi8(_mm_subs_epu8(str_vec, x20), v0), )); loop { _mm_storeu_si128(dst.cast::<__m128i>(), str_vec); if mask != 0 { let cn = mask.trailing_zeros() as usize; nb -= cn; dst = dst.add(cn); scratch_ptr = scratch_ptr.add(cn); nb -= 1; mask >>= cn + 1; write_escape!(*(scratch_ptr), dst); scratch_ptr = scratch_ptr.add(1); str_vec = _mm_loadu_si128(scratch_ptr as *const __m128i); } else { dst = dst.add(nb); break; } } } } core::ptr::write(dst, b'"'); dst = dst.add(1); dst as usize - odst as usize } } ================================================ FILE: src/typeref.rs ================================================ // SPDX-License-Identifier: (Apache-2.0 OR MIT) // Copyright ijl (2020-2026), Aviram Hassan (2020-2021), Nazar Kostetskyi (2022), Ben Sully (2021) use core::ffi::CStr; use core::ptr::{NonNull, null_mut}; use once_cell::race::OnceBox; use std::sync::OnceLock; use crate::ffi::{ Py_DECREF, Py_False, Py_INCREF, Py_None, Py_True, Py_XDECREF, PyBool_Type, PyBytes_Type, PyDict_Type, PyErr_Clear, PyErr_NewException, PyExc_TypeError, PyFloat_Type, PyImport_ImportModule, PyList_Type, PyLong_Type, PyMapping_GetItemString, PyObject, PyObject_GenericGetDict, PyTuple_Type, PyTypeObject, PyUnicode_InternFromString, PyUnicode_New, PyUnicode_Type, orjson_fragmenttype_new, }; pub(crate) static mut DEFAULT: *mut PyObject = null_mut(); pub(crate) static mut OPTION: *mut PyObject = null_mut(); pub(crate) static mut NONE: *mut PyObject = null_mut(); pub(crate) static mut TRUE: *mut PyObject = null_mut(); pub(crate) static mut FALSE: *mut PyObject = null_mut(); pub(crate) static mut EMPTY_UNICODE: *mut PyObject = null_mut(); pub(crate) static mut BYTES_TYPE: *mut PyTypeObject = null_mut(); pub(crate) static mut STR_TYPE: *mut PyTypeObject = null_mut(); pub(crate) static mut INT_TYPE: *mut PyTypeObject = null_mut(); pub(crate) static mut BOOL_TYPE: *mut PyTypeObject = null_mut(); pub(crate) static mut NONE_TYPE: *mut PyTypeObject = null_mut(); pub(crate) static mut FLOAT_TYPE: *mut PyTypeObject = null_mut(); pub(crate) static mut LIST_TYPE: *mut PyTypeObject = null_mut(); pub(crate) static mut DICT_TYPE: *mut PyTypeObject = null_mut(); pub(crate) static mut DATETIME_TYPE: *mut PyTypeObject = null_mut(); pub(crate) static mut DATE_TYPE: *mut PyTypeObject = null_mut(); pub(crate) static mut TIME_TYPE: *mut PyTypeObject = null_mut(); pub(crate) static mut TUPLE_TYPE: *mut PyTypeObject = null_mut(); pub(crate) static mut UUID_TYPE: *mut PyTypeObject = null_mut(); pub(crate) static mut ENUM_TYPE: *mut PyTypeObject = null_mut(); pub(crate) static mut FIELD_TYPE: *mut PyTypeObject = null_mut(); pub(crate) static mut FRAGMENT_TYPE: *mut PyTypeObject = null_mut(); pub(crate) static mut ZONEINFO_TYPE: *mut PyTypeObject = null_mut(); pub(crate) static mut UTCOFFSET_METHOD_STR: *mut PyObject = null_mut(); pub(crate) static mut NORMALIZE_METHOD_STR: *mut PyObject = null_mut(); pub(crate) static mut CONVERT_METHOD_STR: *mut PyObject = null_mut(); pub(crate) static mut DST_STR: *mut PyObject = null_mut(); pub(crate) static mut DICT_STR: *mut PyObject = null_mut(); pub(crate) static mut DATACLASS_FIELDS_STR: *mut PyObject = null_mut(); pub(crate) static mut SLOTS_STR: *mut PyObject = null_mut(); pub(crate) static mut FIELD_TYPE_STR: *mut PyObject = null_mut(); pub(crate) static mut ARRAY_STRUCT_STR: *mut PyObject = null_mut(); pub(crate) static mut DTYPE_STR: *mut PyObject = null_mut(); pub(crate) static mut DESCR_STR: *mut PyObject = null_mut(); pub(crate) static mut VALUE_STR: *mut PyObject = null_mut(); pub(crate) static mut INT_ATTR_STR: *mut PyObject = null_mut(); #[allow(non_upper_case_globals)] pub(crate) static mut JsonEncodeError: *mut PyObject = null_mut(); #[allow(non_upper_case_globals)] pub(crate) static mut JsonDecodeError: *mut PyObject = null_mut(); unsafe fn look_up_type_object(module_name: &CStr, member_name: &CStr) -> *mut PyTypeObject { unsafe { let module = PyImport_ImportModule(module_name.as_ptr()); let module_dict = PyObject_GenericGetDict(module, null_mut()); let ptr = PyMapping_GetItemString(module_dict, member_name.as_ptr()).cast::(); Py_DECREF(module_dict); Py_DECREF(module); ptr } } #[cfg(not(PyPy))] unsafe fn look_up_datetime() { unsafe { crate::ffi::PyDateTime_IMPORT(); let datetime_capsule = crate::ffi::PyCapsule_Import(c"datetime.datetime_CAPI".as_ptr(), 1) .cast::(); debug_assert!(!datetime_capsule.is_null()); DATETIME_TYPE = (*datetime_capsule).DateTimeType; DATE_TYPE = (*datetime_capsule).DateType; TIME_TYPE = (*datetime_capsule).TimeType; ZONEINFO_TYPE = (*datetime_capsule).TZInfoType; } } #[cfg(PyPy)] unsafe fn look_up_datetime() { unsafe { DATETIME_TYPE = look_up_type_object(c"datetime", c"datetime"); DATE_TYPE = look_up_type_object(c"datetime", c"date"); TIME_TYPE = look_up_type_object(c"datetime", c"time"); ZONEINFO_TYPE = look_up_type_object(c"zoneinfo", c"ZoneInfo"); } } static INIT: OnceLock = OnceLock::new(); pub(crate) fn init_typerefs() { INIT.get_or_init(_init_typerefs_impl); } #[cold] #[cfg_attr(feature = "optimize", optimize(size))] fn _init_typerefs_impl() -> bool { unsafe { debug_assert!(crate::opt::MAX_OPT < i32::from(u16::MAX)); #[cfg(not(Py_GIL_DISABLED))] assert!( crate::deserialize::KEY_MAP .set(crate::deserialize::KeyMap::default()) .is_ok() ); crate::serialize::writer::set_str_formatter_fn(); crate::ffi::set_str_create_fn(); NONE = Py_None(); TRUE = Py_True(); FALSE = Py_False(); EMPTY_UNICODE = PyUnicode_New(0, 255); STR_TYPE = &raw mut PyUnicode_Type; BYTES_TYPE = &raw mut PyBytes_Type; DICT_TYPE = &raw mut PyDict_Type; LIST_TYPE = &raw mut PyList_Type; TUPLE_TYPE = &raw mut PyTuple_Type; NONE_TYPE = ob_type!(NONE); BOOL_TYPE = &raw mut PyBool_Type; INT_TYPE = &raw mut PyLong_Type; FLOAT_TYPE = &raw mut PyFloat_Type; look_up_datetime(); UUID_TYPE = look_up_type_object(c"uuid", c"UUID"); ENUM_TYPE = look_up_type_object(c"enum", c"EnumMeta"); FIELD_TYPE = look_up_type_object(c"dataclasses", c"_FIELD"); FRAGMENT_TYPE = orjson_fragmenttype_new(); INT_ATTR_STR = PyUnicode_InternFromString(c"int".as_ptr()); UTCOFFSET_METHOD_STR = PyUnicode_InternFromString(c"utcoffset".as_ptr()); NORMALIZE_METHOD_STR = PyUnicode_InternFromString(c"normalize".as_ptr()); CONVERT_METHOD_STR = PyUnicode_InternFromString(c"convert".as_ptr()); DST_STR = PyUnicode_InternFromString(c"dst".as_ptr()); DICT_STR = PyUnicode_InternFromString(c"__dict__".as_ptr()); DATACLASS_FIELDS_STR = PyUnicode_InternFromString(c"__dataclass_fields__".as_ptr()); SLOTS_STR = PyUnicode_InternFromString(c"__slots__".as_ptr()); FIELD_TYPE_STR = PyUnicode_InternFromString(c"_field_type".as_ptr()); ARRAY_STRUCT_STR = PyUnicode_InternFromString(c"__array_struct__".as_ptr()); DTYPE_STR = PyUnicode_InternFromString(c"dtype".as_ptr()); DESCR_STR = PyUnicode_InternFromString(c"descr".as_ptr()); VALUE_STR = PyUnicode_InternFromString(c"value".as_ptr()); DEFAULT = PyUnicode_InternFromString(c"default".as_ptr()); OPTION = PyUnicode_InternFromString(c"option".as_ptr()); JsonEncodeError = PyExc_TypeError; Py_INCREF(JsonEncodeError); let json_jsondecodeerror = look_up_type_object(c"json", c"JSONDecodeError").cast::(); debug_assert!(!json_jsondecodeerror.is_null()); JsonDecodeError = PyErr_NewException( c"orjson.JSONDecodeError".as_ptr(), json_jsondecodeerror, null_mut(), ); debug_assert!(!JsonDecodeError.is_null()); Py_XDECREF(json_jsondecodeerror); }; true } pub(crate) struct NumpyTypes { pub array: *mut PyTypeObject, pub float64: *mut PyTypeObject, pub float32: *mut PyTypeObject, pub float16: *mut PyTypeObject, pub int64: *mut PyTypeObject, pub int32: *mut PyTypeObject, pub int16: *mut PyTypeObject, pub int8: *mut PyTypeObject, pub uint64: *mut PyTypeObject, pub uint32: *mut PyTypeObject, pub uint16: *mut PyTypeObject, pub uint8: *mut PyTypeObject, pub bool_: *mut PyTypeObject, pub datetime64: *mut PyTypeObject, } pub(crate) static mut NUMPY_TYPES: OnceBox>> = OnceBox::new(); unsafe fn look_up_numpy_type( numpy_module_dict: *mut PyObject, np_type: &CStr, ) -> *mut PyTypeObject { unsafe { let ptr = PyMapping_GetItemString(numpy_module_dict, np_type.as_ptr()); Py_XDECREF(ptr); ptr.cast::() } } #[cold] #[cfg_attr(feature = "optimize", optimize(size))] pub(crate) fn load_numpy_types() -> Box>> { unsafe { let numpy = PyImport_ImportModule(c"numpy".as_ptr()); if numpy.is_null() { PyErr_Clear(); return Box::new(None); } let numpy_module_dict = PyObject_GenericGetDict(numpy, null_mut()); let types = Box::new(NumpyTypes { array: look_up_numpy_type(numpy_module_dict, c"ndarray"), float16: look_up_numpy_type(numpy_module_dict, c"half"), float32: look_up_numpy_type(numpy_module_dict, c"float32"), float64: look_up_numpy_type(numpy_module_dict, c"float64"), int8: look_up_numpy_type(numpy_module_dict, c"int8"), int16: look_up_numpy_type(numpy_module_dict, c"int16"), int32: look_up_numpy_type(numpy_module_dict, c"int32"), int64: look_up_numpy_type(numpy_module_dict, c"int64"), uint16: look_up_numpy_type(numpy_module_dict, c"uint16"), uint32: look_up_numpy_type(numpy_module_dict, c"uint32"), uint64: look_up_numpy_type(numpy_module_dict, c"uint64"), uint8: look_up_numpy_type(numpy_module_dict, c"uint8"), bool_: look_up_numpy_type(numpy_module_dict, c"bool_"), datetime64: look_up_numpy_type(numpy_module_dict, c"datetime64"), }); Py_XDECREF(numpy_module_dict); Py_XDECREF(numpy); Box::new(Some(nonnull!(Box::::into_raw(types)))) } } ================================================ FILE: src/util.rs ================================================ // SPDX-License-Identifier: (Apache-2.0 OR MIT) // Copyright ijl (2019-2026), Marc Mueller (2023) pub(crate) const INVALID_STR: &str = "str is not valid UTF-8: surrogates not allowed"; macro_rules! is_type { ($obj_ptr:expr, $type_ptr:expr) => { unsafe { $obj_ptr == $type_ptr } }; } #[cfg(CPython)] macro_rules! ob_type { ($obj:expr) => { unsafe { (*$obj).ob_type } }; } #[cfg(not(CPython))] macro_rules! ob_type { ($obj:expr) => { unsafe { crate::ffi::Py_TYPE($obj) } }; } macro_rules! is_class_by_type { ($ob_type:expr, $type_ptr:ident) => { unsafe { $ob_type == $type_ptr } }; } #[cfg(not(Py_GIL_DISABLED))] macro_rules! tp_flags { ($ob_type:expr) => { unsafe { (*$ob_type).tp_flags } }; } #[cfg(Py_GIL_DISABLED)] macro_rules! tp_flags { ($ob_type:expr) => { unsafe { (*$ob_type) .tp_flags .load(core::sync::atomic::Ordering::Relaxed) } }; } macro_rules! is_subclass_by_flag { ($tp_flags:expr, $flag:ident) => { unsafe { (($tp_flags & crate::ffi::$flag) != 0) } }; } macro_rules! is_subclass_by_type { ($ob_type:expr, $type:ident) => { unsafe { (*($ob_type.cast::())) .ob_base .ob_base .ob_type == $type } }; } macro_rules! err { ($msg:expr) => { return Err(serde::ser::Error::custom($msg)) }; } macro_rules! opt_enabled { ($var:expr, $flag:expr) => { $var & $flag != 0 }; } macro_rules! opt_disabled { ($var:expr, $flag:expr) => { $var & $flag == 0 }; } macro_rules! cold_path { () => { #[cfg(feature = "cold_path")] core::hint::cold_path(); }; } macro_rules! nonnull { ($exp:expr) => { unsafe { core::ptr::NonNull::new_unchecked($exp) } }; } macro_rules! str_from_slice { ($ptr:expr, $size:expr) => { unsafe { core::str::from_utf8_unchecked(core::slice::from_raw_parts($ptr, $size as usize)) } }; } #[cfg(all(Py_3_12, not(Py_GIL_DISABLED)))] macro_rules! reverse_pydict_incref { ($op:expr) => { unsafe { if crate::ffi::_Py_IsImmortal($op) == 0 { debug_assert!(ffi!(Py_REFCNT($op)) >= 2); (*$op).ob_refcnt.ob_refcnt -= 1; } } }; } #[cfg(Py_GIL_DISABLED)] macro_rules! reverse_pydict_incref { ($op:expr) => { debug_assert!(ffi!(Py_REFCNT($op)) >= 2); ffi!(Py_DECREF($op)) }; } #[cfg(not(Py_3_12))] macro_rules! reverse_pydict_incref { ($op:expr) => { unsafe { debug_assert!(ffi!(Py_REFCNT($op)) >= 2); (*$op).ob_refcnt -= 1; } }; } macro_rules! ffi { ($fn:ident()) => { unsafe { crate::ffi::$fn() } }; ($fn:ident($obj1:expr)) => { unsafe { crate::ffi::$fn($obj1) } }; ($fn:ident($obj1:expr, $obj2:expr)) => { unsafe { crate::ffi::$fn($obj1, $obj2) } }; ($fn:ident($obj1:expr, $obj2:expr, $obj3:expr)) => { unsafe { crate::ffi::$fn($obj1, $obj2, $obj3) } }; ($fn:ident($obj1:expr, $obj2:expr, $obj3:expr, $obj4:expr)) => { unsafe { crate::ffi::$fn($obj1, $obj2, $obj3, $obj4) } }; } #[cfg(CPython)] macro_rules! call_method { ($obj1:expr, $obj2:expr) => { unsafe { crate::ffi::PyObject_CallMethodNoArgs($obj1, $obj2) } }; ($obj1:expr, $obj2:expr, $obj3:expr) => { unsafe { crate::ffi::PyObject_CallMethodOneArg($obj1, $obj2, $obj3) } }; } #[cfg(not(CPython))] macro_rules! call_method { ($obj1:expr, $obj2:expr) => { unsafe { crate::ffi::PyObject_CallMethodObjArgs($obj1, $obj2) } }; ($obj1:expr, $obj2:expr, $obj3:expr) => { unsafe { crate::ffi::PyObject_CallMethodObjArgs($obj1, $obj2, $obj3) } }; } #[cfg(all(CPython, Py_3_13))] macro_rules! pydict_contains { ($obj1:expr, $obj2:expr) => { unsafe { crate::ffi::PyDict_Contains(crate::ffi::PyType_GetDict($obj1), $obj2) == 1 } }; } #[cfg(all(CPython, Py_3_12, not(Py_3_13)))] macro_rules! pydict_contains { ($obj1:expr, $obj2:expr) => { unsafe { debug_assert!((*$obj2.cast::()).hash != -1); crate::ffi::_PyDict_Contains_KnownHash( crate::ffi::PyType_GetDict($obj1), $obj2, (*$obj2.cast::()).hash, ) == 1 } }; } #[cfg(all(CPython, not(Py_3_12)))] macro_rules! pydict_contains { ($obj1:expr, $obj2:expr) => { unsafe { debug_assert!((*$obj2.cast::()).hash != -1); crate::ffi::_PyDict_Contains_KnownHash( (*$obj1).tp_dict, $obj2, (*$obj2.cast::()).hash, ) == 1 } }; } #[cfg(not(CPython))] macro_rules! pydict_contains { ($obj1:expr, $obj2:expr) => { unsafe { crate::ffi::PyDict_Contains((*$obj1).tp_dict, $obj2) == 1 } }; } #[cfg(Py_3_12)] macro_rules! use_immortal { ($op:expr) => { unsafe { $op } }; } #[cfg(not(Py_3_12))] macro_rules! use_immortal { ($op:expr) => { unsafe { ffi!(Py_INCREF($op)); $op } }; } #[cfg(all(CPython, not(Py_3_13)))] macro_rules! pydict_next { ($obj1:expr, $obj2:expr, $obj3:expr, $obj4:expr) => { unsafe { crate::ffi::_PyDict_Next($obj1, $obj2, $obj3, $obj4, core::ptr::null_mut()) } }; } #[cfg(all(CPython, Py_3_13))] macro_rules! pydict_next { ($obj1:expr, $obj2:expr, $obj3:expr, $obj4:expr) => { unsafe { crate::ffi::PyDict_Next($obj1, $obj2, $obj3, $obj4) } }; } #[cfg(not(CPython))] macro_rules! pydict_next { ($obj1:expr, $obj2:expr, $obj3:expr, $obj4:expr) => { unsafe { crate::ffi::PyDict_Next($obj1, $obj2, $obj3, $obj4) } }; } macro_rules! reserve_minimum { ($writer:expr) => { $writer.reserve(128); }; } macro_rules! reserve_pretty { ($writer:expr, $val:expr) => { $writer.reserve($val + 32); }; } macro_rules! assume { ($expr:expr) => { debug_assert!($expr); unsafe { core::hint::assert_unchecked($expr); }; }; } macro_rules! unreachable_unchecked { () => { unsafe { core::hint::unreachable_unchecked() } }; } #[inline(always)] #[allow(clippy::cast_possible_wrap)] pub(crate) fn usize_to_isize(val: usize) -> isize { debug_assert!(val < (isize::MAX as usize)); val as isize } #[inline(always)] pub(crate) fn isize_to_usize(val: isize) -> usize { debug_assert!(val >= 0); val.cast_unsigned() } ================================================ FILE: test/__init__.py ================================================ ================================================ FILE: test/requirements.txt ================================================ faker numpy;(platform_machine=="x86_64" or (platform_machine=="aarch64" and sys_platform == "linux")) and python_version<"3.15" and implementation_name=="cpython" pendulum;sys_platform=="linux" and platform_machine=="x86_64" and python_version<"3.15" and implementation_name=="cpython" psutil;(sys_platform=="linux" or sys_platform == "macos") and platform_machine=="x86_64" and python_version<"3.14" and implementation_name=="cpython" pytest python-dateutil >=2,<3;python_version<"3.15" and implementation_name=="cpython" pytz ================================================ FILE: test/test_api.py ================================================ # SPDX-License-Identifier: (Apache-2.0 OR MIT) # Copyright ijl (2018-2025), hauntsaninja (2020) import datetime import inspect import json import re import pytest import orjson SIMPLE_TYPES = (1, 1.0, -1, None, "str", True, False) LOADS_RECURSION_LIMIT = 1024 def default(obj): return str(obj) class TestApi: def test_loads_trailing(self): """ loads() handles trailing whitespace """ assert orjson.loads("{}\n\t ") == {} def test_loads_trailing_invalid(self): """ loads() handles trailing invalid """ pytest.raises(orjson.JSONDecodeError, orjson.loads, "{}\n\t a") def test_simple_json(self): """ dumps() equivalent to json on simple types """ for obj in SIMPLE_TYPES: assert orjson.dumps(obj) == json.dumps(obj).encode("utf-8") def test_simple_round_trip(self): """ dumps(), loads() round trip on simple types """ for obj in SIMPLE_TYPES: assert orjson.loads(orjson.dumps(obj)) == obj def test_loads_type(self): """ loads() invalid type """ for val in (1, 3.14, [], {}, None): # type: ignore pytest.raises(orjson.JSONDecodeError, orjson.loads, val) def test_loads_recursion_partial(self): """ loads() recursion limit partial """ pytest.raises(orjson.JSONDecodeError, orjson.loads, "[" * (1024 * 1024)) def test_loads_recursion_valid_limit_array(self): """ loads() recursion limit at limit array """ n = LOADS_RECURSION_LIMIT + 1 value = b"[" * n + b"]" * n pytest.raises(orjson.JSONDecodeError, orjson.loads, value) def test_loads_recursion_valid_limit_object(self): """ loads() recursion limit at limit object """ n = LOADS_RECURSION_LIMIT value = b'{"key":' * n + b'{"key":true}' + b"}" * n pytest.raises(orjson.JSONDecodeError, orjson.loads, value) def test_loads_recursion_valid_limit_mixed(self): """ loads() recursion limit at limit mixed """ n = LOADS_RECURSION_LIMIT value = b"".join((b"[", b'{"key":' * n, b'{"key":true}' + b"}" * n, b"]")) pytest.raises(orjson.JSONDecodeError, orjson.loads, value) def test_loads_recursion_valid_excessive_array(self): """ loads() recursion limit excessively high value """ n = 10000000 value = b"[" * n + b"]" * n pytest.raises(orjson.JSONDecodeError, orjson.loads, value) def test_loads_recursion_valid_limit_array_pretty(self): """ loads() recursion limit at limit array pretty """ n = LOADS_RECURSION_LIMIT + 1 value = b"[\n " * n + b"]" * n pytest.raises(orjson.JSONDecodeError, orjson.loads, value) def test_loads_recursion_valid_limit_object_pretty(self): """ loads() recursion limit at limit object pretty """ n = LOADS_RECURSION_LIMIT value = b'{\n "key":' * n + b'{"key":true}' + b"}" * n pytest.raises(orjson.JSONDecodeError, orjson.loads, value) def test_loads_recursion_valid_limit_mixed_pretty(self): """ loads() recursion limit at limit mixed pretty """ n = LOADS_RECURSION_LIMIT value = b'[\n {"key":' * n + b'{"key":true}' + b"}" * n + b"]" pytest.raises(orjson.JSONDecodeError, orjson.loads, value) def test_loads_recursion_valid_excessive_array_pretty(self): """ loads() recursion limit excessively high value pretty """ n = 10000000 value = b"[\n " * n + b"]" * n pytest.raises(orjson.JSONDecodeError, orjson.loads, value) def test_version(self): """ __version__ """ assert re.match(r"^\d+\.\d+(\.\d+)?$", orjson.__version__) def test_valueerror(self): """ orjson.JSONDecodeError is a subclass of ValueError """ pytest.raises(orjson.JSONDecodeError, orjson.loads, "{") pytest.raises(ValueError, orjson.loads, "{") def test_optional_none(self): """ dumps() option, default None """ assert orjson.dumps([], option=None) == b"[]" assert orjson.dumps([], default=None) == b"[]" assert orjson.dumps([], option=None, default=None) == b"[]" assert orjson.dumps([], None, None) == b"[]" def test_option_not_int(self): """ dumps() option not int or None """ with pytest.raises(orjson.JSONEncodeError): orjson.dumps(True, option=True) def test_option_invalid_int(self): """ dumps() option invalid 64-bit number """ with pytest.raises(orjson.JSONEncodeError): orjson.dumps(True, option=9223372036854775809) def test_option_range_low(self): """ dumps() option out of range low """ with pytest.raises(orjson.JSONEncodeError): orjson.dumps(True, option=-1) def test_option_range_high(self): """ dumps() option out of range high """ with pytest.raises(orjson.JSONEncodeError): orjson.dumps(True, option=1 << 12) def test_opts_multiple(self): """ dumps() multiple option """ assert ( orjson.dumps( [1, datetime.datetime(2000, 1, 1, 2, 3, 4)], option=orjson.OPT_STRICT_INTEGER | orjson.OPT_NAIVE_UTC, ) == b'[1,"2000-01-01T02:03:04+00:00"]' ) def test_default_positional(self): """ dumps() positional arg """ with pytest.raises(TypeError): orjson.dumps(__obj={}) # type: ignore with pytest.raises(TypeError): orjson.dumps(zxc={}) # type: ignore def test_default_unknown_kwarg(self): """ dumps() unknown kwarg """ with pytest.raises(TypeError): orjson.dumps({}, zxc=default) # type: ignore def test_default_empty_kwarg(self): """ dumps() empty kwarg """ assert orjson.dumps(None) == b"null" def test_default_twice(self): """ dumps() default twice """ with pytest.raises(TypeError): orjson.dumps({}, default, default=default) # type: ignore def test_option_twice(self): """ dumps() option twice """ with pytest.raises(TypeError): orjson.dumps({}, None, orjson.OPT_NAIVE_UTC, option=orjson.OPT_NAIVE_UTC) # type: ignore def test_option_mixed(self): """ dumps() option one arg, one kwarg """ class Custom: def __str__(self): return "zxc" assert ( orjson.dumps( [Custom(), datetime.datetime(2000, 1, 1, 2, 3, 4)], default, option=orjson.OPT_NAIVE_UTC, ) == b'["zxc","2000-01-01T02:03:04+00:00"]' ) def test_dumps_signature(self): """ dumps() valid __text_signature__ """ assert ( str(inspect.signature(orjson.dumps)) == "(obj, /, default=None, option=None)" ) inspect.signature(orjson.dumps).bind("str") inspect.signature(orjson.dumps).bind("str", default=default, option=1) inspect.signature(orjson.dumps).bind("str", default=None, option=None) def test_loads_signature(self): """ loads() valid __text_signature__ """ assert str(inspect.signature(orjson.loads)), "(obj == /)" inspect.signature(orjson.loads).bind("[]") def test_dumps_module_str(self): """ orjson.dumps.__module__ is a str """ assert orjson.dumps.__module__ == "orjson" def test_loads_module_str(self): """ orjson.loads.__module__ is a str """ assert orjson.loads.__module__ == "orjson" def test_bytes_buffer(self): """ dumps() trigger buffer growing where length is greater than growth """ a = "a" * 900 b = "b" * 4096 c = "c" * 4096 * 4096 assert orjson.dumps([a, b, c]) == f'["{a}","{b}","{c}"]'.encode("utf-8") def test_bytes_null_terminated(self): """ dumps() PyBytesObject buffer is null-terminated """ # would raise ValueError: invalid literal for int() with base 10: b'1596728892' int(orjson.dumps(1596728892)) ================================================ FILE: test/test_append_newline.py ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2020-2025) import orjson from .util import needs_data, read_fixture_obj class TestAppendNewline: def test_dumps_newline(self): """ dumps() OPT_APPEND_NEWLINE """ assert orjson.dumps([], option=orjson.OPT_APPEND_NEWLINE) == b"[]\n" @needs_data def test_twitter_newline(self): """ loads(),dumps() twitter.json OPT_APPEND_NEWLINE """ val = read_fixture_obj("twitter.json.xz") assert orjson.loads(orjson.dumps(val, option=orjson.OPT_APPEND_NEWLINE)) == val @needs_data def test_canada(self): """ loads(), dumps() canada.json OPT_APPEND_NEWLINE """ val = read_fixture_obj("canada.json.xz") assert orjson.loads(orjson.dumps(val, option=orjson.OPT_APPEND_NEWLINE)) == val @needs_data def test_citm_catalog_newline(self): """ loads(), dumps() citm_catalog.json OPT_APPEND_NEWLINE """ val = read_fixture_obj("citm_catalog.json.xz") assert orjson.loads(orjson.dumps(val, option=orjson.OPT_APPEND_NEWLINE)) == val @needs_data def test_github_newline(self): """ loads(), dumps() github.json OPT_APPEND_NEWLINE """ val = read_fixture_obj("github.json.xz") assert orjson.loads(orjson.dumps(val, option=orjson.OPT_APPEND_NEWLINE)) == val ================================================ FILE: test/test_buffer.py ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2025) import os import pytest import orjson ORJSON_RUNNER_MEMORY_GIB = os.getenv("ORJSON_RUNNER_MEMORY_GIB", "") @pytest.mark.skipif( not ORJSON_RUNNER_MEMORY_GIB, reason="ORJSON_RUNNER_MEMORY_GIB not defined", ) def test_memory_loads(): buffer_factor = 12 segment = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" size = ( (int(ORJSON_RUNNER_MEMORY_GIB) * 1024 * 1024 * 1024) // buffer_factor // len(segment) ) doc = "".join(segment for _ in range(size)) with pytest.raises(orjson.JSONDecodeError) as exc_info: _ = orjson.loads(doc) assert ( str(exc_info.value) == "Not enough memory to allocate buffer for parsing: line 1 column 1 (char 0)" ) ================================================ FILE: test/test_canonical.py ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2019-2022) import orjson class TestCanonicalTests: def test_dumps_ctrl_escape(self): """ dumps() ctrl characters """ assert orjson.dumps("text\u0003\r\n") == b'"text\\u0003\\r\\n"' def test_dumps_escape_quote_backslash(self): """ dumps() quote, backslash escape """ assert orjson.dumps(r'"\ test') == b'"\\"\\\\ test"' def test_dumps_escape_line_separator(self): """ dumps() U+2028, U+2029 escape """ assert ( orjson.dumps({"spaces": "\u2028 \u2029"}) == b'{"spaces":"\xe2\x80\xa8 \xe2\x80\xa9"}' ) ================================================ FILE: test/test_circular.py ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2019-2023) import pytest import orjson class TestCircular: def test_circular_dict(self): """ dumps() circular reference dict """ obj = {} # type: ignore obj["obj"] = obj with pytest.raises(orjson.JSONEncodeError): orjson.dumps(obj) def test_circular_dict_sort_keys(self): """ dumps() circular reference dict OPT_SORT_KEYS """ obj = {} # type: ignore obj["obj"] = obj with pytest.raises(orjson.JSONEncodeError): orjson.dumps(obj, option=orjson.OPT_SORT_KEYS) def test_circular_dict_non_str_keys(self): """ dumps() circular reference dict OPT_NON_STR_KEYS """ obj = {} # type: ignore obj["obj"] = obj with pytest.raises(orjson.JSONEncodeError): orjson.dumps(obj, option=orjson.OPT_NON_STR_KEYS) def test_circular_list(self): """ dumps() circular reference list """ obj = [] # type: ignore obj.append(obj) # type: ignore with pytest.raises(orjson.JSONEncodeError): orjson.dumps(obj) def test_circular_nested(self): """ dumps() circular reference nested dict, list """ obj = {} # type: ignore obj["list"] = [{"obj": obj}] with pytest.raises(orjson.JSONEncodeError): orjson.dumps(obj) def test_circular_nested_sort_keys(self): """ dumps() circular reference nested dict, list OPT_SORT_KEYS """ obj = {} # type: ignore obj["list"] = [{"obj": obj}] with pytest.raises(orjson.JSONEncodeError): orjson.dumps(obj, option=orjson.OPT_SORT_KEYS) def test_circular_nested_non_str_keys(self): """ dumps() circular reference nested dict, list OPT_NON_STR_KEYS """ obj = {} # type: ignore obj["list"] = [{"obj": obj}] with pytest.raises(orjson.JSONEncodeError): orjson.dumps(obj, option=orjson.OPT_NON_STR_KEYS) ================================================ FILE: test/test_dataclass.py ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2019-2026) import abc import uuid from dataclasses import InitVar, asdict, dataclass, field from enum import Enum from typing import ClassVar, Optional import pytest import orjson class AnEnum(Enum): ONE = 1 TWO = 2 @dataclass class EmptyDataclass: pass @dataclass class EmptyDataclassSlots: __slots__ = () @dataclass class Dataclass1: name: str number: int sub: Optional["Dataclass1"] @dataclass class Dataclass2: name: str | None = field(default="?") @dataclass class Dataclass3: a: str b: int c: dict d: bool e: float f: list g: tuple @dataclass class Dataclass4: a: str = field() b: int = field(metadata={"unrelated": False}) c: float = 1.1 @dataclass class Datasubclass(Dataclass1): additional: bool @dataclass class Slotsdataclass: __slots__ = ("_c", "a", "b", "d") a: str b: int _c: str d: InitVar[str] cls_var: ClassVar[str] = "cls" @dataclass class Defaultdataclass: a: uuid.UUID b: AnEnum @dataclass class UnsortedDataclass: c: int b: int a: int d: dict | None @dataclass class InitDataclass: a: InitVar[str] b: InitVar[str] cls_var: ClassVar[str] = "cls" ab: str = "" def __post_init__(self, a: str, b: str): self._other = 1 self.ab = f"{a} {b}" class AbstractBase(abc.ABC): @abc.abstractmethod def key(self): raise NotImplementedError @dataclass(frozen=True) class ConcreteAbc(AbstractBase): __slots__ = ("attr",) attr: float def key(self): return "dkjf" class TestDataclass: def test_dataclass(self): """ dumps() dataclass """ obj = Dataclass1("a", 1, None) assert orjson.dumps(obj) == b'{"name":"a","number":1,"sub":null}' def test_dataclass_recursive(self): """ dumps() dataclass recursive """ obj = Dataclass1("a", 1, Dataclass1("b", 2, None)) assert ( orjson.dumps(obj) == b'{"name":"a","number":1,"sub":{"name":"b","number":2,"sub":null}}' ) def test_dataclass_circular(self): """ dumps() dataclass circular """ obj1 = Dataclass1("a", 1, None) obj2 = Dataclass1("b", 2, obj1) obj1.sub = obj2 with pytest.raises(orjson.JSONEncodeError): orjson.dumps(obj1) def test_dataclass_empty(self): """ dumps() no attributes """ assert orjson.dumps(EmptyDataclass()) == b"{}" def test_dataclass_empty_slots(self): """ dumps() no attributes slots """ assert orjson.dumps(EmptyDataclassSlots()) == b"{}" def test_dataclass_default_arg(self): """ dumps() dataclass default arg """ obj = Dataclass2() assert orjson.dumps(obj) == b'{"name":"?"}' def test_dataclass_types(self): """ dumps() dataclass types """ obj = Dataclass3("a", 1, {"a": "b"}, True, 1.1, [1, 2], (3, 4)) assert ( orjson.dumps(obj) == b'{"a":"a","b":1,"c":{"a":"b"},"d":true,"e":1.1,"f":[1,2],"g":[3,4]}' ) def test_dataclass_metadata(self): """ dumps() dataclass metadata """ obj = Dataclass4("a", 1, 2.1) assert orjson.dumps(obj) == b'{"a":"a","b":1,"c":2.1}' def test_dataclass_classvar(self): """ dumps() dataclass class variable """ obj = Dataclass4("a", 1) assert orjson.dumps(obj) == b'{"a":"a","b":1,"c":1.1}' def test_dataclass_subclass(self): """ dumps() dataclass subclass """ obj = Datasubclass("a", 1, None, False) assert ( orjson.dumps(obj) == b'{"name":"a","number":1,"sub":null,"additional":false}' ) def test_dataclass_slots(self): """ dumps() dataclass with __slots__ does not include under attributes, InitVar, or ClassVar """ obj = Slotsdataclass("a", 1, "c", "d") assert "__dict__" not in dir(obj) assert orjson.dumps(obj) == b'{"a":"a","b":1}' def test_dataclass_default(self): """ dumps() dataclass with default """ def default(__obj): if isinstance(__obj, uuid.UUID): return str(__obj) elif isinstance(__obj, Enum): return __obj.value obj = Defaultdataclass( uuid.UUID("808989c0-00d5-48a8-b5c4-c804bf9032f2"), AnEnum.ONE, ) assert ( orjson.dumps(obj, default=default) == b'{"a":"808989c0-00d5-48a8-b5c4-c804bf9032f2","b":1}' ) def test_dataclass_sort(self): """ OPT_SORT_KEYS has no effect on dataclasses """ obj = UnsortedDataclass(1, 2, 3, None) assert ( orjson.dumps(obj, option=orjson.OPT_SORT_KEYS) == b'{"c":1,"b":2,"a":3,"d":null}' ) def test_dataclass_sort_sub(self): """ dataclass fast path does not prevent OPT_SORT_KEYS from cascading """ obj = UnsortedDataclass(1, 2, 3, {"f": 2, "e": 1}) assert ( orjson.dumps(obj, option=orjson.OPT_SORT_KEYS) == b'{"c":1,"b":2,"a":3,"d":{"e":1,"f":2}}' ) def test_dataclass_under(self): """ dumps() does not include under attributes, InitVar, or ClassVar """ obj = InitDataclass("zxc", "vbn") assert orjson.dumps(obj) == b'{"ab":"zxc vbn"}' def test_dataclass_option(self): """ dumps() accepts deprecated OPT_SERIALIZE_DATACLASS """ obj = Dataclass1("a", 1, None) assert ( orjson.dumps(obj, option=orjson.OPT_SERIALIZE_DATACLASS) == b'{"name":"a","number":1,"sub":null}' ) class TestDataclassPassthrough: def test_dataclass_passthrough_raise(self): """ dumps() dataclass passes to default with OPT_PASSTHROUGH_DATACLASS """ obj = Dataclass1("a", 1, None) with pytest.raises(orjson.JSONEncodeError): orjson.dumps(obj, option=orjson.OPT_PASSTHROUGH_DATACLASS) with pytest.raises(orjson.JSONEncodeError): orjson.dumps( InitDataclass("zxc", "vbn"), option=orjson.OPT_PASSTHROUGH_DATACLASS, ) def test_dataclass_passthrough_default(self): """ dumps() dataclass passes to default with OPT_PASSTHROUGH_DATACLASS """ obj = Dataclass1("a", 1, None) assert ( orjson.dumps(obj, option=orjson.OPT_PASSTHROUGH_DATACLASS, default=asdict) == b'{"name":"a","number":1,"sub":null}' ) def default(obj): if isinstance(obj, Dataclass1): return {"name": obj.name, "number": obj.number} raise TypeError assert ( orjson.dumps(obj, option=orjson.OPT_PASSTHROUGH_DATACLASS, default=default) == b'{"name":"a","number":1}' ) class TestAbstractDataclass: def test_dataclass_abc(self): obj = ConcreteAbc(1.0) assert orjson.dumps(obj) == b'{"attr":1.0}' ================================================ FILE: test/test_datetime.py ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2019-2025) import datetime import pytest import orjson try: import zoneinfo _ = zoneinfo.ZoneInfo("Europe/Amsterdam") except Exception: # ImportError,ZoneInfoNotFoundError zoneinfo = None # type: ignore try: import pytz except ImportError: pytz = None # type: ignore try: import pendulum except ImportError: pendulum = None # type: ignore try: from dateutil import tz except ImportError: tz = None # type: ignore AMSTERDAM_1937_DATETIMES = ( b'["1937-01-01T12:00:27.000087+00:20"]', # tzinfo<2022b and an example in RFC 3339 b'["1937-01-01T12:00:27.000087+00:00"]', # tzinfo>=2022b ) AMSTERDAM_1937_DATETIMES_WITH_Z = ( b'["1937-01-01T12:00:27.000087+00:20"]', b'["1937-01-01T12:00:27.000087Z"]', ) class TestDatetime: def test_datetime_naive(self): """ datetime.datetime naive prints without offset """ assert ( orjson.dumps([datetime.datetime(2000, 1, 1, 2, 3, 4, 123)]) == b'["2000-01-01T02:03:04.000123"]' ) def test_datetime_naive_utc(self): """ datetime.datetime naive with opt assumes UTC """ assert ( orjson.dumps( [datetime.datetime(2000, 1, 1, 2, 3, 4, 123)], option=orjson.OPT_NAIVE_UTC, ) == b'["2000-01-01T02:03:04.000123+00:00"]' ) def test_datetime_min(self): """ datetime.datetime min range """ assert ( orjson.dumps( [datetime.datetime(datetime.MINYEAR, 1, 1, 0, 0, 0, 0)], option=orjson.OPT_NAIVE_UTC, ) == b'["0001-01-01T00:00:00+00:00"]' ) def test_datetime_max(self): """ datetime.datetime max range """ assert ( orjson.dumps( [datetime.datetime(datetime.MAXYEAR, 12, 31, 23, 59, 50, 999999)], option=orjson.OPT_NAIVE_UTC, ) == b'["9999-12-31T23:59:50.999999+00:00"]' ) def test_datetime_three_digits(self): """ datetime.datetime three digit year """ assert ( orjson.dumps( [datetime.datetime(312, 1, 1)], option=orjson.OPT_NAIVE_UTC, ) == b'["0312-01-01T00:00:00+00:00"]' ) def test_datetime_two_digits(self): """ datetime.datetime two digit year """ assert ( orjson.dumps( [datetime.datetime(46, 1, 1)], option=orjson.OPT_NAIVE_UTC, ) == b'["0046-01-01T00:00:00+00:00"]' ) @pytest.mark.skipif(tz is None, reason="dateutil optional") def test_datetime_tz_assume(self): """ datetime.datetime tz with assume UTC uses tz """ assert ( orjson.dumps( [ datetime.datetime( 2018, 1, 1, 2, 3, 4, 0, tzinfo=tz.gettz("Asia/Shanghai"), ), ], option=orjson.OPT_NAIVE_UTC, ) == b'["2018-01-01T02:03:04+08:00"]' ) def test_datetime_timezone_utc(self): """ datetime.datetime.utc """ assert ( orjson.dumps( [ datetime.datetime( 2018, 6, 1, 2, 3, 4, 0, tzinfo=datetime.timezone.utc, ), ], ) == b'["2018-06-01T02:03:04+00:00"]' ) @pytest.mark.skipif(pytz is None, reason="pytz optional") def test_datetime_pytz_utc(self): """ pytz.UTC """ assert ( orjson.dumps([datetime.datetime(2018, 6, 1, 2, 3, 4, 0, tzinfo=pytz.UTC)]) == b'["2018-06-01T02:03:04+00:00"]' ) @pytest.mark.skipif(zoneinfo is None, reason="zoneinfo not available") def test_datetime_zoneinfo_utc(self): """ zoneinfo.ZoneInfo("UTC") """ assert ( orjson.dumps( [ datetime.datetime( 2018, 6, 1, 2, 3, 4, 0, tzinfo=zoneinfo.ZoneInfo("UTC"), ), ], ) == b'["2018-06-01T02:03:04+00:00"]' ) @pytest.mark.skipif(zoneinfo is None, reason="zoneinfo not available") def test_datetime_zoneinfo_positive(self): assert ( orjson.dumps( [ datetime.datetime( 2018, 1, 1, 2, 3, 4, 0, tzinfo=zoneinfo.ZoneInfo("Asia/Shanghai"), ), ], ) == b'["2018-01-01T02:03:04+08:00"]' ) @pytest.mark.skipif(zoneinfo is None, reason="zoneinfo not available") def test_datetime_zoneinfo_negative(self): assert ( orjson.dumps( [ datetime.datetime( 2018, 6, 1, 2, 3, 4, 0, tzinfo=zoneinfo.ZoneInfo("America/New_York"), ), ], ) == b'["2018-06-01T02:03:04-04:00"]' ) @pytest.mark.skipif(pendulum is None, reason="pendulum not installed") def test_datetime_pendulum_utc(self): """ datetime.datetime UTC """ assert ( orjson.dumps( [datetime.datetime(2018, 6, 1, 2, 3, 4, 0, tzinfo=pendulum.UTC)], ) == b'["2018-06-01T02:03:04+00:00"]' ) @pytest.mark.skipif(tz is None, reason="dateutil optional") def test_datetime_arrow_positive(self): """ datetime.datetime positive UTC """ assert ( orjson.dumps( [ datetime.datetime( 2018, 1, 1, 2, 3, 4, 0, tzinfo=tz.gettz("Asia/Shanghai"), ), ], ) == b'["2018-01-01T02:03:04+08:00"]' ) @pytest.mark.skipif(pytz is None, reason="pytz optional") def test_datetime_pytz_positive(self): """ datetime.datetime positive UTC """ assert ( orjson.dumps( [ datetime.datetime( 2018, 1, 1, 2, 3, 4, 0, tzinfo=pytz.timezone("Asia/Shanghai"), ), ], ) == b'["2018-01-01T02:03:04+08:00"]' ) @pytest.mark.skipif(pendulum is None, reason="pendulum not installed") def test_datetime_pendulum_positive(self): """ datetime.datetime positive UTC """ assert ( orjson.dumps( [ datetime.datetime( 2018, 1, 1, 2, 3, 4, 0, tzinfo=pendulum.timezone("Asia/Shanghai"), # type: ignore ), ], ) == b'["2018-01-01T02:03:04+08:00"]' ) @pytest.mark.skipif(pytz is None, reason="pytz optional") def test_datetime_pytz_negative_dst(self): """ datetime.datetime negative UTC DST """ assert ( orjson.dumps( [ datetime.datetime( 2018, 6, 1, 2, 3, 4, 0, tzinfo=pytz.timezone("America/New_York"), ), ], ) == b'["2018-06-01T02:03:04-04:00"]' ) @pytest.mark.skipif(pendulum is None, reason="pendulum not installed") def test_datetime_pendulum_negative_dst(self): """ datetime.datetime negative UTC DST """ assert ( orjson.dumps( [ datetime.datetime( 2018, 6, 1, 2, 3, 4, 0, tzinfo=pendulum.timezone("America/New_York"), # type: ignore ), ], ) == b'["2018-06-01T02:03:04-04:00"]' ) @pytest.mark.skipif(zoneinfo is None, reason="zoneinfo not available") def test_datetime_zoneinfo_negative_non_dst(self): """ datetime.datetime negative UTC non-DST """ assert ( orjson.dumps( [ datetime.datetime( 2018, 12, 1, 2, 3, 4, 0, tzinfo=zoneinfo.ZoneInfo("America/New_York"), ), ], ) == b'["2018-12-01T02:03:04-05:00"]' ) @pytest.mark.skipif(pytz is None, reason="pytz optional") def test_datetime_pytz_negative_non_dst(self): """ datetime.datetime negative UTC non-DST """ assert ( orjson.dumps( [ datetime.datetime( 2018, 12, 1, 2, 3, 4, 0, tzinfo=pytz.timezone("America/New_York"), ), ], ) == b'["2018-12-01T02:03:04-05:00"]' ) @pytest.mark.skipif(pendulum is None, reason="pendulum not installed") def test_datetime_pendulum_negative_non_dst(self): """ datetime.datetime negative UTC non-DST """ assert ( orjson.dumps( [ datetime.datetime( 2018, 12, 1, 2, 3, 4, 0, tzinfo=pendulum.timezone("America/New_York"), # type: ignore ), ], ) == b'["2018-12-01T02:03:04-05:00"]' ) @pytest.mark.skipif(zoneinfo is None, reason="zoneinfo not available") def test_datetime_zoneinfo_partial_hour(self): """ datetime.datetime UTC offset partial hour """ assert ( orjson.dumps( [ datetime.datetime( 2018, 12, 1, 2, 3, 4, 0, tzinfo=zoneinfo.ZoneInfo("Australia/Adelaide"), ), ], ) == b'["2018-12-01T02:03:04+10:30"]' ) @pytest.mark.skipif(pytz is None, reason="pytz optional") def test_datetime_pytz_partial_hour(self): """ datetime.datetime UTC offset partial hour """ assert ( orjson.dumps( [ datetime.datetime( 2018, 12, 1, 2, 3, 4, 0, tzinfo=pytz.timezone("Australia/Adelaide"), ), ], ) == b'["2018-12-01T02:03:04+10:30"]' ) @pytest.mark.skipif(pendulum is None, reason="pendulum not installed") def test_datetime_pendulum_partial_hour(self): """ datetime.datetime UTC offset partial hour """ assert ( orjson.dumps( [ datetime.datetime( 2018, 12, 1, 2, 3, 4, 0, tzinfo=pendulum.timezone("Australia/Adelaide"), # type: ignore ), ], ) == b'["2018-12-01T02:03:04+10:30"]' ) @pytest.mark.skipif(pendulum is None, reason="pendulum not installed") def test_datetime_partial_second_pendulum_supported(self): """ datetime.datetime UTC offset round seconds https://tools.ietf.org/html/rfc3339#section-5.8 """ assert ( orjson.dumps( [ datetime.datetime( 1937, 1, 1, 12, 0, 27, 87, tzinfo=pendulum.timezone("Europe/Amsterdam"), # type: ignore ), ], ) in AMSTERDAM_1937_DATETIMES ) @pytest.mark.skipif(zoneinfo is None, reason="zoneinfo not available") def test_datetime_partial_second_zoneinfo(self): """ datetime.datetime UTC offset round seconds https://tools.ietf.org/html/rfc3339#section-5.8 """ assert ( orjson.dumps( [ datetime.datetime( 1937, 1, 1, 12, 0, 27, 87, tzinfo=zoneinfo.ZoneInfo("Europe/Amsterdam"), ), ], ) in AMSTERDAM_1937_DATETIMES ) @pytest.mark.skipif(pytz is None, reason="pytz optional") def test_datetime_partial_second_pytz(self): """ datetime.datetime UTC offset round seconds https://tools.ietf.org/html/rfc3339#section-5.8 """ assert ( orjson.dumps( [ datetime.datetime( 1937, 1, 1, 12, 0, 27, 87, tzinfo=pytz.timezone("Europe/Amsterdam"), ), ], ) in AMSTERDAM_1937_DATETIMES ) @pytest.mark.skipif(tz is None, reason="dateutil optional") def test_datetime_partial_second_dateutil(self): """ datetime.datetime UTC offset round seconds https://tools.ietf.org/html/rfc3339#section-5.8 """ assert ( orjson.dumps( [ datetime.datetime( 1937, 1, 1, 12, 0, 27, 87, tzinfo=tz.gettz("Europe/Amsterdam"), ), ], ) in AMSTERDAM_1937_DATETIMES ) def test_datetime_microsecond_max(self): """ datetime.datetime microsecond max """ assert ( orjson.dumps(datetime.datetime(2000, 1, 1, 0, 0, 0, 999999)) == b'"2000-01-01T00:00:00.999999"' ) def test_datetime_microsecond_min(self): """ datetime.datetime microsecond min """ assert ( orjson.dumps(datetime.datetime(2000, 1, 1, 0, 0, 0, 1)) == b'"2000-01-01T00:00:00.000001"' ) def test_datetime_omit_microseconds(self): """ datetime.datetime OPT_OMIT_MICROSECONDS """ assert ( orjson.dumps( [datetime.datetime(2000, 1, 1, 2, 3, 4, 123)], option=orjson.OPT_OMIT_MICROSECONDS, ) == b'["2000-01-01T02:03:04"]' ) def test_datetime_omit_microseconds_naive(self): """ datetime.datetime naive OPT_OMIT_MICROSECONDS """ assert ( orjson.dumps( [datetime.datetime(2000, 1, 1, 2, 3, 4, 123)], option=orjson.OPT_NAIVE_UTC | orjson.OPT_OMIT_MICROSECONDS, ) == b'["2000-01-01T02:03:04+00:00"]' ) def test_time_omit_microseconds(self): """ datetime.time OPT_OMIT_MICROSECONDS """ assert ( orjson.dumps( [datetime.time(2, 3, 4, 123)], option=orjson.OPT_OMIT_MICROSECONDS, ) == b'["02:03:04"]' ) def test_datetime_utc_z_naive_omit(self): """ datetime.datetime naive OPT_UTC_Z """ assert ( orjson.dumps( [datetime.datetime(2000, 1, 1, 2, 3, 4, 123)], option=orjson.OPT_NAIVE_UTC | orjson.OPT_UTC_Z | orjson.OPT_OMIT_MICROSECONDS, ) == b'["2000-01-01T02:03:04Z"]' ) def test_datetime_utc_z_naive(self): """ datetime.datetime naive OPT_UTC_Z """ assert ( orjson.dumps( [datetime.datetime(2000, 1, 1, 2, 3, 4, 123)], option=orjson.OPT_NAIVE_UTC | orjson.OPT_UTC_Z, ) == b'["2000-01-01T02:03:04.000123Z"]' ) def test_datetime_utc_z_without_tz(self): """ datetime.datetime naive OPT_UTC_Z """ assert ( orjson.dumps( [datetime.datetime(2000, 1, 1, 2, 3, 4, 123)], option=orjson.OPT_UTC_Z, ) == b'["2000-01-01T02:03:04.000123"]' ) @pytest.mark.skipif(tz is None, reason="dateutil optional") def test_datetime_utc_z_with_tz(self): """ datetime.datetime naive OPT_UTC_Z """ assert ( orjson.dumps( [ datetime.datetime( 2000, 1, 1, 0, 0, 0, 1, tzinfo=datetime.timezone.utc, ), ], option=orjson.OPT_UTC_Z, ) == b'["2000-01-01T00:00:00.000001Z"]' ) assert ( orjson.dumps( [ datetime.datetime( 1937, 1, 1, 12, 0, 27, 87, tzinfo=tz.gettz("Europe/Amsterdam"), ), ], option=orjson.OPT_UTC_Z, ) in AMSTERDAM_1937_DATETIMES_WITH_Z ) @pytest.mark.skipif(pendulum is None, reason="pendulum not installed") def test_datetime_roundtrip(self): """ datetime.datetime parsed by pendulum """ obj = datetime.datetime(2000, 1, 1, 0, 0, 0, 1, tzinfo=datetime.timezone.utc) serialized = orjson.dumps(obj).decode("utf-8").replace('"', "") parsed = pendulum.parse(serialized) for attr in ("year", "month", "day", "hour", "minute", "second", "microsecond"): assert getattr(obj, attr) == getattr(parsed, attr) class TestDate: def test_date(self): """ datetime.date """ assert orjson.dumps([datetime.date(2000, 1, 13)]) == b'["2000-01-13"]' def test_date_min(self): """ datetime.date MINYEAR """ assert ( orjson.dumps([datetime.date(datetime.MINYEAR, 1, 1)]) == b'["0001-01-01"]' ) def test_date_max(self): """ datetime.date MAXYEAR """ assert ( orjson.dumps([datetime.date(datetime.MAXYEAR, 12, 31)]) == b'["9999-12-31"]' ) def test_date_three_digits(self): """ datetime.date three digit year """ assert ( orjson.dumps( [datetime.date(312, 1, 1)], ) == b'["0312-01-01"]' ) def test_date_two_digits(self): """ datetime.date two digit year """ assert ( orjson.dumps( [datetime.date(46, 1, 1)], ) == b'["0046-01-01"]' ) class TestTime: def test_time(self): """ datetime.time """ assert orjson.dumps([datetime.time(12, 15, 59, 111)]) == b'["12:15:59.000111"]' assert orjson.dumps([datetime.time(12, 15, 59)]) == b'["12:15:59"]' @pytest.mark.skipif(zoneinfo is None, reason="zoneinfo not available") def test_time_tz(self): """ datetime.time with tzinfo error """ with pytest.raises(orjson.JSONEncodeError): orjson.dumps( [ datetime.time( 12, 15, 59, 111, tzinfo=zoneinfo.ZoneInfo("Asia/Shanghai"), ), ], ) def test_time_microsecond_max(self): """ datetime.time microsecond max """ assert orjson.dumps(datetime.time(0, 0, 0, 999999)) == b'"00:00:00.999999"' def test_time_microsecond_min(self): """ datetime.time microsecond min """ assert orjson.dumps(datetime.time(0, 0, 0, 1)) == b'"00:00:00.000001"' class TestDateclassPassthrough: def test_passthrough_datetime(self): with pytest.raises(orjson.JSONEncodeError): orjson.dumps( datetime.datetime(1970, 1, 1), option=orjson.OPT_PASSTHROUGH_DATETIME, ) def test_passthrough_date(self): with pytest.raises(orjson.JSONEncodeError): orjson.dumps( datetime.date(1970, 1, 1), option=orjson.OPT_PASSTHROUGH_DATETIME, ) def test_passthrough_time(self): with pytest.raises(orjson.JSONEncodeError): orjson.dumps( datetime.time(12, 0, 0), option=orjson.OPT_PASSTHROUGH_DATETIME, ) def test_passthrough_datetime_default(self): def default(obj): return obj.strftime("%a, %d %b %Y %H:%M:%S GMT") assert ( orjson.dumps( datetime.datetime(1970, 1, 1), option=orjson.OPT_PASSTHROUGH_DATETIME, default=default, ) == b'"Thu, 01 Jan 1970 00:00:00 GMT"' ) ================================================ FILE: test/test_default.py ================================================ # SPDX-License-Identifier: (Apache-2.0 OR MIT) # Copyright ijl (2019-2025), Rami Chowdhury (2020), Marc Mueller (2023), Jack Amadeo (2023) import datetime import sys import uuid import pytest import orjson from .util import SUPPORTS_GETREFCOUNT, numpy class Custom: def __init__(self): self.name = uuid.uuid4().hex def __str__(self): return f"{self.__class__.__name__}({self.name})" class Recursive: def __init__(self, cur): self.cur = cur def default_recursive(obj): if obj.cur != 0: obj.cur -= 1 return obj return obj.cur def default_raises(obj): raise TypeError class TestType: def test_default_not_callable(self): """ dumps() default not callable """ with pytest.raises(orjson.JSONEncodeError): orjson.dumps(Custom(), default=NotImplementedError) ran = False try: orjson.dumps(Custom(), default=NotImplementedError) except Exception as err: assert isinstance(err, orjson.JSONEncodeError) assert str(err) == "default serializer exceeds recursion limit" ran = True assert ran def test_default_func(self): """ dumps() default function """ ref = Custom() def default(obj): return str(obj) assert orjson.dumps(ref, default=default) == b'"%s"' % str(ref).encode("utf-8") def test_default_func_none(self): """ dumps() default function None ok """ assert orjson.dumps(Custom(), default=lambda x: None) == b"null" def test_default_func_empty(self): """ dumps() default function no explicit return """ ref = Custom() def default(obj): if isinstance(obj, set): return list(obj) assert orjson.dumps(ref, default=default) == b"null" assert orjson.dumps({ref}, default=default) == b"[null]" def test_default_func_exc(self): """ dumps() default function raises exception """ def default(obj): raise NotImplementedError with pytest.raises(orjson.JSONEncodeError): orjson.dumps(Custom(), default=default) ran = False try: orjson.dumps(Custom(), default=default) except Exception as err: assert isinstance(err, orjson.JSONEncodeError) assert str(err) == "Type is not JSON serializable: Custom" ran = True assert ran def test_default_exception_type(self): """ dumps() TypeError in default() raises orjson.JSONEncodeError """ ref = Custom() with pytest.raises(orjson.JSONEncodeError): orjson.dumps(ref, default=default_raises) def test_default_vectorcall_str(self): """ dumps() default function vectorcall str """ class SubStr(str): pass obj = SubStr("saasa") ref = b'"%s"' % str(obj).encode("utf-8") assert ( orjson.dumps(obj, option=orjson.OPT_PASSTHROUGH_SUBCLASS, default=str) == ref ) def test_default_vectorcall_list(self): """ dumps() default function vectorcall list """ obj = {1, 2} ref = b"[1,2]" assert orjson.dumps(obj, default=list) == ref def test_default_func_nested_str(self): """ dumps() default function nested str """ ref = Custom() def default(obj): return str(obj) assert orjson.dumps({"a": ref}, default=default) == b'{"a":"%s"}' % str( ref, ).encode("utf-8") def test_default_func_list(self): """ dumps() default function nested list """ ref = Custom() def default(obj): if isinstance(obj, Custom): return [str(obj)] assert orjson.dumps({"a": ref}, default=default) == b'{"a":["%s"]}' % str( ref, ).encode("utf-8") def test_default_func_nested_list(self): """ dumps() default function list """ ref = Custom() def default(obj): return str(obj) assert orjson.dumps([ref] * 100, default=default) == b"[%s]" % b",".join( b'"%s"' % str(ref).encode("utf-8") for _ in range(100) ) def test_default_func_bytes(self): """ dumps() default function errors on non-str """ ref = Custom() def default(obj): return bytes(obj) with pytest.raises(orjson.JSONEncodeError): orjson.dumps(ref, default=default) ran = False try: orjson.dumps(ref, default=default) except Exception as err: assert isinstance(err, orjson.JSONEncodeError) assert str(err) == "Type is not JSON serializable: Custom" ran = True assert ran def test_default_func_invalid_str(self): """ dumps() default function errors on invalid str """ ref = Custom() def default(obj): return "\ud800" with pytest.raises(orjson.JSONEncodeError): orjson.dumps(ref, default=default) def test_default_lambda_ok(self): """ dumps() default lambda """ ref = Custom() assert orjson.dumps(ref, default=lambda x: str(x)) == b'"%s"' % str(ref).encode( "utf-8", ) def test_default_callable_ok(self): """ dumps() default callable """ class CustomSerializer: def __init__(self): self._cache = {} def __call__(self, obj): if obj not in self._cache: self._cache[obj] = str(obj) return self._cache[obj] ref_obj = Custom() ref_bytes = b'"%s"' % str(ref_obj).encode("utf-8") for obj in [ref_obj] * 100: assert orjson.dumps(obj, default=CustomSerializer()) == ref_bytes def test_default_recursion(self): """ dumps() default recursion limit """ assert orjson.dumps(Recursive(254), default=default_recursive) == b"0" def test_default_recursion_reset(self): """ dumps() default recursion limit reset """ assert ( orjson.dumps( [Recursive(254), {"a": "b"}, Recursive(254), Recursive(254)], default=default_recursive, ) == b'[0,{"a":"b"},0,0]' ) def test_default_recursion_infinite(self): """ dumps() default infinite recursion """ ref = Custom() def default(obj): return obj if SUPPORTS_GETREFCOUNT: refcount = sys.getrefcount(ref) with pytest.raises(orjson.JSONEncodeError): orjson.dumps(ref, default=default) if SUPPORTS_GETREFCOUNT: assert sys.getrefcount(ref) == refcount def test_reference_cleanup_default_custom_pass(self): ref = Custom() def default(obj): if isinstance(ref, Custom): return str(ref) raise TypeError if SUPPORTS_GETREFCOUNT: refcount = sys.getrefcount(ref) orjson.dumps(ref, default=default) if SUPPORTS_GETREFCOUNT: assert sys.getrefcount(ref) == refcount def test_reference_cleanup_default_custom_error(self): """ references to encoded objects are cleaned up """ ref = Custom() def default(obj): raise TypeError if SUPPORTS_GETREFCOUNT: refcount = sys.getrefcount(ref) with pytest.raises(orjson.JSONEncodeError): orjson.dumps(ref, default=default) if SUPPORTS_GETREFCOUNT: assert sys.getrefcount(ref) == refcount def test_reference_cleanup_default_subclass(self): ref = datetime.datetime(1970, 1, 1, 0, 0, 0) def default(obj): if isinstance(ref, datetime.datetime): return repr(ref) raise TypeError if SUPPORTS_GETREFCOUNT: refcount = sys.getrefcount(ref) orjson.dumps(ref, option=orjson.OPT_PASSTHROUGH_DATETIME, default=default) if SUPPORTS_GETREFCOUNT: assert sys.getrefcount(ref) == refcount def test_reference_cleanup_default_subclass_lambda(self): ref = uuid.uuid4() if SUPPORTS_GETREFCOUNT: refcount = sys.getrefcount(ref) orjson.dumps( ref, option=orjson.OPT_PASSTHROUGH_DATETIME, default=lambda val: str(val), ) if SUPPORTS_GETREFCOUNT: assert sys.getrefcount(ref) == refcount @pytest.mark.skipif(numpy is None, reason="numpy is not installed") def test_default_numpy(self): ref = numpy.array([""] * 100) # type: ignore if SUPPORTS_GETREFCOUNT: refcount = sys.getrefcount(ref) orjson.dumps( ref, option=orjson.OPT_SERIALIZE_NUMPY, default=lambda val: val.tolist(), ) if SUPPORTS_GETREFCOUNT: assert sys.getrefcount(ref) == refcount def test_default_set(self): """ dumps() default function with set """ def default(obj): if isinstance(obj, set): return list(obj) raise TypeError assert orjson.dumps({1, 2}, default=default) == b"[1,2]" ================================================ FILE: test/test_dict.py ================================================ # SPDX-License-Identifier: (Apache-2.0 OR MIT) # Copyright ijl (2018-2025), J. Nick Koston (2022), Anders Kaseorg (2022) import pytest import orjson class TestDict: def test_dict(self): """ dict """ obj = {"key": "value"} ref = '{"key":"value"}' assert orjson.dumps(obj) == ref.encode("utf-8") assert orjson.loads(ref) == obj def test_dict_duplicate_loads(self): assert orjson.loads(b'{"1":true,"1":false}') == {"1": False} def test_dict_empty(self): obj = [{"key": [{}] * 4096}] * 4096 # type:ignore assert orjson.loads(orjson.dumps(obj)) == obj def test_dict_large_dict(self): """ dict with >512 keys """ obj = {f"key_{idx}": [{}, {"a": [{}, {}, {}]}, {}] for idx in range(513)} # type: ignore assert len(obj) == 513 assert orjson.loads(orjson.dumps(obj)) == obj def test_dict_large_4096(self): """ dict with >4096 keys """ obj = {f"key_{idx}": f"value_{idx}" for idx in range(4097)} assert len(obj) == 4097 assert orjson.loads(orjson.dumps(obj)) == obj def test_dict_large_65536(self): """ dict with >65536 keys """ obj = {f"key_{idx}": f"value_{idx}" for idx in range(65537)} assert len(obj) == 65537 assert orjson.loads(orjson.dumps(obj)) == obj def test_dict_large_keys(self): """ dict with keys too large to cache """ obj = { "keeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeey": "value", } ref = '{"keeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeey":"value"}' assert orjson.dumps(obj) == ref.encode("utf-8") assert orjson.loads(ref) == obj def test_dict_unicode(self): """ dict unicode keys """ obj = {"🐈": "value"} ref = b'{"\xf0\x9f\x90\x88":"value"}' assert orjson.dumps(obj) == ref assert orjson.loads(ref) == obj assert orjson.loads(ref)["🐈"] == "value" def test_dict_invalid_key_dumps(self): """ dict invalid key dumps() """ with pytest.raises(orjson.JSONEncodeError): orjson.dumps({1: "value"}) with pytest.raises(orjson.JSONEncodeError): orjson.dumps({b"key": "value"}) def test_dict_invalid_key_loads(self): """ dict invalid key loads() """ with pytest.raises(orjson.JSONDecodeError): orjson.loads('{1:"value"}') with pytest.raises(orjson.JSONDecodeError): orjson.loads('{{"a":true}:true}') def test_dict_similar_keys(self): """ loads() similar keys This was a regression in 3.4.2 caused by using the implementation in wy instead of wyhash. """ assert orjson.loads( '{"cf_status_firefox67": "---", "cf_status_firefox57": "verified"}', ) == {"cf_status_firefox57": "verified", "cf_status_firefox67": "---"} def test_dict_pop_replace_first(self): "Test pop and replace a first key in a dict with other keys." data = {"id": "any", "other": "any"} data.pop("id") assert orjson.dumps(data) == b'{"other":"any"}' data["id"] = "new" assert orjson.dumps(data) == b'{"other":"any","id":"new"}' def test_dict_pop_replace_last(self): "Test pop and replace a last key in a dict with other keys." data = {"other": "any", "id": "any"} data.pop("id") assert orjson.dumps(data) == b'{"other":"any"}' data["id"] = "new" assert orjson.dumps(data) == b'{"other":"any","id":"new"}' def test_dict_pop(self): "Test pop and replace a key in a dict with no other keys." data = {"id": "any"} data.pop("id") assert orjson.dumps(data) == b"{}" data["id"] = "new" assert orjson.dumps(data) == b'{"id":"new"}' def test_in_place(self): "Mutate dict in-place" data = {"id": "any", "static": "msg"} data["id"] = "new" assert orjson.dumps(data) == b'{"id":"new","static":"msg"}' def test_dict_0xff(self): "dk_size <= 0xff" data = {str(idx): idx for idx in range(0xFF)} data.pop("112") data["112"] = 1 data["113"] = 2 assert orjson.loads(orjson.dumps(data)) == data def test_dict_0xff_repeated(self): "dk_size <= 0xff repeated" for _ in range(100): data = {str(idx): idx for idx in range(0xFF)} data.pop("112") data["112"] = 1 data["113"] = 2 assert orjson.loads(orjson.dumps(data)) == data def test_dict_0xffff(self): "dk_size <= 0xffff" data = {str(idx): idx for idx in range(0xFFFF)} data.pop("112") data["112"] = 1 data["113"] = 2 assert orjson.loads(orjson.dumps(data)) == data def test_dict_0xffff_repeated(self): "dk_size <= 0xffff repeated" for _ in range(100): data = {str(idx): idx for idx in range(0xFFFF)} data.pop("112") data["112"] = 1 data["113"] = 2 assert orjson.loads(orjson.dumps(data)) == data def test_dict_dict(self): class C: def __init__(self): self.a = 0 self.b = 1 assert orjson.dumps(C().__dict__) == b'{"a":0,"b":1}' ================================================ FILE: test/test_enum.py ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2020-2025) import datetime import enum import pytest import orjson class StrEnum(str, enum.Enum): AAA = "aaa" class IntEnum(int, enum.Enum): ONE = 1 class IntEnumEnum(enum.IntEnum): ONE = 1 class IntFlagEnum(enum.IntFlag): ONE = 1 class FlagEnum(enum.Flag): ONE = 1 class AutoEnum(enum.auto): A = "a" class FloatEnum(float, enum.Enum): ONE = 1.1 class Custom: def __init__(self, val): self.val = val def default(obj): if isinstance(obj, Custom): return obj.val raise TypeError class UnspecifiedEnum(enum.Enum): A = "a" B = 1 C = FloatEnum.ONE D = {"d": IntEnum.ONE} # noqa: RUF012 E = Custom("c") F = datetime.datetime(1970, 1, 1) class TestEnum: def test_cannot_subclass(self): """ enum.Enum cannot be subclassed obj->ob_type->ob_base will always be enum.EnumMeta """ with pytest.raises(TypeError): class Subclass(StrEnum): # type: ignore B = "b" def test_arbitrary_enum(self): assert orjson.dumps(UnspecifiedEnum.A) == b'"a"' assert orjson.dumps(UnspecifiedEnum.B) == b"1" assert orjson.dumps(UnspecifiedEnum.C) == b"1.1" assert orjson.dumps(UnspecifiedEnum.D) == b'{"d":1}' def test_custom_enum(self): assert orjson.dumps(UnspecifiedEnum.E, default=default) == b'"c"' def test_enum_options(self): assert ( orjson.dumps(UnspecifiedEnum.F, option=orjson.OPT_NAIVE_UTC) == b'"1970-01-01T00:00:00+00:00"' ) def test_int_enum(self): assert orjson.dumps(IntEnum.ONE) == b"1" def test_intenum_enum(self): assert orjson.dumps(IntEnumEnum.ONE) == b"1" def test_intflag_enum(self): assert orjson.dumps(IntFlagEnum.ONE) == b"1" def test_flag_enum(self): assert orjson.dumps(FlagEnum.ONE) == b"1" def test_auto_enum(self): assert orjson.dumps(AutoEnum.A) == b'"a"' def test_float_enum(self): assert orjson.dumps(FloatEnum.ONE) == b"1.1" def test_str_enum(self): assert orjson.dumps(StrEnum.AAA) == b'"aaa"' def test_bool_enum(self): with pytest.raises(TypeError): class BoolEnum(bool, enum.Enum): # type: ignore TRUE = True def test_non_str_keys_enum(self): assert ( orjson.dumps({StrEnum.AAA: 1}, option=orjson.OPT_NON_STR_KEYS) == b'{"aaa":1}' ) assert ( orjson.dumps({IntEnum.ONE: 1}, option=orjson.OPT_NON_STR_KEYS) == b'{"1":1}' ) ================================================ FILE: test/test_error.py ================================================ # SPDX-License-Identifier: (Apache-2.0 OR MIT) # Copyright ijl (2021-2025), Eric Jolibois (2021), o.ermakov (2023) import json import pytest import orjson from .util import needs_data, read_fixture_str ASCII_TEST = b"""\ { "a": "qwe", "b": "qweqwe", "c": "qweq", "d: "qwe" } """ MULTILINE_EMOJI = """[ "😊", "a" """ class TestJsonDecodeError: def _get_error_infos(self, json_decode_error_exc_info): return { k: v for k, v in json_decode_error_exc_info.value.__dict__.items() if k in ("pos", "lineno", "colno") } def _test(self, data, expected_err_infos): with pytest.raises(json.decoder.JSONDecodeError) as json_exc_info: json.loads(data) with pytest.raises(json.decoder.JSONDecodeError) as orjson_exc_info: orjson.loads(data) assert ( self._get_error_infos(json_exc_info) == self._get_error_infos(orjson_exc_info) == expected_err_infos ) def test_empty(self): with pytest.raises(orjson.JSONDecodeError) as json_exc_info: orjson.loads("") assert str(json_exc_info.value).startswith( "Input is a zero-length, empty document:", ) def test_ascii(self): self._test( ASCII_TEST, {"pos": 55, "lineno": 5, "colno": 8}, ) def test_latin1(self): self._test( """["üýþÿ", "a" """, {"pos": 13, "lineno": 1, "colno": 14}, ) def test_two_byte_str(self): self._test( """["東京", "a" """, {"pos": 11, "lineno": 1, "colno": 12}, ) def test_two_byte_bytes(self): self._test( b'["\xe6\x9d\xb1\xe4\xba\xac", "a" ', {"pos": 11, "lineno": 1, "colno": 12}, ) def test_four_byte(self): self._test( MULTILINE_EMOJI, {"pos": 19, "lineno": 4, "colno": 1}, ) @needs_data def test_tab(self): data = read_fixture_str("fail26.json", "jsonchecker") with pytest.raises(json.decoder.JSONDecodeError) as json_exc_info: json.loads(data) assert self._get_error_infos(json_exc_info) == { "pos": 5, "lineno": 1, "colno": 6, } with pytest.raises(json.decoder.JSONDecodeError) as json_exc_info: orjson.loads(data) assert self._get_error_infos(json_exc_info) == { "pos": 6, "lineno": 1, "colno": 7, } class Custom: pass class CustomException(Exception): pass def default_typeerror(obj): raise TypeError def default_notimplementederror(obj): raise NotImplementedError def default_systemerror(obj): raise SystemError def default_importerror(obj): import doesnotexist # noqa: PLC0415 assert doesnotexist CUSTOM_ERROR_MESSAGE = "zxc" def default_customerror(obj): raise CustomException(CUSTOM_ERROR_MESSAGE) class TestJsonEncodeError: def test_dumps_arg(self): with pytest.raises(orjson.JSONEncodeError) as exc_info: orjson.dumps() # type: ignore assert exc_info.type == orjson.JSONEncodeError assert ( str(exc_info.value) == "dumps() missing 1 required positional argument: 'obj'" ) assert exc_info.value.__cause__ is None def test_dumps_chain_none(self): with pytest.raises(orjson.JSONEncodeError) as exc_info: orjson.dumps(Custom()) assert exc_info.type == orjson.JSONEncodeError assert str(exc_info.value) == "Type is not JSON serializable: Custom" assert exc_info.value.__cause__ is None def test_dumps_chain_u64(self): with pytest.raises(orjson.JSONEncodeError) as exc_info: orjson.dumps([18446744073709551615, Custom()]) assert exc_info.type == orjson.JSONEncodeError assert exc_info.value.__cause__ is None def test_dumps_chain_default_typeerror(self): with pytest.raises(orjson.JSONEncodeError) as exc_info: orjson.dumps(Custom(), default=default_typeerror) assert exc_info.type == orjson.JSONEncodeError assert isinstance(exc_info.value.__cause__, TypeError) def test_dumps_chain_default_systemerror(self): with pytest.raises(orjson.JSONEncodeError) as exc_info: orjson.dumps(Custom(), default=default_systemerror) assert exc_info.type == orjson.JSONEncodeError assert isinstance(exc_info.value.__cause__, SystemError) def test_dumps_chain_default_importerror(self): with pytest.raises(orjson.JSONEncodeError) as exc_info: orjson.dumps(Custom(), default=default_importerror) assert exc_info.type == orjson.JSONEncodeError assert isinstance(exc_info.value.__cause__, ImportError) def test_dumps_chain_default_customerror(self): with pytest.raises(orjson.JSONEncodeError) as exc_info: orjson.dumps(Custom(), default=default_customerror) assert exc_info.type == orjson.JSONEncodeError assert isinstance(exc_info.value.__cause__, CustomException) assert str(exc_info.value.__cause__) == CUSTOM_ERROR_MESSAGE def test_dumps_normalize_exception(self): with pytest.raises(orjson.JSONEncodeError) as exc_info: orjson.dumps(10**60) assert exc_info.type == orjson.JSONEncodeError ================================================ FILE: test/test_escape.py ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2025) import orjson def test_issue565(): assert ( orjson.dumps("\n\r\u000b\f\u001c\u001d\u001e") == b'"\\n\\r\\u000b\\f\\u001c\\u001d\\u001e"' ) def test_0x00(): assert orjson.dumps("\u0000") == b'"\\u0000"' def test_0x01(): assert orjson.dumps("\u0001") == b'"\\u0001"' def test_0x02(): assert orjson.dumps("\u0002") == b'"\\u0002"' def test_0x03(): assert orjson.dumps("\u0003") == b'"\\u0003"' def test_0x04(): assert orjson.dumps("\u0004") == b'"\\u0004"' def test_0x05(): assert orjson.dumps("\u0005") == b'"\\u0005"' def test_0x06(): assert orjson.dumps("\u0006") == b'"\\u0006"' def test_0x07(): assert orjson.dumps("\u0007") == b'"\\u0007"' def test_0x08(): assert orjson.dumps("\u0008") == b'"\\b"' def test_0x09(): assert orjson.dumps("\u0009") == b'"\\t"' def test_0x0a(): assert orjson.dumps("\u000a") == b'"\\n"' def test_0x0b(): assert orjson.dumps("\u000b") == b'"\\u000b"' def test_0x0c(): assert orjson.dumps("\u000c") == b'"\\f"' def test_0x0d(): assert orjson.dumps("\u000d") == b'"\\r"' def test_0x0e(): assert orjson.dumps("\u000e") == b'"\\u000e"' def test_0x0f(): assert orjson.dumps("\u000f") == b'"\\u000f"' def test_0x10(): assert orjson.dumps("\u0010") == b'"\\u0010"' def test_0x11(): assert orjson.dumps("\u0011") == b'"\\u0011"' def test_0x12(): assert orjson.dumps("\u0012") == b'"\\u0012"' def test_0x13(): assert orjson.dumps("\u0013") == b'"\\u0013"' def test_0x14(): assert orjson.dumps("\u0014") == b'"\\u0014"' def test_0x15(): assert orjson.dumps("\u0015") == b'"\\u0015"' def test_0x16(): assert orjson.dumps("\u0016") == b'"\\u0016"' def test_0x17(): assert orjson.dumps("\u0017") == b'"\\u0017"' def test_0x18(): assert orjson.dumps("\u0018") == b'"\\u0018"' def test_0x19(): assert orjson.dumps("\u0019") == b'"\\u0019"' def test_0x1a(): assert orjson.dumps("\u001a") == b'"\\u001a"' def test_backslash(): assert orjson.dumps("\\") == b'"\\\\"' def test_quote(): assert orjson.dumps('"') == b'"\\""' ================================================ FILE: test/test_fake.py ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2023-2025) import random import pytest import orjson try: from faker import Faker except ImportError: Faker = None # type: ignore NUM_LOOPS = 10 NUM_SHUFFLES = 10 NUM_ENTRIES = 250 FAKER_LOCALES = [ "ar_AA", "fi_FI", "fil_PH", "he_IL", "ja_JP", "th_TH", "tr_TR", "uk_UA", "vi_VN", ] class TestFaker: @pytest.mark.skipif(Faker is None, reason="faker not available") def test_faker(self): fake = Faker(FAKER_LOCALES) profile_keys = list( set(fake.profile().keys()) - {"birthdate", "current_location"}, ) for _ in range(NUM_LOOPS): data = [ { "person": fake.profile(profile_keys), "emoji": fake.emoji(), "text": fake.paragraphs(), } for _ in range(NUM_ENTRIES) ] for _ in range(NUM_SHUFFLES): random.shuffle(data) output = orjson.dumps(data) assert orjson.loads(output) == data ================================================ FILE: test/test_fixture.py ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2018-2025) import pytest import orjson from .util import needs_data, read_fixture_bytes, read_fixture_str @needs_data class TestFixture: def test_twitter(self): """ loads(),dumps() twitter.json """ val = read_fixture_str("twitter.json.xz") read = orjson.loads(val) assert orjson.loads(orjson.dumps(read)) == read @needs_data def test_canada(self): """ loads(), dumps() canada.json """ val = read_fixture_str("canada.json.xz") read = orjson.loads(val) assert orjson.loads(orjson.dumps(read)) == read def test_citm_catalog(self): """ loads(), dumps() citm_catalog.json """ val = read_fixture_str("citm_catalog.json.xz") read = orjson.loads(val) assert orjson.loads(orjson.dumps(read)) == read def test_github(self): """ loads(), dumps() github.json """ val = read_fixture_str("github.json.xz") read = orjson.loads(val) assert orjson.loads(orjson.dumps(read)) == read def test_blns(self): """ loads() blns.json JSONDecodeError https://github.com/minimaxir/big-list-of-naughty-strings """ val = read_fixture_bytes("blns.txt.xz") for line in val.split(b"\n"): if line and not line.startswith(b"#"): with pytest.raises(orjson.JSONDecodeError): _ = orjson.loads(b'"' + val + b'"') ================================================ FILE: test/test_fragment.py ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2023-2025) import pytest import orjson try: import pandas as pd except ImportError: pd = None # type: ignore from .util import needs_data, read_fixture_bytes class TestFragment: def test_fragment_fragment_eq(self): assert orjson.Fragment(b"{}") != orjson.Fragment(b"{}") def test_fragment_fragment_not_mut(self): fragment = orjson.Fragment(b"{}") with pytest.raises(AttributeError): fragment.contents = b"[]" assert orjson.dumps(fragment) == b"{}" def test_fragment_repr(self): assert repr(orjson.Fragment(b"{}")).startswith("?","hex":"ģ䕧覫췯ꯍ\uef4a","true":true,"false":false,"null":null,"array":[],"object":{},"address":"50 St. James Street","url":"http://www.JSON.org/","comment":"// /* */":" "," s p a c e d ":[1,2,3,4,5,6,7],"compact":[1,2,3,4,5,6,7],"jsontext":"{\\"object with 1 member\\":[\\"array with 1 element\\"]}","quotes":"" \\" %22 0x22 034 "","/\\\\\\"쫾몾ꮘﳞ볚\uef4a\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?":"A key can be any string"},0.5,98.6,99.44,1066,10.0,1.0,0.1,1.0,2.0,2.0,"rosebud"]'.encode() @needs_data class TestJsonChecker: def _run_fail_json(self, filename, exc=orjson.JSONDecodeError): data = read_fixture_str(filename, "jsonchecker") pytest.raises(exc, orjson.loads, data) def _run_pass_json(self, filename, match=""): data = read_fixture_str(filename, "jsonchecker") assert orjson.dumps(orjson.loads(data)) == match def test_fail01(self): """ fail01.json """ self._run_pass_json( "fail01.json", b'"A JSON payload should be an object or array, not a string."', ) def test_fail02(self): """ fail02.json """ self._run_fail_json("fail02.json", orjson.JSONDecodeError) # EOF def test_fail03(self): """ fail03.json """ self._run_fail_json("fail03.json") def test_fail04(self): """ fail04.json """ self._run_fail_json("fail04.json") def test_fail05(self): """ fail05.json """ self._run_fail_json("fail05.json") def test_fail06(self): """ fail06.json """ self._run_fail_json("fail06.json") def test_fail07(self): """ fail07.json """ self._run_fail_json("fail07.json") def test_fail08(self): """ fail08.json """ self._run_fail_json("fail08.json") def test_fail09(self): """ fail09.json """ self._run_fail_json("fail09.json") def test_fail10(self): """ fail10.json """ self._run_fail_json("fail10.json") def test_fail11(self): """ fail11.json """ self._run_fail_json("fail11.json") def test_fail12(self): """ fail12.json """ self._run_fail_json("fail12.json") def test_fail13(self): """ fail13.json """ self._run_fail_json("fail13.json") def test_fail14(self): """ fail14.json """ self._run_fail_json("fail14.json") def test_fail15(self): """ fail15.json """ self._run_fail_json("fail15.json") def test_fail16(self): """ fail16.json """ self._run_fail_json("fail16.json") def test_fail17(self): """ fail17.json """ self._run_fail_json("fail17.json") def test_fail18(self): """ fail18.json """ self._run_pass_json( "fail18.json", b'[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]', ) def test_fail19(self): """ fail19.json """ self._run_fail_json("fail19.json") def test_fail20(self): """ fail20.json """ self._run_fail_json("fail20.json") def test_fail21(self): """ fail21.json """ self._run_fail_json("fail21.json") def test_fail22(self): """ fail22.json """ self._run_fail_json("fail22.json") def test_fail23(self): """ fail23.json """ self._run_fail_json("fail23.json") def test_fail24(self): """ fail24.json """ self._run_fail_json("fail24.json") def test_fail25(self): """ fail25.json """ self._run_fail_json("fail25.json") def test_fail26(self): """ fail26.json """ self._run_fail_json("fail26.json") def test_fail27(self): """ fail27.json """ self._run_fail_json("fail27.json") def test_fail28(self): """ fail28.json """ self._run_fail_json("fail28.json") def test_fail29(self): """ fail29.json """ self._run_fail_json("fail29.json") def test_fail30(self): """ fail30.json """ self._run_fail_json("fail30.json") def test_fail31(self): """ fail31.json """ self._run_fail_json("fail31.json") def test_fail32(self): """ fail32.json """ self._run_fail_json("fail32.json", orjson.JSONDecodeError) # EOF def test_fail33(self): """ fail33.json """ self._run_fail_json("fail33.json") def test_pass01(self): """ pass01.json """ self._run_pass_json("pass01.json", PATTERN_1) def test_pass02(self): """ pass02.json """ self._run_pass_json( "pass02.json", b'[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]', ) def test_pass03(self): """ pass03.json """ self._run_pass_json( "pass03.json", b'{"JSON Test Pattern pass3":{"The outermost value":"must be ' b'an object or array.","In this test":"It is an object."}}', ) ================================================ FILE: test/test_memory.py ================================================ # SPDX-License-Identifier: (Apache-2.0 OR MIT) # Copyright ijl (2019-2026), Rami Chowdhury (2020) import dataclasses import datetime import gc import random from .util import SUPPORTS_MEMORYVIEW, numpy, pandas try: import pytz except ImportError: pytz = None # type: ignore try: import psutil except ImportError: psutil = None # type: ignore import pytest import orjson from .util import IS_FREETHREADING FIXTURE = '{"a":[81891289, 8919812.190129012], "b": false, "c": null, "d": "東京"}' def default(obj): return str(obj) @dataclasses.dataclass class Member: id: int active: bool @dataclasses.dataclass class Object: id: int updated_at: datetime.datetime name: str members: list[Member] DATACLASS_FIXTURE = [ Object( i, datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(seconds=random.randint(0, 10000)), str(i) * 3, [Member(j, True) for j in range(10)], ) for i in range(100000, 101000) ] MAX_INCREASE = 4194304 # 4MiB if IS_FREETHREADING: MAX_INCREASE *= 4 class Unsupported: pass class TestMemory: @pytest.mark.skipif(psutil is None, reason="psutil not installed") def test_memory_loads(self): """ loads() memory leak """ proc = psutil.Process() gc.collect() val = orjson.loads(FIXTURE) assert val mem = proc.memory_info().rss for _ in range(10000): val = orjson.loads(FIXTURE) assert val gc.collect() assert proc.memory_info().rss <= mem + MAX_INCREASE @pytest.mark.skipif(psutil is None, reason="psutil not installed") @pytest.mark.skipif(SUPPORTS_MEMORYVIEW is False, reason="memoryview") def test_memory_loads_memoryview(self): """ loads() memory leak using memoryview """ proc = psutil.Process() gc.collect() fixture = FIXTURE.encode("utf-8") val = orjson.loads(fixture) assert val mem = proc.memory_info().rss for _ in range(10000): val = orjson.loads(memoryview(fixture)) assert val gc.collect() assert proc.memory_info().rss <= mem + MAX_INCREASE @pytest.mark.skipif(psutil is None, reason="psutil not installed") def test_memory_dumps(self): """ dumps() memory leak """ proc = psutil.Process() gc.collect() fixture = orjson.loads(FIXTURE) val = orjson.dumps(fixture) assert val mem = proc.memory_info().rss for _ in range(10000): val = orjson.dumps(fixture) assert val gc.collect() assert proc.memory_info().rss <= mem + MAX_INCREASE assert proc.memory_info().rss <= mem + MAX_INCREASE @pytest.mark.skipif(psutil is None, reason="psutil not installed") def test_memory_loads_exc(self): """ loads() memory leak exception without a GC pause """ proc = psutil.Process() gc.disable() mem = proc.memory_info().rss n = 10000 i = 0 for _ in range(n): try: orjson.loads("") except orjson.JSONDecodeError: i += 1 assert n == i assert proc.memory_info().rss <= mem + MAX_INCREASE gc.enable() @pytest.mark.skipif(psutil is None, reason="psutil not installed") def test_memory_dumps_exc(self): """ dumps() memory leak exception without a GC pause """ proc = psutil.Process() gc.disable() data = Unsupported() mem = proc.memory_info().rss n = 10000 i = 0 for _ in range(n): try: orjson.dumps(data) except orjson.JSONEncodeError: i += 1 assert n == i assert proc.memory_info().rss <= mem + MAX_INCREASE gc.enable() @pytest.mark.skipif(psutil is None, reason="psutil not installed") def test_memory_dumps_default(self): """ dumps() default memory leak """ proc = psutil.Process() gc.collect() fixture = orjson.loads(FIXTURE) class Custom: def __init__(self, name): self.name = name def __str__(self): return f"{self.__class__.__name__}({self.name})" fixture["custom"] = Custom("orjson") val = orjson.dumps(fixture, default=default) mem = proc.memory_info().rss for _ in range(10000): val = orjson.dumps(fixture, default=default) assert val gc.collect() assert proc.memory_info().rss <= mem + MAX_INCREASE @pytest.mark.skipif(psutil is None, reason="psutil not installed") def test_memory_dumps_dataclass(self): """ dumps() dataclass memory leak """ proc = psutil.Process() gc.collect() val = orjson.dumps(DATACLASS_FIXTURE) assert val mem = proc.memory_info().rss for _ in range(100): val = orjson.dumps(DATACLASS_FIXTURE) assert val assert val gc.collect() assert proc.memory_info().rss <= mem + MAX_INCREASE @pytest.mark.skipif( psutil is None or pytz is None, reason="psutil not installed", ) def test_memory_dumps_pytz_tzinfo(self): """ dumps() pytz tzinfo memory leak """ proc = psutil.Process() gc.collect() dt = datetime.datetime.now() val = orjson.dumps(pytz.UTC.localize(dt)) assert val mem = proc.memory_info().rss for _ in range(50000): val = orjson.dumps(pytz.UTC.localize(dt)) assert val assert val gc.collect() assert proc.memory_info().rss <= mem + MAX_INCREASE @pytest.mark.skipif(psutil is None, reason="psutil not installed") def test_memory_loads_keys(self): """ loads() memory leak with number of keys causing cache eviction """ proc = psutil.Process() gc.collect() fixture = {f"key_{idx}": "value" for idx in range(1024)} assert len(fixture) == 1024 val = orjson.dumps(fixture) loaded = orjson.loads(val) assert loaded mem = proc.memory_info().rss for _ in range(100): loaded = orjson.loads(val) assert loaded gc.collect() assert proc.memory_info().rss <= mem + MAX_INCREASE @pytest.mark.skipif(psutil is None, reason="psutil not installed") @pytest.mark.skipif(numpy is None, reason="numpy is not installed") def test_memory_dumps_numpy(self): """ dumps() numpy memory leak """ proc = psutil.Process() gc.collect() fixture = numpy.random.rand(4, 4, 4) # type: ignore val = orjson.dumps(fixture, option=orjson.OPT_SERIALIZE_NUMPY) assert val mem = proc.memory_info().rss for _ in range(100): val = orjson.dumps(fixture, option=orjson.OPT_SERIALIZE_NUMPY) assert val assert val gc.collect() assert proc.memory_info().rss <= mem + MAX_INCREASE @pytest.mark.skipif(psutil is None, reason="psutil not installed") @pytest.mark.skipif(pandas is None, reason="pandas is not installed") def test_memory_dumps_pandas(self): """ dumps() pandas memory leak """ proc = psutil.Process() gc.collect() numpy.random.rand(4, 4, 4) # type: ignore df = pandas.Series(numpy.random.rand(4, 4, 4).tolist()) # type: ignore val = df.map(orjson.dumps) assert not val.empty mem = proc.memory_info().rss for _ in range(100): val = df.map(orjson.dumps) assert not val.empty gc.collect() assert proc.memory_info().rss <= mem + MAX_INCREASE @pytest.mark.skipif(psutil is None, reason="psutil not installed") def test_memory_dumps_fragment(self): """ dumps() Fragment memory leak """ proc = psutil.Process() gc.collect() orjson.dumps(orjson.Fragment(str(0))) mem = proc.memory_info().rss for i in range(10000): orjson.dumps(orjson.Fragment(str(i))) gc.collect() assert proc.memory_info().rss <= mem + MAX_INCREASE ================================================ FILE: test/test_non_str_keys.py ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2020-2025) import dataclasses import datetime import uuid import pytest import orjson try: import pytz except ImportError: pytz = None # type: ignore from .util import numpy class SubStr(str): pass class TestNonStrKeyTests: def test_dict_keys_duplicate(self): """ OPT_NON_STR_KEYS serializes duplicate keys """ assert ( orjson.dumps({"1": True, 1: False}, option=orjson.OPT_NON_STR_KEYS) == b'{"1":true,"1":false}' ) def test_dict_keys_int(self): assert ( orjson.dumps({1: True, 2: False}, option=orjson.OPT_NON_STR_KEYS) == b'{"1":true,"2":false}' ) def test_dict_keys_substr(self): assert ( orjson.dumps({SubStr("aaa"): True}, option=orjson.OPT_NON_STR_KEYS) == b'{"aaa":true}' ) def test_dict_keys_substr_passthrough(self): """ OPT_PASSTHROUGH_SUBCLASS does not affect OPT_NON_STR_KEYS """ assert ( orjson.dumps( {SubStr("aaa"): True}, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_PASSTHROUGH_SUBCLASS, ) == b'{"aaa":true}' ) def test_dict_keys_substr_invalid(self): with pytest.raises(orjson.JSONEncodeError): orjson.dumps({SubStr("\ud800"): True}, option=orjson.OPT_NON_STR_KEYS) def test_dict_keys_strict(self): """ OPT_NON_STR_KEYS does not respect OPT_STRICT_INTEGER """ assert ( orjson.dumps( {9223372036854775807: True}, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_STRICT_INTEGER, ) == b'{"9223372036854775807":true}' ) def test_dict_keys_int_range_valid_i64(self): """ OPT_NON_STR_KEYS has a i64 range for int, valid """ assert ( orjson.dumps( {9223372036854775807: True}, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_STRICT_INTEGER, ) == b'{"9223372036854775807":true}' ) assert ( orjson.dumps( {-9223372036854775807: True}, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_STRICT_INTEGER, ) == b'{"-9223372036854775807":true}' ) assert ( orjson.dumps( {9223372036854775809: True}, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_STRICT_INTEGER, ) == b'{"9223372036854775809":true}' ) def test_dict_keys_int_range_valid_u64(self): """ OPT_NON_STR_KEYS has a u64 range for int, valid """ assert ( orjson.dumps( {0: True}, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_STRICT_INTEGER, ) == b'{"0":true}' ) assert ( orjson.dumps( {18446744073709551615: True}, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_STRICT_INTEGER, ) == b'{"18446744073709551615":true}' ) def test_dict_keys_int_range_invalid(self): """ OPT_NON_STR_KEYS has a range of i64::MIN to u64::MAX """ with pytest.raises(orjson.JSONEncodeError): orjson.dumps({-9223372036854775809: True}, option=orjson.OPT_NON_STR_KEYS) with pytest.raises(orjson.JSONEncodeError): orjson.dumps({18446744073709551616: True}, option=orjson.OPT_NON_STR_KEYS) def test_dict_keys_float(self): assert ( orjson.dumps({1.1: True, 2.2: False}, option=orjson.OPT_NON_STR_KEYS) == b'{"1.1":true,"2.2":false}' ) def test_dict_keys_inf(self): assert ( orjson.dumps({float("Infinity"): True}, option=orjson.OPT_NON_STR_KEYS) == b'{"null":true}' ) assert ( orjson.dumps({float("-Infinity"): True}, option=orjson.OPT_NON_STR_KEYS) == b'{"null":true}' ) def test_dict_keys_nan(self): assert ( orjson.dumps({float("NaN"): True}, option=orjson.OPT_NON_STR_KEYS) == b'{"null":true}' ) def test_dict_keys_bool(self): assert ( orjson.dumps({True: True, False: False}, option=orjson.OPT_NON_STR_KEYS) == b'{"true":true,"false":false}' ) def test_dict_keys_datetime(self): assert ( orjson.dumps( {datetime.datetime(2000, 1, 1, 2, 3, 4, 123): True}, option=orjson.OPT_NON_STR_KEYS, ) == b'{"2000-01-01T02:03:04.000123":true}' ) def test_dict_keys_datetime_opt(self): assert ( orjson.dumps( {datetime.datetime(2000, 1, 1, 2, 3, 4, 123): True}, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_OMIT_MICROSECONDS | orjson.OPT_NAIVE_UTC | orjson.OPT_UTC_Z, ) == b'{"2000-01-01T02:03:04Z":true}' ) def test_dict_keys_datetime_passthrough(self): """ OPT_PASSTHROUGH_DATETIME does not affect OPT_NON_STR_KEYS """ assert ( orjson.dumps( {datetime.datetime(2000, 1, 1, 2, 3, 4, 123): True}, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_PASSTHROUGH_DATETIME, ) == b'{"2000-01-01T02:03:04.000123":true}' ) def test_dict_keys_uuid(self): """ OPT_NON_STR_KEYS always serializes UUID as keys """ assert ( orjson.dumps( {uuid.UUID("7202d115-7ff3-4c81-a7c1-2a1f067b1ece"): True}, option=orjson.OPT_NON_STR_KEYS, ) == b'{"7202d115-7ff3-4c81-a7c1-2a1f067b1ece":true}' ) def test_dict_keys_date(self): assert ( orjson.dumps( {datetime.date(1970, 1, 1): True}, option=orjson.OPT_NON_STR_KEYS, ) == b'{"1970-01-01":true}' ) def test_dict_keys_time(self): assert ( orjson.dumps( {datetime.time(12, 15, 59, 111): True}, option=orjson.OPT_NON_STR_KEYS, ) == b'{"12:15:59.000111":true}' ) def test_dict_non_str_and_sort_keys(self): assert ( orjson.dumps( { "other": 1, datetime.date(1970, 1, 5): 2, datetime.date(1970, 1, 3): 3, }, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SORT_KEYS, ) == b'{"1970-01-03":3,"1970-01-05":2,"other":1}' ) @pytest.mark.skipif(pytz is None, reason="pytz optional") def test_dict_keys_time_err(self): """ OPT_NON_STR_KEYS propagates errors in types """ val = datetime.time(12, 15, 59, 111, tzinfo=pytz.timezone("Asia/Shanghai")) with pytest.raises(orjson.JSONEncodeError): orjson.dumps({val: True}, option=orjson.OPT_NON_STR_KEYS) def test_dict_keys_str(self): assert ( orjson.dumps({"1": True}, option=orjson.OPT_NON_STR_KEYS) == b'{"1":true}' ) def test_dict_keys_type(self): class Obj: a: str val = Obj() with pytest.raises(orjson.JSONEncodeError): orjson.dumps({val: True}, option=orjson.OPT_NON_STR_KEYS) @pytest.mark.skipif(numpy is None, reason="numpy is not installed") def test_dict_keys_array(self): with pytest.raises(TypeError): _ = {numpy.array([1, 2]): True} # type: ignore def test_dict_keys_dataclass(self): @dataclasses.dataclass class Dataclass: a: str with pytest.raises(TypeError): _ = {Dataclass("a"): True} def test_dict_keys_dataclass_hash(self): @dataclasses.dataclass class Dataclass: a: str def __hash__(self): return 1 obj = {Dataclass("a"): True} with pytest.raises(orjson.JSONEncodeError): orjson.dumps(obj, option=orjson.OPT_NON_STR_KEYS) def test_dict_keys_list(self): with pytest.raises(TypeError): _ = {[]: True} def test_dict_keys_dict(self): with pytest.raises(TypeError): _ = {{}: True} def test_dict_keys_tuple(self): obj = {(): True} with pytest.raises(orjson.JSONEncodeError): orjson.dumps(obj, option=orjson.OPT_NON_STR_KEYS) def test_dict_keys_unknown(self): with pytest.raises(orjson.JSONEncodeError): orjson.dumps({frozenset(): True}, option=orjson.OPT_NON_STR_KEYS) def test_dict_keys_no_str_call(self): class Obj: a: str def __str__(self): return "Obj" val = Obj() with pytest.raises(orjson.JSONEncodeError): orjson.dumps({val: True}, option=orjson.OPT_NON_STR_KEYS) ================================================ FILE: test/test_numpy.py ================================================ # SPDX-License-Identifier: (Apache-2.0 OR MIT) # Copyright ijl (2020-2026), Ben Sully (2021), Nazar Kostetskyi (2022), Aviram Hassan (2020-2021), Marco Ribeiro (2020), Eric Jolibois (2021) # mypy: ignore-errors import sys import pytest import orjson from .util import numpy def numpy_default(obj): if isinstance(obj, numpy.ndarray): return obj.tolist() raise TypeError @pytest.mark.skipif(numpy is None, reason="numpy is not installed") class TestNumpy: def test_numpy_array_d1_uintp(self): low = numpy.iinfo(numpy.uintp).min high = numpy.iinfo(numpy.uintp).max assert orjson.dumps( numpy.array([low, high], numpy.uintp), option=orjson.OPT_SERIALIZE_NUMPY, ) == f"[{low},{high}]".encode("ascii") def test_numpy_array_d1_intp(self): low = numpy.iinfo(numpy.intp).min high = numpy.iinfo(numpy.intp).max assert orjson.dumps( numpy.array([low, high], numpy.intp), option=orjson.OPT_SERIALIZE_NUMPY, ) == f"[{low},{high}]".encode("ascii") def test_numpy_array_d1_i64(self): assert ( orjson.dumps( numpy.array([-9223372036854775807, 9223372036854775807], numpy.int64), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"[-9223372036854775807,9223372036854775807]" ) def test_numpy_array_d1_u64(self): assert ( orjson.dumps( numpy.array([0, 18446744073709551615], numpy.uint64), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"[0,18446744073709551615]" ) def test_numpy_array_d1_i8(self): assert ( orjson.dumps( numpy.array([-128, 127], numpy.int8), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"[-128,127]" ) def test_numpy_array_d1_u8(self): assert ( orjson.dumps( numpy.array([0, 255], numpy.uint8), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"[0,255]" ) def test_numpy_array_d1_i32(self): assert ( orjson.dumps( numpy.array([-2147483647, 2147483647], numpy.int32), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"[-2147483647,2147483647]" ) def test_numpy_array_d1_i16(self): assert ( orjson.dumps( numpy.array([-32768, 32767], numpy.int16), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"[-32768,32767]" ) def test_numpy_array_d1_u16(self): assert ( orjson.dumps( numpy.array([0, 65535], numpy.uint16), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"[0,65535]" ) def test_numpy_array_d1_u32(self): assert ( orjson.dumps( numpy.array([0, 4294967295], numpy.uint32), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"[0,4294967295]" ) def test_numpy_array_d1_f32(self): assert ( orjson.dumps( numpy.array([1.0, 3.4028235e38], numpy.float32), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"[1.0,3.4028235e+38]" ) def test_numpy_array_d1_f16(self): assert ( orjson.dumps( numpy.array([-1.0, 0.0009765625, 1.0, 65504.0], numpy.float16), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"[-1.0,0.0009765625,1.0,65504.0]" ) def test_numpy_array_f16_roundtrip(self): ref = [ -1.0, -2.0, 0.000000059604645, 0.000060975552, 0.00006103515625, 0.0009765625, 0.33325195, 0.99951172, 1.0, 1.00097656, 65504.0, ] obj = numpy.array(ref, numpy.float16) # type: ignore serialized = orjson.dumps( obj, option=orjson.OPT_SERIALIZE_NUMPY, ) deserialized = numpy.array(orjson.loads(serialized), numpy.float16) # type: ignore assert numpy.array_equal(obj, deserialized) def test_numpy_array_f16_edge(self): assert ( orjson.dumps( numpy.array( [ numpy.inf, -numpy.inf, numpy.nan, -0.0, 0.0, numpy.pi, ], numpy.float16, ), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"[null,null,null,-0.0,0.0,3.140625]" ) def test_numpy_array_f32_edge(self): assert ( orjson.dumps( numpy.array( [ numpy.inf, -numpy.inf, numpy.nan, -0.0, 0.0, numpy.pi, ], numpy.float32, ), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"[null,null,null,-0.0,0.0,3.1415927]" ) def test_numpy_array_f64_edge(self): assert ( orjson.dumps( numpy.array( [ numpy.inf, -numpy.inf, numpy.nan, -0.0, 0.0, numpy.pi, ], numpy.float64, ), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"[null,null,null,-0.0,0.0,3.141592653589793]" ) def test_numpy_array_d1_f64(self): assert ( orjson.dumps( numpy.array([1.0, 1.7976931348623157e308], numpy.float64), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"[1.0,1.7976931348623157e+308]" ) def test_numpy_array_d1_bool(self): assert ( orjson.dumps( numpy.array([True, False, False, True]), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"[true,false,false,true]" ) def test_numpy_array_d1_datetime64_years(self): assert ( orjson.dumps( numpy.array( [ numpy.datetime64("1"), numpy.datetime64("970"), numpy.datetime64("1920"), numpy.datetime64("1971"), numpy.datetime64("2021"), numpy.datetime64("2022"), numpy.datetime64("2023"), numpy.datetime64("9999"), ], ), option=orjson.OPT_SERIALIZE_NUMPY, ) == b'["0001-01-01T00:00:00","0970-01-01T00:00:00","1920-01-01T00:00:00","1971-01-01T00:00:00","2021-01-01T00:00:00","2022-01-01T00:00:00","2023-01-01T00:00:00","9999-01-01T00:00:00"]' ) def test_numpy_array_d1_datetime64_months(self): assert ( orjson.dumps( numpy.array( [ numpy.datetime64("2021-01"), numpy.datetime64("2022-01"), numpy.datetime64("2023-01"), ], ), option=orjson.OPT_SERIALIZE_NUMPY, ) == b'["2021-01-01T00:00:00","2022-01-01T00:00:00","2023-01-01T00:00:00"]' ) def test_numpy_array_d1_datetime64_days(self): assert ( orjson.dumps( numpy.array( [ numpy.datetime64("2021-01-01"), numpy.datetime64("2021-01-01"), numpy.datetime64("2021-01-01"), ], ), option=orjson.OPT_SERIALIZE_NUMPY, ) == b'["2021-01-01T00:00:00","2021-01-01T00:00:00","2021-01-01T00:00:00"]' ) def test_numpy_array_d1_datetime64_hours(self): assert ( orjson.dumps( numpy.array( [ numpy.datetime64("2021-01-01T00"), numpy.datetime64("2021-01-01T01"), numpy.datetime64("2021-01-01T02"), ], ), option=orjson.OPT_SERIALIZE_NUMPY, ) == b'["2021-01-01T00:00:00","2021-01-01T01:00:00","2021-01-01T02:00:00"]' ) def test_numpy_array_d1_datetime64_minutes(self): assert ( orjson.dumps( numpy.array( [ numpy.datetime64("2021-01-01T00:00"), numpy.datetime64("2021-01-01T00:01"), numpy.datetime64("2021-01-01T00:02"), ], ), option=orjson.OPT_SERIALIZE_NUMPY, ) == b'["2021-01-01T00:00:00","2021-01-01T00:01:00","2021-01-01T00:02:00"]' ) def test_numpy_array_d1_datetime64_seconds(self): assert ( orjson.dumps( numpy.array( [ numpy.datetime64("2021-01-01T00:00:00"), numpy.datetime64("2021-01-01T00:00:01"), numpy.datetime64("2021-01-01T00:00:02"), ], ), option=orjson.OPT_SERIALIZE_NUMPY, ) == b'["2021-01-01T00:00:00","2021-01-01T00:00:01","2021-01-01T00:00:02"]' ) def test_numpy_array_d1_datetime64_milliseconds(self): assert ( orjson.dumps( numpy.array( [ numpy.datetime64("2021-01-01T00:00:00"), numpy.datetime64("2021-01-01T00:00:00.172"), numpy.datetime64("2021-01-01T00:00:00.567"), ], ), option=orjson.OPT_SERIALIZE_NUMPY, ) == b'["2021-01-01T00:00:00","2021-01-01T00:00:00.172000","2021-01-01T00:00:00.567000"]' ) def test_numpy_array_d1_datetime64_microseconds(self): assert ( orjson.dumps( numpy.array( [ numpy.datetime64("2021-01-01T00:00:00"), numpy.datetime64("2021-01-01T00:00:00.172"), numpy.datetime64("2021-01-01T00:00:00.567891"), ], ), option=orjson.OPT_SERIALIZE_NUMPY, ) == b'["2021-01-01T00:00:00","2021-01-01T00:00:00.172000","2021-01-01T00:00:00.567891"]' ) def test_numpy_array_d1_datetime64_nanoseconds(self): assert ( orjson.dumps( numpy.array( [ numpy.datetime64("2021-01-01T00:00:00"), numpy.datetime64("2021-01-01T00:00:00.172"), numpy.datetime64("2021-01-01T00:00:00.567891234"), ], ), option=orjson.OPT_SERIALIZE_NUMPY, ) == b'["2021-01-01T00:00:00","2021-01-01T00:00:00.172000","2021-01-01T00:00:00.567891"]' ) def test_numpy_array_d1_datetime64_picoseconds(self): try: orjson.dumps( numpy.array( [ numpy.datetime64("2021-01-01T00:00:00"), numpy.datetime64("2021-01-01T00:00:00.172"), numpy.datetime64("2021-01-01T00:00:00.567891234567"), ], ), option=orjson.OPT_SERIALIZE_NUMPY, ) raise AssertionError() except TypeError as exc: assert str(exc) == "unsupported numpy.datetime64 unit: picoseconds" def test_numpy_array_d2_i64(self): assert ( orjson.dumps( numpy.array([[1, 2, 3], [4, 5, 6]], numpy.int64), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"[[1,2,3],[4,5,6]]" ) def test_numpy_array_d2_f64(self): assert ( orjson.dumps( numpy.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], numpy.float64), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"[[1.0,2.0,3.0],[4.0,5.0,6.0]]" ) def test_numpy_array_d3_i8(self): assert ( orjson.dumps( numpy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], numpy.int8), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"[[[1,2],[3,4]],[[5,6],[7,8]]]" ) def test_numpy_array_d3_u8(self): assert ( orjson.dumps( numpy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], numpy.uint8), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"[[[1,2],[3,4]],[[5,6],[7,8]]]" ) def test_numpy_array_d3_i32(self): assert ( orjson.dumps( numpy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], numpy.int32), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"[[[1,2],[3,4]],[[5,6],[7,8]]]" ) def test_numpy_array_d3_i64(self): assert ( orjson.dumps( numpy.array([[[1, 2], [3, 4], [5, 6], [7, 8]]], numpy.int64), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"[[[1,2],[3,4],[5,6],[7,8]]]" ) def test_numpy_array_d3_f64(self): assert ( orjson.dumps( numpy.array( [[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]], numpy.float64, ), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"[[[1.0,2.0],[3.0,4.0]],[[5.0,6.0],[7.0,8.0]]]" ) def test_numpy_array_fortran(self): array = numpy.array([[1, 2], [3, 4]], order="F") assert array.flags["F_CONTIGUOUS"] is True with pytest.raises(orjson.JSONEncodeError): orjson.dumps(array, option=orjson.OPT_SERIALIZE_NUMPY) assert orjson.dumps( array, default=numpy_default, option=orjson.OPT_SERIALIZE_NUMPY, ) == orjson.dumps(array.tolist()) def test_numpy_array_non_contiguous_message(self): array = numpy.array([[1, 2], [3, 4]], order="F") assert array.flags["F_CONTIGUOUS"] is True try: orjson.dumps(array, option=orjson.OPT_SERIALIZE_NUMPY) raise AssertionError() except TypeError as exc: assert ( str(exc) == "numpy array is not C contiguous; use ndarray.tolist() in default" ) def test_numpy_array_unsupported_dtype(self): array = numpy.array([[1, 2], [3, 4]], numpy.csingle) # type: ignore with pytest.raises(orjson.JSONEncodeError) as cm: orjson.dumps(array, option=orjson.OPT_SERIALIZE_NUMPY) assert "unsupported datatype in numpy array" in str(cm) def test_numpy_array_d1(self): array = numpy.array([1]) assert ( orjson.loads( orjson.dumps( array, option=orjson.OPT_SERIALIZE_NUMPY, ), ) == array.tolist() ) def test_numpy_array_d2(self): array = numpy.array([[1]]) assert ( orjson.loads( orjson.dumps( array, option=orjson.OPT_SERIALIZE_NUMPY, ), ) == array.tolist() ) def test_numpy_array_d3(self): array = numpy.array([[[1]]]) assert ( orjson.loads( orjson.dumps( array, option=orjson.OPT_SERIALIZE_NUMPY, ), ) == array.tolist() ) def test_numpy_array_d4(self): array = numpy.array([[[[1]]]]) assert ( orjson.loads( orjson.dumps( array, option=orjson.OPT_SERIALIZE_NUMPY, ), ) == array.tolist() ) def test_numpy_array_4_stride(self): array = numpy.random.rand(4, 4, 4, 4) assert ( orjson.loads( orjson.dumps( array, option=orjson.OPT_SERIALIZE_NUMPY, ), ) == array.tolist() ) def test_numpy_array_dimension_zero(self): array = numpy.array(0) assert array.ndim == 0 with pytest.raises(orjson.JSONEncodeError): orjson.dumps(array, option=orjson.OPT_SERIALIZE_NUMPY) array = numpy.empty((0, 4, 2)) assert ( orjson.loads( orjson.dumps( array, option=orjson.OPT_SERIALIZE_NUMPY, ), ) == array.tolist() ) array = numpy.empty((4, 0, 2)) assert ( orjson.loads( orjson.dumps( array, option=orjson.OPT_SERIALIZE_NUMPY, ), ) == array.tolist() ) array = numpy.empty((2, 4, 0)) assert ( orjson.loads( orjson.dumps( array, option=orjson.OPT_SERIALIZE_NUMPY, ), ) == array.tolist() ) def test_numpy_array_dimension_max(self): array = numpy.random.rand( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ) assert array.ndim == 32 assert ( orjson.loads( orjson.dumps( array, option=orjson.OPT_SERIALIZE_NUMPY, ), ) == array.tolist() ) def test_numpy_scalar_int8(self): assert orjson.dumps(numpy.int8(0), option=orjson.OPT_SERIALIZE_NUMPY) == b"0" assert ( orjson.dumps(numpy.int8(127), option=orjson.OPT_SERIALIZE_NUMPY) == b"127" ) assert ( orjson.dumps(numpy.int8(-128), option=orjson.OPT_SERIALIZE_NUMPY) == b"-128" ) def test_numpy_scalar_int16(self): assert orjson.dumps(numpy.int16(0), option=orjson.OPT_SERIALIZE_NUMPY) == b"0" assert ( orjson.dumps(numpy.int16(32767), option=orjson.OPT_SERIALIZE_NUMPY) == b"32767" ) assert ( orjson.dumps(numpy.int16(-32768), option=orjson.OPT_SERIALIZE_NUMPY) == b"-32768" ) def test_numpy_scalar_int32(self): assert orjson.dumps(numpy.int32(1), option=orjson.OPT_SERIALIZE_NUMPY) == b"1" assert ( orjson.dumps(numpy.int32(2147483647), option=orjson.OPT_SERIALIZE_NUMPY) == b"2147483647" ) assert ( orjson.dumps(numpy.int32(-2147483648), option=orjson.OPT_SERIALIZE_NUMPY) == b"-2147483648" ) def test_numpy_scalar_int64(self): assert ( orjson.dumps( numpy.int64(-9223372036854775808), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"-9223372036854775808" ) assert ( orjson.dumps( numpy.int64(9223372036854775807), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"9223372036854775807" ) def test_numpy_scalar_uint8(self): assert orjson.dumps(numpy.uint8(0), option=orjson.OPT_SERIALIZE_NUMPY) == b"0" assert ( orjson.dumps(numpy.uint8(255), option=orjson.OPT_SERIALIZE_NUMPY) == b"255" ) def test_numpy_scalar_uint16(self): assert orjson.dumps(numpy.uint16(0), option=orjson.OPT_SERIALIZE_NUMPY) == b"0" assert ( orjson.dumps(numpy.uint16(65535), option=orjson.OPT_SERIALIZE_NUMPY) == b"65535" ) def test_numpy_scalar_uint32(self): assert orjson.dumps(numpy.uint32(0), option=orjson.OPT_SERIALIZE_NUMPY) == b"0" assert ( orjson.dumps(numpy.uint32(4294967295), option=orjson.OPT_SERIALIZE_NUMPY) == b"4294967295" ) def test_numpy_scalar_uint64(self): assert orjson.dumps(numpy.uint64(0), option=orjson.OPT_SERIALIZE_NUMPY) == b"0" assert ( orjson.dumps( numpy.uint64(18446744073709551615), option=orjson.OPT_SERIALIZE_NUMPY, ) == b"18446744073709551615" ) def test_numpy_scalar_float16(self): assert ( orjson.dumps(numpy.float16(1.0), option=orjson.OPT_SERIALIZE_NUMPY) == b"1.0" ) def test_numpy_scalar_float32(self): assert ( orjson.dumps(numpy.float32(1.0), option=orjson.OPT_SERIALIZE_NUMPY) == b"1.0" ) def test_numpy_scalar_float64(self): assert ( orjson.dumps(numpy.float64(123.123), option=orjson.OPT_SERIALIZE_NUMPY) == b"123.123" ) def test_numpy_bool(self): assert ( orjson.dumps( {"a": numpy.bool_(True), "b": numpy.bool_(False)}, option=orjson.OPT_SERIALIZE_NUMPY, ) == b'{"a":true,"b":false}' ) def test_numpy_datetime_year(self): assert ( orjson.dumps(numpy.datetime64("2021"), option=orjson.OPT_SERIALIZE_NUMPY) == b'"2021-01-01T00:00:00"' ) def test_numpy_datetime_month(self): assert ( orjson.dumps(numpy.datetime64("2021-01"), option=orjson.OPT_SERIALIZE_NUMPY) == b'"2021-01-01T00:00:00"' ) def test_numpy_datetime_day(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01"), option=orjson.OPT_SERIALIZE_NUMPY, ) == b'"2021-01-01T00:00:00"' ) def test_numpy_datetime_hour(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01T00"), option=orjson.OPT_SERIALIZE_NUMPY, ) == b'"2021-01-01T00:00:00"' ) def test_numpy_datetime_minute(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01T00:00"), option=orjson.OPT_SERIALIZE_NUMPY, ) == b'"2021-01-01T00:00:00"' ) def test_numpy_datetime_second(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01T00:00:00"), option=orjson.OPT_SERIALIZE_NUMPY, ) == b'"2021-01-01T00:00:00"' ) def test_numpy_datetime_milli(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01T00:00:00.172"), option=orjson.OPT_SERIALIZE_NUMPY, ) == b'"2021-01-01T00:00:00.172000"' ) def test_numpy_datetime_micro(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01T00:00:00.172576"), option=orjson.OPT_SERIALIZE_NUMPY, ) == b'"2021-01-01T00:00:00.172576"' ) def test_numpy_datetime_nano(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01T00:00:00.172576789"), option=orjson.OPT_SERIALIZE_NUMPY, ) == b'"2021-01-01T00:00:00.172576"' ) def test_numpy_datetime_naive_utc_year(self): assert ( orjson.dumps( numpy.datetime64("2021"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC, ) == b'"2021-01-01T00:00:00+00:00"' ) def test_numpy_datetime_naive_utc_month(self): assert ( orjson.dumps( numpy.datetime64("2021-01"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC, ) == b'"2021-01-01T00:00:00+00:00"' ) def test_numpy_datetime_naive_utc_day(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC, ) == b'"2021-01-01T00:00:00+00:00"' ) def test_numpy_datetime_naive_utc_hour(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01T00"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC, ) == b'"2021-01-01T00:00:00+00:00"' ) def test_numpy_datetime_naive_utc_minute(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01T00:00"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC, ) == b'"2021-01-01T00:00:00+00:00"' ) def test_numpy_datetime_naive_utc_second(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01T00:00:00"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC, ) == b'"2021-01-01T00:00:00+00:00"' ) def test_numpy_datetime_naive_utc_milli(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01T00:00:00.172"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC, ) == b'"2021-01-01T00:00:00.172000+00:00"' ) def test_numpy_datetime_naive_utc_micro(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01T00:00:00.172576"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC, ) == b'"2021-01-01T00:00:00.172576+00:00"' ) def test_numpy_datetime_naive_utc_nano(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01T00:00:00.172576789"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC, ) == b'"2021-01-01T00:00:00.172576+00:00"' ) def test_numpy_datetime_naive_utc_utc_z_year(self): assert ( orjson.dumps( numpy.datetime64("2021"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC | orjson.OPT_UTC_Z, ) == b'"2021-01-01T00:00:00Z"' ) def test_numpy_datetime_naive_utc_utc_z_month(self): assert ( orjson.dumps( numpy.datetime64("2021-01"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC | orjson.OPT_UTC_Z, ) == b'"2021-01-01T00:00:00Z"' ) def test_numpy_datetime_naive_utc_utc_z_day(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC | orjson.OPT_UTC_Z, ) == b'"2021-01-01T00:00:00Z"' ) def test_numpy_datetime_naive_utc_utc_z_hour(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01T00"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC | orjson.OPT_UTC_Z, ) == b'"2021-01-01T00:00:00Z"' ) def test_numpy_datetime_naive_utc_utc_z_minute(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01T00:00"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC | orjson.OPT_UTC_Z, ) == b'"2021-01-01T00:00:00Z"' ) def test_numpy_datetime_naive_utc_utc_z_second(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01T00:00:00"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC | orjson.OPT_UTC_Z, ) == b'"2021-01-01T00:00:00Z"' ) def test_numpy_datetime_naive_utc_utc_z_milli(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01T00:00:00.172"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC | orjson.OPT_UTC_Z, ) == b'"2021-01-01T00:00:00.172000Z"' ) def test_numpy_datetime_naive_utc_utc_z_micro(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01T00:00:00.172576"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC | orjson.OPT_UTC_Z, ) == b'"2021-01-01T00:00:00.172576Z"' ) def test_numpy_datetime_naive_utc_utc_z_nano(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01T00:00:00.172576789"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_NAIVE_UTC | orjson.OPT_UTC_Z, ) == b'"2021-01-01T00:00:00.172576Z"' ) def test_numpy_datetime_omit_microseconds_year(self): assert ( orjson.dumps( numpy.datetime64("2021"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_OMIT_MICROSECONDS, ) == b'"2021-01-01T00:00:00"' ) def test_numpy_datetime_omit_microseconds_month(self): assert ( orjson.dumps( numpy.datetime64("2021-01"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_OMIT_MICROSECONDS, ) == b'"2021-01-01T00:00:00"' ) def test_numpy_datetime_omit_microseconds_day(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_OMIT_MICROSECONDS, ) == b'"2021-01-01T00:00:00"' ) def test_numpy_datetime_omit_microseconds_hour(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01T00"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_OMIT_MICROSECONDS, ) == b'"2021-01-01T00:00:00"' ) def test_numpy_datetime_omit_microseconds_minute(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01T00:00"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_OMIT_MICROSECONDS, ) == b'"2021-01-01T00:00:00"' ) def test_numpy_datetime_omit_microseconds_second(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01T00:00:00"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_OMIT_MICROSECONDS, ) == b'"2021-01-01T00:00:00"' ) def test_numpy_datetime_omit_microseconds_milli(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01T00:00:00.172"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_OMIT_MICROSECONDS, ) == b'"2021-01-01T00:00:00"' ) def test_numpy_datetime_omit_microseconds_micro(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01T00:00:00.172576"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_OMIT_MICROSECONDS, ) == b'"2021-01-01T00:00:00"' ) def test_numpy_datetime_omit_microseconds_nano(self): assert ( orjson.dumps( numpy.datetime64("2021-01-01T00:00:00.172576789"), option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_OMIT_MICROSECONDS, ) == b'"2021-01-01T00:00:00"' ) def test_numpy_datetime_nat(self): with pytest.raises(orjson.JSONEncodeError): orjson.dumps(numpy.datetime64("NaT"), option=orjson.OPT_SERIALIZE_NUMPY) with pytest.raises(orjson.JSONEncodeError): orjson.dumps([numpy.datetime64("NaT")], option=orjson.OPT_SERIALIZE_NUMPY) def test_numpy_repeated(self): data = numpy.array([[[1, 2], [3, 4], [5, 6], [7, 8]]], numpy.int64) # type: ignore for _ in range(3): assert ( orjson.dumps( data, option=orjson.OPT_SERIALIZE_NUMPY, ) == b"[[[1,2],[3,4],[5,6],[7,8]]]" ) @pytest.mark.skipif(numpy is None, reason="numpy is not installed") class TestNumpyEquivalence: def _test(self, obj): assert orjson.dumps(obj, option=orjson.OPT_SERIALIZE_NUMPY) == orjson.dumps( obj.tolist(), ) def test_numpy_uint8(self): self._test(numpy.array([0, 255], numpy.uint8)) def test_numpy_uint16(self): self._test(numpy.array([0, 65535], numpy.uint16)) def test_numpy_uint32(self): self._test(numpy.array([0, 4294967295], numpy.uint32)) def test_numpy_uint64(self): self._test(numpy.array([0, 18446744073709551615], numpy.uint64)) def test_numpy_int8(self): self._test(numpy.array([-128, 127], numpy.int8)) def test_numpy_int16(self): self._test(numpy.array([-32768, 32767], numpy.int16)) def test_numpy_int32(self): self._test(numpy.array([-2147483647, 2147483647], numpy.int32)) def test_numpy_int64(self): self._test( numpy.array([-9223372036854775807, 9223372036854775807], numpy.int64), ) @pytest.mark.skip(reason="tolist() conversion results in 3.4028234663852886e38") def test_numpy_float32(self): self._test( numpy.array( [ -340282346638528859811704183484516925440.0000000000000000, 340282346638528859811704183484516925440.0000000000000000, ], numpy.float32, ), ) self._test(numpy.array([-3.4028235e38, 3.4028235e38], numpy.float32)) def test_numpy_float64(self): self._test( numpy.array( [-1.7976931348623157e308, 1.7976931348623157e308], numpy.float64, ), ) @pytest.mark.skipif(numpy is None, reason="numpy is not installed") class NumpyEndianness: def test_numpy_array_dimension_zero(self): wrong_endianness = ">" if sys.byteorder == "little" else "<" array = numpy.array([0, 1, 0.4, 5.7], dtype=f"{wrong_endianness}f8") with pytest.raises(orjson.JSONEncodeError): orjson.dumps(array, option=orjson.OPT_SERIALIZE_NUMPY) ================================================ FILE: test/test_parsing.py ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2018-2026) import pytest import orjson from .util import ( SUPPORTS_BYTEARRAY, SUPPORTS_MEMORYVIEW, needs_data, read_fixture_bytes, ) @needs_data class TestJSONTestSuiteParsing: def _run_fail_json(self, filename, exc=orjson.JSONDecodeError): data = read_fixture_bytes(filename, "parsing") with pytest.raises(exc): orjson.loads(data) if SUPPORTS_BYTEARRAY: with pytest.raises(exc): orjson.loads(bytearray(data)) if SUPPORTS_MEMORYVIEW: with pytest.raises(exc): orjson.loads(memoryview(data)) try: decoded = data.decode("utf-8") except UnicodeDecodeError: pass else: with pytest.raises(exc): orjson.loads(decoded) def _run_pass_json(self, filename, match=""): data = read_fixture_bytes(filename, "parsing") orjson.loads(data) if SUPPORTS_BYTEARRAY: orjson.loads(bytearray(data)) if SUPPORTS_MEMORYVIEW: orjson.loads(memoryview(data)) orjson.loads(data.decode("utf-8")) def test_y_array_arraysWithSpace(self): """ y_array_arraysWithSpaces.json """ self._run_pass_json("y_array_arraysWithSpaces.json") def test_y_array_empty_string(self): """ y_array_empty-string.json """ self._run_pass_json("y_array_empty-string.json") def test_y_array_empty(self): """ y_array_empty.json """ self._run_pass_json("y_array_empty.json") def test_y_array_ending_with_newline(self): """ y_array_ending_with_newline.json """ self._run_pass_json("y_array_ending_with_newline.json") def test_y_array_false(self): """ y_array_false.json """ self._run_pass_json("y_array_false.json") def test_y_array_heterogeneou(self): """ y_array_heterogeneous.json """ self._run_pass_json("y_array_heterogeneous.json") def test_y_array_null(self): """ y_array_null.json """ self._run_pass_json("y_array_null.json") def test_y_array_with_1_and_newline(self): """ y_array_with_1_and_newline.json """ self._run_pass_json("y_array_with_1_and_newline.json") def test_y_array_with_leading_space(self): """ y_array_with_leading_space.json """ self._run_pass_json("y_array_with_leading_space.json") def test_y_array_with_several_null(self): """ y_array_with_several_null.json """ self._run_pass_json("y_array_with_several_null.json") def test_y_array_with_trailing_space(self): """ y_array_with_trailing_space.json """ self._run_pass_json("y_array_with_trailing_space.json") def test_y_number(self): """ y_number.json """ self._run_pass_json("y_number.json") def test_y_number_0e_1(self): """ y_number_0e+1.json """ self._run_pass_json("y_number_0e+1.json") def test_y_number_0e1(self): """ y_number_0e1.json """ self._run_pass_json("y_number_0e1.json") def test_y_number_after_space(self): """ y_number_after_space.json """ self._run_pass_json("y_number_after_space.json") def test_y_number_double_close_to_zer(self): """ y_number_double_close_to_zero.json """ self._run_pass_json("y_number_double_close_to_zero.json") def test_y_number_int_with_exp(self): """ y_number_int_with_exp.json """ self._run_pass_json("y_number_int_with_exp.json") def test_y_number_minus_zer(self): """ y_number_minus_zero.json """ self._run_pass_json("y_number_minus_zero.json") def test_y_number_negative_int(self): """ y_number_negative_int.json """ self._run_pass_json("y_number_negative_int.json") def test_y_number_negative_one(self): """ y_number_negative_one.json """ self._run_pass_json("y_number_negative_one.json") def test_y_number_negative_zer(self): """ y_number_negative_zero.json """ self._run_pass_json("y_number_negative_zero.json") def test_y_number_real_capital_e(self): """ y_number_real_capital_e.json """ self._run_pass_json("y_number_real_capital_e.json") def test_y_number_real_capital_e_neg_exp(self): """ y_number_real_capital_e_neg_exp.json """ self._run_pass_json("y_number_real_capital_e_neg_exp.json") def test_y_number_real_capital_e_pos_exp(self): """ y_number_real_capital_e_pos_exp.json """ self._run_pass_json("y_number_real_capital_e_pos_exp.json") def test_y_number_real_exponent(self): """ y_number_real_exponent.json """ self._run_pass_json("y_number_real_exponent.json") def test_y_number_real_fraction_exponent(self): """ y_number_real_fraction_exponent.json """ self._run_pass_json("y_number_real_fraction_exponent.json") def test_y_number_real_neg_exp(self): """ y_number_real_neg_exp.json """ self._run_pass_json("y_number_real_neg_exp.json") def test_y_number_real_pos_exponent(self): """ y_number_real_pos_exponent.json """ self._run_pass_json("y_number_real_pos_exponent.json") def test_y_number_simple_int(self): """ y_number_simple_int.json """ self._run_pass_json("y_number_simple_int.json") def test_y_number_simple_real(self): """ y_number_simple_real.json """ self._run_pass_json("y_number_simple_real.json") def test_y_object(self): """ y_object.json """ self._run_pass_json("y_object.json") def test_y_object_basic(self): """ y_object_basic.json """ self._run_pass_json("y_object_basic.json") def test_y_object_duplicated_key(self): """ y_object_duplicated_key.json """ self._run_pass_json("y_object_duplicated_key.json") def test_y_object_duplicated_key_and_value(self): """ y_object_duplicated_key_and_value.json """ self._run_pass_json("y_object_duplicated_key_and_value.json") def test_y_object_empty(self): """ y_object_empty.json """ self._run_pass_json("y_object_empty.json") def test_y_object_empty_key(self): """ y_object_empty_key.json """ self._run_pass_json("y_object_empty_key.json") def test_y_object_escaped_null_in_key(self): """ y_object_escaped_null_in_key.json """ self._run_pass_json("y_object_escaped_null_in_key.json") def test_y_object_extreme_number(self): """ y_object_extreme_numbers.json """ self._run_pass_json("y_object_extreme_numbers.json") def test_y_object_long_string(self): """ y_object_long_strings.json """ self._run_pass_json("y_object_long_strings.json") def test_y_object_simple(self): """ y_object_simple.json """ self._run_pass_json("y_object_simple.json") def test_y_object_string_unicode(self): """ y_object_string_unicode.json """ self._run_pass_json("y_object_string_unicode.json") def test_y_object_with_newline(self): """ y_object_with_newlines.json """ self._run_pass_json("y_object_with_newlines.json") def test_y_string_1_2_3_bytes_UTF_8_sequence(self): """ y_string_1_2_3_bytes_UTF-8_sequences.json """ self._run_pass_json("y_string_1_2_3_bytes_UTF-8_sequences.json") def test_y_string_accepted_surrogate_pair(self): """ y_string_accepted_surrogate_pair.json """ self._run_pass_json("y_string_accepted_surrogate_pair.json") def test_y_string_accepted_surrogate_pairs(self): """ y_string_accepted_surrogate_pairs.json """ self._run_pass_json("y_string_accepted_surrogate_pairs.json") def test_y_string_allowed_escape(self): """ y_string_allowed_escapes.json """ self._run_pass_json("y_string_allowed_escapes.json") def test_y_string_backslash_and_u_escaped_zer(self): """ y_string_backslash_and_u_escaped_zero.json """ self._run_pass_json("y_string_backslash_and_u_escaped_zero.json") def test_y_string_backslash_doublequote(self): """ y_string_backslash_doublequotes.json """ self._run_pass_json("y_string_backslash_doublequotes.json") def test_y_string_comment(self): """ y_string_comments.json """ self._run_pass_json("y_string_comments.json") def test_y_string_double_escape_a(self): """ y_string_double_escape_a.json """ self._run_pass_json("y_string_double_escape_a.json") def test_y_string_double_escape_(self): """ y_string_double_escape_n.json """ self._run_pass_json("y_string_double_escape_n.json") def test_y_string_escaped_control_character(self): """ y_string_escaped_control_character.json """ self._run_pass_json("y_string_escaped_control_character.json") def test_y_string_escaped_noncharacter(self): """ y_string_escaped_noncharacter.json """ self._run_pass_json("y_string_escaped_noncharacter.json") def test_y_string_in_array(self): """ y_string_in_array.json """ self._run_pass_json("y_string_in_array.json") def test_y_string_in_array_with_leading_space(self): """ y_string_in_array_with_leading_space.json """ self._run_pass_json("y_string_in_array_with_leading_space.json") def test_y_string_last_surrogates_1_and_2(self): """ y_string_last_surrogates_1_and_2.json """ self._run_pass_json("y_string_last_surrogates_1_and_2.json") def test_y_string_nbsp_uescaped(self): """ y_string_nbsp_uescaped.json """ self._run_pass_json("y_string_nbsp_uescaped.json") def test_y_string_nonCharacterInUTF_8_U_10FFFF(self): """ y_string_nonCharacterInUTF-8_U+10FFFF.json """ self._run_pass_json("y_string_nonCharacterInUTF-8_U+10FFFF.json") def test_y_string_nonCharacterInUTF_8_U_FFFF(self): """ y_string_nonCharacterInUTF-8_U+FFFF.json """ self._run_pass_json("y_string_nonCharacterInUTF-8_U+FFFF.json") def test_y_string_null_escape(self): """ y_string_null_escape.json """ self._run_pass_json("y_string_null_escape.json") def test_y_string_one_byte_utf_8(self): """ y_string_one-byte-utf-8.json """ self._run_pass_json("y_string_one-byte-utf-8.json") def test_y_string_pi(self): """ y_string_pi.json """ self._run_pass_json("y_string_pi.json") def test_y_string_reservedCharacterInUTF_8_U_1BFFF(self): """ y_string_reservedCharacterInUTF-8_U+1BFFF.json """ self._run_pass_json("y_string_reservedCharacterInUTF-8_U+1BFFF.json") def test_y_string_simple_ascii(self): """ y_string_simple_ascii.json """ self._run_pass_json("y_string_simple_ascii.json") def test_y_string_space(self): """ y_string_space.json """ self._run_pass_json("y_string_space.json") def test_y_string_surrogates_U_1D11E_MUSICAL_SYMBOL_G_CLEF(self): """ y_string_surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF.json """ self._run_pass_json("y_string_surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF.json") def test_y_string_three_byte_utf_8(self): """ y_string_three-byte-utf-8.json """ self._run_pass_json("y_string_three-byte-utf-8.json") def test_y_string_two_byte_utf_8(self): """ y_string_two-byte-utf-8.json """ self._run_pass_json("y_string_two-byte-utf-8.json") def test_y_string_u_2028_line_sep(self): """ y_string_u+2028_line_sep.json """ self._run_pass_json("y_string_u+2028_line_sep.json") def test_y_string_u_2029_par_sep(self): """ y_string_u+2029_par_sep.json """ self._run_pass_json("y_string_u+2029_par_sep.json") def test_y_string_uEscape(self): """ y_string_uEscape.json """ self._run_pass_json("y_string_uEscape.json") def test_y_string_uescaped_newline(self): """ y_string_uescaped_newline.json """ self._run_pass_json("y_string_uescaped_newline.json") def test_y_string_unescaped_char_delete(self): """ y_string_unescaped_char_delete.json """ self._run_pass_json("y_string_unescaped_char_delete.json") def test_y_string_unicode(self): """ y_string_unicode.json """ self._run_pass_json("y_string_unicode.json") def test_y_string_unicodeEscapedBackslash(self): """ y_string_unicodeEscapedBackslash.json """ self._run_pass_json("y_string_unicodeEscapedBackslash.json") def test_y_string_unicode_2(self): """ y_string_unicode_2.json """ self._run_pass_json("y_string_unicode_2.json") def test_y_string_unicode_U_10FFFE_nonchar(self): """ y_string_unicode_U+10FFFE_nonchar.json """ self._run_pass_json("y_string_unicode_U+10FFFE_nonchar.json") def test_y_string_unicode_U_1FFFE_nonchar(self): """ y_string_unicode_U+1FFFE_nonchar.json """ self._run_pass_json("y_string_unicode_U+1FFFE_nonchar.json") def test_y_string_unicode_U_200B_ZERO_WIDTH_SPACE(self): """ y_string_unicode_U+200B_ZERO_WIDTH_SPACE.json """ self._run_pass_json("y_string_unicode_U+200B_ZERO_WIDTH_SPACE.json") def test_y_string_unicode_U_2064_invisible_plu(self): """ y_string_unicode_U+2064_invisible_plus.json """ self._run_pass_json("y_string_unicode_U+2064_invisible_plus.json") def test_y_string_unicode_U_FDD0_nonchar(self): """ y_string_unicode_U+FDD0_nonchar.json """ self._run_pass_json("y_string_unicode_U+FDD0_nonchar.json") def test_y_string_unicode_U_FFFE_nonchar(self): """ y_string_unicode_U+FFFE_nonchar.json """ self._run_pass_json("y_string_unicode_U+FFFE_nonchar.json") def test_y_string_unicode_escaped_double_quote(self): """ y_string_unicode_escaped_double_quote.json """ self._run_pass_json("y_string_unicode_escaped_double_quote.json") def test_y_string_utf8(self): """ y_string_utf8.json """ self._run_pass_json("y_string_utf8.json") def test_y_string_with_del_character(self): """ y_string_with_del_character.json """ self._run_pass_json("y_string_with_del_character.json") def test_y_structure_lonely_false(self): """ y_structure_lonely_false.json """ self._run_pass_json("y_structure_lonely_false.json") def test_y_structure_lonely_int(self): """ y_structure_lonely_int.json """ self._run_pass_json("y_structure_lonely_int.json") def test_y_structure_lonely_negative_real(self): """ y_structure_lonely_negative_real.json """ self._run_pass_json("y_structure_lonely_negative_real.json") def test_y_structure_lonely_null(self): """ y_structure_lonely_null.json """ self._run_pass_json("y_structure_lonely_null.json") def test_y_structure_lonely_string(self): """ y_structure_lonely_string.json """ self._run_pass_json("y_structure_lonely_string.json") def test_y_structure_lonely_true(self): """ y_structure_lonely_true.json """ self._run_pass_json("y_structure_lonely_true.json") def test_y_structure_string_empty(self): """ y_structure_string_empty.json """ self._run_pass_json("y_structure_string_empty.json") def test_y_structure_trailing_newline(self): """ y_structure_trailing_newline.json """ self._run_pass_json("y_structure_trailing_newline.json") def test_y_structure_true_in_array(self): """ y_structure_true_in_array.json """ self._run_pass_json("y_structure_true_in_array.json") def test_y_structure_whitespace_array(self): """ y_structure_whitespace_array.json """ self._run_pass_json("y_structure_whitespace_array.json") def test_n_array_1_true_without_comma(self): """ n_array_1_true_without_comma.json """ self._run_fail_json("n_array_1_true_without_comma.json") def test_n_array_a_invalid_utf8(self): """ n_array_a_invalid_utf8.json """ self._run_fail_json("n_array_a_invalid_utf8.json") def test_n_array_colon_instead_of_comma(self): """ n_array_colon_instead_of_comma.json """ self._run_fail_json("n_array_colon_instead_of_comma.json") def test_n_array_comma_after_close(self): """ n_array_comma_after_close.json """ self._run_fail_json("n_array_comma_after_close.json") def test_n_array_comma_and_number(self): """ n_array_comma_and_number.json """ self._run_fail_json("n_array_comma_and_number.json") def test_n_array_double_comma(self): """ n_array_double_comma.json """ self._run_fail_json("n_array_double_comma.json") def test_n_array_double_extra_comma(self): """ n_array_double_extra_comma.json """ self._run_fail_json("n_array_double_extra_comma.json") def test_n_array_extra_close(self): """ n_array_extra_close.json """ self._run_fail_json("n_array_extra_close.json") def test_n_array_extra_comma(self): """ n_array_extra_comma.json """ self._run_fail_json("n_array_extra_comma.json") def test_n_array_incomplete(self): """ n_array_incomplete.json """ self._run_fail_json("n_array_incomplete.json") def test_n_array_incomplete_invalid_value(self): """ n_array_incomplete_invalid_value.json """ self._run_fail_json("n_array_incomplete_invalid_value.json") def test_n_array_inner_array_no_comma(self): """ n_array_inner_array_no_comma.json """ self._run_fail_json("n_array_inner_array_no_comma.json") def test_n_array_invalid_utf8(self): """ n_array_invalid_utf8.json """ self._run_fail_json("n_array_invalid_utf8.json") def test_n_array_items_separated_by_semicol(self): """ n_array_items_separated_by_semicolon.json """ self._run_fail_json("n_array_items_separated_by_semicolon.json") def test_n_array_just_comma(self): """ n_array_just_comma.json """ self._run_fail_json("n_array_just_comma.json") def test_n_array_just_minu(self): """ n_array_just_minus.json """ self._run_fail_json("n_array_just_minus.json") def test_n_array_missing_value(self): """ n_array_missing_value.json """ self._run_fail_json("n_array_missing_value.json") def test_n_array_newlines_unclosed(self): """ n_array_newlines_unclosed.json """ self._run_fail_json("n_array_newlines_unclosed.json") def test_n_array_number_and_comma(self): """ n_array_number_and_comma.json """ self._run_fail_json("n_array_number_and_comma.json") def test_n_array_number_and_several_comma(self): """ n_array_number_and_several_commas.json """ self._run_fail_json("n_array_number_and_several_commas.json") def test_n_array_spaces_vertical_tab_formfeed(self): """ n_array_spaces_vertical_tab_formfeed.json """ self._run_fail_json("n_array_spaces_vertical_tab_formfeed.json") def test_n_array_star_inside(self): """ n_array_star_inside.json """ self._run_fail_json("n_array_star_inside.json") def test_n_array_unclosed(self): """ n_array_unclosed.json """ self._run_fail_json("n_array_unclosed.json") def test_n_array_unclosed_trailing_comma(self): """ n_array_unclosed_trailing_comma.json """ self._run_fail_json("n_array_unclosed_trailing_comma.json") def test_n_array_unclosed_with_new_line(self): """ n_array_unclosed_with_new_lines.json """ self._run_fail_json("n_array_unclosed_with_new_lines.json") def test_n_array_unclosed_with_object_inside(self): """ n_array_unclosed_with_object_inside.json """ self._run_fail_json("n_array_unclosed_with_object_inside.json") def test_n_incomplete_false(self): """ n_incomplete_false.json """ self._run_fail_json("n_incomplete_false.json") def test_n_incomplete_null(self): """ n_incomplete_null.json """ self._run_fail_json("n_incomplete_null.json") def test_n_incomplete_true(self): """ n_incomplete_true.json """ self._run_fail_json("n_incomplete_true.json") def test_n_multidigit_number_then_00(self): """ n_multidigit_number_then_00.json """ self._run_fail_json("n_multidigit_number_then_00.json") def test_n_number__(self): """ n_number_++.json """ self._run_fail_json("n_number_++.json") def test_n_number_1(self): """ n_number_+1.json """ self._run_fail_json("n_number_+1.json") def test_n_number_Inf(self): """ n_number_+Inf.json """ self._run_fail_json("n_number_+Inf.json") def test_n_number_01(self): """ n_number_-01.json """ self._run_fail_json("n_number_-01.json") def test_n_number_1_0(self): """ n_number_-1.0..json """ self._run_fail_json("n_number_-1.0..json") def test_n_number_2(self): """ n_number_-2..json """ self._run_fail_json("n_number_-2..json") def test_n_number_negative_NaN(self): """ n_number_-NaN.json """ self._run_fail_json("n_number_-NaN.json") def test_n_number_negative_1(self): """ n_number_.-1.json """ self._run_fail_json("n_number_.-1.json") def test_n_number_2e_3(self): """ n_number_.2e-3.json """ self._run_fail_json("n_number_.2e-3.json") def test_n_number_0_1_2(self): """ n_number_0.1.2.json """ self._run_fail_json("n_number_0.1.2.json") def test_n_number_0_3e_(self): """ n_number_0.3e+.json """ self._run_fail_json("n_number_0.3e+.json") def test_n_number_0_3e(self): """ n_number_0.3e.json """ self._run_fail_json("n_number_0.3e.json") def test_n_number_0_e1(self): """ n_number_0.e1.json """ self._run_fail_json("n_number_0.e1.json") def test_n_number_0_capital_E_(self): """ n_number_0_capital_E+.json """ self._run_fail_json("n_number_0_capital_E+.json") def test_n_number_0_capital_E(self): """ n_number_0_capital_E.json """ self._run_fail_json("n_number_0_capital_E.json") def test_n_number_0e_(self): """ n_number_0e+.json """ self._run_fail_json("n_number_0e+.json") def test_n_number_0e(self): """ n_number_0e.json """ self._run_fail_json("n_number_0e.json") def test_n_number_1_0e_(self): """ n_number_1.0e+.json """ self._run_fail_json("n_number_1.0e+.json") def test_n_number_1_0e_2(self): """ n_number_1.0e-.json """ self._run_fail_json("n_number_1.0e-.json") def test_n_number_1_0e(self): """ n_number_1.0e.json """ self._run_fail_json("n_number_1.0e.json") def test_n_number_1_000(self): """ n_number_1_000.json """ self._run_fail_json("n_number_1_000.json") def test_n_number_1eE2(self): """ n_number_1eE2.json """ self._run_fail_json("n_number_1eE2.json") def test_n_number_2_e_3(self): """ n_number_2.e+3.json """ self._run_fail_json("n_number_2.e+3.json") def test_n_number_2_e_3_2(self): """ n_number_2.e-3.json """ self._run_fail_json("n_number_2.e-3.json") def test_n_number_2_e3_3(self): """ n_number_2.e3.json """ self._run_fail_json("n_number_2.e3.json") def test_n_number_9_e_(self): """ n_number_9.e+.json """ self._run_fail_json("n_number_9.e+.json") def test_n_number_negative_Inf(self): """ n_number_Inf.json """ self._run_fail_json("n_number_Inf.json") def test_n_number_NaN(self): """ n_number_NaN.json """ self._run_fail_json("n_number_NaN.json") def test_n_number_U_FF11_fullwidth_digit_one(self): """ n_number_U+FF11_fullwidth_digit_one.json """ self._run_fail_json("n_number_U+FF11_fullwidth_digit_one.json") def test_n_number_expressi(self): """ n_number_expression.json """ self._run_fail_json("n_number_expression.json") def test_n_number_hex_1_digit(self): """ n_number_hex_1_digit.json """ self._run_fail_json("n_number_hex_1_digit.json") def test_n_number_hex_2_digit(self): """ n_number_hex_2_digits.json """ self._run_fail_json("n_number_hex_2_digits.json") def test_n_number_infinity(self): """ n_number_infinity.json """ self._run_fail_json("n_number_infinity.json") def test_n_number_invalid_(self): """ n_number_invalid+-.json """ self._run_fail_json("n_number_invalid+-.json") def test_n_number_invalid_negative_real(self): """ n_number_invalid-negative-real.json """ self._run_fail_json("n_number_invalid-negative-real.json") def test_n_number_invalid_utf_8_in_bigger_int(self): """ n_number_invalid-utf-8-in-bigger-int.json """ self._run_fail_json("n_number_invalid-utf-8-in-bigger-int.json") def test_n_number_invalid_utf_8_in_exponent(self): """ n_number_invalid-utf-8-in-exponent.json """ self._run_fail_json("n_number_invalid-utf-8-in-exponent.json") def test_n_number_invalid_utf_8_in_int(self): """ n_number_invalid-utf-8-in-int.json """ self._run_fail_json("n_number_invalid-utf-8-in-int.json") def test_n_number_minus_infinity(self): """ n_number_minus_infinity.json """ self._run_fail_json("n_number_minus_infinity.json") def test_n_number_minus_sign_with_trailing_garbage(self): """ n_number_minus_sign_with_trailing_garbage.json """ self._run_fail_json("n_number_minus_sign_with_trailing_garbage.json") def test_n_number_minus_space_1(self): """ n_number_minus_space_1.json """ self._run_fail_json("n_number_minus_space_1.json") def test_n_number_neg_int_starting_with_zer(self): """ n_number_neg_int_starting_with_zero.json """ self._run_fail_json("n_number_neg_int_starting_with_zero.json") def test_n_number_neg_real_without_int_part(self): """ n_number_neg_real_without_int_part.json """ self._run_fail_json("n_number_neg_real_without_int_part.json") def test_n_number_neg_with_garbage_at_end(self): """ n_number_neg_with_garbage_at_end.json """ self._run_fail_json("n_number_neg_with_garbage_at_end.json") def test_n_number_real_garbage_after_e(self): """ n_number_real_garbage_after_e.json """ self._run_fail_json("n_number_real_garbage_after_e.json") def test_n_number_real_with_invalid_utf8_after_e(self): """ n_number_real_with_invalid_utf8_after_e.json """ self._run_fail_json("n_number_real_with_invalid_utf8_after_e.json") def test_n_number_real_without_fractional_part(self): """ n_number_real_without_fractional_part.json """ self._run_fail_json("n_number_real_without_fractional_part.json") def test_n_number_starting_with_dot(self): """ n_number_starting_with_dot.json """ self._run_fail_json("n_number_starting_with_dot.json") def test_n_number_with_alpha(self): """ n_number_with_alpha.json """ self._run_fail_json("n_number_with_alpha.json") def test_n_number_with_alpha_char(self): """ n_number_with_alpha_char.json """ self._run_fail_json("n_number_with_alpha_char.json") def test_n_number_with_leading_zer(self): """ n_number_with_leading_zero.json """ self._run_fail_json("n_number_with_leading_zero.json") def test_n_object_bad_value(self): """ n_object_bad_value.json """ self._run_fail_json("n_object_bad_value.json") def test_n_object_bracket_key(self): """ n_object_bracket_key.json """ self._run_fail_json("n_object_bracket_key.json") def test_n_object_comma_instead_of_col(self): """ n_object_comma_instead_of_colon.json """ self._run_fail_json("n_object_comma_instead_of_colon.json") def test_n_object_double_col(self): """ n_object_double_colon.json """ self._run_fail_json("n_object_double_colon.json") def test_n_object_emoji(self): """ n_object_emoji.json """ self._run_fail_json("n_object_emoji.json") def test_n_object_garbage_at_end(self): """ n_object_garbage_at_end.json """ self._run_fail_json("n_object_garbage_at_end.json") def test_n_object_key_with_single_quote(self): """ n_object_key_with_single_quotes.json """ self._run_fail_json("n_object_key_with_single_quotes.json") def test_n_object_lone_continuation_byte_in_key_and_trailing_comma(self): """ n_object_lone_continuation_byte_in_key_and_trailing_comma.json """ self._run_fail_json( "n_object_lone_continuation_byte_in_key_and_trailing_comma.json", ) def test_n_object_missing_col(self): """ n_object_missing_colon.json """ self._run_fail_json("n_object_missing_colon.json") def test_n_object_missing_key(self): """ n_object_missing_key.json """ self._run_fail_json("n_object_missing_key.json") def test_n_object_missing_semicol(self): """ n_object_missing_semicolon.json """ self._run_fail_json("n_object_missing_semicolon.json") def test_n_object_missing_value(self): """ n_object_missing_value.json """ self._run_fail_json("n_object_missing_value.json") def test_n_object_no_col(self): """ n_object_no-colon.json """ self._run_fail_json("n_object_no-colon.json") def test_n_object_non_string_key(self): """ n_object_non_string_key.json """ self._run_fail_json("n_object_non_string_key.json") def test_n_object_non_string_key_but_huge_number_instead(self): """ n_object_non_string_key_but_huge_number_instead.json """ self._run_fail_json("n_object_non_string_key_but_huge_number_instead.json") def test_n_object_repeated_null_null(self): """ n_object_repeated_null_null.json """ self._run_fail_json("n_object_repeated_null_null.json") def test_n_object_several_trailing_comma(self): """ n_object_several_trailing_commas.json """ self._run_fail_json("n_object_several_trailing_commas.json") def test_n_object_single_quote(self): """ n_object_single_quote.json """ self._run_fail_json("n_object_single_quote.json") def test_n_object_trailing_comma(self): """ n_object_trailing_comma.json """ self._run_fail_json("n_object_trailing_comma.json") def test_n_object_trailing_comment(self): """ n_object_trailing_comment.json """ self._run_fail_json("n_object_trailing_comment.json") def test_n_object_trailing_comment_ope(self): """ n_object_trailing_comment_open.json """ self._run_fail_json("n_object_trailing_comment_open.json") def test_n_object_trailing_comment_slash_ope(self): """ n_object_trailing_comment_slash_open.json """ self._run_fail_json("n_object_trailing_comment_slash_open.json") def test_n_object_trailing_comment_slash_open_incomplete(self): """ n_object_trailing_comment_slash_open_incomplete.json """ self._run_fail_json("n_object_trailing_comment_slash_open_incomplete.json") def test_n_object_two_commas_in_a_row(self): """ n_object_two_commas_in_a_row.json """ self._run_fail_json("n_object_two_commas_in_a_row.json") def test_n_object_unquoted_key(self): """ n_object_unquoted_key.json """ self._run_fail_json("n_object_unquoted_key.json") def test_n_object_unterminated_value(self): """ n_object_unterminated-value.json """ self._run_fail_json("n_object_unterminated-value.json") def test_n_object_with_single_string(self): """ n_object_with_single_string.json """ self._run_fail_json("n_object_with_single_string.json") def test_n_object_with_trailing_garbage(self): """ n_object_with_trailing_garbage.json """ self._run_fail_json("n_object_with_trailing_garbage.json") def test_n_single_space(self): """ n_single_space.json """ self._run_fail_json("n_single_space.json") def test_n_string_1_surrogate_then_escape(self): """ n_string_1_surrogate_then_escape.json """ self._run_fail_json("n_string_1_surrogate_then_escape.json") def test_n_string_1_surrogate_then_escape_u(self): """ n_string_1_surrogate_then_escape_u.json """ self._run_fail_json("n_string_1_surrogate_then_escape_u.json") def test_n_string_1_surrogate_then_escape_u1(self): """ n_string_1_surrogate_then_escape_u1.json """ self._run_fail_json("n_string_1_surrogate_then_escape_u1.json") def test_n_string_1_surrogate_then_escape_u1x(self): """ n_string_1_surrogate_then_escape_u1x.json """ self._run_fail_json("n_string_1_surrogate_then_escape_u1x.json") def test_n_string_accentuated_char_no_quote(self): """ n_string_accentuated_char_no_quotes.json """ self._run_fail_json("n_string_accentuated_char_no_quotes.json") def test_n_string_backslash_00(self): """ n_string_backslash_00.json """ self._run_fail_json("n_string_backslash_00.json") def test_n_string_escape_x(self): """ n_string_escape_x.json """ self._run_fail_json("n_string_escape_x.json") def test_n_string_escaped_backslash_bad(self): """ n_string_escaped_backslash_bad.json """ self._run_fail_json("n_string_escaped_backslash_bad.json") def test_n_string_escaped_ctrl_char_tab(self): """ n_string_escaped_ctrl_char_tab.json """ self._run_fail_json("n_string_escaped_ctrl_char_tab.json") def test_n_string_escaped_emoji(self): """ n_string_escaped_emoji.json """ self._run_fail_json("n_string_escaped_emoji.json") def test_n_string_incomplete_escape(self): """ n_string_incomplete_escape.json """ self._run_fail_json("n_string_incomplete_escape.json") def test_n_string_incomplete_escaped_character(self): """ n_string_incomplete_escaped_character.json """ self._run_fail_json("n_string_incomplete_escaped_character.json") def test_n_string_incomplete_surrogate(self): """ n_string_incomplete_surrogate.json """ self._run_fail_json("n_string_incomplete_surrogate.json") def test_n_string_incomplete_surrogate_escape_invalid(self): """ n_string_incomplete_surrogate_escape_invalid.json """ self._run_fail_json("n_string_incomplete_surrogate_escape_invalid.json") def test_n_string_invalid_utf_8_in_escape(self): """ n_string_invalid-utf-8-in-escape.json """ self._run_fail_json("n_string_invalid-utf-8-in-escape.json") def test_n_string_invalid_backslash_esc(self): """ n_string_invalid_backslash_esc.json """ self._run_fail_json("n_string_invalid_backslash_esc.json") def test_n_string_invalid_unicode_escape(self): """ n_string_invalid_unicode_escape.json """ self._run_fail_json("n_string_invalid_unicode_escape.json") def test_n_string_invalid_utf8_after_escape(self): """ n_string_invalid_utf8_after_escape.json """ self._run_fail_json("n_string_invalid_utf8_after_escape.json") def test_n_string_leading_uescaped_thinspace(self): """ n_string_leading_uescaped_thinspace.json """ self._run_fail_json("n_string_leading_uescaped_thinspace.json") def test_n_string_no_quotes_with_bad_escape(self): """ n_string_no_quotes_with_bad_escape.json """ self._run_fail_json("n_string_no_quotes_with_bad_escape.json") def test_n_string_single_doublequote(self): """ n_string_single_doublequote.json """ self._run_fail_json("n_string_single_doublequote.json") def test_n_string_single_quote(self): """ n_string_single_quote.json """ self._run_fail_json("n_string_single_quote.json") def test_n_string_single_string_no_double_quote(self): """ n_string_single_string_no_double_quotes.json """ self._run_fail_json("n_string_single_string_no_double_quotes.json") def test_n_string_start_escape_unclosed(self): """ n_string_start_escape_unclosed.json """ self._run_fail_json("n_string_start_escape_unclosed.json") def test_n_string_unescaped_crtl_char(self): """ n_string_unescaped_crtl_char.json """ self._run_fail_json("n_string_unescaped_crtl_char.json") def test_n_string_unescaped_newline(self): """ n_string_unescaped_newline.json """ self._run_fail_json("n_string_unescaped_newline.json") def test_n_string_unescaped_tab(self): """ n_string_unescaped_tab.json """ self._run_fail_json("n_string_unescaped_tab.json") def test_n_string_unicode_CapitalU(self): """ n_string_unicode_CapitalU.json """ self._run_fail_json("n_string_unicode_CapitalU.json") def test_n_string_with_trailing_garbage(self): """ n_string_with_trailing_garbage.json """ self._run_fail_json("n_string_with_trailing_garbage.json") def test_n_structure_100000_opening_array(self): """ n_structure_100000_opening_arrays.json """ self._run_fail_json("n_structure_100000_opening_arrays.json.xz") def test_n_structure_U_2060_word_joined(self): """ n_structure_U+2060_word_joined.json """ self._run_fail_json("n_structure_U+2060_word_joined.json") def test_n_structure_UTF8_BOM_no_data(self): """ n_structure_UTF8_BOM_no_data.json """ self._run_fail_json("n_structure_UTF8_BOM_no_data.json") def test_n_structure_angle_bracket_(self): """ n_structure_angle_bracket_..json """ self._run_fail_json("n_structure_angle_bracket_..json") def test_n_structure_angle_bracket_null(self): """ n_structure_angle_bracket_null.json """ self._run_fail_json("n_structure_angle_bracket_null.json") def test_n_structure_array_trailing_garbage(self): """ n_structure_array_trailing_garbage.json """ self._run_fail_json("n_structure_array_trailing_garbage.json") def test_n_structure_array_with_extra_array_close(self): """ n_structure_array_with_extra_array_close.json """ self._run_fail_json("n_structure_array_with_extra_array_close.json") def test_n_structure_array_with_unclosed_string(self): """ n_structure_array_with_unclosed_string.json """ self._run_fail_json("n_structure_array_with_unclosed_string.json") def test_n_structure_ascii_unicode_identifier(self): """ n_structure_ascii-unicode-identifier.json """ self._run_fail_json("n_structure_ascii-unicode-identifier.json") def test_n_structure_capitalized_True(self): """ n_structure_capitalized_True.json """ self._run_fail_json("n_structure_capitalized_True.json") def test_n_structure_close_unopened_array(self): """ n_structure_close_unopened_array.json """ self._run_fail_json("n_structure_close_unopened_array.json") def test_n_structure_comma_instead_of_closing_brace(self): """ n_structure_comma_instead_of_closing_brace.json """ self._run_fail_json("n_structure_comma_instead_of_closing_brace.json") def test_n_structure_double_array(self): """ n_structure_double_array.json """ self._run_fail_json("n_structure_double_array.json") def test_n_structure_end_array(self): """ n_structure_end_array.json """ self._run_fail_json("n_structure_end_array.json") def test_n_structure_incomplete_UTF8_BOM(self): """ n_structure_incomplete_UTF8_BOM.json """ self._run_fail_json("n_structure_incomplete_UTF8_BOM.json") def test_n_structure_lone_invalid_utf_8(self): """ n_structure_lone-invalid-utf-8.json """ self._run_fail_json("n_structure_lone-invalid-utf-8.json") def test_n_structure_lone_open_bracket(self): """ n_structure_lone-open-bracket.json """ self._run_fail_json("n_structure_lone-open-bracket.json") def test_n_structure_no_data(self): """ n_structure_no_data.json """ self._run_fail_json("n_structure_no_data.json") def test_n_structure_null_byte_outside_string(self): """ n_structure_null-byte-outside-string.json """ self._run_fail_json("n_structure_null-byte-outside-string.json") def test_n_structure_number_with_trailing_garbage(self): """ n_structure_number_with_trailing_garbage.json """ self._run_fail_json("n_structure_number_with_trailing_garbage.json") def test_n_structure_object_followed_by_closing_object(self): """ n_structure_object_followed_by_closing_object.json """ self._run_fail_json("n_structure_object_followed_by_closing_object.json") def test_n_structure_object_unclosed_no_value(self): """ n_structure_object_unclosed_no_value.json """ self._run_fail_json("n_structure_object_unclosed_no_value.json") def test_n_structure_object_with_comment(self): """ n_structure_object_with_comment.json """ self._run_fail_json("n_structure_object_with_comment.json") def test_n_structure_object_with_trailing_garbage(self): """ n_structure_object_with_trailing_garbage.json """ self._run_fail_json("n_structure_object_with_trailing_garbage.json") def test_n_structure_open_array_apostrophe(self): """ n_structure_open_array_apostrophe.json """ self._run_fail_json("n_structure_open_array_apostrophe.json") def test_n_structure_open_array_comma(self): """ n_structure_open_array_comma.json """ self._run_fail_json("n_structure_open_array_comma.json") def test_n_structure_open_array_object(self): """ n_structure_open_array_object.json """ self._run_fail_json("n_structure_open_array_object.json.xz") def test_n_structure_open_array_open_object(self): """ n_structure_open_array_open_object.json """ self._run_fail_json("n_structure_open_array_open_object.json") def test_n_structure_open_array_open_string(self): """ n_structure_open_array_open_string.json """ self._run_fail_json("n_structure_open_array_open_string.json") def test_n_structure_open_array_string(self): """ n_structure_open_array_string.json """ self._run_fail_json("n_structure_open_array_string.json") def test_n_structure_open_object(self): """ n_structure_open_object.json """ self._run_fail_json("n_structure_open_object.json") def test_n_structure_open_object_close_array(self): """ n_structure_open_object_close_array.json """ self._run_fail_json("n_structure_open_object_close_array.json") def test_n_structure_open_object_comma(self): """ n_structure_open_object_comma.json """ self._run_fail_json("n_structure_open_object_comma.json") def test_n_structure_open_object_open_array(self): """ n_structure_open_object_open_array.json """ self._run_fail_json("n_structure_open_object_open_array.json") def test_n_structure_open_object_open_string(self): """ n_structure_open_object_open_string.json """ self._run_fail_json("n_structure_open_object_open_string.json") def test_n_structure_open_object_string_with_apostrophe(self): """ n_structure_open_object_string_with_apostrophes.json """ self._run_fail_json("n_structure_open_object_string_with_apostrophes.json") def test_n_structure_open_ope(self): """ n_structure_open_open.json """ self._run_fail_json("n_structure_open_open.json") def test_n_structure_single_eacute(self): """ n_structure_single_eacute.json """ self._run_fail_json("n_structure_single_eacute.json") def test_n_structure_single_star(self): """ n_structure_single_star.json """ self._run_fail_json("n_structure_single_star.json") def test_n_structure_trailing_(self): """ n_structure_trailing_#.json """ self._run_fail_json("n_structure_trailing_#.json") def test_n_structure_uescaped_LF_before_string(self): """ n_structure_uescaped_LF_before_string.json """ self._run_fail_json("n_structure_uescaped_LF_before_string.json") def test_n_structure_unclosed_array(self): """ n_structure_unclosed_array.json """ self._run_fail_json("n_structure_unclosed_array.json") def test_n_structure_unclosed_array_partial_null(self): """ n_structure_unclosed_array_partial_null.json """ self._run_fail_json("n_structure_unclosed_array_partial_null.json") def test_n_structure_unclosed_array_unfinished_false(self): """ n_structure_unclosed_array_unfinished_false.json """ self._run_fail_json("n_structure_unclosed_array_unfinished_false.json") def test_n_structure_unclosed_array_unfinished_true(self): """ n_structure_unclosed_array_unfinished_true.json """ self._run_fail_json("n_structure_unclosed_array_unfinished_true.json") def test_n_structure_unclosed_object(self): """ n_structure_unclosed_object.json """ self._run_fail_json("n_structure_unclosed_object.json") def test_n_structure_unicode_identifier(self): """ n_structure_unicode-identifier.json """ self._run_fail_json("n_structure_unicode-identifier.json") def test_n_structure_whitespace_U_2060_word_joiner(self): """ n_structure_whitespace_U+2060_word_joiner.json """ self._run_fail_json("n_structure_whitespace_U+2060_word_joiner.json") def test_n_structure_whitespace_formfeed(self): """ n_structure_whitespace_formfeed.json """ self._run_fail_json("n_structure_whitespace_formfeed.json") def test_i_number_double_huge_neg_exp(self): """ i_number_double_huge_neg_exp.json """ self._run_pass_json("i_number_double_huge_neg_exp.json") def test_i_number_huge_exp(self): """ i_number_huge_exp.json """ self._run_fail_json("i_number_huge_exp.json") def test_i_number_neg_int_huge_exp(self): """ i_number_neg_int_huge_exp.json """ self._run_fail_json("i_number_neg_int_huge_exp.json") def test_i_number_pos_double_huge_exp(self): """ i_number_pos_double_huge_exp.json """ self._run_fail_json("i_number_pos_double_huge_exp.json") def test_i_number_real_neg_overflow(self): """ i_number_real_neg_overflow.json """ self._run_fail_json("i_number_real_neg_overflow.json") def test_i_number_real_pos_overflow(self): """ i_number_real_pos_overflow.json """ self._run_fail_json("i_number_real_pos_overflow.json") def test_i_number_real_underflow(self): """ i_number_real_underflow.json """ self._run_pass_json("i_number_real_underflow.json") def test_i_number_too_big_neg_int(self): """ i_number_too_big_neg_int.json """ self._run_pass_json("i_number_too_big_neg_int.json") def test_i_number_too_big_pos_int(self): """ i_number_too_big_pos_int.json """ self._run_pass_json("i_number_too_big_pos_int.json") def test_i_number_very_big_negative_int(self): """ i_number_very_big_negative_int.json """ self._run_pass_json("i_number_very_big_negative_int.json") def test_i_object_key_lone_2nd_surrogate(self): """ i_object_key_lone_2nd_surrogate.json """ self._run_fail_json("i_object_key_lone_2nd_surrogate.json") def test_i_string_1st_surrogate_but_2nd_missing(self): """ i_string_1st_surrogate_but_2nd_missing.json """ self._run_fail_json("i_string_1st_surrogate_but_2nd_missing.json") def test_i_string_1st_valid_surrogate_2nd_invalid(self): """ i_string_1st_valid_surrogate_2nd_invalid.json """ self._run_fail_json("i_string_1st_valid_surrogate_2nd_invalid.json") def test_i_string_UTF_16LE_with_BOM(self): """ i_string_UTF-16LE_with_BOM.json """ self._run_fail_json("i_string_UTF-16LE_with_BOM.json") def test_i_string_UTF_8_invalid_sequence(self): """ i_string_UTF-8_invalid_sequence.json """ self._run_fail_json("i_string_UTF-8_invalid_sequence.json") def test_i_string_UTF8_surrogate_U_D800(self): """ i_string_UTF8_surrogate_U+D800.json """ self._run_fail_json("i_string_UTF8_surrogate_U+D800.json") def test_i_string_incomplete_surrogate_and_escape_valid(self): """ i_string_incomplete_surrogate_and_escape_valid.json """ self._run_fail_json("i_string_incomplete_surrogate_and_escape_valid.json") def test_i_string_incomplete_surrogate_pair(self): """ i_string_incomplete_surrogate_pair.json """ self._run_fail_json("i_string_incomplete_surrogate_pair.json") def test_i_string_incomplete_surrogates_escape_valid(self): """ i_string_incomplete_surrogates_escape_valid.json """ self._run_fail_json("i_string_incomplete_surrogates_escape_valid.json") def test_i_string_invalid_lonely_surrogate(self): """ i_string_invalid_lonely_surrogate.json """ self._run_fail_json("i_string_invalid_lonely_surrogate.json") def test_i_string_invalid_surrogate(self): """ i_string_invalid_surrogate.json """ self._run_fail_json("i_string_invalid_surrogate.json") def test_i_string_invalid_utf_8(self): """ i_string_invalid_utf-8.json """ self._run_fail_json("i_string_invalid_utf-8.json") def test_i_string_inverted_surrogates_U_1D11E(self): """ i_string_inverted_surrogates_U+1D11E.json """ self._run_fail_json("i_string_inverted_surrogates_U+1D11E.json") def test_i_string_iso_latin_1(self): """ i_string_iso_latin_1.json """ self._run_fail_json("i_string_iso_latin_1.json") def test_i_string_lone_second_surrogate(self): """ i_string_lone_second_surrogate.json """ self._run_fail_json("i_string_lone_second_surrogate.json") def test_i_string_lone_utf8_continuation_byte(self): """ i_string_lone_utf8_continuation_byte.json """ self._run_fail_json("i_string_lone_utf8_continuation_byte.json") def test_i_string_not_in_unicode_range(self): """ i_string_not_in_unicode_range.json """ self._run_fail_json("i_string_not_in_unicode_range.json") def test_i_string_overlong_sequence_2_byte(self): """ i_string_overlong_sequence_2_bytes.json """ self._run_fail_json("i_string_overlong_sequence_2_bytes.json") def test_i_string_overlong_sequence_6_byte(self): """ i_string_overlong_sequence_6_bytes.json """ self._run_fail_json("i_string_overlong_sequence_6_bytes.json") def test_i_string_overlong_sequence_6_bytes_null(self): """ i_string_overlong_sequence_6_bytes_null.json """ self._run_fail_json("i_string_overlong_sequence_6_bytes_null.json") def test_i_string_truncated_utf_8(self): """ i_string_truncated-utf-8.json """ self._run_fail_json("i_string_truncated-utf-8.json") def test_i_string_utf16BE_no_BOM(self): """ i_string_utf16BE_no_BOM.json """ self._run_fail_json("i_string_utf16BE_no_BOM.json") def test_i_string_utf16LE_no_BOM(self): """ i_string_utf16LE_no_BOM.json """ self._run_fail_json("i_string_utf16LE_no_BOM.json") def test_i_structure_500_nested_array(self): """ i_structure_500_nested_arrays.json """ try: self._run_pass_json("i_structure_500_nested_arrays.json.xz") except orjson.JSONDecodeError: # fails on serde, passes on yyjson pass def test_i_structure_UTF_8_BOM_empty_object(self): """ i_structure_UTF-8_BOM_empty_object.json """ self._run_fail_json("i_structure_UTF-8_BOM_empty_object.json") ================================================ FILE: test/test_recursion.py ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2026) import pytest import orjson def make_recursive_list_dict(limit: int, envelope_key: str, recurse_key: str): i = 0 root = [{envelope_key: i, recurse_key: []}] i += 1 while i < limit: sub = [{envelope_key: i, recurse_key: []}] sub[0][recurse_key] = root root = sub i += 1 return root class TestSerializeRecursion: @pytest.mark.parametrize("i", range(1, 127)) def test_dumps_recursion_valid_long(self, i): root = make_recursive_list_dict(i, "🐈" * 512, "b" * 1024) orjson.dumps(root) @pytest.mark.parametrize("i", range(1, 127)) def test_dumps_recursion_valid_short_1(self, i): root = make_recursive_list_dict(i, "a", "") orjson.dumps(root) @pytest.mark.parametrize("i", range(1, 127)) def test_dumps_recursion_valid_short_2(self, i): root = make_recursive_list_dict(i, "level", "next") orjson.dumps(root) def test_dumps_recursion_limit(self): root = make_recursive_list_dict(128, "level", "next") with pytest.raises(orjson.JSONEncodeError): orjson.dumps(root) ================================================ FILE: test/test_reentrant.py ================================================ # SPDX-License-Identifier: (Apache-2.0 OR MIT) # Copyright Anders Kaseorg (2023) import orjson class C: c: "C" def __del__(self): orjson.loads('"' + "a" * 10000 + '"') def test_reentrant(): c = C() c.c = c del c orjson.loads("[" + "[]," * 1000 + "[]]") ================================================ FILE: test/test_roundtrip.py ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2018-2026) import orjson from .util import needs_data, read_fixture_str @needs_data class TestJsonChecker: def _run_roundtrip_json(self, filename, byte_exact=True): data = read_fixture_str(filename, "roundtrip") if byte_exact: assert orjson.dumps(orjson.loads(data)) == data.encode("utf-8") else: assert orjson.loads(orjson.dumps(orjson.loads(data))) == orjson.loads(data) def test_roundtrip001(self): """ roundtrip001.json """ self._run_roundtrip_json("roundtrip01.json") def test_roundtrip002(self): """ roundtrip002.json """ self._run_roundtrip_json("roundtrip02.json") def test_roundtrip003(self): """ roundtrip003.json """ self._run_roundtrip_json("roundtrip03.json") def test_roundtrip004(self): """ roundtrip004.json """ self._run_roundtrip_json("roundtrip04.json") def test_roundtrip005(self): """ roundtrip005.json """ self._run_roundtrip_json("roundtrip05.json") def test_roundtrip006(self): """ roundtrip006.json """ self._run_roundtrip_json("roundtrip06.json") def test_roundtrip007(self): """ roundtrip007.json """ self._run_roundtrip_json("roundtrip07.json") def test_roundtrip008(self): """ roundtrip008.json """ self._run_roundtrip_json("roundtrip08.json") def test_roundtrip009(self): """ roundtrip009.json """ self._run_roundtrip_json("roundtrip09.json") def test_roundtrip010(self): """ roundtrip010.json """ self._run_roundtrip_json("roundtrip10.json") def test_roundtrip011(self): """ roundtrip011.json """ self._run_roundtrip_json("roundtrip11.json") def test_roundtrip012(self): """ roundtrip012.json """ self._run_roundtrip_json("roundtrip12.json") def test_roundtrip013(self): """ roundtrip013.json """ self._run_roundtrip_json("roundtrip13.json") def test_roundtrip014(self): """ roundtrip014.json """ self._run_roundtrip_json("roundtrip14.json") def test_roundtrip015(self): """ roundtrip015.json """ self._run_roundtrip_json("roundtrip15.json") def test_roundtrip016(self): """ roundtrip016.json """ self._run_roundtrip_json("roundtrip16.json") def test_roundtrip017(self): """ roundtrip017.json """ self._run_roundtrip_json("roundtrip17.json") def test_roundtrip018(self): """ roundtrip018.json """ self._run_roundtrip_json("roundtrip18.json") def test_roundtrip019(self): """ roundtrip019.json """ self._run_roundtrip_json("roundtrip19.json") def test_roundtrip020(self): """ roundtrip020.json """ self._run_roundtrip_json("roundtrip20.json") def test_roundtrip021(self): """ roundtrip021.json """ self._run_roundtrip_json("roundtrip21.json") def test_roundtrip022(self): """ roundtrip022.json """ self._run_roundtrip_json("roundtrip22.json") def test_roundtrip023(self): """ roundtrip023.json """ self._run_roundtrip_json("roundtrip23.json") def test_roundtrip024(self): """ roundtrip024.json """ self._run_roundtrip_json("roundtrip24.json") def test_roundtrip025(self): """ roundtrip025.json """ self._run_roundtrip_json("roundtrip25.json") def test_roundtrip026(self): """ roundtrip026.json """ self._run_roundtrip_json("roundtrip26.json") def test_roundtrip027(self): """ roundtrip027.json """ self._run_roundtrip_json("roundtrip27.json", byte_exact=False) ================================================ FILE: test/test_sort_keys.py ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2020-2025) import orjson from .util import needs_data, read_fixture_obj @needs_data class TestDictSortKeys: # citm_catalog is already sorted def test_twitter_sorted(self): """ twitter.json sorted """ obj = read_fixture_obj("twitter.json.xz") assert list(obj.keys()) != sorted(list(obj.keys())) serialized = orjson.dumps(obj, option=orjson.OPT_SORT_KEYS) val = orjson.loads(serialized) assert list(val.keys()) == sorted(list(val.keys())) def test_canada_sorted(self): """ canada.json sorted """ obj = read_fixture_obj("canada.json.xz") assert list(obj.keys()) != sorted(list(obj.keys())) serialized = orjson.dumps(obj, option=orjson.OPT_SORT_KEYS) val = orjson.loads(serialized) assert list(val.keys()) == sorted(list(val.keys())) def test_github_sorted(self): """ github.json sorted """ obj = read_fixture_obj("github.json.xz") for each in obj: assert list(each.keys()) != sorted(list(each.keys())) serialized = orjson.dumps(obj, option=orjson.OPT_SORT_KEYS) val = orjson.loads(serialized) for each in val: assert list(each.keys()) == sorted(list(each.keys())) def test_utf8_sorted(self): """ UTF-8 sorted """ obj = {"a": 1, "ä": 2, "A": 3} assert list(obj.keys()) != sorted(list(obj.keys())) serialized = orjson.dumps(obj, option=orjson.OPT_SORT_KEYS) val = orjson.loads(serialized) assert list(val.keys()) == sorted(list(val.keys())) ================================================ FILE: test/test_subclass.py ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2018-2022) import collections import json import pytest import orjson class SubStr(str): pass class SubInt(int): pass class SubDict(dict): pass class SubList(list): pass class SubFloat(float): pass class SubTuple(tuple): pass class TestSubclass: def test_subclass_str(self): assert orjson.dumps(SubStr("zxc")) == b'"zxc"' def test_subclass_str_invalid(self): with pytest.raises(orjson.JSONEncodeError): orjson.dumps(SubStr("\ud800")) def test_subclass_int(self): assert orjson.dumps(SubInt(1)) == b"1" def test_subclass_int_64(self): for val in (9223372036854775807, -9223372036854775807): assert orjson.dumps(SubInt(val)) == str(val).encode("utf-8") def test_subclass_int_53(self): for val in (9007199254740992, -9007199254740992): with pytest.raises(orjson.JSONEncodeError): orjson.dumps(SubInt(val), option=orjson.OPT_STRICT_INTEGER) def test_subclass_dict(self): assert orjson.dumps(SubDict({"a": "b"})) == b'{"a":"b"}' def test_subclass_list(self): assert orjson.dumps(SubList(["a", "b"])) == b'["a","b"]' ref = [True] * 512 assert orjson.loads(orjson.dumps(SubList(ref))) == ref def test_subclass_float(self): with pytest.raises(orjson.JSONEncodeError): orjson.dumps(SubFloat(1.1)) assert json.dumps(SubFloat(1.1)) == "1.1" def test_subclass_tuple(self): with pytest.raises(orjson.JSONEncodeError): orjson.dumps(SubTuple((1, 2))) assert json.dumps(SubTuple((1, 2))) == "[1, 2]" def test_namedtuple(self): Point = collections.namedtuple("Point", ["x", "y"]) with pytest.raises(orjson.JSONEncodeError): orjson.dumps(Point(1, 2)) def test_subclass_circular_dict(self): obj = SubDict({}) obj["obj"] = obj with pytest.raises(orjson.JSONEncodeError): orjson.dumps(obj) def test_subclass_circular_list(self): obj = SubList([]) obj.append(obj) with pytest.raises(orjson.JSONEncodeError): orjson.dumps(obj) def test_subclass_circular_nested(self): obj = SubDict({}) obj["list"] = SubList([{"obj": obj}]) with pytest.raises(orjson.JSONEncodeError): orjson.dumps(obj) class TestSubclassPassthrough: def test_subclass_str(self): with pytest.raises(orjson.JSONEncodeError): orjson.dumps(SubStr("zxc"), option=orjson.OPT_PASSTHROUGH_SUBCLASS) def test_subclass_int(self): with pytest.raises(orjson.JSONEncodeError): orjson.dumps(SubInt(1), option=orjson.OPT_PASSTHROUGH_SUBCLASS) def test_subclass_dict(self): with pytest.raises(orjson.JSONEncodeError): orjson.dumps(SubDict({"a": "b"}), option=orjson.OPT_PASSTHROUGH_SUBCLASS) def test_subclass_list(self): with pytest.raises(orjson.JSONEncodeError): orjson.dumps(SubList(["a", "b"]), option=orjson.OPT_PASSTHROUGH_SUBCLASS) ================================================ FILE: test/test_transform.py ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2019-2025) import pytest import orjson from .util import needs_data, read_fixture_bytes def _read_file(filename): return read_fixture_bytes(filename, "transform").strip(b"\n").strip(b"\r") @needs_data class TestJSONTestSuiteTransform: def _pass_transform(self, filename, reference=None): data = _read_file(filename) assert orjson.dumps(orjson.loads(data)) == (reference or data) def _fail_transform(self, filename): data = _read_file(filename) with pytest.raises(orjson.JSONDecodeError): orjson.loads(data) def test_number_1(self): """ number_1.0.json """ self._pass_transform("number_1.0.json") def test_number_1e6(self): """ number_1e6.json """ self._pass_transform("number_1e6.json", b"[1000000.0]") def test_number_1e_999(self): """ number_1e-999.json """ self._pass_transform("number_1e-999.json", b"[0.0]") def test_number_10000000000000000999(self): """ number_10000000000000000999.json """ # cannot serialize due to range assert orjson.loads(_read_file("number_10000000000000000999.json")) == [ 10000000000000000999, ] def test_number_1000000000000000(self): """ number_1000000000000000.json """ self._pass_transform("number_1000000000000000.json") def test_object_key_nfc_nfd(self): """ object_key_nfc_nfd.json """ self._pass_transform("object_key_nfc_nfd.json") def test_object_key_nfd_nfc(self): """ object_key_nfd_nfc.json """ self._pass_transform("object_key_nfd_nfc.json") def test_object_same_key_different_values(self): """ object_same_key_different_values.json """ self._pass_transform("object_same_key_different_values.json", b'{"a":2}') def test_object_same_key_same_value(self): """ object_same_key_same_value.json """ self._pass_transform("object_same_key_same_value.json", b'{"a":1}') def test_object_same_key_unclear_values(self): """ object_same_key_unclear_values.json """ data = _read_file("object_same_key_unclear_values.json") # varies by backend assert data in (b'{"a":-0.0}', b'{"a":0, "a":-0}') def test_string_1_escaped_invalid_codepoint(self): """ string_1_escaped_invalid_codepoint.json """ self._fail_transform("string_1_escaped_invalid_codepoint.json") def test_string_1_invalid_codepoint(self): """ string_1_invalid_codepoint.json """ self._fail_transform("string_1_invalid_codepoint.json") def test_string_2_escaped_invalid_codepoints(self): """ string_2_escaped_invalid_codepoints.json """ self._fail_transform("string_2_escaped_invalid_codepoints.json") def test_string_2_invalid_codepoints(self): """ string_2_invalid_codepoints.json """ self._fail_transform("string_2_invalid_codepoints.json") def test_string_3_escaped_invalid_codepoints(self): """ string_3_escaped_invalid_codepoints.json """ self._fail_transform("string_3_escaped_invalid_codepoints.json") def test_string_3_invalid_codepoints(self): """ string_3_invalid_codepoints.json """ self._fail_transform("string_3_invalid_codepoints.json") def test_string_with_escaped_NULL(self): """ string_with_escaped_NULL.json """ self._pass_transform("string_with_escaped_NULL.json") ================================================ FILE: test/test_type.py ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2018-2026) import io import sys import pytest import orjson from .util import SUPPORTS_BYTEARRAY, SUPPORTS_MEMORYVIEW class TestType: def test_fragment(self): """ orjson.JSONDecodeError on fragments """ for val in ("n", "{", "[", "t"): pytest.raises(orjson.JSONDecodeError, orjson.loads, val) def test_invalid(self): """ orjson.JSONDecodeError on invalid """ for val in ('{"age", 44}', "[31337,]", "[,31337]", "[]]", "[,]"): pytest.raises(orjson.JSONDecodeError, orjson.loads, val) def test_str(self): """ str """ for obj, ref in (("blah", b'"blah"'), ("東京", b'"\xe6\x9d\xb1\xe4\xba\xac"')): assert orjson.dumps(obj) == ref assert orjson.loads(ref) == obj def test_str_latin1(self): """ str latin1 """ assert orjson.loads(orjson.dumps("üýþÿ")) == "üýþÿ" def test_str_long(self): """ str long """ for obj in ("aaaa" * 1024, "üýþÿ" * 1024, "好" * 1024, "�" * 1024): assert orjson.loads(orjson.dumps(obj)) == obj def test_str_2mib(self): ref = '🐈🐈🐈🐈🐈"üýa0s9999🐈🐈🐈🐈🐈9\0999\\9999' * 1024 * 50 assert orjson.loads(orjson.dumps(ref)) == ref def test_str_very_long(self): """ str long enough to trigger overflow in bytecount """ for obj in ("aaaa" * 20000, "üýþÿ" * 20000, "好" * 20000, "�" * 20000): assert orjson.loads(orjson.dumps(obj)) == obj def test_str_replacement(self): """ str roundtrip � """ assert orjson.dumps("�") == b'"\xef\xbf\xbd"' assert orjson.loads(b'"\xef\xbf\xbd"') == "�" def test_str_trailing_4_byte(self): ref = "うぞ〜😏🙌" assert orjson.loads(orjson.dumps(ref)) == ref def test_str_ascii_control(self): """ worst case format_escaped_str_with_escapes() allocation """ ref = "\x01\x1f" * 1024 * 16 assert orjson.loads(orjson.dumps(ref)) == ref assert orjson.loads(orjson.dumps(ref, option=orjson.OPT_INDENT_2)) == ref def test_str_escape_quote_0(self): assert orjson.dumps('"aaaaaaabb') == b'"\\"aaaaaaabb"' def test_str_escape_quote_1(self): assert orjson.dumps('a"aaaaaabb') == b'"a\\"aaaaaabb"' def test_str_escape_quote_2(self): assert orjson.dumps('aa"aaaaabb') == b'"aa\\"aaaaabb"' def test_str_escape_quote_3(self): assert orjson.dumps('aaa"aaaabb') == b'"aaa\\"aaaabb"' def test_str_escape_quote_4(self): assert orjson.dumps('aaaa"aaabb') == b'"aaaa\\"aaabb"' def test_str_escape_quote_5(self): assert orjson.dumps('aaaaa"aabb') == b'"aaaaa\\"aabb"' def test_str_escape_quote_6(self): assert orjson.dumps('aaaaaa"abb') == b'"aaaaaa\\"abb"' def test_str_escape_quote_7(self): assert orjson.dumps('aaaaaaa"bb') == b'"aaaaaaa\\"bb"' def test_str_escape_quote_8(self): assert orjson.dumps('aaaaaaaab"') == b'"aaaaaaaab\\""' def test_str_escape_quote_multi(self): assert ( orjson.dumps('aa"aaaaabbbbbbbbbbbbbbbbbbbb"bb') == b'"aa\\"aaaaabbbbbbbbbbbbbbbbbbbb\\"bb"' ) def test_str_escape_quote_buffer(self): orjson.dumps(['"' * 4096] * 1024) def test_str_escape_backslash_0(self): assert orjson.dumps("\\aaaaaaabb") == b'"\\\\aaaaaaabb"' def test_str_escape_backslash_1(self): assert orjson.dumps("a\\aaaaaabb") == b'"a\\\\aaaaaabb"' def test_str_escape_backslash_2(self): assert orjson.dumps("aa\\aaaaabb") == b'"aa\\\\aaaaabb"' def test_str_escape_backslash_3(self): assert orjson.dumps("aaa\\aaaabb") == b'"aaa\\\\aaaabb"' def test_str_escape_backslash_4(self): assert orjson.dumps("aaaa\\aaabb") == b'"aaaa\\\\aaabb"' def test_str_escape_backslash_5(self): assert orjson.dumps("aaaaa\\aabb") == b'"aaaaa\\\\aabb"' def test_str_escape_backslash_6(self): assert orjson.dumps("aaaaaa\\abb") == b'"aaaaaa\\\\abb"' def test_str_escape_backslash_7(self): assert orjson.dumps("aaaaaaa\\bb") == b'"aaaaaaa\\\\bb"' def test_str_escape_backslash_8(self): assert orjson.dumps("aaaaaaaab\\") == b'"aaaaaaaab\\\\"' def test_str_escape_backslash_multi(self): assert ( orjson.dumps("aa\\aaaaabbbbbbbbbbbbbbbbbbbb\\bb") == b'"aa\\\\aaaaabbbbbbbbbbbbbbbbbbbb\\\\bb"' ) def test_str_escape_backslash_buffer(self): orjson.dumps(["\\" * 4096] * 1024) def test_str_escape_x32_0(self): assert orjson.dumps("\taaaaaaabb") == b'"\\taaaaaaabb"' def test_str_escape_x32_1(self): assert orjson.dumps("a\taaaaaabb") == b'"a\\taaaaaabb"' def test_str_escape_x32_2(self): assert orjson.dumps("aa\taaaaabb") == b'"aa\\taaaaabb"' def test_str_escape_x32_3(self): assert orjson.dumps("aaa\taaaabb") == b'"aaa\\taaaabb"' def test_str_escape_x32_4(self): assert orjson.dumps("aaaa\taaabb") == b'"aaaa\\taaabb"' def test_str_escape_x32_5(self): assert orjson.dumps("aaaaa\taabb") == b'"aaaaa\\taabb"' def test_str_escape_x32_6(self): assert orjson.dumps("aaaaaa\tabb") == b'"aaaaaa\\tabb"' def test_str_escape_x32_7(self): assert orjson.dumps("aaaaaaa\tbb") == b'"aaaaaaa\\tbb"' def test_str_escape_x32_8(self): assert orjson.dumps("aaaaaaaab\t") == b'"aaaaaaaab\\t"' def test_str_escape_x32_multi(self): assert ( orjson.dumps("aa\taaaaabbbbbbbbbbbbbbbbbbbb\tbb") == b'"aa\\taaaaabbbbbbbbbbbbbbbbbbbb\\tbb"' ) def test_str_escape_x32_buffer(self): orjson.dumps(["\t" * 4096] * 1024) def test_str_emoji(self): ref = "®️" assert orjson.loads(orjson.dumps(ref)) == ref def test_str_emoji_escape(self): ref = '/"®️/"' assert orjson.loads(orjson.dumps(ref)) == ref def test_very_long_list(self): orjson.dumps([[]] * 1024 * 16) def test_very_long_list_pretty(self): orjson.dumps([[]] * 1024 * 16, option=orjson.OPT_INDENT_2) def test_very_long_dict(self): orjson.dumps([{}] * 1024 * 16) def test_very_long_dict_pretty(self): orjson.dumps([{}] * 1024 * 16, option=orjson.OPT_INDENT_2) def test_very_long_str_empty(self): orjson.dumps([""] * 1024 * 16) def test_very_long_str_empty_pretty(self): orjson.dumps([""] * 1024 * 16, option=orjson.OPT_INDENT_2) def test_very_long_str_not_empty(self): orjson.dumps(["a"] * 1024 * 16) def test_very_long_str_not_empty_pretty(self): orjson.dumps(["a"] * 1024 * 16, option=orjson.OPT_INDENT_2) def test_very_long_bool(self): orjson.dumps([True] * 1024 * 16) def test_very_long_bool_pretty(self): orjson.dumps([True] * 1024 * 16, option=orjson.OPT_INDENT_2) def test_very_long_int(self): orjson.dumps([(2**64) - 1] * 1024 * 16) def test_very_long_int_pretty(self): orjson.dumps([(2**64) - 1] * 1024 * 16, option=orjson.OPT_INDENT_2) def test_very_long_float(self): orjson.dumps([sys.float_info.max] * 1024 * 16) def test_very_long_float_pretty(self): orjson.dumps([sys.float_info.max] * 1024 * 16, option=orjson.OPT_INDENT_2) def test_str_surrogates_loads(self): """ str unicode surrogates loads() """ pytest.raises(orjson.JSONDecodeError, orjson.loads, '"\ud800"') pytest.raises(orjson.JSONDecodeError, orjson.loads, '"\ud83d\ude80"') pytest.raises(orjson.JSONDecodeError, orjson.loads, '"\udcff"') pytest.raises( orjson.JSONDecodeError, orjson.loads, b'"\xed\xa0\xbd\xed\xba\x80"', ) # \ud83d\ude80 def test_str_surrogates_dumps(self): """ str unicode surrogates dumps() """ pytest.raises(orjson.JSONEncodeError, orjson.dumps, "\ud800") pytest.raises(orjson.JSONEncodeError, orjson.dumps, "\ud83d\ude80") pytest.raises(orjson.JSONEncodeError, orjson.dumps, "\udcff") pytest.raises(orjson.JSONEncodeError, orjson.dumps, {"\ud83d\ude80": None}) pytest.raises( orjson.JSONEncodeError, orjson.dumps, b"\xed\xa0\xbd\xed\xba\x80", ) # \ud83d\ude80 def test_bytes_dumps(self): """ bytes dumps not supported """ with pytest.raises(orjson.JSONEncodeError): orjson.dumps([b"a"]) def test_bytes_loads(self): """ bytes loads """ assert orjson.loads(b"[]") == [] @pytest.mark.skipif(SUPPORTS_BYTEARRAY is False, reason="bytearray") def test_bytearray_loads(self): """ bytearray loads """ arr = bytearray() arr.extend(b"[]") assert orjson.loads(arr) == [] @pytest.mark.skipif(SUPPORTS_MEMORYVIEW is True, reason="memoryview") def test_memoryview_loads_supported(self): """ memoryview loads supported """ assert orjson.loads(memoryview(b"[]")) == [] @pytest.mark.skipif(SUPPORTS_MEMORYVIEW is False, reason="memoryview") def test_memoryview_loads_unsupported(self): """ memoryview loads unsupported """ with pytest.raises(orjson.JSONDecodeError): orjson.loads(memoryview(b"[]")) @pytest.mark.skipif(SUPPORTS_BYTEARRAY is False, reason="bytearray") def test_bytesio_loads_supported(self): """ BytesIO loads supported """ arr = io.BytesIO(b"[]") assert orjson.loads(arr.getbuffer()) == [] @pytest.mark.skipif(SUPPORTS_BYTEARRAY is True, reason="bytearray") def test_bytesio_loads_unsupported(self): """ BytesIO loads unsupported """ arr = io.BytesIO(b"[]") with pytest.raises(orjson.JSONDecodeError): orjson.loads(arr.getbuffer()) def test_bool(self): """ bool """ for obj, ref in ((True, "true"), (False, "false")): assert orjson.dumps(obj) == ref.encode("utf-8") assert orjson.loads(ref) == obj def test_bool_true_array(self): """ bool true array """ obj = [True] * 256 ref = ("[" + ("true," * 255) + "true]").encode("utf-8") assert orjson.dumps(obj) == ref assert orjson.loads(ref) == obj def test_bool_false_array(self): """ bool false array """ obj = [False] * 256 ref = ("[" + ("false," * 255) + "false]").encode("utf-8") assert orjson.dumps(obj) == ref assert orjson.loads(ref) == obj def test_none(self): """ null """ obj = None ref = "null" assert orjson.dumps(obj) == ref.encode("utf-8") assert orjson.loads(ref) == obj def test_int(self): """ int compact and non-compact """ obj = [-5000, -1000, -10, -5, -2, -1, 0, 1, 2, 5, 10, 1000, 50000] ref = b"[-5000,-1000,-10,-5,-2,-1,0,1,2,5,10,1000,50000]" assert orjson.dumps(obj) == ref assert orjson.loads(ref) == obj def test_null_array(self): """ null array """ obj = [None] * 256 ref = ("[" + ("null," * 255) + "null]").encode("utf-8") assert orjson.dumps(obj) == ref assert orjson.loads(ref) == obj def test_nan_dumps(self): """ NaN serializes to null """ assert orjson.dumps(float("NaN")) == b"null" def test_nan_loads(self): """ NaN is not valid JSON """ with pytest.raises(orjson.JSONDecodeError): orjson.loads("[NaN]") with pytest.raises(orjson.JSONDecodeError): orjson.loads("[nan]") def test_infinity_dumps(self): """ Infinity serializes to null """ assert orjson.dumps(float("Infinity")) == b"null" def test_infinity_loads(self): """ Infinity, -Infinity is not valid JSON """ with pytest.raises(orjson.JSONDecodeError): orjson.loads("[infinity]") with pytest.raises(orjson.JSONDecodeError): orjson.loads("[Infinity]") with pytest.raises(orjson.JSONDecodeError): orjson.loads("[-Infinity]") with pytest.raises(orjson.JSONDecodeError): orjson.loads("[-infinity]") def test_int_53(self): """ int 53-bit """ for val in (9007199254740991, -9007199254740991): assert orjson.loads(str(val)) == val assert orjson.dumps(val, option=orjson.OPT_STRICT_INTEGER) == str( val, ).encode("utf-8") def test_int_53_exc(self): """ int 53-bit exception on 64-bit """ for val in (9007199254740992, -9007199254740992): with pytest.raises(orjson.JSONEncodeError): orjson.dumps(val, option=orjson.OPT_STRICT_INTEGER) def test_int_53_exc_usize(self): """ int 53-bit exception on 64-bit usize """ for val in (9223372036854775808, 18446744073709551615): with pytest.raises(orjson.JSONEncodeError): orjson.dumps(val, option=orjson.OPT_STRICT_INTEGER) def test_int_53_exc_128(self): """ int 53-bit exception on 128-bit """ val = 2**65 with pytest.raises(orjson.JSONEncodeError): orjson.dumps(val, option=orjson.OPT_STRICT_INTEGER) def test_int_64(self): """ int 64-bit """ for val in (9223372036854775807, -9223372036854775807): assert orjson.loads(str(val)) == val assert orjson.dumps(val) == str(val).encode("utf-8") def test_uint_64(self): """ uint 64-bit """ for val in (0, 9223372036854775808, 18446744073709551615): assert orjson.loads(str(val)) == val assert orjson.dumps(val) == str(val).encode("utf-8") def test_int_128(self): """ int 128-bit """ for val in (18446744073709551616, -9223372036854775809): pytest.raises(orjson.JSONEncodeError, orjson.dumps, val) def test_float(self): """ float """ assert -1.1234567893 == orjson.loads("-1.1234567893") assert -1.234567893 == orjson.loads("-1.234567893") assert -1.34567893 == orjson.loads("-1.34567893") assert -1.4567893 == orjson.loads("-1.4567893") assert -1.567893 == orjson.loads("-1.567893") assert -1.67893 == orjson.loads("-1.67893") assert -1.7893 == orjson.loads("-1.7893") assert -1.893 == orjson.loads("-1.893") assert -1.3 == orjson.loads("-1.3") assert 1.1234567893 == orjson.loads("1.1234567893") assert 1.234567893 == orjson.loads("1.234567893") assert 1.34567893 == orjson.loads("1.34567893") assert 1.4567893 == orjson.loads("1.4567893") assert 1.567893 == orjson.loads("1.567893") assert 1.67893 == orjson.loads("1.67893") assert 1.7893 == orjson.loads("1.7893") assert 1.893 == orjson.loads("1.893") assert 1.3 == orjson.loads("1.3") def test_float_precision_loads(self): """ float precision loads() """ assert orjson.loads("31.245270191439438") == 31.245270191439438 assert orjson.loads("-31.245270191439438") == -31.245270191439438 assert orjson.loads("121.48791951161945") == 121.48791951161945 assert orjson.loads("-121.48791951161945") == -121.48791951161945 assert orjson.loads("100.78399658203125") == 100.78399658203125 assert orjson.loads("-100.78399658203125") == -100.78399658203125 def test_float_precision_dumps(self): """ float precision dumps() """ assert orjson.dumps(31.245270191439438) == b"31.245270191439438" assert orjson.dumps(-31.245270191439438) == b"-31.245270191439438" assert orjson.dumps(121.48791951161945) == b"121.48791951161945" assert orjson.dumps(-121.48791951161945) == b"-121.48791951161945" assert orjson.dumps(100.78399658203125) == b"100.78399658203125" assert orjson.dumps(-100.78399658203125) == b"-100.78399658203125" def test_float_edge(self): """ float edge cases """ assert orjson.dumps(0.8701) == b"0.8701" assert orjson.loads("0.8701") == 0.8701 assert ( orjson.loads("0.0000000000000000000000000000000000000000000000000123e50") == 1.23 ) assert orjson.loads("0.4e5") == 40000.0 assert orjson.loads("0.00e-00") == 0.0 assert orjson.loads("0.4e-001") == 0.04 assert orjson.loads("0.123456789e-12") == 1.23456789e-13 assert orjson.loads("1.234567890E+34") == 1.23456789e34 assert orjson.loads("23456789012E66") == 2.3456789012e76 def test_float_notation(self): """ float notation """ for val in ("1.337E40", "1.337e+40", "1337e40", "1.337E-4"): obj = orjson.loads(val) assert obj == float(val) assert orjson.dumps(val) == (f'"{val}"').encode("utf-8") def test_list(self): """ list """ obj = ["a", "😊", True, {"b": 1.1}, 2] ref = '["a","😊",true,{"b":1.1},2]' assert orjson.dumps(obj) == ref.encode("utf-8") assert orjson.loads(ref) == obj def test_tuple(self): """ tuple """ obj = ("a", "😊", True, {"b": 1.1}, 2) ref = '["a","😊",true,{"b":1.1},2]' assert orjson.dumps(obj) == ref.encode("utf-8") assert orjson.loads(ref) == list(obj) def test_object(self): """ object() dumps() """ with pytest.raises(orjson.JSONEncodeError): orjson.dumps(object()) ================================================ FILE: test/test_typeddict.py ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2019-2023) import orjson try: from typing import TypedDict # type: ignore except ImportError: from typing_extensions import TypedDict class TestTypedDict: def test_typeddict(self): """ dumps() TypedDict """ class TypedDict1(TypedDict): a: str b: int obj = TypedDict1(a="a", b=1) assert orjson.dumps(obj) == b'{"a":"a","b":1}' ================================================ FILE: test/test_uuid.py ================================================ # SPDX-License-Identifier: (Apache-2.0 OR MIT) # Copyright ijl (2020-2025), Rami Chowdhury (2020) import uuid import pytest import orjson class TestUUID: def test_uuid_immutable(self): """ UUID objects are immutable """ val = uuid.uuid4() with pytest.raises(TypeError): val.int = 1 # type: ignore with pytest.raises(TypeError): val.int = None # type: ignore def test_uuid_int(self): """ UUID.int is a 128-bit integer """ val = uuid.UUID("7202d115-7ff3-4c81-a7c1-2a1f067b1ece") assert isinstance(val.int, int) assert val.int >= 2**64 assert val.int < 2**128 assert val.int == 151546616840194781678008611711208857294 def test_uuid_overflow(self): """ UUID.int can't trigger errors in _PyLong_AsByteArray """ with pytest.raises(ValueError): uuid.UUID(int=2**128) with pytest.raises(ValueError): uuid.UUID(int=-1) def test_uuid_subclass(self): """ UUID subclasses are not serialized """ class AUUID(uuid.UUID): pass with pytest.raises(orjson.JSONEncodeError): orjson.dumps(AUUID("{12345678-1234-5678-1234-567812345678}")) def test_serializes_withopt(self): """ dumps() accepts deprecated OPT_SERIALIZE_UUID """ assert ( orjson.dumps( uuid.UUID("7202d115-7ff3-4c81-a7c1-2a1f067b1ece"), option=orjson.OPT_SERIALIZE_UUID, ) == b'"7202d115-7ff3-4c81-a7c1-2a1f067b1ece"' ) def test_nil_uuid(self): assert ( orjson.dumps(uuid.UUID("00000000-0000-0000-0000-000000000000")) == b'"00000000-0000-0000-0000-000000000000"' ) def test_all_ways_to_create_uuid_behave_equivalently(self): # Note that according to the docstring for the uuid.UUID class, all the # forms below are equivalent -- they end up with the same value for # `self.int`, which is all that really matters uuids = [ uuid.UUID("{12345678-1234-5678-1234-567812345678}"), uuid.UUID("12345678123456781234567812345678"), uuid.UUID("urn:uuid:12345678-1234-5678-1234-567812345678"), uuid.UUID(bytes=b"\x12\x34\x56\x78" * 4), uuid.UUID( bytes_le=b"\x78\x56\x34\x12\x34\x12\x78\x56\x12\x34\x56\x78\x12\x34\x56\x78", ), uuid.UUID(fields=(0x12345678, 0x1234, 0x5678, 0x12, 0x34, 0x567812345678)), uuid.UUID(int=0x12345678123456781234567812345678), ] result = orjson.dumps(uuids) canonical_uuids = [f'"{u!s}"' for u in uuids] serialized = ("[{}]".format(",".join(canonical_uuids))).encode("utf8") assert result == serialized def test_serializes_correctly_with_leading_zeroes(self): instance = uuid.UUID(int=0x00345678123456781234567812345678) assert orjson.dumps(instance) == (f'"{instance!s}"').encode("utf-8") def test_all_uuid_creation_functions_create_serializable_uuids(self): uuids = ( uuid.uuid1(), uuid.uuid3(uuid.NAMESPACE_DNS, "python.org"), uuid.uuid4(), uuid.uuid5(uuid.NAMESPACE_DNS, "python.org"), ) for val in uuids: assert orjson.dumps(val) == f'"{val}"'.encode("utf-8") ================================================ FILE: test/util.py ================================================ # SPDX-License-Identifier: MPL-2.0 # Copyright ijl (2018-2026) import lzma import os import sys import sysconfig from pathlib import Path from typing import Any IS_FREETHREADING = sysconfig.get_config_var("Py_GIL_DISABLED") SUPPORTS_MEMORYVIEW = sys.implementation == "cpython" and not IS_FREETHREADING SUPPORTS_BYTEARRAY = not IS_FREETHREADING SUPPORTS_GETREFCOUNT = sys.implementation == "cpython" numpy = None # type: ignore if not IS_FREETHREADING: try: import numpy # type: ignore # noqa: F401 except ImportError: pass pandas = None # type: ignore if not IS_FREETHREADING: try: import pandas # type: ignore # noqa: F401 except ImportError: pass import pytest import orjson data_dir = os.path.join(os.path.dirname(__file__), "../data") STR_CACHE: dict[str, str] = {} OBJ_CACHE: dict[str, Any] = {} def read_fixture_bytes(filename, subdir=None): if subdir is None: path = Path(data_dir, filename) else: path = Path(data_dir, subdir, filename) if path.suffix == ".xz": contents = lzma.decompress(path.read_bytes()) else: contents = path.read_bytes() return contents def read_fixture_str(filename, subdir=None): if filename not in STR_CACHE: STR_CACHE[filename] = read_fixture_bytes(filename, subdir).decode("utf-8") return STR_CACHE[filename] def read_fixture_obj(filename): if filename not in OBJ_CACHE: OBJ_CACHE[filename] = orjson.loads(read_fixture_str(filename)) return OBJ_CACHE[filename] needs_data = pytest.mark.skipif( not Path(data_dir).exists(), reason="Test depends on ./data dir that contains fixtures", )