本文整理匯總了Golang中encoding/json.Decoder.Token方法的典型用法代碼示例。如果您正苦於以下問題:Golang Decoder.Token方法的具體用法?Golang Decoder.Token怎麽用?Golang Decoder.Token使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類encoding/json.Decoder
的用法示例。
在下文中一共展示了Decoder.Token方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: findFeatureArray
func findFeatureArray(dec *json.Decoder) error {
for {
t, err := dec.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}
if s, ok := t.(string); ok && s == "features" && dec.More() {
// we found the features element
d, err := dec.Token()
if err != nil {
return err
}
if delim, ok := d.(json.Delim); ok {
if delim.String() == "[" {
// we have our start of the array
break
} else {
// A different kind of delimiter
return fmt.Errorf("Expected features to be an array.")
}
}
}
}
if !dec.More() {
return fmt.Errorf("Cannot find any features.")
}
return nil
}
示例2: toList
func toList(cxDecoder *json.Decoder, w io.Writer) {
// Node ID to node name map
nodeMap := make(map[string]int64)
for {
_, err := cxDecoder.Token()
if err == io.EOF {
break
} else if err != nil {
fmt.Println("Error: ", err)
return
}
for cxDecoder.More() {
token, err := cxDecoder.Token()
if err != nil {
fmt.Println(err)
}
if token == "nodes" {
processNode(cxDecoder, nodeMap)
}
}
}
err := json.NewEncoder(w).Encode(nodeMap)
if err != nil {
panic("parse err")
}
}
示例3: decodeArray
func decodeArray(dec *json.Decoder) (Array, error) {
arr := make(Array, 0)
for {
tok, err := dec.Token()
if err != nil {
return arr, err
}
switch token := tok.(type) {
case json.Delim:
switch rune(token) {
case ']':
return arr, nil
case '{':
obj, err := decodeObject(dec)
if err != nil {
return arr, err
}
arr = append(arr, obj)
case '[':
return decodeArray(dec)
default:
return nil, Errorf("Unexpected token %s", token)
}
}
}
}
示例4: decodeObject
func decodeObject(dec *json.Decoder) (Object, error) {
obj := make(Object)
for {
key, err := dec.Token()
if err != nil {
return nil, err
}
if delim, is := key.(json.Delim); is {
switch delim {
case '}':
return obj, nil
default:
return obj, Errorf("Unexpected delim: %s", delim)
}
}
// key is a Value
value, err := decode(dec)
obj[key] = value
if err != nil {
return obj, err
}
}
}
示例5: parseSif
func parseSif(cxDecoder *json.Decoder, w *csv.Writer) {
// Edge slice used for later mapping
var edges []cx.Edge
// Node ID to node name map
nodeMap := make(map[int64]string)
for {
_, err := cxDecoder.Token()
if err == io.EOF {
break
} else if err != nil {
fmt.Println("Error: ", err)
return
}
for cxDecoder.More() {
token, err := cxDecoder.Token()
if err != nil {
fmt.Println(err)
}
if token == "nodes" {
processNode(cxDecoder, nodeMap)
} else if token == "edges" {
processEdge(cxDecoder, &edges)
}
}
}
writeSif(nodeMap, edges, *w)
}
示例6: getVal
func getVal(dec *json.Decoder) (any, error) {
t, err := dec.Token()
if err != nil {
return nil, err
}
switch tok := t.(type) {
case json.Delim:
switch tok {
case '[':
return decToSlice(dec)
case '{':
om := New()
err := om.(*omap).unmarshalJSON(dec)
if err != nil {
return nil, err
}
_, err = dec.Token() // }
return om, err
case ']':
return nil, endOfSlice
case '}':
return nil, fmt.Errorf("unexpected '}'")
default:
panic("unreachable code")
}
default:
return tok, nil
}
}
示例7: consumeExpectedToken
func consumeExpectedToken(decoder *json.Decoder, expected json.Token) error {
if actual, err := decoder.Token(); err != nil {
return err
} else if actual != expected {
return fmt.Errorf("Expected %v, not %v", expected, actual)
} else {
return nil
}
}
示例8: processEdge
func processEdge(decoder *json.Decoder, edges *[]cx.Edge) {
token, err := decoder.Token()
if err != nil || token != arrayStart {
return
}
var entry cx.Edge
for decoder.More() {
err := decoder.Decode(&entry)
if err != nil {
return
}
*edges = append(*edges, entry)
}
}
示例9: unmarshalJSON
func (m *omap) unmarshalJSON(dec *json.Decoder) error {
for dec.More() {
t, err := dec.Token()
if err != nil {
return err
}
key, isKey := t.(string)
if !isKey {
return fmt.Errorf("%t %s is not string(expected key)", t, t)
}
val, err := getVal(dec)
if err != nil {
return err
}
m.Set(key, val)
}
return nil
}
示例10: sliceFromJSONDecoder
func (store Store) sliceFromJSONDecoder(dec *json.Decoder) ([]interface{}, error) {
var slice []interface{}
for {
t, err := dec.Token()
if err != nil {
return slice, err
}
if delim, ok := t.(json.Delim); ok && delim.String() == "]" {
return slice, nil
} else if ok && delim.String() == "{" {
item, err := store.treeBranchFromJSONDecoder(dec)
if err != nil {
return slice, err
}
slice = append(slice, item)
} else {
slice = append(slice, t)
}
}
}
示例11: ProcJsonString
func ProcJsonString(dec *json.Decoder) int {
count := 0.0
for {
t, err := dec.Token()
if err == io.EOF {
break
}
if v, ok := t.(float64); ok {
count += v
}
if t == nil {
break
}
}
return int(count)
}
示例12: processNode
func processNode(decoder *json.Decoder, nodes map[string]int64) {
_, err := decoder.Token()
if err != nil {
return
}
var entry cx.Node
for decoder.More() {
err := decoder.Decode(&entry)
if err != nil {
return
}
if entry.N == "" {
nodes[strconv.FormatInt(entry.ID, 10)] = entry.ID
} else {
nodes[entry.N] = entry.ID
}
}
}
示例13: processNode
func processNode(decoder *json.Decoder, nodes map[int64]string) {
token, err := decoder.Token()
if err != nil || token != arrayStart {
return
}
var entry cx.Node
for decoder.More() {
err := decoder.Decode(&entry)
if err != nil {
return
}
if entry.N == "" {
nodes[entry.ID] = strconv.FormatInt(entry.ID, 10)
} else {
nodes[entry.ID] = entry.N
}
}
}
示例14: ProcJsonObject
func ProcJsonObject(dec *json.Decoder) (float64, bool) {
count := 0.0
seen_red := false
for {
t, err := dec.Token()
if err == io.EOF || t == nil {
break
}
if v, ok := t.(json.Delim); ok {
if v.String() == "{" {
ncount, seen := ProcJsonObject(dec)
if !seen {
count += ncount
}
} else if v.String() == "}" {
return count, seen_red
} else if v.String() == "[" {
ncount, _ := ProcJsonObject(dec)
count += ncount
} else if v.String() == "]" {
return count, seen_red
}
}
if v, ok := t.(string); ok {
if v == "red" {
seen_red = true
}
}
if v, ok := t.(float64); ok {
count += v
}
}
return count, seen_red
}
示例15: decode
func decode(dec *json.Decoder) (Model, error) {
tok, err := dec.Token()
if err != nil {
return nil, err
}
switch token := tok.(type) {
case json.Delim:
switch rune(token) {
case '{':
return decodeObject(dec)
case '[':
return decodeArray(dec)
default:
return nil, Errorf("Unexpected token %s", token)
}
default:
return token, nil
}
}