mirror of
https://github.com/json-iterator/go.git
synced 2025-04-20 11:28:49 +02:00
Merge branch 'master' of https://github.com/json-iterator/go
This commit is contained in:
commit
f50c4cfbbe
10
.idea/libraries/Go_SDK.xml
generated
Normal file
10
.idea/libraries/Go_SDK.xml
generated
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<component name="libraryTable">
|
||||||
|
<library name="Go SDK">
|
||||||
|
<CLASSES>
|
||||||
|
<root url="file:///usr/local/go/src" />
|
||||||
|
</CLASSES>
|
||||||
|
<SOURCES>
|
||||||
|
<root url="file:///usr/local/go/src" />
|
||||||
|
</SOURCES>
|
||||||
|
</library>
|
||||||
|
</component>
|
27
example_test.go
Normal file
27
example_test.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package jsoniter_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/json-iterator/go"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
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 := jsoniter.Marshal(group)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("error:", err)
|
||||||
|
}
|
||||||
|
os.Stdout.Write(b)
|
||||||
|
// Output:
|
||||||
|
// {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
|
||||||
|
}
|
@ -5,7 +5,10 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Unmarshal adapts to json/encoding APIs
|
// Unmarshal adapts to json/encoding Unmarshal API
|
||||||
|
//
|
||||||
|
// Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v.
|
||||||
|
// Refer to https://godoc.org/encoding/json#Unmarshal for more information
|
||||||
func Unmarshal(data []byte, v interface{}) error {
|
func Unmarshal(data []byte, v interface{}) error {
|
||||||
data = data[:lastNotSpacePos(data)]
|
data = data[:lastNotSpacePos(data)]
|
||||||
iter := ParseBytes(data)
|
iter := ParseBytes(data)
|
||||||
@ -81,6 +84,10 @@ func UnmarshalAnyFromString(str string) (Any, error) {
|
|||||||
return nil, iter.Error
|
return nil, iter.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Marshal adapts to json/encoding Marshal API
|
||||||
|
//
|
||||||
|
// Marshal returns the JSON encoding of v, adapts to json/encoding Marshal API
|
||||||
|
// Refer to https://godoc.org/encoding/json#Marshal for more information
|
||||||
func Marshal(v interface{}) ([]byte, error) {
|
func Marshal(v interface{}) ([]byte, error) {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
stream := NewStream(buf, 512)
|
stream := NewStream(buf, 512)
|
||||||
@ -100,11 +107,19 @@ func MarshalToString(v interface{}) (string, error) {
|
|||||||
return string(buf), nil
|
return string(buf), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewDecoder adapts to json/stream NewDecoder API.
|
||||||
|
//
|
||||||
|
// NewDecoder returns a new decoder that reads from r.
|
||||||
|
//
|
||||||
|
// Instead of a json/encoding Decoder, an AdaptedDecoder is returned
|
||||||
|
// Refer to https://godoc.org/encoding/json#NewDecoder for more information
|
||||||
func NewDecoder(reader io.Reader) *AdaptedDecoder {
|
func NewDecoder(reader io.Reader) *AdaptedDecoder {
|
||||||
iter := Parse(reader, 512)
|
iter := Parse(reader, 512)
|
||||||
return &AdaptedDecoder{iter}
|
return &AdaptedDecoder{iter}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AdaptedDecoder reads and decodes JSON values from an input stream.
|
||||||
|
// AdaptedDecoder provides identical APIs with json/stream Decoder (Token() and UseNumber() are in progress)
|
||||||
type AdaptedDecoder struct {
|
type AdaptedDecoder struct {
|
||||||
iter *Iterator
|
iter *Iterator
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user