Skip to content

Commit

Permalink
Commit v1 encoding/json (#72)
Browse files Browse the repository at this point in the history
This pulls in v1 of "encoding/json" at commit 99dad5281660c4e644602e0c8790dd24b3eb45f3.

Some local changes were made:
* Remove reference to "internal/testenv".
* Replaced all references to "encoding/json" with "github.com/go-json-experiment/json/v1".
* Replaced test logic to read testdata/code.json.gz with local copy in ./internal/jsontest.Data.

The exact diff is as follows:

    diff -U 0 go/src/encoding/json/bench_test.go jsonv2/v1/bench_test.go
    --- go/src/encoding/json/bench_test.go	2024-12-11 11:08:22.381698273 -0800
    +++ jsonv2/v1/bench_test.go	2024-12-14 15:27:52.619776408 -0800
    @@ -15 +14,0 @@
    -	"compress/gzip"
    @@ -17 +15,0 @@
    -	"internal/testenv"
    @@ -19 +16,0 @@
    -	"os"
    @@ -25,0 +23,2 @@
    +
    +	"github.com/go-json-experiment/json/internal/jsontest"
    @@ -47,12 +46,5 @@
    -	f, err := os.Open("testdata/code.json.gz")
    -	if err != nil {
    -		panic(err)
    -	}
    -	defer f.Close()
    -	gz, err := gzip.NewReader(f)
    -	if err != nil {
    -		panic(err)
    -	}
    -	data, err := io.ReadAll(gz)
    -	if err != nil {
    -		panic(err)
    +	var data []byte
    +	for _, entry := range jsontest.Data {
    +		if entry.Name == "GolangSource" {
    +			data = entry.Data()
    +		}
    @@ -60 +51,0 @@
    -
    @@ -66,0 +58 @@
    +	var err error
    @@ -465,3 +456,0 @@
    -	if testenv.Builder() != "" {
    -		maxTypes = 1e3 // restrict cache sizes on builders
    -	}
    diff -U 0 go/src/encoding/json/example_marshaling_test.go jsonv2/v1/example_marshaling_test.go
    --- go/src/encoding/json/example_marshaling_test.go	2021-08-26 13:12:00.197473917 -0700
    +++ jsonv2/v1/example_marshaling_test.go	2024-12-14 15:26:17.366263865 -0800
    @@ -8 +7,0 @@
    -	"encoding/json"
    @@ -11,0 +11,2 @@
    +
    +	"github.com/go-json-experiment/json/v1"
    diff -U 0 go/src/encoding/json/example_test.go jsonv2/v1/example_test.go
    --- go/src/encoding/json/example_test.go	2024-12-11 10:13:36.500960427 -0800
    +++ jsonv2/v1/example_test.go	2024-12-14 15:26:24.712222257 -0800
    @@ -9 +8,0 @@
    -	"encoding/json"
    @@ -14,0 +14,2 @@
    +
    +	"github.com/go-json-experiment/json/v1"
    diff -U 0 go/src/encoding/json/example_text_marshaling_test.go jsonv2/v1/example_text_marshaling_test.go
    --- go/src/encoding/json/example_text_marshaling_test.go	2022-03-09 09:27:59.164793033 -0800
    +++ jsonv2/v1/example_text_marshaling_test.go	2024-12-14 15:26:21.389145838 -0800
    @@ -8 +7,0 @@
    -	"encoding/json"
    @@ -11,0 +11,2 @@
    +
    +	"github.com/go-json-experiment/json/v1"
  • Loading branch information
dsnet authored Dec 15, 2024
1 parent 68309eb commit a8b2846
Show file tree
Hide file tree
Showing 21 changed files with 10,564 additions and 0 deletions.
573 changes: 573 additions & 0 deletions v1/bench_test.go

Large diffs are not rendered by default.

1,312 changes: 1,312 additions & 0 deletions v1/decode.go

Large diffs are not rendered by default.

2,648 changes: 2,648 additions & 0 deletions v1/decode_test.go

Large diffs are not rendered by default.

1,341 changes: 1,341 additions & 0 deletions v1/encode.go

Large diffs are not rendered by default.

1,403 changes: 1,403 additions & 0 deletions v1/encode_test.go

Large diffs are not rendered by default.

74 changes: 74 additions & 0 deletions v1/example_marshaling_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package json_test

import (
"fmt"
"log"
"strings"

"github.com/go-json-experiment/json/v1"
)

type Animal int

const (
Unknown Animal = iota
Gopher
Zebra
)

func (a *Animal) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
switch strings.ToLower(s) {
default:
*a = Unknown
case "gopher":
*a = Gopher
case "zebra":
*a = Zebra
}

return nil
}

func (a Animal) MarshalJSON() ([]byte, error) {
var s string
switch a {
default:
s = "unknown"
case Gopher:
s = "gopher"
case Zebra:
s = "zebra"
}

return json.Marshal(s)
}

func Example_customMarshalJSON() {
blob := `["gopher","armadillo","zebra","unknown","gopher","bee","gopher","zebra"]`
var zoo []Animal
if err := json.Unmarshal([]byte(blob), &zoo); err != nil {
log.Fatal(err)
}

census := make(map[Animal]int)
for _, animal := range zoo {
census[animal] += 1
}

fmt.Printf("Zoo Census:\n* Gophers: %d\n* Zebras: %d\n* Unknown: %d\n",
census[Gopher], census[Zebra], census[Unknown])

// Output:
// Zoo Census:
// * Gophers: 3
// * Zebras: 2
// * Unknown: 3
}
311 changes: 311 additions & 0 deletions v1/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,311 @@
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package json_test

import (
"bytes"
"fmt"
"io"
"log"
"os"
"strings"

"github.com/go-json-experiment/json/v1"
)

func ExampleMarshal() {
type ColorGroup struct {
ID int
Name string
Colors []string
}
group := ColorGroup{
ID: 1,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
}
b, err := json.Marshal(group)
if err != nil {
fmt.Println("error:", err)
}
os.Stdout.Write(b)
// Output:
// {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
}

func ExampleUnmarshal() {
var jsonBlob = []byte(`[
{"Name": "Platypus", "Order": "Monotremata"},
{"Name": "Quoll", "Order": "Dasyuromorphia"}
]`)
type Animal struct {
Name string
Order string
}
var animals []Animal
err := json.Unmarshal(jsonBlob, &animals)
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("%+v", animals)
// Output:
// [{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
}

// This example uses a Decoder to decode a stream of distinct JSON values.
func ExampleDecoder() {
const jsonStream = `
{"Name": "Ed", "Text": "Knock knock."}
{"Name": "Sam", "Text": "Who's there?"}
{"Name": "Ed", "Text": "Go fmt."}
{"Name": "Sam", "Text": "Go fmt who?"}
{"Name": "Ed", "Text": "Go fmt yourself!"}
`
type Message struct {
Name, Text string
}
dec := json.NewDecoder(strings.NewReader(jsonStream))
for {
var m Message
if err := dec.Decode(&m); err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
fmt.Printf("%s: %s\n", m.Name, m.Text)
}
// Output:
// Ed: Knock knock.
// Sam: Who's there?
// Ed: Go fmt.
// Sam: Go fmt who?
// Ed: Go fmt yourself!
}

// This example uses a Decoder to decode a stream of distinct JSON values.
func ExampleDecoder_Token() {
const jsonStream = `
{"Message": "Hello", "Array": [1, 2, 3], "Null": null, "Number": 1.234}
`
dec := json.NewDecoder(strings.NewReader(jsonStream))
for {
t, err := dec.Token()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
fmt.Printf("%T: %v", t, t)
if dec.More() {
fmt.Printf(" (more)")
}
fmt.Printf("\n")
}
// Output:
// json.Delim: { (more)
// string: Message (more)
// string: Hello (more)
// string: Array (more)
// json.Delim: [ (more)
// float64: 1 (more)
// float64: 2 (more)
// float64: 3
// json.Delim: ] (more)
// string: Null (more)
// <nil>: <nil> (more)
// string: Number (more)
// float64: 1.234
// json.Delim: }
}

// This example uses a Decoder to decode a streaming array of JSON objects.
func ExampleDecoder_Decode_stream() {
const jsonStream = `
[
{"Name": "Ed", "Text": "Knock knock."},
{"Name": "Sam", "Text": "Who's there?"},
{"Name": "Ed", "Text": "Go fmt."},
{"Name": "Sam", "Text": "Go fmt who?"},
{"Name": "Ed", "Text": "Go fmt yourself!"}
]
`
type Message struct {
Name, Text string
}
dec := json.NewDecoder(strings.NewReader(jsonStream))

// read open bracket
t, err := dec.Token()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%T: %v\n", t, t)

// while the array contains values
for dec.More() {
var m Message
// decode an array value (Message)
err := dec.Decode(&m)
if err != nil {
log.Fatal(err)
}

fmt.Printf("%v: %v\n", m.Name, m.Text)
}

// read closing bracket
t, err = dec.Token()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%T: %v\n", t, t)

// Output:
// json.Delim: [
// Ed: Knock knock.
// Sam: Who's there?
// Ed: Go fmt.
// Sam: Go fmt who?
// Ed: Go fmt yourself!
// json.Delim: ]
}

// This example uses RawMessage to delay parsing part of a JSON message.
func ExampleRawMessage_unmarshal() {
type Color struct {
Space string
Point json.RawMessage // delay parsing until we know the color space
}
type RGB struct {
R uint8
G uint8
B uint8
}
type YCbCr struct {
Y uint8
Cb int8
Cr int8
}

var j = []byte(`[
{"Space": "YCbCr", "Point": {"Y": 255, "Cb": 0, "Cr": -10}},
{"Space": "RGB", "Point": {"R": 98, "G": 218, "B": 255}}
]`)
var colors []Color
err := json.Unmarshal(j, &colors)
if err != nil {
log.Fatalln("error:", err)
}

for _, c := range colors {
var dst any
switch c.Space {
case "RGB":
dst = new(RGB)
case "YCbCr":
dst = new(YCbCr)
}
err := json.Unmarshal(c.Point, dst)
if err != nil {
log.Fatalln("error:", err)
}
fmt.Println(c.Space, dst)
}
// Output:
// YCbCr &{255 0 -10}
// RGB &{98 218 255}
}

// This example uses RawMessage to use a precomputed JSON during marshal.
func ExampleRawMessage_marshal() {
h := json.RawMessage(`{"precomputed": true}`)

c := struct {
Header *json.RawMessage `json:"header"`
Body string `json:"body"`
}{Header: &h, Body: "Hello Gophers!"}

b, err := json.MarshalIndent(&c, "", "\t")
if err != nil {
fmt.Println("error:", err)
}
os.Stdout.Write(b)

// Output:
// {
// "header": {
// "precomputed": true
// },
// "body": "Hello Gophers!"
// }
}

func ExampleIndent() {
type Road struct {
Name string
Number int
}
roads := []Road{
{"Diamond Fork", 29},
{"Sheep Creek", 51},
}

b, err := json.Marshal(roads)
if err != nil {
log.Fatal(err)
}

var out bytes.Buffer
json.Indent(&out, b, "=", "\t")
out.WriteTo(os.Stdout)
// Output:
// [
// = {
// = "Name": "Diamond Fork",
// = "Number": 29
// = },
// = {
// = "Name": "Sheep Creek",
// = "Number": 51
// = }
// =]
}

func ExampleMarshalIndent() {
data := map[string]int{
"a": 1,
"b": 2,
}

b, err := json.MarshalIndent(data, "<prefix>", "<indent>")
if err != nil {
log.Fatal(err)
}

fmt.Println(string(b))
// Output:
// {
// <prefix><indent>"a": 1,
// <prefix><indent>"b": 2
// <prefix>}
}

func ExampleValid() {
goodJSON := `{"example": 1}`
badJSON := `{"example":2:]}}`

fmt.Println(json.Valid([]byte(goodJSON)), json.Valid([]byte(badJSON)))
// Output:
// true false
}

func ExampleHTMLEscape() {
var out bytes.Buffer
json.HTMLEscape(&out, []byte(`{"Name":"<b>HTML content</b>"}`))
out.WriteTo(os.Stdout)
// Output:
//{"Name":"\u003cb\u003eHTML content\u003c/b\u003e"}
}
Loading

0 comments on commit a8b2846

Please sign in to comment.