encoding/jsonschema: fix a few json schema bugs

Issue #378

- encoders should strip leading BOM. (Note that we do
not write the BOM back).

- fix bug in Validate that incorrectly required a struct
to be concrete when in non-concrete mode

- improve some error messages

- disable example handling (was a noop currently anyway)

- don't mark a type corresponding to a constraint as  used
if it is not actually used.

Change-Id: I687fb1e17b9d4c2ef2136f829690569cf77d6707
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6104
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/internal/encoding/encoding.go b/internal/encoding/encoding.go
index a828a1b..dbe069e 100644
--- a/internal/encoding/encoding.go
+++ b/internal/encoding/encoding.go
@@ -40,6 +40,8 @@
 	"cuelang.org/go/internal"
 	"cuelang.org/go/internal/filetypes"
 	"cuelang.org/go/internal/third_party/yaml"
+	"golang.org/x/text/encoding/unicode"
+	"golang.org/x/text/transform"
 )
 
 type Decoder struct {
@@ -169,13 +171,21 @@
 		return i
 	}
 
-	r, err := reader(f, cfg.Stdin)
-	i.closer = r
+	rc, err := reader(f, cfg.Stdin)
+	i.closer = rc
 	i.err = err
 	if err != nil {
 		return i
 	}
 
+	// For now we assume that all encodings require UTF-8. This will not be the
+	// case for some binary protocols. We need to exempt those explicitly here
+	// once we introduce them.
+	// TODO: this code also allows UTF16, which is too permissive for some
+	// encodings. Switch to unicode.UTF8Sig once available.
+	t := unicode.BOMOverride(unicode.UTF8.NewDecoder())
+	r := transform.NewReader(rc, t)
+
 	switch f.Interpretation {
 	case "":
 	case build.Auto: