summaryrefslogtreecommitdiff
path: root/src/pkg/json/decode.go
blob: c2dd93eca5d2b04bb9b96c40054aa5f3d821d981 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2009 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.

// Represents JSON data structure using native Go types: booleans, floats,
// strings, arrays, and maps.

package json

import (
	"container/vector"
	"os"
)

// Decode a JSON string

// Decode parses the string s as a JSON-syntax string and returns the
// generic JSON object representation.  The object representation is a tree
// of Go data types.  The data return value may be one of float64, string,
// bool, nil, []interface{} or map[string]interface{}.  The array and map
// elements may in turn contain any of the types listed above and so on.

// If Decode encounters a syntax error, it returns with err set to an
// instance of ParseError.  See ParseError documentation for details.
func Decode(s string) (data interface{}, err os.Error) {
	jb := newDecoder(nil, nil)
	ok, errPos, errTok := Parse(s, jb)
	if ok {
		data = jb.Data()
	} else {
		err = &ParseError{Index: errPos, Token: errTok}
	}
	return
}

type decoder struct {
	// A value being constructed.
	value interface{}
	// Container entity to flush into.  Can be either vector.Vector or
	// map[string]interface{}.
	container interface{}
	// The index into the container interface.  Either int or string.
	index interface{}
}

func newDecoder(container interface{}, key interface{}) *decoder {
	return &decoder{container: container, index: key}
}

func (j *decoder) Int64(i int64) { j.value = float64(i) }

func (j *decoder) Uint64(i uint64) { j.value = float64(i) }

func (j *decoder) Float64(f float64) { j.value = float64(f) }

func (j *decoder) String(s string) { j.value = s }

func (j *decoder) Bool(b bool) { j.value = b }

func (j *decoder) Null() { j.value = nil }

func (j *decoder) Array() { j.value = new(vector.Vector) }

func (j *decoder) Map() { j.value = make(map[string]interface{}) }

func (j *decoder) Elem(i int) Builder {
	v, ok := j.value.(*vector.Vector)
	if !ok {
		v = new(vector.Vector)
		j.value = v
	}
	if v.Len() <= i {
		v.Resize(i+1, (i+1)*2)
	}
	return newDecoder(v, i)
}

func (j *decoder) Key(s string) Builder {
	m, ok := j.value.(map[string]interface{})
	if !ok {
		m = make(map[string]interface{})
		j.value = m
	}
	return newDecoder(m, s)
}

func (j *decoder) Flush() {
	switch c := j.container.(type) {
	case *vector.Vector:
		index := j.index.(int)
		c.Set(index, j.Data())
	case map[string]interface{}:
		index := j.index.(string)
		c[index] = j.Data()
	}
}

// Get the value built by this builder.
func (j *decoder) Data() interface{} {
	switch v := j.value.(type) {
	case *vector.Vector:
		return v.Data()
	}
	return j.value
}