-
Notifications
You must be signed in to change notification settings - Fork 768
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
427 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package json | ||
|
||
// Extension holds a set of additional rules to be used when unmarshaling | ||
// strict JSON or JSON-like content. | ||
type Extension struct { | ||
funcs map[string]funcExt | ||
keyed map[string]func() interface{} | ||
} | ||
|
||
type funcExt struct { | ||
key string | ||
args []string | ||
} | ||
|
||
// Extend changes the decoder behavior to consider the provided extension. | ||
func (dec *Decoder) Extend(ext *Extension) { dec.d.ext = ext } | ||
|
||
// Func defines a function call that may be observed inside JSON content. | ||
// A function with the provided name will be unmarshaled as the document | ||
// {key: {args[0]: ..., args[N]: ...}}. | ||
func (e *Extension) Func(name string, key string, args ...string) { | ||
if e.funcs == nil { | ||
e.funcs = make(map[string]funcExt) | ||
} | ||
e.funcs[name] = funcExt{key, args} | ||
} | ||
|
||
// KeyedDoc defines a key that when observed as the first element inside a | ||
// JSON document or sub-document triggers the parsing of that document as | ||
// the value returned by the provided function. | ||
func (e *Extension) KeyedDoc(key string, new func() interface{}) { | ||
if e.keyed == nil { | ||
e.keyed = make(map[string]func() interface{}) | ||
} | ||
e.keyed[key] = new | ||
} |
Oops, something went wrong.