-1

I am using go to collect responses from URLs and store them in JSON for later processing. I want to enforce a limit on the size of the responses I store, however I've been unable to enforce this limit properly as when the JSON is marshalled the string is escaped which pushes it's length over the limit. How do I limit the length of the string including the escape characters when marshaling?

J. Doe
  • 1
  • 5
  • You could unmarshal and marshal the JSON through a `map[string]interface{}` to try and minimize the JSON. Removing unnecessary white space is about all I think you can do. – Corey Ogburn Oct 03 '19 at 20:00
  • 3
    Please clarify what you want to happen when the limit is breached. Do you want to fail encoding when the limit is breached, discard the result when the limit is breached, or something else? – Charlie Tumahai Oct 03 '19 at 20:18
  • See [Size control on logging an unknown length of parameters](https://stackoverflow.com/questions/57357799/size-control-on-logging-an-unknown-length-of-parameters/57358344#57358344). – icza Oct 04 '19 at 07:29

1 Answers1

0

How do I limit the length of the string including the escape characters when marshaling?

You cannot. Dead simple.

Volker
  • 40,468
  • 7
  • 81
  • 87