json_encoding.sql 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. --
  2. -- encoding-sensitive tests for json and jsonb
  3. --
  4. -- We provide expected-results files for UTF8 (json_encoding.out)
  5. -- and for SQL_ASCII (json_encoding_1.out). Skip otherwise.
  6. SELECT getdatabaseencoding() NOT IN ('UTF8', 'SQL_ASCII')
  7. AS skip_test \gset
  8. \if :skip_test
  9. \quit
  10. \endif
  11. SELECT getdatabaseencoding(); -- just to label the results files
  12. -- first json
  13. -- basic unicode input
  14. SELECT '"\u"'::json; -- ERROR, incomplete escape
  15. SELECT '"\u00"'::json; -- ERROR, incomplete escape
  16. SELECT '"\u000g"'::json; -- ERROR, g is not a hex digit
  17. SELECT '"\u0000"'::json; -- OK, legal escape
  18. SELECT '"\uaBcD"'::json; -- OK, uppercase and lower case both OK
  19. -- handling of unicode surrogate pairs
  20. select json '{ "a": "\ud83d\ude04\ud83d\udc36" }' -> 'a' as correct_in_utf8;
  21. select json '{ "a": "\ud83d\ud83d" }' -> 'a'; -- 2 high surrogates in a row
  22. select json '{ "a": "\ude04\ud83d" }' -> 'a'; -- surrogates in wrong order
  23. select json '{ "a": "\ud83dX" }' -> 'a'; -- orphan high surrogate
  24. select json '{ "a": "\ude04X" }' -> 'a'; -- orphan low surrogate
  25. --handling of simple unicode escapes
  26. select json '{ "a": "the Copyright \u00a9 sign" }' as correct_in_utf8;
  27. select json '{ "a": "dollar \u0024 character" }' as correct_everywhere;
  28. select json '{ "a": "dollar \\u0024 character" }' as not_an_escape;
  29. select json '{ "a": "null \u0000 escape" }' as not_unescaped;
  30. select json '{ "a": "null \\u0000 escape" }' as not_an_escape;
  31. select json '{ "a": "the Copyright \u00a9 sign" }' ->> 'a' as correct_in_utf8;
  32. select json '{ "a": "dollar \u0024 character" }' ->> 'a' as correct_everywhere;
  33. select json '{ "a": "dollar \\u0024 character" }' ->> 'a' as not_an_escape;
  34. select json '{ "a": "null \u0000 escape" }' ->> 'a' as fails;
  35. select json '{ "a": "null \\u0000 escape" }' ->> 'a' as not_an_escape;
  36. -- then jsonb
  37. -- basic unicode input
  38. SELECT '"\u"'::jsonb; -- ERROR, incomplete escape
  39. SELECT '"\u00"'::jsonb; -- ERROR, incomplete escape
  40. SELECT '"\u000g"'::jsonb; -- ERROR, g is not a hex digit
  41. SELECT '"\u0045"'::jsonb; -- OK, legal escape
  42. SELECT '"\u0000"'::jsonb; -- ERROR, we don't support U+0000
  43. -- use octet_length here so we don't get an odd unicode char in the
  44. -- output
  45. SELECT octet_length('"\uaBcD"'::jsonb::text); -- OK, uppercase and lower case both OK
  46. -- handling of unicode surrogate pairs
  47. SELECT octet_length((jsonb '{ "a": "\ud83d\ude04\ud83d\udc36" }' -> 'a')::text) AS correct_in_utf8;
  48. SELECT jsonb '{ "a": "\ud83d\ud83d" }' -> 'a'; -- 2 high surrogates in a row
  49. SELECT jsonb '{ "a": "\ude04\ud83d" }' -> 'a'; -- surrogates in wrong order
  50. SELECT jsonb '{ "a": "\ud83dX" }' -> 'a'; -- orphan high surrogate
  51. SELECT jsonb '{ "a": "\ude04X" }' -> 'a'; -- orphan low surrogate
  52. -- handling of simple unicode escapes
  53. SELECT jsonb '{ "a": "the Copyright \u00a9 sign" }' as correct_in_utf8;
  54. SELECT jsonb '{ "a": "dollar \u0024 character" }' as correct_everywhere;
  55. SELECT jsonb '{ "a": "dollar \\u0024 character" }' as not_an_escape;
  56. SELECT jsonb '{ "a": "null \u0000 escape" }' as fails;
  57. SELECT jsonb '{ "a": "null \\u0000 escape" }' as not_an_escape;
  58. SELECT jsonb '{ "a": "the Copyright \u00a9 sign" }' ->> 'a' as correct_in_utf8;
  59. SELECT jsonb '{ "a": "dollar \u0024 character" }' ->> 'a' as correct_everywhere;
  60. SELECT jsonb '{ "a": "dollar \\u0024 character" }' ->> 'a' as not_an_escape;
  61. SELECT jsonb '{ "a": "null \u0000 escape" }' ->> 'a' as fails;
  62. SELECT jsonb '{ "a": "null \\u0000 escape" }' ->> 'a' as not_an_escape;