Skip to content

Commit e4e03ca

Browse files
authored
Switch to jsonv2 wholesale (#1486)
1 parent b0ee841 commit e4e03ca

File tree

189 files changed

+2672
-2720
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

189 files changed

+2672
-2720
lines changed

.golangci.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ linters:
1515
- canonicalheader
1616
- copyloopvar
1717
- customlint
18+
- depguard
1819
- durationcheck
1920
- errcheck
2021
- errchkjson
@@ -62,10 +63,18 @@ linters:
6263
customlint:
6364
type: module
6465

66+
depguard:
67+
rules:
68+
main:
69+
deny:
70+
- pkg: 'encoding/json$'
71+
desc: 'Use "github.com/microsoft/typescript-go/internal/json" instead.'
72+
6573
exclusions:
6674
rules:
6775
- path: internal/fourslash/tests/gen/
68-
linters: misspell
76+
linters:
77+
- misspell
6978

7079
presets:
7180
- comments

NOTICE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ SOFTWARE.
230230

231231
---------------------------------------------------------
232232

233-
github.com/go-json-experiment/json v0.0.0-20250714165856-be8212f5270d - BSD-3-Clause
233+
github.com/go-json-experiment/json v0.0.0-20250725192818-e39067aee2d2 - BSD-3-Clause
234234

235235
Copyright (c) 2020 The Go Authors. All rights reserved.
236236

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module github.com/microsoft/typescript-go
22

3-
go 1.24.0
3+
go 1.24.2
44

55
require (
66
github.com/dlclark/regexp2 v1.11.5
7-
github.com/go-json-experiment/json v0.0.0-20250714165856-be8212f5270d
7+
github.com/go-json-experiment/json v0.0.0-20250725192818-e39067aee2d2
88
github.com/google/go-cmp v0.7.0
99
github.com/peter-evans/patience v0.3.0
1010
golang.org/x/sync v0.16.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
github.com/dlclark/regexp2 v1.11.5 h1:Q/sSnsKerHeCkc/jSTNq1oCm7KiVgUMZRDUoRu0JQZQ=
22
github.com/dlclark/regexp2 v1.11.5/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
3-
github.com/go-json-experiment/json v0.0.0-20250714165856-be8212f5270d h1:+d6m5Bjvv0/RJct1VcOw2P5bvBOGjENmxORJYnSYDow=
4-
github.com/go-json-experiment/json v0.0.0-20250714165856-be8212f5270d/go.mod h1:TiCD2a1pcmjd7YnhGH0f/zKNcCD06B029pHhzV23c2M=
3+
github.com/go-json-experiment/json v0.0.0-20250725192818-e39067aee2d2 h1:iizUGZ9pEquQS5jTGkh4AqeeHCMbfbjeb0zMt0aEFzs=
4+
github.com/go-json-experiment/json v0.0.0-20250725192818-e39067aee2d2/go.mod h1:TiCD2a1pcmjd7YnhGH0f/zKNcCD06B029pHhzV23c2M=
55
github.com/go-quicktest/qt v1.101.0 h1:O1K29Txy5P2OK0dGo59b7b0LR6wKfIhttaAhHUyn7eI=
66
github.com/go-quicktest/qt v1.101.0/go.mod h1:14Bz/f7NwaXPtdYEgzsx46kqSxVwTbzVZsDC26tQJow=
77
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=

internal/api/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package api
22

33
import (
44
"context"
5-
"encoding/json"
65
"errors"
76
"fmt"
87
"sync"
@@ -12,6 +11,7 @@ import (
1211
"github.com/microsoft/typescript-go/internal/astnav"
1312
"github.com/microsoft/typescript-go/internal/checker"
1413
"github.com/microsoft/typescript-go/internal/core"
14+
"github.com/microsoft/typescript-go/internal/json"
1515
"github.com/microsoft/typescript-go/internal/lsp/lsproto"
1616
"github.com/microsoft/typescript-go/internal/project"
1717
"github.com/microsoft/typescript-go/internal/tsoptions"

internal/api/proto.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package api
22

33
import (
4-
"encoding/json"
54
"errors"
65
"fmt"
76
"strconv"
@@ -10,6 +9,7 @@ import (
109
"github.com/microsoft/typescript-go/internal/ast"
1110
"github.com/microsoft/typescript-go/internal/checker"
1211
"github.com/microsoft/typescript-go/internal/core"
12+
"github.com/microsoft/typescript-go/internal/json"
1313
"github.com/microsoft/typescript-go/internal/project"
1414
)
1515

internal/api/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import (
44
"bufio"
55
"context"
66
"encoding/binary"
7-
"encoding/json"
87
"fmt"
98
"io"
109
"strconv"
1110
"sync"
1211

1312
"github.com/microsoft/typescript-go/internal/bundled"
1413
"github.com/microsoft/typescript-go/internal/core"
14+
"github.com/microsoft/typescript-go/internal/json"
1515
"github.com/microsoft/typescript-go/internal/project"
1616
"github.com/microsoft/typescript-go/internal/vfs"
1717
"github.com/microsoft/typescript-go/internal/vfs/osvfs"

internal/collections/ordered_map.go

Lines changed: 19 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
package collections
22

33
import (
4-
"bytes"
54
"encoding"
6-
"encoding/json"
75
"errors"
8-
"fmt"
96
"iter"
107
"maps"
118
"reflect"
129
"slices"
1310
"strconv"
1411

15-
json2 "github.com/go-json-experiment/json"
1612
"github.com/go-json-experiment/json/jsontext"
13+
"github.com/microsoft/typescript-go/internal/json"
1714
)
1815

1916
// OrderedMap is an insertion ordered map.
@@ -203,37 +200,30 @@ func (m *OrderedMap[K, V]) clone() OrderedMap[K, V] {
203200
}
204201
}
205202

206-
func (m *OrderedMap[K, V]) MarshalJSON() ([]byte, error) {
207-
if len(m.mp) == 0 {
208-
return []byte("{}"), nil
203+
var _ json.MarshalerTo = (*OrderedMap[string, string])(nil)
204+
205+
func (m *OrderedMap[K, V]) MarshalJSONTo(enc *jsontext.Encoder) error {
206+
if err := enc.WriteToken(jsontext.BeginObject); err != nil {
207+
return err
209208
}
210-
var buf bytes.Buffer
211-
buf.WriteByte('{')
212-
enc := json.NewEncoder(&buf)
213-
enc.SetEscapeHTML(false)
214-
215-
for i, k := range m.keys {
216-
if i > 0 {
217-
buf.WriteByte(',')
218-
}
219209

210+
for _, k := range m.keys {
211+
// TODO: is this needed? Can we just MarshalEncode k directly?
220212
keyString, err := resolveKeyName(reflect.ValueOf(k))
221213
if err != nil {
222-
return nil, err
214+
return err
223215
}
224216

225-
if err := enc.Encode(keyString); err != nil {
226-
return nil, err
217+
if err := json.MarshalEncode(enc, keyString); err != nil {
218+
return err
227219
}
228220

229-
buf.WriteByte(':')
230-
231-
if err := enc.Encode(m.mp[k]); err != nil {
232-
return nil, err
221+
if err := json.MarshalEncode(enc, m.mp[k]); err != nil {
222+
return err
233223
}
234224
}
235-
buf.WriteByte('}')
236-
return buf.Bytes(), nil
225+
226+
return enc.WriteToken(jsontext.EndObject)
237227
}
238228

239229
func resolveKeyName(k reflect.Value) (string, error) {
@@ -256,50 +246,7 @@ func resolveKeyName(k reflect.Value) (string, error) {
256246
panic("unexpected map key type")
257247
}
258248

259-
var (
260-
_ json.Unmarshaler = (*OrderedMap[string, string])(nil)
261-
_ json2.UnmarshalerFrom = (*OrderedMap[string, string])(nil)
262-
)
263-
264-
func (m *OrderedMap[K, V]) UnmarshalJSON(data []byte) error {
265-
if string(data) == "null" {
266-
// By convention, to approximate the behavior of Unmarshal itself,
267-
// Unmarshalers implement UnmarshalJSON([]byte("null")) as a no-op.
268-
// https://pkg.go.dev/encoding/json#Unmarshaler
269-
return nil
270-
}
271-
dec := json.NewDecoder(bytes.NewReader(data))
272-
token, err := dec.Token()
273-
if err != nil {
274-
return err
275-
}
276-
if token != json.Delim('{') {
277-
return errors.New("cannot unmarshal non-object JSON value into Map")
278-
}
279-
for dec.More() {
280-
nameToken, err := dec.Token()
281-
if err != nil {
282-
return err
283-
}
284-
if nameToken == json.Delim('}') {
285-
break
286-
}
287-
if key, ok := nameToken.(K); ok {
288-
var valueBytes json.RawMessage
289-
if err := dec.Decode(&valueBytes); err != nil {
290-
return err
291-
}
292-
var value V
293-
if err := json.Unmarshal(valueBytes, &value); err != nil {
294-
return err
295-
}
296-
m.Set(key, value)
297-
} else {
298-
return fmt.Errorf("cannot unmarshal key into Map[%v, ...]", reflect.TypeFor[K]())
299-
}
300-
}
301-
return nil
302-
}
249+
var _ json.UnmarshalerFrom = (*OrderedMap[string, string])(nil)
303250

304251
func (m *OrderedMap[K, V]) UnmarshalJSONFrom(dec *jsontext.Decoder) error {
305252
token, err := dec.ReadToken()
@@ -310,6 +257,7 @@ func (m *OrderedMap[K, V]) UnmarshalJSONFrom(dec *jsontext.Decoder) error {
310257
// By convention, to approximate the behavior of Unmarshal itself,
311258
// Unmarshalers implement UnmarshalJSON([]byte("null")) as a no-op.
312259
// https://pkg.go.dev/encoding/json#Unmarshaler
260+
// TODO: reconsider
313261
return nil
314262
}
315263
if token.Kind() != '{' { // jsontext.ObjectStart.Kind()
@@ -318,10 +266,10 @@ func (m *OrderedMap[K, V]) UnmarshalJSONFrom(dec *jsontext.Decoder) error {
318266
for dec.PeekKind() != '}' { // jsontext.ObjectEnd.Kind()
319267
var key K
320268
var value V
321-
if err := json2.UnmarshalDecode(dec, &key); err != nil {
269+
if err := json.UnmarshalDecode(dec, &key); err != nil {
322270
return err
323271
}
324-
if err := json2.UnmarshalDecode(dec, &value); err != nil {
272+
if err := json.UnmarshalDecode(dec, &value); err != nil {
325273
return err
326274
}
327275
m.Set(key, value)

internal/collections/ordered_map_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package collections_test
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"slices"
76
"testing"
87

9-
json2 "github.com/go-json-experiment/json"
108
"github.com/microsoft/typescript-go/internal/collections"
9+
"github.com/microsoft/typescript-go/internal/json"
1110
"gotest.tools/v3/assert"
1211
)
1312

@@ -162,13 +161,9 @@ func TestOrderedMapWithSizeHint(t *testing.T) { //nolint:paralleltest
162161
func TestOrderedMapUnmarshalJSON(t *testing.T) {
163162
t.Parallel()
164163

165-
t.Run("UnmarshalJSON", func(t *testing.T) {
166-
t.Parallel()
167-
testOrderedMapUnmarshalJSON(t, json.Unmarshal)
168-
})
169164
t.Run("UnmarshalJSONV2", func(t *testing.T) {
170165
t.Parallel()
171-
testOrderedMapUnmarshalJSON(t, func(in []byte, out any) error { return json2.Unmarshal(in, out) })
166+
testOrderedMapUnmarshalJSON(t, func(in []byte, out any) error { return json.Unmarshal(in, out) })
172167
})
173168
}
174169

internal/compiler/program.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package compiler
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76
"maps"
87
"slices"
@@ -15,6 +14,7 @@ import (
1514
"github.com/microsoft/typescript-go/internal/collections"
1615
"github.com/microsoft/typescript-go/internal/core"
1716
"github.com/microsoft/typescript-go/internal/diagnostics"
17+
"github.com/microsoft/typescript-go/internal/json"
1818
"github.com/microsoft/typescript-go/internal/module"
1919
"github.com/microsoft/typescript-go/internal/modulespecifiers"
2020
"github.com/microsoft/typescript-go/internal/outputpaths"

0 commit comments

Comments
 (0)