-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson.go
532 lines (500 loc) · 12.9 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// Copyright 2021 Cloud Privacy Labs, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package lpg
import (
"encoding/json"
"fmt"
"io"
"strconv"
)
type ErrInvalidGraph struct {
Msg string
}
func (e ErrInvalidGraph) Error() string { return "Invalid graph: " + e.Msg }
// Interner is a string interface, that can be as simple as a
// map[string]string, that is used to intern property keys
type Interner interface {
// Intern should return the interned copy of the string
Intern(string) string
}
// MapInterner is a basic interner that uses a map[string]string to intern strings
type MapInterner map[string]string
// Intern keeps the interned copy of s in the map
func (i MapInterner) Intern(s string) string {
if k, exists := i[s]; exists {
return k
}
i[s] = s
return s
}
// JSON marshals/unmarshals a graph to/from JSON.
//
// The JSON unmarshaling uses a string Interner if it is set. This
// allows reusing the strings for repeated keys. Setting this interner
// to an implementation will reduce memory footprint of the graph. If
// letf uninitialized, the unmarshaler still uses an interner, but
// this interner will not be shared between different graphs
// unmarshaled using this unmarshaler.
//
// If PropertyMarshaler/PropertyUnmarshaler is set, these functions
// are called to marshal/unmarshal individual properties. These
// function can return an empty string for the key to skip the
// property.
//
// If MarshalEdgesSeparately is set, graph edges are marshaled under
// the "edges" key. If this is false, the graph edges are included
// under the source node.
//
// The JSON representation of nodes use a numeric node index. These
// indexes are used to refer to nodes in edges.
//
// With MarshalEdgesSeparately=false:
//
// {
// "nodes": [
// {
// "n": 0,
// "labels": ["lbl1","lbl2"],
// "properties": {
// "key": "value"
// },
// "edges": [
// {
// "to": 1,
// "label": "edgeLabel"
// }
// ]
// },
// {
// "n": 1,
// "labels": ["lbl1"]
// }
// ]
// }
//
// With MarshalEdgesSeparately=true:
//
// {
// "nodes": [
// {
// "n": 0,
// "labels": ["lbl1","lbl2"],
// "properties": {
// "key": "value"
// }
// },
// {
// "n": 1,
// "labels": ["lbl1"]
// }
// ],
// "edges": [
// {
// "from": 0,
// "to": 1,
// "label": "edgeLabel"
// }
// ]
// }
type JSON struct {
Interner Interner
// If false, dring marshaling edges are embedded in the source
// nodes. If true, edges are marshaled separately as a JSON array.
MarshalEdgesSeparately bool
// PropertyUnmarshaler unmarshals a property value. The return
// values are the key, value, and possible error. If the returned
// key is empty, the property is not unmarshaled. If this is nil,
// default json unmarshaler is used for property value.
PropertyUnmarshaler func(key string, value json.RawMessage) (string, interface{}, error)
// PropertyMarshaler marshals a property value. The return values
// are the key, the marshaled value, and possible error. If the
// returned key is empty, the property is not marshaled. If this is
// nil, the default json marshaler is used for property value.
PropertyMarshaler func(key string, value interface{}) (string, json.RawMessage, error)
}
// jsonNode contains the graph representation of a JSON node
type jsonNode struct {
N int `json:"n"`
Labels []string `json:"labels,omitempty"`
Properties map[string]json.RawMessage `json:"properties,omitempty"`
Edges []jsonOutgoingEdge `json:"edges,omitempty"`
}
// jsonEdge contains the graph representation of a JSON edge, using
// node indexes to address nodes
type jsonEdge struct {
From int `json:"from"`
To int `json:"to"`
Label string `json:"label,omitempty"`
Properties map[string]json.RawMessage `json:"properties,omitempty"`
}
// jsonOutgoingEdge contains an edge included in a node
type jsonOutgoingEdge struct {
To int `json:"to"`
Label string `json:"label,omitempty"`
Properties map[string]json.RawMessage `json:"properties,omitempty"`
}
var (
objBegin = []byte{'{'}
objEnd = []byte{'}'}
nKey = []byte(`"n":`)
comma = []byte{','}
labelsKey = []byte(`"labels":`)
propertiesKey = []byte(`"properties":`)
edgesKey = []byte(`"edges":`)
nodesKey = []byte(`"nodes":`)
arrBegin = []byte{'['}
arrEnd = []byte{']'}
)
// Encode the graph in JSON
func (j JSON) Encode(g *Graph, out io.Writer) error {
nodeMap := make(map[*Node]int)
marshalProperties := func(in map[string]interface{}) (map[string]json.RawMessage, error) {
ret := make(map[string]json.RawMessage)
for k, v := range in {
if j.PropertyMarshaler == nil {
d, _ := json.Marshal(v)
ret[k] = d
} else {
k, d, err := j.PropertyMarshaler(k, v)
if err != nil {
return nil, err
}
if len(k) > 0 {
ret[k] = d
}
}
}
return ret, nil
}
encodeEdge := func(edge *Edge, writeFrom bool) error {
var e interface{}
edgeProps := make(map[string]any)
for ix, v := range edge.properties {
edgeProps[ix] = v
}
properties, err := marshalProperties(edgeProps)
if err != nil {
return err
}
if writeFrom {
e = jsonEdge{
From: nodeMap[edge.GetFrom()],
To: nodeMap[edge.GetTo()],
Label: edge.label,
Properties: properties,
}
} else {
e = jsonOutgoingEdge{
To: nodeMap[edge.GetTo()],
Label: edge.label,
Properties: properties,
}
}
data, err := json.Marshal(e)
if err != nil {
return err
}
if _, err := out.Write(data); err != nil {
return err
}
return nil
}
i := 0
// Give each node an index
for nodes := g.GetNodes(); nodes.Next(); {
node := nodes.Node()
nodeMap[node] = i
i++
}
if _, err := out.Write(objBegin); err != nil {
return err
}
if len(nodeMap) > 0 {
if _, err := out.Write(nodesKey); err != nil {
return err
}
if _, err := out.Write(arrBegin); err != nil {
return err
}
// Write nodes
firstNode := true
for node, ix := range nodeMap {
if firstNode {
firstNode = false
} else {
if _, err := out.Write(comma); err != nil {
return err
}
}
if _, err := out.Write(objBegin); err != nil {
return err
}
if _, err := out.Write(nKey); err != nil {
return err
}
s := strconv.Itoa(ix)
if _, err := out.Write([]byte(s)); err != nil {
return err
}
if node.labels.Len() > 0 {
data, _ := json.Marshal(node.labels)
if _, err := out.Write(comma); err != nil {
return err
}
if _, err := out.Write(labelsKey); err != nil {
return err
}
if _, err := out.Write(data); err != nil {
return err
}
}
if len(node.properties) > 0 {
if _, err := out.Write(comma); err != nil {
return err
}
if _, err := out.Write(propertiesKey); err != nil {
return err
}
nodeProps := make(map[string]any)
for ix, v := range node.properties {
nodeProps[ix] = v
}
m, err := marshalProperties(nodeProps)
if err != nil {
return err
}
data, err := json.Marshal(m)
if err != nil {
return err
}
if _, err := out.Write(data); err != nil {
return err
}
}
if j.MarshalEdgesSeparately {
if _, err := out.Write(objEnd); err != nil {
return err
}
continue
}
first := true
for edges := node.GetEdges(OutgoingEdge); edges.Next(); {
edge := edges.Edge()
if first {
if _, err := out.Write(comma); err != nil {
return err
}
if _, err := out.Write(edgesKey); err != nil {
return err
}
if _, err := out.Write(arrBegin); err != nil {
return err
}
first = false
} else {
if _, err := out.Write(comma); err != nil {
return err
}
}
if err := encodeEdge(edge, false); err != nil {
return err
}
}
if !first {
if _, err := out.Write(arrEnd); err != nil {
return err
}
}
if _, err := out.Write(objEnd); err != nil {
return err
}
}
if _, err := out.Write(arrEnd); err != nil {
return err
}
}
if j.MarshalEdgesSeparately {
if _, err := out.Write(comma); err != nil {
return err
}
if _, err := out.Write(edgesKey); err != nil {
return err
}
if _, err := out.Write(arrBegin); err != nil {
return err
}
first := true
for edges := g.GetEdges(); edges.Next(); {
edge := edges.Edge()
if first {
first = false
} else {
if _, err := out.Write(comma); err != nil {
return err
}
}
if err := encodeEdge(edge, true); err != nil {
return err
}
}
if _, err := out.Write(arrEnd); err != nil {
return err
}
}
if _, err := out.Write(objEnd); err != nil {
return err
}
return nil
}
func (j JSON) unmarshalProperty(key string, value json.RawMessage) (string, interface{}, error) {
if j.PropertyUnmarshaler == nil {
var v interface{}
if err := json.Unmarshal(value, &v); err != nil {
return "", nil, err
}
return key, v, nil
}
return j.PropertyUnmarshaler(key, value)
}
func (j JSON) unmarshalProperties(input map[string]json.RawMessage) (map[string]interface{}, error) {
if input == nil {
return nil, nil
}
ret := make(map[string]interface{})
for k, v := range input {
k, v, err := j.unmarshalProperty(k, v)
if err != nil {
return nil, err
}
if len(k) > 0 {
k = j.Interner.Intern(k)
ret[k] = v
}
}
return ret, nil
}
func (j JSON) addNode(g *Graph, node jsonNode, nodeMap map[int]*Node, edgeQueue []jsonEdge) ([]jsonEdge, error) {
p, err := j.unmarshalProperties(node.Properties)
if err != nil {
return nil, err
}
for i := range node.Labels {
node.Labels[i] = j.Interner.Intern(node.Labels[i])
}
newNode := g.NewNode(node.Labels, p)
nodeMap[node.N] = newNode
for _, edge := range node.Edges {
to, ok := nodeMap[edge.To]
if !ok {
edgeQueue = append(edgeQueue, jsonEdge{
From: node.N,
To: edge.To,
Label: edge.Label,
Properties: edge.Properties,
})
} else {
if err := j.addEdge(g, newNode, to, edge.Label, edge.Properties); err != nil {
return nil, err
}
}
}
return edgeQueue, nil
}
func (j JSON) addEdge(g *Graph, from, to *Node, label string, properties map[string]json.RawMessage) error {
p, err := j.unmarshalProperties(properties)
if err != nil {
return err
}
g.NewEdge(from, to, label, p)
return nil
}
// Decode a graph in JSON
func (j JSON) Decode(g *Graph, input *json.Decoder) error {
if j.Interner == nil {
j.Interner = make(MapInterner)
}
tok, err := input.Token()
if err != nil {
return err
}
if tok != json.Delim('{') {
return &json.SyntaxError{Offset: input.InputOffset()}
}
depth := 0
nodeMap := make(map[int]*Node)
edgeQueue := make([]jsonEdge, 0)
for {
tok, err = input.Token()
if err != nil {
return err
}
if depth == 0 {
if s, ok := tok.(string); ok {
if s == "nodes" {
var nodes []jsonNode
if err := input.Decode(&nodes); err != nil {
return err
}
for _, node := range nodes {
if edgeQueue, err = j.addNode(g, node, nodeMap, edgeQueue); err != nil {
return err
}
}
} else if s == "edges" {
var edges []jsonEdge
if err := input.Decode(&edges); err != nil {
return err
}
for _, edge := range edges {
from, fromExists := nodeMap[edge.From]
to, toExists := nodeMap[edge.To]
if fromExists && toExists {
if err := j.addEdge(g, from, to, edge.Label, edge.Properties); err != nil {
return err
}
} else {
edgeQueue = append(edgeQueue, edge)
}
}
} else {
depth = 1
}
} else if tok == json.Delim('}') {
for _, edge := range edgeQueue {
from, ok := nodeMap[edge.From]
if !ok {
return ErrInvalidGraph{Msg: fmt.Sprintf("Invalid edge.from: %d", edge.From)}
}
to, ok := nodeMap[edge.To]
if !ok {
return ErrInvalidGraph{Msg: fmt.Sprintf("Invalid edge.to: %d", edge.To)}
}
if err := j.addEdge(g, from, to, edge.Label, edge.Properties); err != nil {
return err
}
}
return nil
}
} else {
switch tok {
case json.Delim('{'), json.Delim('['):
depth++
case json.Delim('}'), json.Delim(']'):
depth--
default:
depth--
}
}
}
}