fastJson.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* eslint-disable */
  2. var FastJson = {
  3. isArray: function (a) {
  4. return typeof a === 'object' && Object.prototype.toString.call(a).toLowerCase() === '[object array]'
  5. },
  6. isObject: function (a) {
  7. return typeof a == 'object' && Object.prototype.toString.call(a).toLowerCase() == '[object object]'
  8. },
  9. format: function (a) {
  10. if (a == null) { return null }
  11. typeof a == 'string' && (a = eval('(' + a + ')'))
  12. return this._format(a, a, null, null, null)
  13. },
  14. _randomId: function () {
  15. return 'randomId_' + parseInt(1E9 * Math.random())
  16. },
  17. _getJsonValue: function (a, c) {
  18. var d = this._randomId()
  19. var b
  20. b = '' + ('function' + d + '(root){') + ('return root.' + c + ';')
  21. b += '}'
  22. b += ''
  23. var e = document.createElement('script')
  24. e.id = d
  25. e.text = b
  26. document.body.appendChild(e)
  27. d = window[d](a)
  28. e.parentNode.removeChild(e)
  29. return d
  30. },
  31. _format: function (a, c, d, b, e) {
  32. d || (d = '')
  33. if (this.isObject(c)) {
  34. if (c.$ref) {
  35. var g = c.$ref
  36. g.indexOf('$.') == 0 &&
  37. (b[e] = this._getJsonValue(a, g.substring(2)))
  38. return
  39. }
  40. for (var f in c) {
  41. b = d, b != '' && (b += '.'), g = c[f], b += f, this
  42. ._format(a, g, b, c, f)
  43. }
  44. } else if (this.isArray(c)) {
  45. for (f in c) {
  46. b = d, g = c[f], b = b + '[' + f + ']', this._format(a, g,
  47. b, c, f)
  48. }
  49. }
  50. return a
  51. }
  52. }
  53. module.exports = {
  54. FastJson
  55. }