How to deterministically verify that a JSON object hasn't been modified?

前端 未结 8 1739
忘了有多久
忘了有多久 2020-11-30 04:38

According to MDN documentation for JSON.stringify:

Properties of non-array objects are not guaranteed to be stringified in any particular order. Do

8条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 05:06

    You may want to try JSON.sortify, a little helper that I wrote.

    In contrast to the answers given so far, it

    • works with any level of nesting
    • can handle numeric keys
    • escapes special characters in keys
    • accepts the space parameter as well as the little used replacer parameter
    • throws a TypeError on cyclical references (as it should)
    • filters undefined values and functions
    • respects toJSON()

提交回复
热议问题