1

A colleague and I were trying to write the minimal logic that we needed for compacting, validating, parsing, and storing json coming from a client.

Upon doing so, we realized that compacting and validating were two steps that were both being accomplished by json.Compact anyways, since the code indicates json.Compact calls the json Scanner. The scanner then validates json and errors on invalid json.

The docs do not make this explicit, but we think this is the case.

Here is a link: https://forum.golangbridge.org/t/json-compact-appears-to-also-validate-json-but-is-not-documented/23088

Let us know thoughts.

Jorvy
  • 69
  • 4
  • "Compact appends to dst the JSON-encoded src with insignificant space characters elided." It clearly states "JSON-encoded" and returns an error. How could it compact invalid JSON? What would a compacted invalid JSON look like? – Volker Apr 09 '21 at 21:35

1 Answers1

1

Yes.

json.Compact uses json.scanner while scanning the json. If the scanner encounters invalid JSON sets scanner.err, which is returned by json.Compact if there is an error.

This is the same way json.Valid checks for valid json, by simply scanning the JSON and checking for scanner.err.

Here's the relevant code sections:

Zamicol
  • 4,626
  • 1
  • 37
  • 42