Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6164561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:00:54+00:00 2026-05-23T22:00:54+00:00

I have a hash that looks something like this: { :a => some string,

  • 0

I have a hash that looks something like this:

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

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

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.)

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.

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:00:56+00:00Added an answer on May 23, 2026 at 10:00 pm

    How about:

    # 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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code in my application that looks something like this: char *hash
I have a hash that looks something like this: params = { :irrelevant =>
I have an PHP array parsed from a JSON string that looks like this:
I am working with a hash that looks something like this: h = {
I have a twenty byte hex hash that I would like to store in
I have some code on my PHP powered site that creates a random hash
I have a text in an email on a windows box that looks something
I have a URL that looks like http://mysite.com/#id/Blah-blah-blah , it's used for Ajax-ey bits.
I have a method that creates a MessageDigest (a hash) from a file, and
i have a long running function¹: public string FindPasswordFromHash(String hash) { ... } which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.