Use json v2

This commit is contained in:
dwrz
2026-10-11 00:55:57 +00:00
parent 81b981c07f
commit 75040646e3
5 changed files with 23 additions and 24 deletions
+4 -4
View File
@@ -4,7 +4,7 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"context" "context"
"encoding/json" "encoding/json/v2"
"fmt" "fmt"
"io" "io"
"log/slog" "log/slog"
@@ -83,7 +83,7 @@ func (c *Client) completions(
ThinkingBudgetTokens: p.ThinkingBudgetTokens, ThinkingBudgetTokens: p.ThinkingBudgetTokens,
ChatTemplateKWARGS: kwargs, ChatTemplateKWARGS: kwargs,
ParallelToolCalls: new(true), ParallelToolCalls: new(true),
}) }, json.FormatNilSliceAsNull(true))
if err != nil { if err != nil {
return nil, fmt.Errorf( return nil, fmt.Errorf(
"completions: marshal request body: %w", err, "completions: marshal request body: %w", err,
@@ -152,7 +152,7 @@ func (c *Client) completions(
} }
var cr = &ChatResponse{} var cr = &ChatResponse{}
if err := json.NewDecoder(res.Body).Decode(cr); err != nil { if err := json.UnmarshalRead(res.Body, cr); err != nil {
return nil, fmt.Errorf("completions: decode response: %w", err) return nil, fmt.Errorf("completions: decode response: %w", err)
} }
if len(cr.Choices) == 0 { if len(cr.Choices) == 0 {
@@ -283,7 +283,7 @@ func (c *Client) CompletionsStream(
StreamOptions: &StreamOptions{IncludeUsage: true}, StreamOptions: &StreamOptions{IncludeUsage: true},
ThinkingBudgetTokens: p.ThinkingBudgetTokens, ThinkingBudgetTokens: p.ThinkingBudgetTokens,
Tools: p.Tools, Tools: p.Tools,
}) }, json.FormatNilSliceAsNull(true))
if err != nil { if err != nil {
cancel() cancel()
return nil, fmt.Errorf( return nil, fmt.Errorf(
+11 -12
View File
@@ -2,8 +2,7 @@ package llm
import ( import (
"context" "context"
"encoding/json" "encoding/json/v2"
jsonv2 "encoding/json/v2"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@@ -457,7 +456,7 @@ func TestCompletionsReturnsUsageAndTimings(t *testing.T) {
t.Errorf("unexpected path: %s", r.URL.Path) t.Errorf("unexpected path: %s", r.URL.Path)
} }
var request ChatRequest var request ChatRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil { if err := json.UnmarshalRead(r.Body, &request); err != nil {
t.Error(err) t.Error(err)
} }
if request.LogProbs || request.TopLogProbs != 0 { if request.LogProbs || request.TopLogProbs != 0 {
@@ -480,17 +479,17 @@ func TestCompletionsReturnsUsageAndTimings(t *testing.T) {
if !reflect.DeepEqual(res.Metadata, want) { if !reflect.DeepEqual(res.Metadata, want) {
t.Errorf("metadata = %#v, want %#v", res.Metadata, want) t.Errorf("metadata = %#v, want %#v", res.Metadata, want)
} }
encoded, err := jsonv2.Marshal( encoded, err := json.Marshal(
res.Metadata, jsonv2.FormatNilSliceAsNull(true), res.Metadata, json.FormatNilSliceAsNull(true),
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
var gotJSON, wantJSON map[string]any var gotJSON, wantJSON map[string]any
if err := jsonv2.Unmarshal(encoded, &gotJSON); err != nil { if err := json.Unmarshal(encoded, &gotJSON); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := jsonv2.Unmarshal([]byte(tt.want), &wantJSON); err != nil { if err := json.Unmarshal([]byte(tt.want), &wantJSON); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if !reflect.DeepEqual(gotJSON, wantJSON) { if !reflect.DeepEqual(gotJSON, wantJSON) {
@@ -737,7 +736,7 @@ data: {"choices":[{"index":0,"delta":{},"logprobs":{"content":null,"refusal":[{"
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
client := newCompletionTestClient(t, func(w http.ResponseWriter, r *http.Request) { client := newCompletionTestClient(t, func(w http.ResponseWriter, r *http.Request) {
var request ChatRequest var request ChatRequest
if err := json.NewDecoder(r.Body).Decode(&request); err != nil { if err := json.UnmarshalRead(r.Body, &request); err != nil {
t.Error(err) t.Error(err)
} }
if request.LogProbs || request.TopLogProbs != 0 { if request.LogProbs || request.TopLogProbs != 0 {
@@ -761,17 +760,17 @@ data: {"choices":[{"index":0,"delta":{},"logprobs":{"content":null,"refusal":[{"
if err := json.Unmarshal([]byte(tt.wantMetadata), &want); err != nil { if err := json.Unmarshal([]byte(tt.wantMetadata), &want); err != nil {
t.Fatal(err) t.Fatal(err)
} }
encoded, err := jsonv2.Marshal( encoded, err := json.Marshal(
result.Metadata, jsonv2.FormatNilSliceAsNull(true), result.Metadata, json.FormatNilSliceAsNull(true),
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
var gotJSON, wantJSON map[string]any var gotJSON, wantJSON map[string]any
if err := jsonv2.Unmarshal(encoded, &gotJSON); err != nil { if err := json.Unmarshal(encoded, &gotJSON); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if err := jsonv2.Unmarshal([]byte(tt.wantMetadata), &wantJSON); err != nil { if err := json.Unmarshal([]byte(tt.wantMetadata), &wantJSON); err != nil {
t.Fatal(err) t.Fatal(err)
} }
if !reflect.DeepEqual(gotJSON, wantJSON) { if !reflect.DeepEqual(gotJSON, wantJSON) {
+2 -2
View File
@@ -2,7 +2,7 @@ package llm
import ( import (
"context" "context"
"encoding/json" "encoding/json/v2"
"fmt" "fmt"
"io" "io"
"log/slog" "log/slog"
@@ -74,7 +74,7 @@ func (c *Client) ListModels(ctx context.Context) ([]Model, error) {
} }
var body ModelsResponse var body ModelsResponse
if err := json.NewDecoder(res.Body).Decode(&body); err != nil { if err := json.UnmarshalRead(res.Body, &body); err != nil {
return nil, fmt.Errorf( return nil, fmt.Errorf(
"list models: decode response: %w", err, "list models: decode response: %w", err,
) )
+1 -1
View File
@@ -3,7 +3,7 @@
package llm package llm
import ( import (
"encoding/json" "encoding/json/v2"
"strings" "strings"
) )
+1 -1
View File
@@ -2,7 +2,7 @@ package llm
import ( import (
"context" "context"
"encoding/json" "encoding/json/v2"
"errors" "errors"
"fmt" "fmt"
"io" "io"