This repository has been archived by the owner on Aug 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 267
/
to_json.go
122 lines (115 loc) · 2.95 KB
/
to_json.go
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2019 Attic Labs, Inc. All rights reserved.
// Licensed under the Apache License, version 2.0:
// http://www.apache.org/licenses/LICENSE-2.0
package json
import (
"encoding/json"
"errors"
"fmt"
"io"
"github.com/attic-labs/noms/go/types"
)
// ToJSON encodes a Noms value as JSON.
func ToJSON(v types.Value, w io.Writer, opts ToOptions) error {
// TODO: This is a quick hack that is expedient. We should marshal directly to the writer without
// allocating a bunch of Go values.
p, err := toPile(v, opts)
if err != nil {
return err
}
enc := json.NewEncoder(w)
enc.SetIndent("", opts.Indent)
return enc.Encode(p)
}
// ToOptions controls how ToJSON works.
type ToOptions struct {
// Enable support for encoding Noms Lists. Lists are encoded as JSON arrays.
Lists bool
// Enable support for encoding Noms Maps. Maps are encoded as JSON objects.
Maps bool
// Enable support for encoding Noms Sets. Sets are encoded as JSON arrays.
Sets bool
// Enable support for encoding Noms Structs. Structs are encoded as JSON objects.
Structs bool
// String to use for indent when pretty-printing
Indent string
}
func toPile(v types.Value, opts ToOptions) (ret interface{}, err error) {
switch v := v.(type) {
case types.Bool:
return bool(v), nil
case types.Number:
return float64(v), nil
case types.String:
return string(v), nil
case types.Struct:
if !opts.Structs {
return nil, errors.New("Struct marshaling not enabled")
}
r := map[string]interface{}{}
if v.Name() != "" {
return nil, errors.New("Named struct marshaling not supported")
}
v.IterFields(func(k string, cv types.Value) (stop bool) {
var cp interface{}
cp, err = toPile(cv, opts)
if err != nil {
return true
}
r[k] = cp
return false
})
return r, err
case types.Map:
if !opts.Maps {
return nil, errors.New("Map marshaling not enabled")
}
r := make(map[string]interface{}, v.Len())
v.Iter(func(k, cv types.Value) (stop bool) {
sk, ok := k.(types.String)
if !ok {
err = fmt.Errorf("Map key kind %s not supported", types.KindToString[k.Kind()])
return true
}
var cp interface{}
cp, err = toPile(cv, opts)
if err != nil {
return true
}
r[string(sk)] = cp
return false
})
return r, err
case types.List:
if !opts.Lists {
return nil, errors.New("List marshaling not enabled")
}
r := make([]interface{}, v.Len())
v.Iter(func(cv types.Value, i uint64) (stop bool) {
var cp interface{}
cp, err = toPile(cv, opts)
if err != nil {
return true
}
r[i] = cp
return false
})
return r, err
case types.Set:
if !opts.Sets {
return nil, errors.New("Set marshaling not enabled")
}
r := make([]interface{}, 0, v.Len())
v.Iter(func(cv types.Value) (stop bool) {
var cp interface{}
cp, err = toPile(cv, opts)
if err != nil {
return true
}
r = append(r, cp)
return false
})
return r, err
}
return nil, fmt.Errorf("Unsupported kind: %s", types.KindToString[v.Kind()])
}