json_encoding.sql 3.2 KB

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