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.

101 lines
3.9 KiB

пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
пре 13 година
пре 14 година
пре 14 година
пре 14 година
пре 14 година
  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. [![Build Status](https://travis-ci.org/davisp/jiffy.svg?branch=master)](https://travis-ci.org/davisp/jiffy)
  8. Usage
  9. -----
  10. Jiffy is a simple API. The only thing that might catch you off guard
  11. is that the return type of `jiffy:encode/1` is an iolist even though
  12. it returns a binary most of the time.
  13. A quick note on unicode. Jiffy only understands UTF-8 in binaries. End
  14. of story.
  15. Errors are raised as exceptions.
  16. Eshell V5.8.2 (abort with ^G)
  17. 1> jiffy:decode(<<"{\"foo\": \"bar\"}">>).
  18. {[{<<"foo">>,<<"bar">>}]}
  19. 2> Doc = {[{foo, [<<"bing">>, 2.3, true]}]}.
  20. {[{foo,[<<"bing">>,2.3,true]}]}
  21. 3> jiffy:encode(Doc).
  22. <<"{\"foo\":[\"bing\",2.3,true]}">>
  23. `jiffy:decode/1,2`
  24. ------------------
  25. * `jiffy:decode(IoData)`
  26. * `jiffy:decode(IoData, Options)`
  27. The options for decode are:
  28. * `{bytes_per_iter, N}` where N &gt;= 0 - This controls the number of
  29. bytes that Jiffy will process before yielding back to the VM. The
  30. mechanics of this yield are completely hidden from the end user.
  31. * `return_maps` - Tell Jiffy to return objects using the maps data type
  32. on VMs that support it. This raises an error on VMs that don't support
  33. maps.
  34. * `with_trailer` - Tell Jiffy to return the trailing unparsed data (if any) along with
  35. the parsed term instead of failing with {error,{_,invalid_traling_data}}. When
  36. the trailer is available, the return value is {with_trailer, EJson, Trailer},
  37. where Trailer is a sub-binary of the input, for efficiency.
  38. `jiffy:encode/1,2`
  39. ------------------
  40. * `jiffy:encode(EJSON)`
  41. * `jiffy:encode(EJSON, Options)`
  42. where EJSON is a valid representation of JSON in Erlang according to
  43. the table below.
  44. The options for encode are:
  45. * `uescape` - Escapes UTF-8 sequences to produce a 7-bit clean output
  46. * `pretty` - Produce JSON using two-space indentation
  47. * `force_utf8` - Force strings to encode as UTF-8 by fixing broken
  48. surrogate pairs and/or using the replacement character to remove
  49. broken UTF-8 sequences in data.
  50. * `{bytes_per_iter, N}` where N &gt;= 0 - This controls the number of
  51. bytes that Jiffy will generate before yielding back to the VM. The
  52. mechanics of this yield are completely hidden from the end user.
  53. Data Format
  54. -----------
  55. Erlang JSON Erlang
  56. ==========================================================================
  57. null -> null -> null
  58. true -> true -> true
  59. false -> false -> false
  60. "hi" -> [104, 105] -> [104, 105]
  61. <<"hi">> -> "hi" -> <<"hi">>
  62. hi -> "hi" -> <<"hi">>
  63. 1 -> 1 -> 1
  64. 1.25 -> 1.25 -> 1.25
  65. [] -> [] -> []
  66. [true, 1.0] -> [true, 1.0] -> [true, 1.0]
  67. {[]} -> {} -> {[]}
  68. {[{foo, bar}]} -> {"foo": "bar"} -> {[{<<"foo">>, <<"bar">>}]}
  69. {[{<<"foo">>, <<"bar">>}]} -> {"foo": "bar"} -> {[{<<"foo">>, <<"bar">>}]}
  70. #{<<"foo">> => <<"bar">>} -> {"foo": "bar"} -> #{<<"foo">> => <<"bar">>}
  71. N.B. The last entry in this table is only valid for VM's that support
  72. the `maps` data type (i.e., 17.0 and newer) and client code must pass
  73. the `return_maps` option to `jiffy:decode/2`.
  74. Improvements over EEP0018
  75. -------------------------
  76. Jiffy should be in all ways an improvemnt over EEP0018. It no longer
  77. imposes limits on the nesting depth. It is capable of encoding and
  78. decoding large numbers and it does quite a bit more validation of UTF-8 in strings.