generated from okp4/template-oss
-
Notifications
You must be signed in to change notification settings - Fork 120
/
json.go
65 lines (57 loc) · 1.73 KB
/
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
package prolog
import (
"github.com/ichiban/prolog/engine"
)
// JSONNull returns the compound term @(null).
// It is used to represent the null value in json objects.
func JSONNull() engine.Term {
return AtomAt.Apply(AtomNull)
}
// JSONBool returns the compound term @(true) if b is true, otherwise @(false).
func JSONBool(b bool) engine.Term {
if b {
return AtomAt.Apply(AtomTrue)
}
return AtomAt.Apply(AtomFalse)
}
// JSONEmptyArray returns is the compound term @([]).
// It is used to represent the empty array in json objects.
func JSONEmptyArray() engine.Term {
return AtomAt.Apply(AtomEmptyArray)
}
// ExtractJSONTerm is a utility function that would extract all attribute of a JSON object
// that is represented in prolog with the `json` atom.
//
// This function will ensure the json atom follow our json object representation in prolog.
//
// A JSON object is represented like this :
//
// ```
// json([foo-bar])
// ```
//
// That give a JSON object: `{"foo": "bar"}`
// Returns the map of all attributes with its term value.
func ExtractJSONTerm(term engine.Compound, env *engine.Env) (map[string]engine.Term, error) {
if term.Functor() != AtomJSON || term.Arity() != 1 {
return nil, engine.TypeError(AtomTypeJSON, term, env)
}
iter, err := ListIterator(term.Arg(0), env)
if err != nil {
return nil, err
}
terms := make(map[string]engine.Term, 0)
for iter.Next() {
current := iter.Current()
pair, ok := current.(engine.Compound)
if !ok || pair.Functor() != AtomPair || pair.Arity() != 2 {
return nil, engine.TypeError(AtomTypePair, current, env)
}
key, ok := pair.Arg(0).(engine.Atom)
if !ok {
return nil, engine.TypeError(AtomTypeAtom, pair.Arg(0), env)
}
terms[key.String()] = pair.Arg(1)
}
return terms, nil
}