(experimental) upgraded to encoding/json/v2

This commit is contained in:
Gani Georgiev
2026-08-18 18:16:22 +03:00
parent 4d4275c9aa
commit 97dd775455
125 changed files with 433 additions and 364 deletions
+1 -1
View File
@@ -8,7 +8,7 @@ package ghupdate
import (
"context"
"encoding/json"
"encoding/json/v2"
"errors"
"fmt"
"io"
+5 -5
View File
@@ -3,7 +3,7 @@ package jsvm
import (
"bytes"
"context"
"encoding/json"
"encoding/json/v2"
"errors"
"io"
"io/fs"
@@ -352,7 +352,7 @@ func BindCore(vm *goja.Runtime) {
}
// as a last attempt try to json encode the value
rawBytes, _ := json.Marshal(raw)
rawBytes, _ := json.Marshal(raw, json.Deterministic(true))
return rawBytes, nil
}
@@ -381,7 +381,7 @@ func BindCore(vm *goja.Runtime) {
}
// as a last attempt try to json encode the value
rawBytes, _ := json.Marshal(raw)
rawBytes, _ := json.Marshal(raw, json.Deterministic(true))
return string(rawBytes), nil
}
@@ -1217,13 +1217,13 @@ func newDynamicModel(shape map[string]any) any {
case reflect.Map:
raw, _ := json.Marshal(v)
newV := types.JSONMap[any]{}
newV.Scan(raw)
_ = newV.Scan(raw)
v = newV
vt = reflect.TypeOf(v)
case reflect.Slice, reflect.Array:
raw, _ := json.Marshal(v)
newV := types.JSONArray[any]{}
newV.Scan(raw)
_ = newV.Scan(raw)
v = newV
vt = reflect.TypeOf(newV)
case reflect.Pointer:
+5 -5
View File
@@ -2,7 +2,7 @@ package jsvm
import (
"bytes"
"encoding/json"
"encoding/json/v2"
"errors"
"fmt"
"io"
@@ -505,12 +505,12 @@ func TestBindCoreMailerMessage(t *testing.T) {
t.Fatalf("Expected mailer.Message, got %v", m)
}
raw, err := json.Marshal(m)
raw, err := json.Marshal(m, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
expected := `{"from":{"Name":"test_from","Address":"test_from@example.com"},"to":[{"Name":"test_to1","Address":"test_to1@example.com"},{"Name":"test_to2","Address":"test_to2@example.com"}],"bcc":[{"Name":"test_bcc1","Address":"test_bcc1@example.com"},{"Name":"test_bcc2","Address":"test_bcc2@example.com"}],"cc":[{"Name":"test_cc1","Address":"test_cc1@example.com"},{"Name":"test_cc2","Address":"test_cc2@example.com"}],"subject":"test_subject","html":"test_html","text":"test_text","headers":{"header1":"a","header2":"b"},"attachments":null,"inlineAttachments":null}`
expected := `{"from":{"Name":"test_from","Address":"test_from@example.com"},"to":[{"Name":"test_to1","Address":"test_to1@example.com"},{"Name":"test_to2","Address":"test_to2@example.com"}],"bcc":[{"Name":"test_bcc1","Address":"test_bcc1@example.com"},{"Name":"test_bcc2","Address":"test_bcc2@example.com"}],"cc":[{"Name":"test_cc1","Address":"test_cc1@example.com"},{"Name":"test_cc2","Address":"test_cc2@example.com"}],"subject":"test_subject","html":"test_html","text":"test_text","headers":{"header1":"a","header2":"b"},"attachments":{},"inlineAttachments":{}}`
if string(raw) != expected {
t.Fatalf("Expected \n%s, \ngot \n%s", expected, raw)
@@ -1178,7 +1178,7 @@ func TestBindApisErrors(t *testing.T) {
t.Errorf("[%s] Expected Message %q, got %q", s.js, s.expectMessage, apiErr.Message)
}
dataRaw, _ := json.Marshal(apiErr.RawData())
dataRaw, _ := json.Marshal(apiErr.RawData(), json.Deterministic(true))
if string(dataRaw) != s.expectData {
t.Errorf("[%s] Expected Data %q, got %q", s.js, s.expectData, dataRaw)
}
@@ -1439,7 +1439,7 @@ func TestBindHTTPSend(t *testing.T) {
res.Header().Add("X-Custom", "custom_header")
res.Header().Add("Set-Cookie", "sessionId=123456")
infoRaw, _ := json.Marshal(info)
infoRaw, _ := json.Marshal(info, json.Deterministic(true))
// write back the submitted request
res.Write(infoRaw)
+2 -2
View File
@@ -2,7 +2,7 @@ package jsvm
import (
"bytes"
"encoding/json"
"encoding/json/v2"
"strings"
"testing"
@@ -166,7 +166,7 @@ func TestFormDataEntries(t *testing.T) {
entries := data.Entries()
rawEntries, err := json.Marshal(entries)
rawEntries, err := json.Marshal(entries, json.Deterministic(true))
if err != nil {
t.Fatal(err)
}
+11 -11
View File
@@ -93,12 +93,12 @@ migrate((app) => {
"type": "text"
},
{
"exceptDomains": null,
"exceptDomains": [],
"help": "",
"hidden": false,
"id": "email@TEST_RANDOM",
"name": "email",
"onlyDomains": null,
"onlyDomains": [],
"presentable": false,
"required": true,
"system": true,
@@ -200,7 +200,7 @@ migrate((app) => {
package _test_migrations
import (
"encoding/json"
"encoding/json/v2"
"github.com/pocketbase/pocketbase/core"
m "github.com/pocketbase/pocketbase/migrations"
@@ -275,12 +275,12 @@ func init() {
"type": "text"
},
{
"exceptDomains": null,
"exceptDomains": [],
"help": "",
"hidden": false,
"id": "email@TEST_RANDOM",
"name": "email",
"onlyDomains": null,
"onlyDomains": [],
"presentable": false,
"required": true,
"system": true,
@@ -546,12 +546,12 @@ migrate((app) => {
"type": "text"
},
{
"exceptDomains": null,
"exceptDomains": [],
"help": "",
"hidden": false,
"id": "email3885137012",
"name": "email",
"onlyDomains": null,
"onlyDomains": [],
"presentable": false,
"required": true,
"system": true,
@@ -649,7 +649,7 @@ migrate((app) => {
package _test_migrations
import (
"encoding/json"
"encoding/json/v2"
"github.com/pocketbase/pocketbase/core"
m "github.com/pocketbase/pocketbase/migrations"
@@ -731,12 +731,12 @@ func init() {
"type": "text"
},
{
"exceptDomains": null,
"exceptDomains": [],
"help": "",
"hidden": false,
"id": "email3885137012",
"name": "email",
"onlyDomains": null,
"onlyDomains": [],
"presentable": false,
"required": true,
"system": true,
@@ -1041,7 +1041,7 @@ migrate((app) => {
package _test_migrations
import (
"encoding/json"
"encoding/json/v2"
"github.com/pocketbase/pocketbase/core"
m "github.com/pocketbase/pocketbase/migrations"
+12 -7
View File
@@ -2,7 +2,8 @@ package migratecmd
import (
"bytes"
"encoding/json"
"encoding/json/jsontext"
"encoding/json/v2"
"errors"
"fmt"
"path/filepath"
@@ -383,7 +384,7 @@ func (p *plugin) goCreateTemplate(collection *core.Collection) (string, error) {
const template = `package %s
import (
"encoding/json"
"encoding/json/v2"
"github.com/pocketbase/pocketbase/core"
m "github.com/pocketbase/pocketbase/migrations"
@@ -436,7 +437,7 @@ func (p *plugin) goDeleteTemplate(collection *core.Collection) (string, error) {
const template = `package %s
import (
"encoding/json"
"encoding/json/v2"
"github.com/pocketbase/pocketbase/core"
m "github.com/pocketbase/pocketbase/migrations"
@@ -621,7 +622,7 @@ func (p *plugin) goDiffTemplate(new *core.Collection, old *core.Collection) (str
if strings.Contains(combined, "json.Unmarshal(") ||
strings.Contains(combined, "json.Marshal(") {
imports += "\n\t\"encoding/json\"\n"
imports += "\n\t\"encoding/json/v2\"\n"
}
imports += "\n\t\"github.com/pocketbase/pocketbase/core\""
@@ -666,7 +667,11 @@ func init() {
}
func marhshalWithoutEscape(v any, prefix string, indent string) ([]byte, error) {
raw, err := json.MarshalIndent(v, prefix, indent)
raw, err := json.Marshal(v,
json.Deterministic(true),
jsontext.WithIndentPrefix(prefix),
jsontext.WithIndent(indent),
)
if err != nil {
return nil, err
}
@@ -720,8 +725,8 @@ func diffMaps(old, new map[string]any, excludeKeys ...string) map[string]any {
}
// compare the serialized version of the values in case of slice or other custom type
rawOld, _ := json.Marshal(vOld)
rawNew, _ := json.Marshal(vNew)
rawOld, _ := json.Marshal(vOld, json.Deterministic(true))
rawNew, _ := json.Marshal(vNew, json.Deterministic(true))
if !bytes.Equal(rawOld, rawNew) {
// if both are maps add recursively only the changed fields