如何截断数据中的散列,使得所得的JSON不大于n个字节长? [英] How to truncate data in a hash so that the resulting JSON isn't longer than n bytes?

查看:177
本文介绍了如何截断数据中的散列,使得所得的JSON不大于n个字节长?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个哈希值,看起来是这样的:

I have a hash that looks something like this:

{ :a => "some string", :b => "another string", :c => "yet another string" }

我wan't叫 to_json 就可以了,最终,但由此产生的JSON字符串的长度不能超过 N 字节。

I wan't to call to_jsonon it eventually, but the resulting json-string cannot be longer than n bytes.

如果字符串过大,那么首先:C 应首先截断。如果这还不够,:乙应该被截断。最后,:一个。另外,字符串可以包含多字节字符,如德国的变音符号和Ruby的版本是1.8.7。 (该变音先取2个字节,但JSON他们是5个字节长。)

If the string is too big, then first :c should be truncated first. If that's not enough, :b should be truncated. Finally :a. Also the strings can contain multi-byte characters like German umlauts and the Ruby version is 1.8.7. (The umlauts first take 2 bytes, but as json they are 5 bytes long.)

我写的是可以转换的散列to_json并检查长度的循环。如果其小于或等于 N 它回来,否则我Concat的值:一个 + 是b + :C ,并缩短了一半。如果新的哈希值过大(小),我​​缩短(延长),1/4,1/8,原始字符串的1/16。最后我得到的 hash.as_json长度==ñ

What I wrote was a loop that converts the hash to_json and checks the length. If its less or equal n it's returned, otherwise I concat the values of :a + :b + :c and shorten by half. If the new hash is too big(small), I shorten(extend) by 1/4th, 1/8th, 1/16th of the original string. Finally I get length of hash.as_json == n.

这一切都感觉非常的hackish,虽然所有的测试检查出来,我不知道,甚至稳定。

It all feels very hackish and although all tests check out I'm not sure that's even stable.

有没有人有一个很好的建议如何解决这个正常吗?

Does anyone have a good suggestion how to solve this properly?

推荐答案

怎么样:

# encoding:UTF-8

require 'rubygems'
require 'json'

def constrained_json(limit, a, b, c)
  output, size, hash  = nil, 0, { :a => a, :b => b, :c => c}
  [:c, :b, :a, :a].each do |key|
    output = hash.to_json
    size = output.bytesize
    break if size <= limit
    # on 1.9:
    # hash[key] = hash[key][0...(limit - size)]
    # on 1.8.7
    hash[key] = hash[key].unpack("U*")[0...(limit - size)].pack("U*")
  end
  raise "Size exceeds limit even after truncation" if size > limit
  output
end

38.downto(21) do |length|
  puts "# #{constrained_json(length, "Qué te", "parece", "eh?")}"
end

# {"a":"Qué te","b":"parece","c":"eh?"}
# {"a":"Qué te","b":"parece","c":"eh"}
# {"a":"Qué te","b":"parece","c":"e"}
# {"a":"Qué te","b":"parece","c":""}
# {"a":"Qué te","b":"parec","c":""}
# {"a":"Qué te","b":"pare","c":""}
# ...
# {"a":"","b":"","c":""}
# test.rb:14:in `constrained_json': Size exceeds limit even after truncation (RuntimeError)

这篇关于如何截断数据中的散列,使得所得的JSON不大于n个字节长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆