Ohm-Management - Projektarbeit B-ME
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 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. CircularJSON
  2. ============
  3. ![Downloads](https://img.shields.io/npm/dm/circular-json.svg) [![Build Status](https://travis-ci.org/WebReflection/circular-json.svg?branch=master)](https://travis-ci.org/WebReflection/circular-json) [![Coverage Status](https://coveralls.io/repos/github/WebReflection/circular-json/badge.svg?branch=master)](https://coveralls.io/github/WebReflection/circular-json?branch=master) [![donate](https://img.shields.io/badge/$-donate-ff69b4.svg?maxAge=2592000&style=flat)](https://github.com/WebReflection/donate)
  4. Serializes and deserializes otherwise valid JSON objects containing circular references into and from a specialized JSON format.
  5. - - -
  6. ### A Working Solution To A Common Problem
  7. A usage example:
  8. ```JavaScript
  9. var object = {};
  10. object.arr = [
  11. object, object
  12. ];
  13. object.arr.push(object.arr);
  14. object.obj = object;
  15. var serialized = CircularJSON.stringify(object);
  16. // '{"arr":["~","~","~arr"],"obj":"~"}'
  17. // NOTE: CircularJSON DOES NOT parse JS
  18. // it handles receiver and reviver callbacks
  19. var unserialized = CircularJSON.parse(serialized);
  20. // { arr: [ [Circular], [Circular] ],
  21. // obj: [Circular] }
  22. unserialized.obj === unserialized;
  23. unserialized.arr[0] === unserialized;
  24. unserialized.arr.pop() === unserialized.arr;
  25. ```
  26. A quick summary:
  27. * uses `~` as a special prefix symbol to denote which parent the reference belongs to (i.e. `~root~child1~child2`)
  28. * reasonably fast in both serialization and deserialization
  29. * compact serialization for easier and slimmer transportation across environments
  30. * [tested and covered](test/circular-json.js) over nasty structures too
  31. * compatible with all JavaScript engines
  32. Node Installation & Usage
  33. ============
  34. ```bash
  35. npm install --save circular-json
  36. ```
  37. ```javascript
  38. 'use strict';
  39. var
  40. CircularJSON = require('circular-json'),
  41. obj = { foo: 'bar' },
  42. str
  43. ;
  44. obj.self = obj;
  45. str = CircularJSON.stringify(obj);
  46. ```
  47. There are no dependencies.
  48. Browser Installation & Usage
  49. ================
  50. * Global: <build/circular-json.js>
  51. * AMD: <build/circular-json.amd.js>
  52. * CommonJS: <build/circular-json.node.js>
  53. (generated via [gitstrap](https://github.com/WebReflection/gitstrap))
  54. ```html
  55. <script src="build/circular-json.js"></script>
  56. ```
  57. ```javascript
  58. 'use strict';
  59. var CircularJSON = window.CircularJSON
  60. , obj = { foo: 'bar' }
  61. , str
  62. ;
  63. obj.self = obj;
  64. str = CircularJSON.stringify(obj);
  65. ```
  66. NOTE: Platforms without native JSON (i.e. MSIE <= 8) requires `json3.js` or similar.
  67. It is also *a bad idea* to `CircularJSON.parse(JSON.stringify(object))` because of those manipulation used in `CircularJSON.stringify()` able to make parsing safe and secure.
  68. As summary: `CircularJSON.parse(CircularJSON.stringify(object))` is the way to go, same is for `JSON.parse(JSON.stringify(object))`.
  69. API
  70. ===
  71. It's the same as native JSON, except the fourth parameter `placeholder`, which circular references to be replaced with `"[Circular]"` (i.e. for logging).
  72. * CircularJSON.stringify(object, replacer, spacer, placeholder)
  73. * CircularJSON.parse(string, reviver)
  74. Bear in mind `JSON.parse(CircularJSON.stringify(object))` will work but not produce the expected output.
  75. Similar Libraries
  76. =======
  77. ### Why Not the [@izs](https://twitter.com/izs) One
  78. The module [json-stringify-safe](https://github.com/isaacs/json-stringify-safe) seems to be for `console.log()` but it's completely pointless for `JSON.parse()`, being latter one unable to retrieve back the initial structure. Here an example:
  79. ```JavaScript
  80. // a logged object with circular references
  81. {
  82. "circularRef": "[Circular]",
  83. "list": [
  84. "[Circular]",
  85. "[Circular]"
  86. ]
  87. }
  88. // what do we do with above output ?
  89. ```
  90. Just type this in your `node` console: `var o = {}; o.a = o; console.log(o);`. The output will be `{ a: [Circular] }` ... good, but that ain't really solving the problem.
  91. However, if that's all you need, the function used to create that kind of output is probably faster than `CircularJSON` and surely fits in less lines of code.
  92. ### Why Not {{put random name}} Solution
  93. So here the thing: circular references can be wrong but, if there is a need for them, any attempt to ignore them or remove them can be considered just a failure.
  94. Not because the method is bad or it's not working, simply because the circular info, the one we needed and used in the first place, is lost!
  95. In this case, `CircularJSON` does even more than just solve circular and recursions: it maps all same objects so that less memory is used as well on deserialization as less bandwidth too!
  96. It's able to redefine those references back later on so the way we store is the way we retrieve and in a reasonably performant way, also trusting the snappy and native `JSON` methods to iterate.