You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 regels
3.1 KiB

14 jaren geleden
14 jaren geleden
14 jaren geleden
14 jaren geleden
  1. Jiffy - JSON NIFs for Erlang
  2. ============================
  3. A JSON parser as a NIF. This is a complete rewrite of the work I did
  4. in EEP0018 that was based on Yajl. This new version is a hand crafted
  5. state machine that does its best to be as quick and efficient as
  6. possible while not placing any constraints on the parsed JSON.
  7. Usage
  8. -----
  9. Jiffy is a simple API. The only thing that might catch you off guard
  10. is that the return type of `jiffy:encode/1` is an iolist even though
  11. it returns a binary most of the time.
  12. A quick note on unicode. Jiffy only understands UTF-8 in binaries. End
  13. of story.
  14. Errors are raised as exceptions.
  15. Eshell V5.8.2 (abort with ^G)
  16. 1> jiffy:decode(<<"{\"foo\": \"bar\"}">>).
  17. {[{<<"foo">>,<<"bar">>}]}
  18. 2> Doc = {[{foo, [<<"bing">>, 2.3, true]}]}.
  19. {[{foo,[<<"bing">>,2.3,true]}]}
  20. 3> jiffy:encode(Doc).
  21. <<"{\"foo\":[\"bing\",2.3,true]}">>
  22. `jiffy:decode/1,2`
  23. ------------------
  24. * `jiffy:decode(IoData)`
  25. * `jiffy:decode(IoData, Options)`
  26. The options for decode are:
  27. * `{bytes_per_iter, N}` where N &gt;= 0 - This controls the number of
  28. bytes that Jiffy will process before yielding back to the VM. The
  29. mechanics of this yield are completely hidden from the end user.
  30. `jiffy:encode/1,2`
  31. ------------------
  32. * `jiffy:encode(EJSON)`
  33. * `jiffy:encode(EJSON, Options)`
  34. where EJSON is a valid representation of JSON in Erlang according to
  35. the table below.
  36. The options for encode are:
  37. * `uescape` - Escapes UTF-8 sequences to produce a 7-bit clean output
  38. * `pretty` - Produce JSON using two-space indentation
  39. * `force_utf8` - Force strings to encode as UTF-8 by fixing broken
  40. surrogate pairs and/or using the replacement character to remove
  41. broken UTF-8 sequences in data.
  42. * `{bytes_per_iter, N}` where N &gt;= 0 - This controls the number of
  43. bytes that Jiffy will generate before yielding back to the VM. The
  44. mechanics of this yield are completely hidden from the end user.
  45. Data Format
  46. -----------
  47. Erlang JSON Erlang
  48. ==========================================================================
  49. null -> null -> null
  50. true -> true -> true
  51. false -> false -> false
  52. "hi" -> [104, 105] -> [104, 105]
  53. <<"hi">> -> "hi" -> <<"hi">>
  54. hi -> "hi" -> <<"hi">>
  55. 1 -> 1 -> 1
  56. 1.25 -> 1.25 -> 1.25
  57. [] -> [] -> []
  58. [true, 1.0] -> [true, 1.0] -> [true, 1.0]
  59. {[]} -> {} -> {[]}
  60. {[{foo, bar}]} -> {"foo": "bar"} -> {[{<<"foo">>, <<"bar">>}]}
  61. {[{<<"foo">>, <<"bar">>}]} -> {"foo": "bar"} -> {[{<<"foo">>, <<"bar">>}]}
  62. Improvements over EEP0018
  63. -------------------------
  64. Jiffy should be in all ways an improvemnt over EEP0018. It no longer
  65. imposes limits on the nesting depth. It is capable of encoding and
  66. decoding large numbers and it does quite a bit more validation of UTF-8 in strings.