最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - reducing json string size by shortening variable names - Stack Overflow

programmeradmin2浏览0评论

I have some json formatted data that I parse with JSON.parse. The problem I have is with the length of the varialble names:

MyObjectName.SuperLongPropertyName;
MyObjectName.AnotherLongPropertyName;

and so on. I'm using JavascriptSerializer and a custom JavascriptConverter to generate the json. It's used in several ajax calls and the data is about 70K.

However, if I could change the variable to

   a.a;
   a.b;

that could shave off half the size of the json string.

I can easily change the serialization/deserialization classes on the server. However, on the client-side, it'd be more tricky because I'm using these objects everywhere in my code. And then, even if I do manage to change the variable name, it'd then bee pretty difficult code to debug in a javascript debugger.

What are some good suggestions to a) convert my existing variable names on the client, and b) maintain readability and debuggability?

Thanks.

I have some json formatted data that I parse with JSON.parse. The problem I have is with the length of the varialble names:

MyObjectName.SuperLongPropertyName;
MyObjectName.AnotherLongPropertyName;

and so on. I'm using JavascriptSerializer and a custom JavascriptConverter to generate the json. It's used in several ajax calls and the data is about 70K.

However, if I could change the variable to

   a.a;
   a.b;

that could shave off half the size of the json string.

I can easily change the serialization/deserialization classes on the server. However, on the client-side, it'd be more tricky because I'm using these objects everywhere in my code. And then, even if I do manage to change the variable name, it'd then bee pretty difficult code to debug in a javascript debugger.

What are some good suggestions to a) convert my existing variable names on the client, and b) maintain readability and debuggability?

Thanks.

Share Improve this question asked Jan 11, 2012 at 12:25 frenchiefrenchie 52.1k117 gold badges320 silver badges528 bronze badges 2
  • The real questions is, why would you want shorter json strings. Are you sending that much data? 70k on its own isn't that shocking – hoppa Commented Jan 11, 2012 at 12:28
  • You could build a sorted map (by number of occurrences) of property names and short names. Rewrite the json with those shorter names and send the map + json. ;) – Yoshi Commented Jan 11, 2012 at 12:35
Add a ment  | 

1 Answer 1

Reset to default 8

Use transparent HTTP gzip pression. Your server then presses the sent data on-the-fly, and the web browser decodes it again.

gzip does precisely what you suggested, only better. Instead of picking static aliases for some long repeating names, gzip picks aliasses for all names, and assigns the shortest ones to the aliasses which occur most. Also, gzip doesn't care about syntax, and can therefore assign , "SuperLongName": { (including punctuation and spaces) to a single symbol.

发布评论

评论列表(0)

  1. 暂无评论