Skip to content
Browse files

Move customized json into its own package

  • Loading branch information...
1 parent b3107ee commit 018f3212217edeb152a91a41e32d09f23262b3cb @titanous titanous committed Jan 31, 2013
View
2 model_json_decode.go → json/decode.go
@@ -1,4 +1,4 @@
-package riak
+package json
// This file is a minor customization of the original encoding/json package for use in goriakpbc
View
12 model_json_encode.go → json/encode.go
@@ -1,4 +1,4 @@
-package riak
+package json
// This file is a minor customization of the original encoding/json package for use in goriakpbc
@@ -487,6 +487,8 @@ var (
encodeFieldsCache = make(map[reflect.Type][]encodeField)
)
+var SkipTypes = make(map[reflect.Type]bool)
+
// encodeFields returns a slice of encodeField for a given
// struct type.
func encodeFields(t reflect.Type) []encodeField {
@@ -517,13 +519,7 @@ func encodeFields(t reflect.Type) []encodeField {
continue
}
// Some special exceptions for riak - skip these fields!
- if f.Type == reflect.TypeOf(Model{}) {
- continue
- }
- if f.Type == reflect.TypeOf(One{}) {
- continue
- }
- if f.Type == reflect.TypeOf(Many{}) {
+ if SkipTypes[f.Type] {
continue
}
var ef encodeField
View
2 model_json_indent.go → json/indent.go
@@ -1,4 +1,4 @@
-package riak
+package json
// This file is a minor customization of the original encoding/json package for use in goriakpbc
View
2 model_json_scanner.go → json/scanner.go
@@ -1,4 +1,4 @@
-package riak
+package json
// This file is a minor customization of the original encoding/json package for use in goriakpbc
View
2 model_json_stream.go → json/stream.go
@@ -1,4 +1,4 @@
-package riak
+package json
// This file is a minor customization of the original encoding/json package for use in goriakpbc
View
14 model.go
@@ -5,6 +5,8 @@ import (
"fmt"
"reflect"
"strings"
+
+ "github.com/tpjg/goriakpbc/json"
)
/*
@@ -146,12 +148,12 @@ func (c *Client) mapData(dv reflect.Value, dt reflect.Type, data []byte, links [
// Double check there is a "_type" field that is the same as the struct
// name, this is only a warning though.
var mn modelName
- err = Unmarshal(data, &mn)
+ err = json.Unmarshal(data, &mn)
if err != nil || dt.Name() != mn.Type {
err = fmt.Errorf("Warning: struct name does not match _type in Riak (%v)", err)
}
// Unmarshal the destination model
- jserr := Unmarshal(data, dest)
+ jserr := json.Unmarshal(data, dest)
if jserr != nil {
err = fmt.Errorf("%v - %v", err, jserr) // Add error
}
@@ -373,7 +375,7 @@ func (c *Client) SaveAs(newKey string, dest Resolver) (err error) {
return DestinationNotInitialized
}
// JSON encode the entire struct
- data, err := Marshal(dest)
+ data, err := json.Marshal(dest)
if err != nil {
return err
}
@@ -592,3 +594,9 @@ func (m *Many) Add(dest Resolver) (err error) {
}
//TODO: create "Remove" for Many links
+
+func init() {
+ json.SkipTypes[reflect.TypeOf(Model{})] = true
+ json.SkipTypes[reflect.TypeOf(One{})] = true
+ json.SkipTypes[reflect.TypeOf(Many{})] = true
+}

0 comments on commit 018f321

Please sign in to comment.
Something went wrong with that request. Please try again.