First, we do not recommend that you use cookies for Object Persistence. Because cookies are included in each request, they increase the size of the request.
If the domain of the cookie is not properly managed, the problem will be even greater. For example, all cookies under domain.com will be included in your request for js.domain.com or img.domain.com, however, cookies in these domains are meaningless. Therefore, many websites use other domain names such as jsdomain.com or imgdomain.com to reduce the Cookie's request overhead.
Existing tool: jquery. jsoncookie. js (see http://lab.distilldesign.com/json-cookie/), this plug-in depends on jquery. Cookie. JS, if the browser does not support the JSON method, you may also need a json2.js.
Why not use this plug-in directly? Let's look at an example first:
Object:{ID: 123, name: "mapping", test: "Hello World "}
Convert the string to JSON. stringify:'{& Quot; Id & quot;: 123, & quot; Name & quot;: & quot; Mapping & quot;, & quot; Test & quot;: & quot; Hello World & quot "}'Length: 48
Convert it to Uri encode and useEncodeuricomponent :'% 7b % 22id % 22% 3a123% 2C % 22 name % 22% 3A % 22 mapping % 22% 2C % 22 test % 22% 3A % 22 Hello % 20 world % 22% 7d'Length: 84
We can see that the length of the transcoded JSON string is almost doubled because the browser will escape the special characters {, ", and so on for security considerations.
If the plug-in above will inevitably increase the cookie overhead, we need to avoid the cookie content being transcoded as much as possible. You can consider using -,_,. use ". "It may conflict with the decimal point of the data, but in most cases it is an integer that uses the cookie to record the ID, so you can also consider
Cookiestring = {Setting: {groupfix: '_' , Childfix: '-' , Itemfix: '.' }, Stringify: Function (OBJ ){ VaR T = This , Groups = [], Isarray = Function (AR ){ Return Object. Prototype. tostring. Call (AR) = '[object array]' ;}, O, items; If (Isarray (OBJ) {groups = OBJ ;} Else { For (O In OBJ ){ If (Obj. hasownproperty (O) {items = OBJ [O]; If (Isarray (OBJ [O]) {items = Items. Join (T. Setting. itemfix);} groups. Push (o + T. Setting. childfix +Items );}}} Return Groups. Join (T. Setting. groupfix) ;}, parse: Function (STR ){ VaR T = This , Groups = Str. Split (T. Setting. groupfix), O, C, I, OBJ = False ; If (Str. indexof (T. Setting. childfix )! =-1 ) {OBJ = {};} Else {OBJ = [];} For (O In Groups) {C = Groups [O]. Split (T. Setting. childfix ); If (C [1] = Undefined) {obj. Push (C [ 0 ]);} Else {I = C [1 ]. Split (T. Setting. itemfix); OBJ [C [ 0] = I. Length = 1? I [0 ]: I ;}} Return OBJ ;}};
We recommend that you use qunit as the testing framework. TIPS: If you need to see the test result on the page, add a <Div id = "qunit"> </div> node
Test ('cookiestring. stringify ',Function() {Equal (cookiestring. stringify ({1: [, 3],}), '1-1.2.3 _ 4', 'stringify Object'); Equal (cookiestring. stringify ([1, 2, 3, 4]), '1 _ 2_3_4 ', 'stringify array') ;}); Test ('Cookiestring. parse ',Function() {Deepequal (cookiestring. parse ('1-1.2.3 _ 2-4 '), {1: ['1', '2', '3'], 2: '4'}, 'parse Object'); Deepequal (cookiestring. parse ('1 _ 2_3_4 '), ['1', '2', '3', '4'], 'parse Object');});
Passed the test. ^_^
The object can be converted to a string. You can use jquery. Cookie. js to read and write cookies;
The process is slow and the process is not over yet. In this way, the backend will not parse the cookie value as easily as converting the JSON file. However, if the cookie format is specified in the project, the back-end public libraries that parse the format will also be handy.
PS: the use of open-source libraries can indeed save a lot of development time, but it will also increase a lot of additional overhead; there is no right or wrong between the use and use, only suitable and not suitable, if you feel inappropriate, write it yourself ~~