Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

README.md 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # clarinet
  2. ![NPM Downloads](http://img.shields.io/npm/dm/clarinet.svg?style=flat) ![NPM Version](http://img.shields.io/npm/v/clarinet.svg?style=flat) [![CDNJS](https://img.shields.io/cdnjs/v/clarinet.svg)](https://cdnjs.com/libraries/clarinet)
  3. `clarinet` is a sax-like streaming parser for JSON. works in the browser and node.js. `clarinet` is inspired (and forked) from [sax-js][saxjs]. just like you shouldn't use `sax` when you need `dom` you shouldn't use `clarinet` when you need `JSON.parse`. for a more detailed introduction and a performance study please refer to this [article][blog].
  4. # design goals
  5. `clarinet` is very much like [yajl] but written in javascript:
  6. * written in javascript
  7. * portable
  8. * robust (~110 tests pass before even announcing the project)
  9. * data representation independent
  10. * fast
  11. * generates verbose, useful error messages including context of where
  12. the error occurs in the input text.
  13. * can parse json data off a stream, incrementally
  14. * simple to use
  15. * tiny
  16. # motivation
  17. the reason behind this work was to create better full text support in node. creating indexes out of large (or many) json files doesn't require a full understanding of the json file, but it does require something like `clarinet`.
  18. # installation
  19. ## node.js
  20. 1. install [npm]
  21. 2. `npm install clarinet`
  22. 3. `var clarinet = require('clarinet');`
  23. ## browser
  24. 1. minimize clarinet.js
  25. 2. load it into your webpage
  26. # usage
  27. ## basics
  28. ``` js
  29. var clarinet = require("clarinet")
  30. , parser = clarinet.parser()
  31. ;
  32. parser.onerror = function (e) {
  33. // an error happened. e is the error.
  34. };
  35. parser.onvalue = function (v) {
  36. // got some value. v is the value. can be string, double, bool, or null.
  37. };
  38. parser.onopenobject = function (key) {
  39. // opened an object. key is the first key.
  40. };
  41. parser.onkey = function (key) {
  42. // got a subsequent key in an object.
  43. };
  44. parser.oncloseobject = function () {
  45. // closed an object.
  46. };
  47. parser.onopenarray = function () {
  48. // opened an array.
  49. };
  50. parser.onclosearray = function () {
  51. // closed an array.
  52. };
  53. parser.onend = function () {
  54. // parser stream is done, and ready to have more stuff written to it.
  55. };
  56. parser.write('{"foo": "bar"}').close();
  57. ```
  58. ``` js
  59. // stream usage
  60. // takes the same options as the parser
  61. var stream = require("clarinet").createStream(options);
  62. stream.on("error", function (e) {
  63. // unhandled errors will throw, since this is a proper node
  64. // event emitter.
  65. console.error("error!", e)
  66. // clear the error
  67. this._parser.error = null
  68. this._parser.resume()
  69. })
  70. stream.on("openobject", function (node) {
  71. // same object as above
  72. })
  73. // pipe is supported, and it's readable/writable
  74. // same chunks coming in also go out.
  75. fs.createReadStream("file.json")
  76. .pipe(stream)
  77. .pipe(fs.createReadStream("file-altered.json"))
  78. ```
  79. ## arguments
  80. pass the following arguments to the parser function. all are optional.
  81. `opt` - object bag of settings regarding string formatting. all default to `false`.
  82. settings supported:
  83. * `trim` - boolean. whether or not to trim text and comment nodes.
  84. * `normalize` - boolean. if true, then turn any whitespace into a single
  85. space.
  86. ## methods
  87. `write` - write bytes onto the stream. you don't have to do this all at
  88. once. you can keep writing as much as you want.
  89. `close` - close the stream. once closed, no more data may be written until
  90. it is done processing the buffer, which is signaled by the `end` event.
  91. `resume` - to gracefully handle errors, assign a listener to the `error`
  92. event. then, when the error is taken care of, you can call `resume` to
  93. continue parsing. otherwise, the parser will not continue while in an error
  94. state.
  95. ## members
  96. at all times, the parser object will have the following members:
  97. `line`, `column`, `position` - indications of the position in the json
  98. document where the parser currently is looking.
  99. `closed` - boolean indicating whether or not the parser can be written to.
  100. if it's `true`, then wait for the `ready` event to write again.
  101. `opt` - any options passed into the constructor.
  102. and a bunch of other stuff that you probably shouldn't touch.
  103. ## events
  104. all events emit with a single argument. to listen to an event, assign a
  105. function to `on<eventname>`. functions get executed in the this-context of
  106. the parser object. the list of supported events are also in the exported
  107. `EVENTS` array.
  108. when using the stream interface, assign handlers using the `EventEmitter`
  109. `on` function in the normal fashion.
  110. `error` - indication that something bad happened. the error will be hanging
  111. out on `parser.error`, and must be deleted before parsing can continue. by
  112. listening to this event, you can keep an eye on that kind of stuff. note:
  113. this happens *much* more in strict mode. argument: instance of `Error`.
  114. `value` - a json value. argument: value, can be a bool, null, string on number
  115. `openobject` - object was opened. argument: key, a string with the first key of the object (if any)
  116. `key` - an object key: argument: key, a string with the current key. Not called for first key (use `openobject` for that).
  117. `closeobject` - indication that an object was closed
  118. `openarray` - indication that an array was opened
  119. `closearray` - indication that an array was closed
  120. `end` - indication that the closed stream has ended.
  121. `ready` - indication that the stream has reset, and is ready to be written
  122. to.
  123. ## samples
  124. some [samples] are available to help you get started. one that creates a list of top npm contributors, and another that gets a bunch of data from twitter and generates valid json.
  125. # roadmap
  126. check [issues]
  127. # contribute
  128. everyone is welcome to contribute. patches, bug-fixes, new features
  129. 1. create an [issue][issues] so the community can comment on your idea
  130. 2. fork `clarinet`
  131. 3. create a new branch `git checkout -b my_branch`
  132. 4. create tests for the changes you made
  133. 5. make sure you pass both existing and newly inserted tests
  134. 6. commit your changes
  135. 7. push to your branch `git push origin my_branch`
  136. 8. create an pull request
  137. helpful tips:
  138. check `index.html`. there's two env vars you can set, `CRECORD` and `CDEBUG`.
  139. * `CRECORD` allows you to `record` the event sequence from a new json test so you don't have to write everything.
  140. * `CDEBUG` can be set to `info` or `debug`. `info` will `console.log` all emits, `debug` will `console.log` what happens to each char.
  141. in `test/clarinet.js` there's two lines you might want to change. `#8` where you define `seps`, if you are isolating a test you probably just want to run one sep, so change this array to `[undefined]`. `#718` which says `for (var key in docs) {` is where you can change the docs you want to run. e.g. to run `foobar` i would do something like `for (var key in {foobar:''}) {`.
  142. # meta
  143. * code: `git clone git://github.com/dscape/clarinet.git`
  144. * home: <http://github.com/dscape/clarinet>
  145. * bugs: <http://github.com/dscape/clarinet/issues>
  146. * build: [![build status](https://secure.travis-ci.org/dscape/clarinet.png)](http://travis-ci.org/dscape/clarinet)
  147. `(oO)--',-` in [caos]
  148. [npm]: http://npmjs.org
  149. [issues]: http://github.com/dscape/clarinet/issues
  150. [caos]: http://caos.di.uminho.pt/
  151. [saxjs]: http://github.com/isaacs/sax-js
  152. [yajl]: https://github.com/lloyd/yajl
  153. [samples]: https://github.com/dscape/clarinet/tree/master/samples
  154. [blog]: http://writings.nunojob.com/2011/12/clarinet-sax-based-evented-streaming-json-parser-in-javascript-for-the-browser-and-nodejs.html