encoding/json: don't simplify hidden fields

Hidden fields are no longer supported in the
spec, but as of now they still are.

Change-Id: I57cb8e95fd47af8aba0528bf243f3b0a6c309457
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/4422
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/encoding/json/json.go b/encoding/json/json.go
index db87de5..25600e9 100644
--- a/encoding/json/json.go
+++ b/encoding/json/json.go
@@ -218,7 +218,9 @@
 					break // should not happen: implies invalid JSON
 				}
 
-				if !ast.IsValidIdent(u) {
+				// TODO(legacy): remove checking for '_' prefix once hidden
+				// fields are removed.
+				if !ast.IsValidIdent(u) || strings.HasPrefix(u, "_") {
 					break // keep string
 				}
 
diff --git a/encoding/json/json_test.go b/encoding/json/json_test.go
index 853e654..f8469ca 100644
--- a/encoding/json/json_test.go
+++ b/encoding/json/json_test.go
@@ -108,6 +108,10 @@
 		name: "numeric keys: Issue #219",
 		in:   `{"20": "a"}`,
 		out:  `{"20": "a"}`,
+	}, {
+		name: "legacy: hidden fields",
+		in:   `{"_legacy": 1}`,
+		out:  `{"_legacy": 1}`,
 	}}
 	for _, tc := range testCases {
 		t.Run(tc.name, func(t *testing.T) {