-
Notifications
You must be signed in to change notification settings - Fork 0
/
dson.go
65 lines (59 loc) · 1.74 KB
/
dson.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 dson implements encoding, decoding, marshaling, unmarshaling,
// and verification of the DSON (Doge Serialized Object Notation)
// as defined here: https://dogeon.xyz/.
//
// Examples can be found here: https://muzzammil.xyz/dson.go
//
// Version 1.0.0
//
// MIT License;
// https://github.com/muhammadmuzzammil1998/dsongo/blob/master/LICENSE
//
// (c) 2018 Muhammad Muzzammil <[email protected]>
package dson
import "encoding/json"
// Encode encodes a JSON string into the DSON equivalent and
// retruns that DSON string
func Encode(json string) string {
return translate([]rune(json), map[string]string{
"{": "such",
"}": "wow",
":": "is",
"[": "so",
"]": "many",
"true": "yes",
"false": "no",
"null": "empty",
}, false)
}
// Decode decodes a DSON string into the JSON equivalent and
// retruns that JSON string
func Decode(dson string) string {
return translate([]rune(dson), map[string]string{
"such": "{",
"wow": "}",
"is": ":",
"so": "[",
"many": "]",
"and": ",",
"also": ",",
".": ",",
",": ",",
"!": ",",
"?": ",",
"yes": "true",
"no": "false",
"empty": "null",
}, true)
}
// Marshal returns the DSON encoding of v.
func Marshal(v interface{}) (string, error) {
m, err := json.Marshal(v)
return Encode(string(m)), err
}
// Unmarshal parses the DSON-encoded data and stores the result
// in the value pointed to by v. If v is nil or not a pointer,
// Unmarshal returns an InvalidUnmarshalError.
func Unmarshal(dson string, v interface{}) error { return json.Unmarshal([]byte(Decode(dson)), &v) }
// Valid returns true if the input DSON string is valid, otherwise returns false
func Valid(dson string) bool { return json.Valid([]byte(Decode(dson))) }