From 6505a08ce95a5a5576da17e8a0db3c0eadcc9339 Mon Sep 17 00:00:00 2001 From: AzuraMeta Date: Mon, 14 Jul 2014 13:59:13 +0300 Subject: [PATCH 01/16] Added GetPathAny(), which is like GetPath(), but can also go through arrays --- simplejson.go | 19 +++++++++++++++++++ simplejson_test.go | 6 ++++++ 2 files changed, 25 insertions(+) diff --git a/simplejson.go b/simplejson.go index 516fe76..1f2e9e8 100644 --- a/simplejson.go +++ b/simplejson.go @@ -154,6 +154,25 @@ func (j *Json) GetIndex(index int) *Json { return &Json{nil} } +// GetPathAny is like GetPath, except it can also go through arrays +// using GetIndex(). +// +// js.GetPathAny("top_level", "entries", 3, "dict") +func (j *Json) GetPathAny(branch ...interface{}) *Json { + jin := j + for _, p := range branch { + switch p.(type) { + case string: + jin = jin.Get(p.(string)) + case int: + jin = jin.GetIndex(p.(int)) + default: + jin = &Json{nil} + } + } + return jin +} + // CheckGet returns a pointer to a new `Json` object and // a `bool` identifying success or failure // diff --git a/simplejson_test.go b/simplejson_test.go index 5a11156..375caa8 100644 --- a/simplejson_test.go +++ b/simplejson_test.go @@ -93,6 +93,12 @@ func TestSimplejson(t *testing.T) { gp2, _ := js.GetPath("test", "int").Int() assert.Equal(t, 10, gp2) + gpa, _ := js.GetPathAny("test", "string_array", 0).String() + assert.Equal(t, "asdf", gpa) + + gpa2, _ := js.GetPathAny("test", "arraywithsubs", 1, "subkeythree").Int() + assert.Equal(t, 3, gpa2) + assert.Equal(t, js.Get("test").Get("bool").MustBool(), true) js.Set("float2", 300.0) From 110b96b99ec5caf8f4e0addf3157c56e24eed124 Mon Sep 17 00:00:00 2001 From: AzuraMeta Date: Mon, 14 Jul 2014 15:02:58 +0300 Subject: [PATCH 02/16] Added MapJson() and ArrayJson() --- simplejson.go | 26 ++++++++++++++++++++++++++ simplejson_test.go | 10 ++++++++++ 2 files changed, 36 insertions(+) diff --git a/simplejson.go b/simplejson.go index 1f2e9e8..3370d08 100644 --- a/simplejson.go +++ b/simplejson.go @@ -190,6 +190,32 @@ func (j *Json) CheckGet(key string) (*Json, bool) { return nil, false } +// MapJson returns a copy of a Json map, but with values as Json objects +func (j *Json) MapJson() (map[string]*Json, error) { + m, err := j.Map() + if err != nil { + return nil, err + } + jm := make(map[string]*Json) + for key, val := range m { + jm[key] = &Json{val} + } + return jm, nil +} + +// ArrayJson splits a Json array into individual Jsons +func (j *Json) ArrayJson() ([]*Json, error) { + a, err := j.Array() + if err != nil { + return nil, err + } + ja := make([]*Json, len(a)) + for key, val := range a { + ja[key] = &Json{val} + } + return ja, nil +} + // Map type asserts to `map` func (j *Json) Map() (map[string]interface{}, error) { if m, ok := (j.data).(map[string]interface{}); ok { diff --git a/simplejson_test.go b/simplejson_test.go index 375caa8..8cc7970 100644 --- a/simplejson_test.go +++ b/simplejson_test.go @@ -99,6 +99,16 @@ func TestSimplejson(t *testing.T) { gpa2, _ := js.GetPathAny("test", "arraywithsubs", 1, "subkeythree").Int() assert.Equal(t, 3, gpa2) + gm, err := js.Get("test").MapJson() + assert.Equal(t, err, nil) + gmbool, _ := gm["bool"].Bool() + assert.Equal(t, true, gmbool) + + ga, err := js.GetPath("test", "string_array").ArrayJson() + assert.Equal(t, err, nil) + gastr, _ := ga[0].String() + assert.Equal(t, "asdf", gastr) + assert.Equal(t, js.Get("test").Get("bool").MustBool(), true) js.Set("float2", 300.0) From 287f6fbf32395ef107dd6de189a2655270790af5 Mon Sep 17 00:00:00 2001 From: AzuraMeta Date: Mon, 14 Jul 2014 19:33:14 +0300 Subject: [PATCH 03/16] Some naming and comment edits. --- simplejson.go | 8 ++++---- simplejson_test.go | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/simplejson.go b/simplejson.go index 3370d08..554f4b6 100644 --- a/simplejson.go +++ b/simplejson.go @@ -190,8 +190,8 @@ func (j *Json) CheckGet(key string) (*Json, bool) { return nil, false } -// MapJson returns a copy of a Json map, but with values as Json objects -func (j *Json) MapJson() (map[string]*Json, error) { +// JsonMap returns a copy of a Json map, but with values as Jsons +func (j *Json) JsonMap() (map[string]*Json, error) { m, err := j.Map() if err != nil { return nil, err @@ -203,8 +203,8 @@ func (j *Json) MapJson() (map[string]*Json, error) { return jm, nil } -// ArrayJson splits a Json array into individual Jsons -func (j *Json) ArrayJson() ([]*Json, error) { +// JsonArray returns a copy of an array, but with each value as a Json +func (j *Json) JsonArray() ([]*Json, error) { a, err := j.Array() if err != nil { return nil, err diff --git a/simplejson_test.go b/simplejson_test.go index 8cc7970..8ba56ff 100644 --- a/simplejson_test.go +++ b/simplejson_test.go @@ -99,15 +99,15 @@ func TestSimplejson(t *testing.T) { gpa2, _ := js.GetPathAny("test", "arraywithsubs", 1, "subkeythree").Int() assert.Equal(t, 3, gpa2) - gm, err := js.Get("test").MapJson() + jm, err := js.Get("test").JsonMap() assert.Equal(t, err, nil) - gmbool, _ := gm["bool"].Bool() - assert.Equal(t, true, gmbool) + jmbool, _ := jm["bool"].Bool() + assert.Equal(t, true, jmbool) - ga, err := js.GetPath("test", "string_array").ArrayJson() + ja, err := js.GetPath("test", "string_array").JsonArray() assert.Equal(t, err, nil) - gastr, _ := ga[0].String() - assert.Equal(t, "asdf", gastr) + jastr, _ := ja[0].String() + assert.Equal(t, "asdf", jastr) assert.Equal(t, js.Get("test").Get("bool").MustBool(), true) From 70959dcb0fbe93df192cfe3f462e0d087be42129 Mon Sep 17 00:00:00 2001 From: AzuraMeta Date: Wed, 16 Jul 2014 10:41:53 +0300 Subject: [PATCH 04/16] Added MustJsonMap() and MustJsonArray() --- simplejson.go | 40 ++++++++++++++++++++++++++++++++++++++++ simplejson_test.go | 6 ++++++ 2 files changed, 46 insertions(+) diff --git a/simplejson.go b/simplejson.go index 554f4b6..8815d13 100644 --- a/simplejson.go +++ b/simplejson.go @@ -277,6 +277,46 @@ func (j *Json) StringArray() ([]string, error) { return retArr, nil } +// MustJsonArray guarantees the return of a `[]interface{}` (with optional default) +func (j *Json) MustJsonArray(args ...[]*Json) []*Json { + var def []*Json + + switch len(args) { + case 0: + case 1: + def = args[0] + default: + log.Panicf("MustJsonArray() received too many arguments %d", len(args)) + } + + a, err := j.JsonArray() + if err == nil { + return a + } + + return def +} + +// MustJsonMap guarantees the return of a `map[string]interface{}` (with optional default) +func (j *Json) MustJsonMap(args ...map[string]*Json) map[string]*Json { + var def map[string]*Json + + switch len(args) { + case 0: + case 1: + def = args[0] + default: + log.Panicf("MustJsonMap() received too many arguments %d", len(args)) + } + + a, err := j.JsonMap() + if err == nil { + return a + } + + return def +} + // MustArray guarantees the return of a `[]interface{}` (with optional default) // // useful when you want to interate over array values in a succinct manner: diff --git a/simplejson_test.go b/simplejson_test.go index 8ba56ff..09d17b6 100644 --- a/simplejson_test.go +++ b/simplejson_test.go @@ -125,6 +125,12 @@ func TestSimplejson(t *testing.T) { js.GetPath("test", "sub_obj").Set("a", 3) assert.Equal(t, 3, js.GetPath("test", "sub_obj", "a").MustInt()) + + jmm:= js.Get("missing_map").MustJsonMap(map[string]*Json{"js1":js}) + assert.Equal(t, js, jmm["js1"]) + + jma := js.GetPath("missing_array").MustJsonArray([]*Json{js}) + assert.Equal(t, js, jma[0]) } func TestStdlibInterfaces(t *testing.T) { From bb58a70e58e50ae8f52ce82579c4fd594628d780 Mon Sep 17 00:00:00 2001 From: AzuraMeta Date: Wed, 16 Jul 2014 11:57:37 +0300 Subject: [PATCH 05/16] (COMP BREAK) Reworked the Get() methods - Get() now works like the old GetPathAny() - Reworked CheckGet() to also take a path - Removed GetIndex(), GetPath() and GetPathAny() because the new Get() supersedes them. --- simplejson.go | 87 +++++++++++++++++------------------------ simplejson_go10_test.go | 4 +- simplejson_go11_test.go | 4 +- simplejson_test.go | 52 ++++++++++++------------ 4 files changed, 66 insertions(+), 81 deletions(-) diff --git a/simplejson.go b/simplejson.go index 8815d13..6bfd71f 100644 --- a/simplejson.go +++ b/simplejson.go @@ -111,83 +111,68 @@ func (j *Json) Del(key string) { delete(m, key) } -// Get returns a pointer to a new `Json` object +// getKey returns a pointer to a new `Json` object // for `key` in its `map` representation -// -// useful for chaining operations (to traverse a nested JSON): -// js.Get("top_level").Get("dict").Get("value").Int() -func (j *Json) Get(key string) *Json { +// and a bool identifying success or failure +func (j *Json) getKey(key string) (*Json, bool) { m, err := j.Map() if err == nil { if val, ok := m[key]; ok { - return &Json{val} + return &Json{val}, true } } - return &Json{nil} -} - -// GetPath searches for the item as specified by the branch -// without the need to deep dive using Get()'s. -// -// js.GetPath("top_level", "dict") -func (j *Json) GetPath(branch ...string) *Json { - jin := j - for _, p := range branch { - jin = jin.Get(p) - } - return jin + return nil, false } -// GetIndex returns a pointer to a new `Json` object +// getIndex returns a pointer to a new `Json` object // for `index` in its `array` representation -// -// this is the analog to Get when accessing elements of -// a json array instead of a json object: -// js.Get("top_level").Get("array").GetIndex(1).Get("key").Int() -func (j *Json) GetIndex(index int) *Json { +// and a bool identifying success or failure +func (j *Json) getIndex(index int) (*Json, bool) { a, err := j.Array() if err == nil { if len(a) > index { - return &Json{a[index]} + return &Json{a[index]}, true } } - return &Json{nil} + return nil, false } -// GetPathAny is like GetPath, except it can also go through arrays -// using GetIndex(). +// Get searches for the item as specified by the branch +// within a nested Json and returns a new Json pointer +// the pointer is always a valid Json, allowing for chained operations // -// js.GetPathAny("top_level", "entries", 3, "dict") -func (j *Json) GetPathAny(branch ...interface{}) *Json { +// newJs := js.Get("top_level", "entries", 3, "dict") +func (j *Json) Get(branch ...interface{}) *Json { + jin, ok := j.CheckGet(branch...) + if ok { + return jin + } else { + return &Json{nil} + } +} + +// CheckGet is like Get, except it also returns a bool +// indicating whenever the branch was found or not +// the Json pointer mai be nil +// +// newJs, ok := js.Get("top_level", "entries", 3, "dict") +func (j *Json) CheckGet(branch ...interface{}) (*Json, bool) { jin := j + var ok bool for _, p := range branch { switch p.(type) { case string: - jin = jin.Get(p.(string)) + jin, ok = jin.getKey(p.(string)) case int: - jin = jin.GetIndex(p.(int)) + jin, ok = jin.getIndex(p.(int)) default: - jin = &Json{nil} + ok = false } - } - return jin -} - -// CheckGet returns a pointer to a new `Json` object and -// a `bool` identifying success or failure -// -// useful for chained operations when success is important: -// if data, ok := js.Get("top_level").CheckGet("inner"); ok { -// log.Println(data) -// } -func (j *Json) CheckGet(key string) (*Json, bool) { - m, err := j.Map() - if err == nil { - if val, ok := m[key]; ok { - return &Json{val}, true + if !ok { + return nil, false } } - return nil, false + return jin, true } // JsonMap returns a copy of a Json map, but with values as Jsons diff --git a/simplejson_go10_test.go b/simplejson_go10_test.go index 4aa6a51..28c3595 100644 --- a/simplejson_go10_test.go +++ b/simplejson_go10_test.go @@ -42,7 +42,7 @@ func TestNewFromReader(t *testing.T) { ma := js.Get("test").Get("array").MustArray() assert.Equal(t, ma, []interface{}{float64(1), "2", float64(3)}) - mm := js.Get("test").Get("arraywithsubs").GetIndex(0).MustMap() + mm := js.Get("test").Get("arraywithsubs").Get(0).MustMap() assert.Equal(t, mm, map[string]interface{}{"subkeyone": float64(1)}) assert.Equal(t, js.Get("test").Get("bignum").MustInt64(), int64(8000000000)) @@ -79,7 +79,7 @@ func TestSimplejsonGo10(t *testing.T) { ma := js.Get("test").Get("array").MustArray() assert.Equal(t, ma, []interface{}{float64(1), "2", float64(3)}) - mm := js.Get("test").Get("arraywithsubs").GetIndex(0).MustMap() + mm := js.Get("test").Get("arraywithsubs").Get(0).MustMap() assert.Equal(t, mm, map[string]interface{}{"subkeyone": float64(1)}) assert.Equal(t, js.Get("test").Get("bignum").MustInt64(), int64(8000000000)) diff --git a/simplejson_go11_test.go b/simplejson_go11_test.go index 16362ad..196d744 100644 --- a/simplejson_go11_test.go +++ b/simplejson_go11_test.go @@ -47,7 +47,7 @@ func TestNewFromReader(t *testing.T) { ma := js.Get("test").Get("array").MustArray() assert.Equal(t, ma, []interface{}{json.Number("1"), "2", json.Number("3")}) - mm := js.Get("test").Get("arraywithsubs").GetIndex(0).MustMap() + mm := js.Get("test").Get("arraywithsubs").Get(0).MustMap() assert.Equal(t, mm, map[string]interface{}{"subkeyone": json.Number("1")}) assert.Equal(t, js.Get("test").Get("bignum").MustInt64(), int64(9223372036854775807)) @@ -88,7 +88,7 @@ func TestSimplejsonGo11(t *testing.T) { ma := js.Get("test").Get("array").MustArray() assert.Equal(t, ma, []interface{}{json.Number("1"), "2", json.Number("3")}) - mm := js.Get("test").Get("arraywithsubs").GetIndex(0).MustMap() + mm := js.Get("test").Get("arraywithsubs").Get(0).MustMap() assert.Equal(t, mm, map[string]interface{}{"subkeyone": json.Number("1")}) assert.Equal(t, js.Get("test").Get("bignum").MustInt64(), int64(9223372036854775807)) diff --git a/simplejson_test.go b/simplejson_test.go index 09d17b6..184c182 100644 --- a/simplejson_test.go +++ b/simplejson_test.go @@ -38,11 +38,11 @@ func TestSimplejson(t *testing.T) { aws := js.Get("test").Get("arraywithsubs") assert.NotEqual(t, nil, aws) var awsval int - awsval, _ = aws.GetIndex(0).Get("subkeyone").Int() + awsval, _ = aws.Get(0).Get("subkeyone").Int() assert.Equal(t, 1, awsval) - awsval, _ = aws.GetIndex(1).Get("subkeytwo").Int() + awsval, _ = aws.Get(1).Get("subkeytwo").Int() assert.Equal(t, 2, awsval) - awsval, _ = aws.GetIndex(1).Get("subkeythree").Int() + awsval, _ = aws.Get(1).Get("subkeythree").Int() assert.Equal(t, 3, awsval) i, _ := js.Get("test").Get("int").Int() @@ -87,16 +87,16 @@ func TestSimplejson(t *testing.T) { assert.Equal(t, strs2[1], "") assert.Equal(t, strs2[2], "efg") - gp, _ := js.GetPath("test", "string").String() + gp, _ := js.Get("test", "string").String() assert.Equal(t, "simplejson", gp) - gp2, _ := js.GetPath("test", "int").Int() + gp2, _ := js.Get("test", "int").Int() assert.Equal(t, 10, gp2) - gpa, _ := js.GetPathAny("test", "string_array", 0).String() + gpa, _ := js.Get("test", "string_array", 0).String() assert.Equal(t, "asdf", gpa) - gpa2, _ := js.GetPathAny("test", "arraywithsubs", 1, "subkeythree").Int() + gpa2, _ := js.Get("test", "arraywithsubs", 1, "subkeythree").Int() assert.Equal(t, 3, gpa2) jm, err := js.Get("test").JsonMap() @@ -104,7 +104,7 @@ func TestSimplejson(t *testing.T) { jmbool, _ := jm["bool"].Bool() assert.Equal(t, true, jmbool) - ja, err := js.GetPath("test", "string_array").JsonArray() + ja, err := js.Get("test", "string_array").JsonArray() assert.Equal(t, err, nil) jastr, _ := ja[0].String() assert.Equal(t, "asdf", jastr) @@ -123,13 +123,13 @@ func TestSimplejson(t *testing.T) { js.Get("test").Get("sub_obj").Set("a", 2) assert.Equal(t, 2, js.Get("test").Get("sub_obj").Get("a").MustInt()) - js.GetPath("test", "sub_obj").Set("a", 3) - assert.Equal(t, 3, js.GetPath("test", "sub_obj", "a").MustInt()) - - jmm:= js.Get("missing_map").MustJsonMap(map[string]*Json{"js1":js}) + js.Get("test", "sub_obj").Set("a", 3) + assert.Equal(t, 3, js.Get("test", "sub_obj", "a").MustInt()) + + jmm := js.Get("missing_map").MustJsonMap(map[string]*Json{"js1": js}) assert.Equal(t, js, jmm["js1"]) - jma := js.GetPath("missing_array").MustJsonArray([]*Json{js}) + jma := js.Get("missing_array").MustJsonArray([]*Json{js}) assert.Equal(t, js, jma[0]) } @@ -164,7 +164,7 @@ func TestSet(t *testing.T) { js.Set("baz", "bing") - s, err := js.GetPath("baz").String() + s, err := js.Get("baz").String() assert.Equal(t, nil, err) assert.Equal(t, "bing", s) } @@ -176,7 +176,7 @@ func TestReplace(t *testing.T) { err = js.UnmarshalJSON([]byte(`{"baz":"bing"}`)) assert.Equal(t, nil, err) - s, err := js.GetPath("baz").String() + s, err := js.Get("baz").String() assert.Equal(t, nil, err) assert.Equal(t, "bing", s) } @@ -187,7 +187,7 @@ func TestSetPath(t *testing.T) { js.SetPath([]string{"foo", "bar"}, "baz") - s, err := js.GetPath("foo", "bar").String() + s, err := js.Get("foo", "bar").String() assert.Equal(t, nil, err) assert.Equal(t, "baz", s) } @@ -196,16 +196,16 @@ func TestSetPathNoPath(t *testing.T) { js, err := NewJson([]byte(`{"some":"data","some_number":1.0,"some_bool":false}`)) assert.Equal(t, nil, err) - f := js.GetPath("some_number").MustFloat64(99.0) + f := js.Get("some_number").MustFloat64(99.0) assert.Equal(t, f, 1.0) js.SetPath([]string{}, map[string]interface{}{"foo": "bar"}) - s, err := js.GetPath("foo").String() + s, err := js.Get("foo").String() assert.Equal(t, nil, err) assert.Equal(t, "bar", s) - f = js.GetPath("some_number").MustFloat64(99.0) + f = js.Get("some_number").MustFloat64(99.0) assert.Equal(t, f, 99.0) } @@ -216,29 +216,29 @@ func TestPathWillAugmentExisting(t *testing.T) { js.SetPath([]string{"this", "d"}, "dd") cases := []struct { - path []string + path []interface{} outcome string }{ { - path: []string{"this", "a"}, + path: []interface{}{"this", "a"}, outcome: "aa", }, { - path: []string{"this", "b"}, + path: []interface{}{"this", "b"}, outcome: "bb", }, { - path: []string{"this", "c"}, + path: []interface{}{"this", "c"}, outcome: "cc", }, { - path: []string{"this", "d"}, + path: []interface{}{"this", "d"}, outcome: "dd", }, } for _, tc := range cases { - s, err := js.GetPath(tc.path...).String() + s, err := js.Get(tc.path...).String() assert.Equal(t, nil, err) assert.Equal(t, tc.outcome, s) } @@ -251,7 +251,7 @@ func TestPathWillOverwriteExisting(t *testing.T) { js.SetPath([]string{"this", "a", "foo"}, "bar") - s, err := js.GetPath("this", "a", "foo").String() + s, err := js.Get("this", "a", "foo").String() assert.Equal(t, nil, err) assert.Equal(t, "bar", s) } From cfb04abd83b49ea9a3f712d8dd470e420b6d7172 Mon Sep 17 00:00:00 2001 From: AzuraMeta Date: Thu, 17 Jul 2014 16:10:55 +0300 Subject: [PATCH 06/16] Removed StringArray() Too specific, should use Array() or JsonArray() instead, and then cast the individual values as the type that you need. --- simplejson.go | 21 --------------------- simplejson_test.go | 12 ------------ 2 files changed, 33 deletions(-) diff --git a/simplejson.go b/simplejson.go index 6bfd71f..3e8c6eb 100644 --- a/simplejson.go +++ b/simplejson.go @@ -241,27 +241,6 @@ func (j *Json) Bytes() ([]byte, error) { return nil, errors.New("type assertion to []byte failed") } -// StringArray type asserts to an `array` of `string` -func (j *Json) StringArray() ([]string, error) { - arr, err := j.Array() - if err != nil { - return nil, err - } - retArr := make([]string, 0, len(arr)) - for _, a := range arr { - if a == nil { - retArr = append(retArr, "") - continue - } - s, ok := a.(string) - if !ok { - return nil, err - } - retArr = append(retArr, s) - } - return retArr, nil -} - // MustJsonArray guarantees the return of a `[]interface{}` (with optional default) func (j *Json) MustJsonArray(args ...[]*Json) []*Json { var def []*Json diff --git a/simplejson_test.go b/simplejson_test.go index 184c182..7bfc539 100644 --- a/simplejson_test.go +++ b/simplejson_test.go @@ -75,18 +75,6 @@ func TestSimplejson(t *testing.T) { mm2 := js.Get("test").Get("missing_map").MustMap(map[string]interface{}{"found": false}) assert.Equal(t, mm2, map[string]interface{}{"found": false}) - strs, err := js.Get("test").Get("string_array").StringArray() - assert.Equal(t, err, nil) - assert.Equal(t, strs[0], "asdf") - assert.Equal(t, strs[1], "ghjk") - assert.Equal(t, strs[2], "zxcv") - - strs2, err := js.Get("test").Get("string_array_null").StringArray() - assert.Equal(t, err, nil) - assert.Equal(t, strs2[0], "abc") - assert.Equal(t, strs2[1], "") - assert.Equal(t, strs2[2], "efg") - gp, _ := js.Get("test", "string").String() assert.Equal(t, "simplejson", gp) From d6f7ad38d98c78479bfe10f46e689cbb80e0284a Mon Sep 17 00:00:00 2001 From: AzuraMeta Date: Thu, 17 Jul 2014 17:05:10 +0300 Subject: [PATCH 07/16] Changed the cast methods - Added "Check" prefix to the ones that also return an error. - Removed the "Must" prefix from those that always return a valid value. fixes issue #15 --- simplejson.go | 140 ++++++++++++++++++++-------------------- simplejson_go10.go | 14 ++-- simplejson_go10_test.go | 16 ++--- simplejson_go11.go | 16 ++--- simplejson_go11_test.go | 20 +++--- simplejson_test.go | 76 +++++++++++----------- 6 files changed, 141 insertions(+), 141 deletions(-) diff --git a/simplejson.go b/simplejson.go index 3e8c6eb..9307005 100644 --- a/simplejson.go +++ b/simplejson.go @@ -33,8 +33,8 @@ func New() *Json { } } -// Interface returns the underlying data -func (j *Json) Interface() interface{} { +// CheckInterface returns the underlying data +func (j *Json) CheckInterface() interface{} { return j.data } @@ -56,7 +56,7 @@ func (j *Json) MarshalJSON() ([]byte, error) { // Set modifies `Json` map by `key` and `value` // Useful for changing single key/value in a `Json` object easily. func (j *Json) Set(key string, val interface{}) { - m, err := j.Map() + m, err := j.CheckMap() if err != nil { return } @@ -104,7 +104,7 @@ func (j *Json) SetPath(branch []string, val interface{}) { // Del modifies `Json` map by deleting `key` if it is present. func (j *Json) Del(key string) { - m, err := j.Map() + m, err := j.CheckMap() if err != nil { return } @@ -115,7 +115,7 @@ func (j *Json) Del(key string) { // for `key` in its `map` representation // and a bool identifying success or failure func (j *Json) getKey(key string) (*Json, bool) { - m, err := j.Map() + m, err := j.CheckMap() if err == nil { if val, ok := m[key]; ok { return &Json{val}, true @@ -128,7 +128,7 @@ func (j *Json) getKey(key string) (*Json, bool) { // for `index` in its `array` representation // and a bool identifying success or failure func (j *Json) getIndex(index int) (*Json, bool) { - a, err := j.Array() + a, err := j.CheckArray() if err == nil { if len(a) > index { return &Json{a[index]}, true @@ -175,9 +175,9 @@ func (j *Json) CheckGet(branch ...interface{}) (*Json, bool) { return jin, true } -// JsonMap returns a copy of a Json map, but with values as Jsons -func (j *Json) JsonMap() (map[string]*Json, error) { - m, err := j.Map() +// CheckJsonMap returns a copy of a Json map, but with values as Jsons +func (j *Json) CheckJsonMap() (map[string]*Json, error) { + m, err := j.CheckMap() if err != nil { return nil, err } @@ -188,9 +188,9 @@ func (j *Json) JsonMap() (map[string]*Json, error) { return jm, nil } -// JsonArray returns a copy of an array, but with each value as a Json -func (j *Json) JsonArray() ([]*Json, error) { - a, err := j.Array() +// CheckJsonArray returns a copy of an array, but with each value as a Json +func (j *Json) CheckJsonArray() ([]*Json, error) { + a, err := j.CheckArray() if err != nil { return nil, err } @@ -201,48 +201,48 @@ func (j *Json) JsonArray() ([]*Json, error) { return ja, nil } -// Map type asserts to `map` -func (j *Json) Map() (map[string]interface{}, error) { +// CheckMap type asserts to `map` +func (j *Json) CheckMap() (map[string]interface{}, error) { if m, ok := (j.data).(map[string]interface{}); ok { return m, nil } return nil, errors.New("type assertion to map[string]interface{} failed") } -// Array type asserts to an `array` -func (j *Json) Array() ([]interface{}, error) { +// CheckArray type asserts to an `array` +func (j *Json) CheckArray() ([]interface{}, error) { if a, ok := (j.data).([]interface{}); ok { return a, nil } return nil, errors.New("type assertion to []interface{} failed") } -// Bool type asserts to `bool` -func (j *Json) Bool() (bool, error) { +// CheckBool type asserts to `bool` +func (j *Json) CheckBool() (bool, error) { if s, ok := (j.data).(bool); ok { return s, nil } return false, errors.New("type assertion to bool failed") } -// String type asserts to `string` -func (j *Json) String() (string, error) { +// CheckString type asserts to `string` +func (j *Json) CheckString() (string, error) { if s, ok := (j.data).(string); ok { return s, nil } return "", errors.New("type assertion to string failed") } -// Bytes type asserts to `[]byte` -func (j *Json) Bytes() ([]byte, error) { +// CheckBytes type asserts to `[]byte` +func (j *Json) CheckBytes() ([]byte, error) { if s, ok := (j.data).(string); ok { return []byte(s), nil } return nil, errors.New("type assertion to []byte failed") } -// MustJsonArray guarantees the return of a `[]interface{}` (with optional default) -func (j *Json) MustJsonArray(args ...[]*Json) []*Json { +// JsonArray guarantees the return of a `[]interface{}` (with optional default) +func (j *Json) JsonArray(args ...[]*Json) []*Json { var def []*Json switch len(args) { @@ -250,10 +250,10 @@ func (j *Json) MustJsonArray(args ...[]*Json) []*Json { case 1: def = args[0] default: - log.Panicf("MustJsonArray() received too many arguments %d", len(args)) + log.Panicf("JsonArray() received too many arguments %d", len(args)) } - a, err := j.JsonArray() + a, err := j.CheckJsonArray() if err == nil { return a } @@ -261,8 +261,8 @@ func (j *Json) MustJsonArray(args ...[]*Json) []*Json { return def } -// MustJsonMap guarantees the return of a `map[string]interface{}` (with optional default) -func (j *Json) MustJsonMap(args ...map[string]*Json) map[string]*Json { +// JsonMap guarantees the return of a `map[string]interface{}` (with optional default) +func (j *Json) JsonMap(args ...map[string]*Json) map[string]*Json { var def map[string]*Json switch len(args) { @@ -270,10 +270,10 @@ func (j *Json) MustJsonMap(args ...map[string]*Json) map[string]*Json { case 1: def = args[0] default: - log.Panicf("MustJsonMap() received too many arguments %d", len(args)) + log.Panicf("JsonMap() received too many arguments %d", len(args)) } - a, err := j.JsonMap() + a, err := j.CheckJsonMap() if err == nil { return a } @@ -281,13 +281,13 @@ func (j *Json) MustJsonMap(args ...map[string]*Json) map[string]*Json { return def } -// MustArray guarantees the return of a `[]interface{}` (with optional default) +// Array guarantees the return of a `[]interface{}` (with optional default) // // useful when you want to interate over array values in a succinct manner: -// for i, v := range js.Get("results").MustArray() { +// for i, v := range js.Get("results").Array() { // fmt.Println(i, v) // } -func (j *Json) MustArray(args ...[]interface{}) []interface{} { +func (j *Json) Array(args ...[]interface{}) []interface{} { var def []interface{} switch len(args) { @@ -295,10 +295,10 @@ func (j *Json) MustArray(args ...[]interface{}) []interface{} { case 1: def = args[0] default: - log.Panicf("MustArray() received too many arguments %d", len(args)) + log.Panicf("Array() received too many arguments %d", len(args)) } - a, err := j.Array() + a, err := j.CheckArray() if err == nil { return a } @@ -306,13 +306,13 @@ func (j *Json) MustArray(args ...[]interface{}) []interface{} { return def } -// MustMap guarantees the return of a `map[string]interface{}` (with optional default) +// Map guarantees the return of a `map[string]interface{}` (with optional default) // // useful when you want to interate over map values in a succinct manner: -// for k, v := range js.Get("dictionary").MustMap() { +// for k, v := range js.Get("dictionary").Map() { // fmt.Println(k, v) // } -func (j *Json) MustMap(args ...map[string]interface{}) map[string]interface{} { +func (j *Json) Map(args ...map[string]interface{}) map[string]interface{} { var def map[string]interface{} switch len(args) { @@ -320,10 +320,10 @@ func (j *Json) MustMap(args ...map[string]interface{}) map[string]interface{} { case 1: def = args[0] default: - log.Panicf("MustMap() received too many arguments %d", len(args)) + log.Panicf("Map() received too many arguments %d", len(args)) } - a, err := j.Map() + a, err := j.CheckMap() if err == nil { return a } @@ -331,11 +331,11 @@ func (j *Json) MustMap(args ...map[string]interface{}) map[string]interface{} { return def } -// MustString guarantees the return of a `string` (with optional default) +// String guarantees the return of a `string` (with optional default) // // useful when you explicitly want a `string` in a single value return context: -// myFunc(js.Get("param1").MustString(), js.Get("optional_param").MustString("my_default")) -func (j *Json) MustString(args ...string) string { +// myFunc(js.Get("param1").String(), js.Get("optional_param").String("my_default")) +func (j *Json) String(args ...string) string { var def string switch len(args) { @@ -343,10 +343,10 @@ func (j *Json) MustString(args ...string) string { case 1: def = args[0] default: - log.Panicf("MustString() received too many arguments %d", len(args)) + log.Panicf("String() received too many arguments %d", len(args)) } - s, err := j.String() + s, err := j.CheckString() if err == nil { return s } @@ -354,11 +354,11 @@ func (j *Json) MustString(args ...string) string { return def } -// MustInt guarantees the return of an `int` (with optional default) +// Int guarantees the return of an `int` (with optional default) // // useful when you explicitly want an `int` in a single value return context: -// myFunc(js.Get("param1").MustInt(), js.Get("optional_param").MustInt(5150)) -func (j *Json) MustInt(args ...int) int { +// myFunc(js.Get("param1").Int(), js.Get("optional_param").Int(5150)) +func (j *Json) Int(args ...int) int { var def int switch len(args) { @@ -366,10 +366,10 @@ func (j *Json) MustInt(args ...int) int { case 1: def = args[0] default: - log.Panicf("MustInt() received too many arguments %d", len(args)) + log.Panicf("Int() received too many arguments %d", len(args)) } - i, err := j.Int() + i, err := j.CheckInt() if err == nil { return i } @@ -377,11 +377,11 @@ func (j *Json) MustInt(args ...int) int { return def } -// MustFloat64 guarantees the return of a `float64` (with optional default) +// Float64 guarantees the return of a `float64` (with optional default) // // useful when you explicitly want a `float64` in a single value return context: -// myFunc(js.Get("param1").MustFloat64(), js.Get("optional_param").MustFloat64(5.150)) -func (j *Json) MustFloat64(args ...float64) float64 { +// myFunc(js.Get("param1").Float64(), js.Get("optional_param").Float64(5.150)) +func (j *Json) Float64(args ...float64) float64 { var def float64 switch len(args) { @@ -389,10 +389,10 @@ func (j *Json) MustFloat64(args ...float64) float64 { case 1: def = args[0] default: - log.Panicf("MustFloat64() received too many arguments %d", len(args)) + log.Panicf("Float64() received too many arguments %d", len(args)) } - f, err := j.Float64() + f, err := j.CheckFloat64() if err == nil { return f } @@ -400,11 +400,11 @@ func (j *Json) MustFloat64(args ...float64) float64 { return def } -// MustBool guarantees the return of a `bool` (with optional default) +// Bool guarantees the return of a `bool` (with optional default) // // useful when you explicitly want a `bool` in a single value return context: -// myFunc(js.Get("param1").MustBool(), js.Get("optional_param").MustBool(true)) -func (j *Json) MustBool(args ...bool) bool { +// myFunc(js.Get("param1").Bool(), js.Get("optional_param").Bool(true)) +func (j *Json) Bool(args ...bool) bool { var def bool switch len(args) { @@ -412,10 +412,10 @@ func (j *Json) MustBool(args ...bool) bool { case 1: def = args[0] default: - log.Panicf("MustBool() received too many arguments %d", len(args)) + log.Panicf("Bool() received too many arguments %d", len(args)) } - b, err := j.Bool() + b, err := j.CheckBool() if err == nil { return b } @@ -423,11 +423,11 @@ func (j *Json) MustBool(args ...bool) bool { return def } -// MustInt64 guarantees the return of an `int64` (with optional default) +// Int64 guarantees the return of an `int64` (with optional default) // // useful when you explicitly want an `int64` in a single value return context: -// myFunc(js.Get("param1").MustInt64(), js.Get("optional_param").MustInt64(5150)) -func (j *Json) MustInt64(args ...int64) int64 { +// myFunc(js.Get("param1").Int64(), js.Get("optional_param").Int64(5150)) +func (j *Json) Int64(args ...int64) int64 { var def int64 switch len(args) { @@ -435,10 +435,10 @@ func (j *Json) MustInt64(args ...int64) int64 { case 1: def = args[0] default: - log.Panicf("MustInt64() received too many arguments %d", len(args)) + log.Panicf("Int64() received too many arguments %d", len(args)) } - i, err := j.Int64() + i, err := j.CheckInt64() if err == nil { return i } @@ -446,11 +446,11 @@ func (j *Json) MustInt64(args ...int64) int64 { return def } -// MustUInt64 guarantees the return of an `uint64` (with optional default) +// UInt64 guarantees the return of an `uint64` (with optional default) // // useful when you explicitly want an `uint64` in a single value return context: -// myFunc(js.Get("param1").MustUint64(), js.Get("optional_param").MustUint64(5150)) -func (j *Json) MustUint64(args ...uint64) uint64 { +// myFunc(js.Get("param1").Uint64(), js.Get("optional_param").Uint64(5150)) +func (j *Json) Uint64(args ...uint64) uint64 { var def uint64 switch len(args) { @@ -458,10 +458,10 @@ func (j *Json) MustUint64(args ...uint64) uint64 { case 1: def = args[0] default: - log.Panicf("MustUint64() received too many arguments %d", len(args)) + log.Panicf("Uint64() received too many arguments %d", len(args)) } - i, err := j.Uint64() + i, err := j.CheckUint64() if err == nil { return i } diff --git a/simplejson_go10.go b/simplejson_go10.go index c9151e9..0a0faa0 100644 --- a/simplejson_go10.go +++ b/simplejson_go10.go @@ -22,8 +22,8 @@ func (j *Json) UnmarshalJSON(p []byte) error { return json.Unmarshal(p, &j.data) } -// Float64 coerces into a float64 -func (j *Json) Float64() (float64, error) { +// CheckFloat64 coerces into a float64 +func (j *Json) CheckFloat64() (float64, error) { switch j.data.(type) { case float32, float64: return reflect.ValueOf(j.data).Float(), nil @@ -36,7 +36,7 @@ func (j *Json) Float64() (float64, error) { } // Int coerces into an int -func (j *Json) Int() (int, error) { +func (j *Json) CheckInt() (int, error) { switch j.data.(type) { case float32, float64: return int(reflect.ValueOf(j.data).Float()), nil @@ -48,8 +48,8 @@ func (j *Json) Int() (int, error) { return 0, errors.New("invalid value type") } -// Int64 coerces into an int64 -func (j *Json) Int64() (int64, error) { +// CheckInt64 coerces into an int64 +func (j *Json) CheckInt64() (int64, error) { switch j.data.(type) { case float32, float64: return int64(reflect.ValueOf(j.data).Float()), nil @@ -61,8 +61,8 @@ func (j *Json) Int64() (int64, error) { return 0, errors.New("invalid value type") } -// Uint64 coerces into an uint64 -func (j *Json) Uint64() (uint64, error) { +// CheckUint64 coerces into an uint64 +func (j *Json) CheckUint64() (uint64, error) { switch j.data.(type) { case float32, float64: return uint64(reflect.ValueOf(j.data).Float()), nil diff --git a/simplejson_go10_test.go b/simplejson_go10_test.go index 28c3595..e925f8a 100644 --- a/simplejson_go10_test.go +++ b/simplejson_go10_test.go @@ -26,7 +26,7 @@ func TestNewFromReader(t *testing.T) { assert.NotEqual(t, nil, js) assert.Equal(t, nil, err) - arr, _ := js.Get("test").Get("array").Array() + arr, _ := js.Get("test").Get("array").CheckArray() assert.NotEqual(t, nil, arr) for i, v := range arr { var iv int @@ -39,13 +39,13 @@ func TestNewFromReader(t *testing.T) { assert.Equal(t, i+1, iv) } - ma := js.Get("test").Get("array").MustArray() + ma := js.Get("test").Get("array").Array() assert.Equal(t, ma, []interface{}{float64(1), "2", float64(3)}) - mm := js.Get("test").Get("arraywithsubs").Get(0).MustMap() + mm := js.Get("test").Get("arraywithsubs").Get(0).Map() assert.Equal(t, mm, map[string]interface{}{"subkeyone": float64(1)}) - assert.Equal(t, js.Get("test").Get("bignum").MustInt64(), int64(8000000000)) + assert.Equal(t, js.Get("test").Get("bignum").Int64(), int64(8000000000)) } func TestSimplejsonGo10(t *testing.T) { @@ -63,7 +63,7 @@ func TestSimplejsonGo10(t *testing.T) { assert.NotEqual(t, nil, js) assert.Equal(t, nil, err) - arr, _ := js.Get("test").Get("array").Array() + arr, _ := js.Get("test").Get("array").CheckArray() assert.NotEqual(t, nil, arr) for i, v := range arr { var iv int @@ -76,11 +76,11 @@ func TestSimplejsonGo10(t *testing.T) { assert.Equal(t, i+1, iv) } - ma := js.Get("test").Get("array").MustArray() + ma := js.Get("test").Get("array").Array() assert.Equal(t, ma, []interface{}{float64(1), "2", float64(3)}) - mm := js.Get("test").Get("arraywithsubs").Get(0).MustMap() + mm := js.Get("test").Get("arraywithsubs").Get(0).Map() assert.Equal(t, mm, map[string]interface{}{"subkeyone": float64(1)}) - assert.Equal(t, js.Get("test").Get("bignum").MustInt64(), int64(8000000000)) + assert.Equal(t, js.Get("test").Get("bignum").Int64(), int64(8000000000)) } diff --git a/simplejson_go11.go b/simplejson_go11.go index 1c47953..8adaf97 100644 --- a/simplejson_go11.go +++ b/simplejson_go11.go @@ -27,8 +27,8 @@ func NewFromReader(r io.Reader) (*Json, error) { return j, err } -// Float64 coerces into a float64 -func (j *Json) Float64() (float64, error) { +// CheckFloat64 coerces into a float64 +func (j *Json) CheckFloat64() (float64, error) { switch j.data.(type) { case json.Number: return j.data.(json.Number).Float64() @@ -42,8 +42,8 @@ func (j *Json) Float64() (float64, error) { return 0, errors.New("invalid value type") } -// Int coerces into an int -func (j *Json) Int() (int, error) { +// CheckInt coerces into an int +func (j *Json) CheckInt() (int, error) { switch j.data.(type) { case json.Number: i, err := j.data.(json.Number).Int64() @@ -58,8 +58,8 @@ func (j *Json) Int() (int, error) { return 0, errors.New("invalid value type") } -// Int64 coerces into an int64 -func (j *Json) Int64() (int64, error) { +// CheckInt64 coerces into an int64 +func (j *Json) CheckInt64() (int64, error) { switch j.data.(type) { case json.Number: return j.data.(json.Number).Int64() @@ -73,8 +73,8 @@ func (j *Json) Int64() (int64, error) { return 0, errors.New("invalid value type") } -// Uint64 coerces into an uint64 -func (j *Json) Uint64() (uint64, error) { +// CheckUint64 coerces into an uint64 +func (j *Json) CheckUint64() (uint64, error) { switch j.data.(type) { case json.Number: return strconv.ParseUint(j.data.(json.Number).String(), 10, 64) diff --git a/simplejson_go11_test.go b/simplejson_go11_test.go index 196d744..93e4de2 100644 --- a/simplejson_go11_test.go +++ b/simplejson_go11_test.go @@ -29,7 +29,7 @@ func TestNewFromReader(t *testing.T) { assert.NotEqual(t, nil, js) assert.Equal(t, nil, err) - arr, _ := js.Get("test").Get("array").Array() + arr, _ := js.Get("test").Get("array").CheckArray() assert.NotEqual(t, nil, arr) for i, v := range arr { var iv int @@ -44,14 +44,14 @@ func TestNewFromReader(t *testing.T) { assert.Equal(t, i+1, iv) } - ma := js.Get("test").Get("array").MustArray() + ma := js.Get("test").Get("array").Array() assert.Equal(t, ma, []interface{}{json.Number("1"), "2", json.Number("3")}) - mm := js.Get("test").Get("arraywithsubs").Get(0).MustMap() + mm := js.Get("test").Get("arraywithsubs").Get(0).Map() assert.Equal(t, mm, map[string]interface{}{"subkeyone": json.Number("1")}) - assert.Equal(t, js.Get("test").Get("bignum").MustInt64(), int64(9223372036854775807)) - assert.Equal(t, js.Get("test").Get("uint64").MustUint64(), uint64(18446744073709551615)) + assert.Equal(t, js.Get("test").Get("bignum").Int64(), int64(9223372036854775807)) + assert.Equal(t, js.Get("test").Get("uint64").Uint64(), uint64(18446744073709551615)) } func TestSimplejsonGo11(t *testing.T) { @@ -70,7 +70,7 @@ func TestSimplejsonGo11(t *testing.T) { assert.NotEqual(t, nil, js) assert.Equal(t, nil, err) - arr, _ := js.Get("test").Get("array").Array() + arr, _ := js.Get("test").Get("array").CheckArray() assert.NotEqual(t, nil, arr) for i, v := range arr { var iv int @@ -85,12 +85,12 @@ func TestSimplejsonGo11(t *testing.T) { assert.Equal(t, i+1, iv) } - ma := js.Get("test").Get("array").MustArray() + ma := js.Get("test").Get("array").Array() assert.Equal(t, ma, []interface{}{json.Number("1"), "2", json.Number("3")}) - mm := js.Get("test").Get("arraywithsubs").Get(0).MustMap() + mm := js.Get("test").Get("arraywithsubs").Get(0).Map() assert.Equal(t, mm, map[string]interface{}{"subkeyone": json.Number("1")}) - assert.Equal(t, js.Get("test").Get("bignum").MustInt64(), int64(9223372036854775807)) - assert.Equal(t, js.Get("test").Get("uint64").MustUint64(), uint64(18446744073709551615)) + assert.Equal(t, js.Get("test").Get("bignum").Int64(), int64(9223372036854775807)) + assert.Equal(t, js.Get("test").Get("uint64").Uint64(), uint64(18446744073709551615)) } diff --git a/simplejson_test.go b/simplejson_test.go index 7bfc539..6a1ae82 100644 --- a/simplejson_test.go +++ b/simplejson_test.go @@ -38,86 +38,86 @@ func TestSimplejson(t *testing.T) { aws := js.Get("test").Get("arraywithsubs") assert.NotEqual(t, nil, aws) var awsval int - awsval, _ = aws.Get(0).Get("subkeyone").Int() + awsval, _ = aws.Get(0).Get("subkeyone").CheckInt() assert.Equal(t, 1, awsval) - awsval, _ = aws.Get(1).Get("subkeytwo").Int() + awsval, _ = aws.Get(1).Get("subkeytwo").CheckInt() assert.Equal(t, 2, awsval) - awsval, _ = aws.Get(1).Get("subkeythree").Int() + awsval, _ = aws.Get(1).Get("subkeythree").CheckInt() assert.Equal(t, 3, awsval) - i, _ := js.Get("test").Get("int").Int() + i, _ := js.Get("test").Get("int").CheckInt() assert.Equal(t, 10, i) - f, _ := js.Get("test").Get("float").Float64() + f, _ := js.Get("test").Get("float").CheckFloat64() assert.Equal(t, 5.150, f) - s, _ := js.Get("test").Get("string").String() + s, _ := js.Get("test").Get("string").CheckString() assert.Equal(t, "simplejson", s) - b, _ := js.Get("test").Get("bool").Bool() + b, _ := js.Get("test").Get("bool").CheckBool() assert.Equal(t, true, b) - mi := js.Get("test").Get("int").MustInt() + mi := js.Get("test").Get("int").Int() assert.Equal(t, 10, mi) - mi2 := js.Get("test").Get("missing_int").MustInt(5150) + mi2 := js.Get("test").Get("missing_int").Int(5150) assert.Equal(t, 5150, mi2) - ms := js.Get("test").Get("string").MustString() + ms := js.Get("test").Get("string").String() assert.Equal(t, "simplejson", ms) - ms2 := js.Get("test").Get("missing_string").MustString("fyea") + ms2 := js.Get("test").Get("missing_string").String("fyea") assert.Equal(t, "fyea", ms2) - ma2 := js.Get("test").Get("missing_array").MustArray([]interface{}{"1", 2, "3"}) + ma2 := js.Get("test").Get("missing_array").Array([]interface{}{"1", 2, "3"}) assert.Equal(t, ma2, []interface{}{"1", 2, "3"}) - mm2 := js.Get("test").Get("missing_map").MustMap(map[string]interface{}{"found": false}) + mm2 := js.Get("test").Get("missing_map").Map(map[string]interface{}{"found": false}) assert.Equal(t, mm2, map[string]interface{}{"found": false}) - gp, _ := js.Get("test", "string").String() + gp, _ := js.Get("test", "string").CheckString() assert.Equal(t, "simplejson", gp) - gp2, _ := js.Get("test", "int").Int() + gp2, _ := js.Get("test", "int").CheckInt() assert.Equal(t, 10, gp2) - gpa, _ := js.Get("test", "string_array", 0).String() + gpa, _ := js.Get("test", "string_array", 0).CheckString() assert.Equal(t, "asdf", gpa) - gpa2, _ := js.Get("test", "arraywithsubs", 1, "subkeythree").Int() + gpa2, _ := js.Get("test", "arraywithsubs", 1, "subkeythree").CheckInt() assert.Equal(t, 3, gpa2) - jm, err := js.Get("test").JsonMap() + jm, err := js.Get("test").CheckJsonMap() assert.Equal(t, err, nil) - jmbool, _ := jm["bool"].Bool() + jmbool, _ := jm["bool"].CheckBool() assert.Equal(t, true, jmbool) - ja, err := js.Get("test", "string_array").JsonArray() + ja, err := js.Get("test", "string_array").CheckJsonArray() assert.Equal(t, err, nil) - jastr, _ := ja[0].String() + jastr, _ := ja[0].CheckString() assert.Equal(t, "asdf", jastr) - assert.Equal(t, js.Get("test").Get("bool").MustBool(), true) + assert.Equal(t, js.Get("test").Get("bool").Bool(), true) js.Set("float2", 300.0) - assert.Equal(t, js.Get("float2").MustFloat64(), 300.0) + assert.Equal(t, js.Get("float2").Float64(), 300.0) js.Set("test2", "setTest") - assert.Equal(t, "setTest", js.Get("test2").MustString()) + assert.Equal(t, "setTest", js.Get("test2").String()) js.Del("test2") - assert.NotEqual(t, "setTest", js.Get("test2").MustString()) + assert.NotEqual(t, "setTest", js.Get("test2").String()) js.Get("test").Get("sub_obj").Set("a", 2) - assert.Equal(t, 2, js.Get("test").Get("sub_obj").Get("a").MustInt()) + assert.Equal(t, 2, js.Get("test").Get("sub_obj").Get("a").Int()) js.Get("test", "sub_obj").Set("a", 3) - assert.Equal(t, 3, js.Get("test", "sub_obj", "a").MustInt()) + assert.Equal(t, 3, js.Get("test", "sub_obj", "a").Int()) - jmm := js.Get("missing_map").MustJsonMap(map[string]*Json{"js1": js}) + jmm := js.Get("missing_map").JsonMap(map[string]*Json{"js1": js}) assert.Equal(t, js, jmm["js1"]) - jma := js.Get("missing_array").MustJsonArray([]*Json{js}) + jma := js.Get("missing_array").JsonArray([]*Json{js}) assert.Equal(t, js, jma[0]) } @@ -137,7 +137,7 @@ func TestStdlibInterfaces(t *testing.T) { assert.Equal(t, "myobject", val.Name) assert.NotEqual(t, nil, val.Params.data) - s, _ := val.Params.Get("string").String() + s, _ := val.Params.Get("string").CheckString() assert.Equal(t, "simplejson", s) p, err := json.Marshal(val) @@ -152,7 +152,7 @@ func TestSet(t *testing.T) { js.Set("baz", "bing") - s, err := js.Get("baz").String() + s, err := js.Get("baz").CheckString() assert.Equal(t, nil, err) assert.Equal(t, "bing", s) } @@ -164,7 +164,7 @@ func TestReplace(t *testing.T) { err = js.UnmarshalJSON([]byte(`{"baz":"bing"}`)) assert.Equal(t, nil, err) - s, err := js.Get("baz").String() + s, err := js.Get("baz").CheckString() assert.Equal(t, nil, err) assert.Equal(t, "bing", s) } @@ -175,7 +175,7 @@ func TestSetPath(t *testing.T) { js.SetPath([]string{"foo", "bar"}, "baz") - s, err := js.Get("foo", "bar").String() + s, err := js.Get("foo", "bar").CheckString() assert.Equal(t, nil, err) assert.Equal(t, "baz", s) } @@ -184,16 +184,16 @@ func TestSetPathNoPath(t *testing.T) { js, err := NewJson([]byte(`{"some":"data","some_number":1.0,"some_bool":false}`)) assert.Equal(t, nil, err) - f := js.Get("some_number").MustFloat64(99.0) + f := js.Get("some_number").Float64(99.0) assert.Equal(t, f, 1.0) js.SetPath([]string{}, map[string]interface{}{"foo": "bar"}) - s, err := js.Get("foo").String() + s, err := js.Get("foo").CheckString() assert.Equal(t, nil, err) assert.Equal(t, "bar", s) - f = js.Get("some_number").MustFloat64(99.0) + f = js.Get("some_number").Float64(99.0) assert.Equal(t, f, 99.0) } @@ -226,7 +226,7 @@ func TestPathWillAugmentExisting(t *testing.T) { } for _, tc := range cases { - s, err := js.Get(tc.path...).String() + s, err := js.Get(tc.path...).CheckString() assert.Equal(t, nil, err) assert.Equal(t, tc.outcome, s) } @@ -239,7 +239,7 @@ func TestPathWillOverwriteExisting(t *testing.T) { js.SetPath([]string{"this", "a", "foo"}, "bar") - s, err := js.Get("this", "a", "foo").String() + s, err := js.Get("this", "a", "foo").CheckString() assert.Equal(t, nil, err) assert.Equal(t, "bar", s) } From f48d2850498a9b8b568cca92c310e430bc111136 Mon Sep 17 00:00:00 2001 From: AzuraMeta Date: Thu, 17 Jul 2014 17:23:33 +0300 Subject: [PATCH 08/16] Fixed missing return error in go 1.0.3 --- simplejson.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/simplejson.go b/simplejson.go index 9307005..c02001a 100644 --- a/simplejson.go +++ b/simplejson.go @@ -146,9 +146,8 @@ func (j *Json) Get(branch ...interface{}) *Json { jin, ok := j.CheckGet(branch...) if ok { return jin - } else { - return &Json{nil} } + return &Json{nil} } // CheckGet is like Get, except it also returns a bool From a487d55458c2b3d57cf46988900fa1a502475ba7 Mon Sep 17 00:00:00 2001 From: AzuraMeta Date: Thu, 17 Jul 2014 20:01:54 +0300 Subject: [PATCH 09/16] Removed misplaced prefix from CheckInterface() --- simplejson.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/simplejson.go b/simplejson.go index c02001a..2f112a2 100644 --- a/simplejson.go +++ b/simplejson.go @@ -33,8 +33,8 @@ func New() *Json { } } -// CheckInterface returns the underlying data -func (j *Json) CheckInterface() interface{} { +// Interface returns the underlying data +func (j *Json) Interface() interface{} { return j.data } From 0030fe87a3afcf5f6a23808b9e9b2b737dca6d31 Mon Sep 17 00:00:00 2001 From: AzuraMeta Date: Fri, 18 Jul 2014 13:32:50 +0300 Subject: [PATCH 10/16] Renamed *Json*() methods to *JSON*() --- simplejson.go | 24 ++++++++++++------------ simplejson_test.go | 8 ++++---- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/simplejson.go b/simplejson.go index 2f112a2..e6b4cd1 100644 --- a/simplejson.go +++ b/simplejson.go @@ -174,8 +174,8 @@ func (j *Json) CheckGet(branch ...interface{}) (*Json, bool) { return jin, true } -// CheckJsonMap returns a copy of a Json map, but with values as Jsons -func (j *Json) CheckJsonMap() (map[string]*Json, error) { +// CheckJSONMap returns a copy of a Json map, but with values as Jsons +func (j *Json) CheckJSONMap() (map[string]*Json, error) { m, err := j.CheckMap() if err != nil { return nil, err @@ -187,8 +187,8 @@ func (j *Json) CheckJsonMap() (map[string]*Json, error) { return jm, nil } -// CheckJsonArray returns a copy of an array, but with each value as a Json -func (j *Json) CheckJsonArray() ([]*Json, error) { +// CheckJSONArray returns a copy of an array, but with each value as a Json +func (j *Json) CheckJSONArray() ([]*Json, error) { a, err := j.CheckArray() if err != nil { return nil, err @@ -240,8 +240,8 @@ func (j *Json) CheckBytes() ([]byte, error) { return nil, errors.New("type assertion to []byte failed") } -// JsonArray guarantees the return of a `[]interface{}` (with optional default) -func (j *Json) JsonArray(args ...[]*Json) []*Json { +// JSONArray guarantees the return of a `[]interface{}` (with optional default) +func (j *Json) JSONArray(args ...[]*Json) []*Json { var def []*Json switch len(args) { @@ -249,10 +249,10 @@ func (j *Json) JsonArray(args ...[]*Json) []*Json { case 1: def = args[0] default: - log.Panicf("JsonArray() received too many arguments %d", len(args)) + log.Panicf("JSONArray() received too many arguments %d", len(args)) } - a, err := j.CheckJsonArray() + a, err := j.CheckJSONArray() if err == nil { return a } @@ -260,8 +260,8 @@ func (j *Json) JsonArray(args ...[]*Json) []*Json { return def } -// JsonMap guarantees the return of a `map[string]interface{}` (with optional default) -func (j *Json) JsonMap(args ...map[string]*Json) map[string]*Json { +// JSONMap guarantees the return of a `map[string]interface{}` (with optional default) +func (j *Json) JSONMap(args ...map[string]*Json) map[string]*Json { var def map[string]*Json switch len(args) { @@ -269,10 +269,10 @@ func (j *Json) JsonMap(args ...map[string]*Json) map[string]*Json { case 1: def = args[0] default: - log.Panicf("JsonMap() received too many arguments %d", len(args)) + log.Panicf("JSONMap() received too many arguments %d", len(args)) } - a, err := j.CheckJsonMap() + a, err := j.CheckJSONMap() if err == nil { return a } diff --git a/simplejson_test.go b/simplejson_test.go index 6a1ae82..0d79d68 100644 --- a/simplejson_test.go +++ b/simplejson_test.go @@ -87,12 +87,12 @@ func TestSimplejson(t *testing.T) { gpa2, _ := js.Get("test", "arraywithsubs", 1, "subkeythree").CheckInt() assert.Equal(t, 3, gpa2) - jm, err := js.Get("test").CheckJsonMap() + jm, err := js.Get("test").CheckJSONMap() assert.Equal(t, err, nil) jmbool, _ := jm["bool"].CheckBool() assert.Equal(t, true, jmbool) - ja, err := js.Get("test", "string_array").CheckJsonArray() + ja, err := js.Get("test", "string_array").CheckJSONArray() assert.Equal(t, err, nil) jastr, _ := ja[0].CheckString() assert.Equal(t, "asdf", jastr) @@ -114,10 +114,10 @@ func TestSimplejson(t *testing.T) { js.Get("test", "sub_obj").Set("a", 3) assert.Equal(t, 3, js.Get("test", "sub_obj", "a").Int()) - jmm := js.Get("missing_map").JsonMap(map[string]*Json{"js1": js}) + jmm := js.Get("missing_map").JSONMap(map[string]*Json{"js1": js}) assert.Equal(t, js, jmm["js1"]) - jma := js.Get("missing_array").JsonArray([]*Json{js}) + jma := js.Get("missing_array").JSONArray([]*Json{js}) assert.Equal(t, js, jma[0]) } From bbcf2908db8aa460333274dd46260f8df82635e3 Mon Sep 17 00:00:00 2001 From: AzuraMeta Date: Fri, 18 Jul 2014 14:01:06 +0300 Subject: [PATCH 11/16] Removed CheckBytes() It's not a cast, but a straight-up conversion, with limited uses. Also, fixed JSON*() descriptions. --- simplejson.go | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/simplejson.go b/simplejson.go index e6b4cd1..14bd8c2 100644 --- a/simplejson.go +++ b/simplejson.go @@ -232,15 +232,7 @@ func (j *Json) CheckString() (string, error) { return "", errors.New("type assertion to string failed") } -// CheckBytes type asserts to `[]byte` -func (j *Json) CheckBytes() ([]byte, error) { - if s, ok := (j.data).(string); ok { - return []byte(s), nil - } - return nil, errors.New("type assertion to []byte failed") -} - -// JSONArray guarantees the return of a `[]interface{}` (with optional default) +// JSONArray guarantees the return of a `[]*Json` (with optional default) func (j *Json) JSONArray(args ...[]*Json) []*Json { var def []*Json @@ -260,7 +252,7 @@ func (j *Json) JSONArray(args ...[]*Json) []*Json { return def } -// JSONMap guarantees the return of a `map[string]interface{}` (with optional default) +// JSONMap guarantees the return of a `map[string]*Json` (with optional default) func (j *Json) JSONMap(args ...map[string]*Json) map[string]*Json { var def map[string]*Json From ea30edb7a7f208a65231de5cd714ab348a9460f2 Mon Sep 17 00:00:00 2001 From: AzuraMeta Date: Fri, 18 Jul 2014 17:56:05 +0300 Subject: [PATCH 12/16] Check*() methods now return a bool instead of an error --- simplejson.go | 101 ++++++++++++++++++++++----------------------- simplejson_go10.go | 42 +++++++++---------- simplejson_go11.go | 54 ++++++++++++------------ simplejson_test.go | 32 +++++++------- 4 files changed, 115 insertions(+), 114 deletions(-) diff --git a/simplejson.go b/simplejson.go index 14bd8c2..0bc2f53 100644 --- a/simplejson.go +++ b/simplejson.go @@ -2,7 +2,6 @@ package simplejson import ( "encoding/json" - "errors" "log" ) @@ -56,8 +55,8 @@ func (j *Json) MarshalJSON() ([]byte, error) { // Set modifies `Json` map by `key` and `value` // Useful for changing single key/value in a `Json` object easily. func (j *Json) Set(key string, val interface{}) { - m, err := j.CheckMap() - if err != nil { + m, ok := j.CheckMap() + if !ok { return } m[key] = val @@ -104,8 +103,8 @@ func (j *Json) SetPath(branch []string, val interface{}) { // Del modifies `Json` map by deleting `key` if it is present. func (j *Json) Del(key string) { - m, err := j.CheckMap() - if err != nil { + m, ok := j.CheckMap() + if !ok { return } delete(m, key) @@ -115,8 +114,8 @@ func (j *Json) Del(key string) { // for `key` in its `map` representation // and a bool identifying success or failure func (j *Json) getKey(key string) (*Json, bool) { - m, err := j.CheckMap() - if err == nil { + m, ok := j.CheckMap() + if ok { if val, ok := m[key]; ok { return &Json{val}, true } @@ -128,8 +127,8 @@ func (j *Json) getKey(key string) (*Json, bool) { // for `index` in its `array` representation // and a bool identifying success or failure func (j *Json) getIndex(index int) (*Json, bool) { - a, err := j.CheckArray() - if err == nil { + a, ok := j.CheckArray() + if ok { if len(a) > index { return &Json{a[index]}, true } @@ -175,61 +174,61 @@ func (j *Json) CheckGet(branch ...interface{}) (*Json, bool) { } // CheckJSONMap returns a copy of a Json map, but with values as Jsons -func (j *Json) CheckJSONMap() (map[string]*Json, error) { - m, err := j.CheckMap() - if err != nil { - return nil, err +func (j *Json) CheckJSONMap() (map[string]*Json, bool) { + m, ok := j.CheckMap() + if !ok { + return nil, false } jm := make(map[string]*Json) for key, val := range m { jm[key] = &Json{val} } - return jm, nil + return jm, true } // CheckJSONArray returns a copy of an array, but with each value as a Json -func (j *Json) CheckJSONArray() ([]*Json, error) { - a, err := j.CheckArray() - if err != nil { - return nil, err +func (j *Json) CheckJSONArray() ([]*Json, bool) { + a, ok := j.CheckArray() + if !ok { + return nil, false } ja := make([]*Json, len(a)) for key, val := range a { ja[key] = &Json{val} } - return ja, nil + return ja, true } // CheckMap type asserts to `map` -func (j *Json) CheckMap() (map[string]interface{}, error) { +func (j *Json) CheckMap() (map[string]interface{}, bool) { if m, ok := (j.data).(map[string]interface{}); ok { - return m, nil + return m, true } - return nil, errors.New("type assertion to map[string]interface{} failed") + return nil, false } // CheckArray type asserts to an `array` -func (j *Json) CheckArray() ([]interface{}, error) { +func (j *Json) CheckArray() ([]interface{}, bool) { if a, ok := (j.data).([]interface{}); ok { - return a, nil + return a, true } - return nil, errors.New("type assertion to []interface{} failed") + return nil, false } // CheckBool type asserts to `bool` -func (j *Json) CheckBool() (bool, error) { +func (j *Json) CheckBool() (bool, bool) { if s, ok := (j.data).(bool); ok { - return s, nil + return s, true } - return false, errors.New("type assertion to bool failed") + return false, false } // CheckString type asserts to `string` -func (j *Json) CheckString() (string, error) { +func (j *Json) CheckString() (string, bool) { if s, ok := (j.data).(string); ok { - return s, nil + return s, true } - return "", errors.New("type assertion to string failed") + return "", false } // JSONArray guarantees the return of a `[]*Json` (with optional default) @@ -244,8 +243,8 @@ func (j *Json) JSONArray(args ...[]*Json) []*Json { log.Panicf("JSONArray() received too many arguments %d", len(args)) } - a, err := j.CheckJSONArray() - if err == nil { + a, ok := j.CheckJSONArray() + if ok { return a } @@ -264,8 +263,8 @@ func (j *Json) JSONMap(args ...map[string]*Json) map[string]*Json { log.Panicf("JSONMap() received too many arguments %d", len(args)) } - a, err := j.CheckJSONMap() - if err == nil { + a, ok := j.CheckJSONMap() + if ok { return a } @@ -289,8 +288,8 @@ func (j *Json) Array(args ...[]interface{}) []interface{} { log.Panicf("Array() received too many arguments %d", len(args)) } - a, err := j.CheckArray() - if err == nil { + a, ok := j.CheckArray() + if ok { return a } @@ -314,8 +313,8 @@ func (j *Json) Map(args ...map[string]interface{}) map[string]interface{} { log.Panicf("Map() received too many arguments %d", len(args)) } - a, err := j.CheckMap() - if err == nil { + a, ok := j.CheckMap() + if ok { return a } @@ -337,8 +336,8 @@ func (j *Json) String(args ...string) string { log.Panicf("String() received too many arguments %d", len(args)) } - s, err := j.CheckString() - if err == nil { + s, ok := j.CheckString() + if ok { return s } @@ -360,8 +359,8 @@ func (j *Json) Int(args ...int) int { log.Panicf("Int() received too many arguments %d", len(args)) } - i, err := j.CheckInt() - if err == nil { + i, ok := j.CheckInt() + if ok { return i } @@ -383,8 +382,8 @@ func (j *Json) Float64(args ...float64) float64 { log.Panicf("Float64() received too many arguments %d", len(args)) } - f, err := j.CheckFloat64() - if err == nil { + f, ok := j.CheckFloat64() + if ok { return f } @@ -406,8 +405,8 @@ func (j *Json) Bool(args ...bool) bool { log.Panicf("Bool() received too many arguments %d", len(args)) } - b, err := j.CheckBool() - if err == nil { + b, ok := j.CheckBool() + if ok { return b } @@ -429,8 +428,8 @@ func (j *Json) Int64(args ...int64) int64 { log.Panicf("Int64() received too many arguments %d", len(args)) } - i, err := j.CheckInt64() - if err == nil { + i, ok := j.CheckInt64() + if ok { return i } @@ -452,8 +451,8 @@ func (j *Json) Uint64(args ...uint64) uint64 { log.Panicf("Uint64() received too many arguments %d", len(args)) } - i, err := j.CheckUint64() - if err == nil { + i, ok := j.CheckUint64() + if ok { return i } diff --git a/simplejson_go10.go b/simplejson_go10.go index 0a0faa0..d0b6847 100644 --- a/simplejson_go10.go +++ b/simplejson_go10.go @@ -23,53 +23,53 @@ func (j *Json) UnmarshalJSON(p []byte) error { } // CheckFloat64 coerces into a float64 -func (j *Json) CheckFloat64() (float64, error) { +func (j *Json) CheckFloat64() (float64, bool) { switch j.data.(type) { case float32, float64: - return reflect.ValueOf(j.data).Float(), nil + return reflect.ValueOf(j.data).Float(), true case int, int8, int16, int32, int64: - return float64(reflect.ValueOf(j.data).Int()), nil + return float64(reflect.ValueOf(j.data).Int()), true case uint, uint8, uint16, uint32, uint64: - return float64(reflect.ValueOf(j.data).Uint()), nil + return float64(reflect.ValueOf(j.data).Uint()), true } - return 0, errors.New("invalid value type") + return 0, false } -// Int coerces into an int -func (j *Json) CheckInt() (int, error) { +// CheckInt coerces into an int +func (j *Json) CheckInt() (int, bool) { switch j.data.(type) { case float32, float64: - return int(reflect.ValueOf(j.data).Float()), nil + return int(reflect.ValueOf(j.data).Float()), true case int, int8, int16, int32, int64: - return int(reflect.ValueOf(j.data).Int()), nil + return int(reflect.ValueOf(j.data).Int()), true case uint, uint8, uint16, uint32, uint64: - return int(reflect.ValueOf(j.data).Uint()), nil + return int(reflect.ValueOf(j.data).Uint()), true } - return 0, errors.New("invalid value type") + return 0, false } // CheckInt64 coerces into an int64 -func (j *Json) CheckInt64() (int64, error) { +func (j *Json) CheckInt64() (int64, bool) { switch j.data.(type) { case float32, float64: - return int64(reflect.ValueOf(j.data).Float()), nil + return int64(reflect.ValueOf(j.data).Float()), true case int, int8, int16, int32, int64: - return reflect.ValueOf(j.data).Int(), nil + return reflect.ValueOf(j.data).Int(), true case uint, uint8, uint16, uint32, uint64: - return int64(reflect.ValueOf(j.data).Uint()), nil + return int64(reflect.ValueOf(j.data).Uint()), true } - return 0, errors.New("invalid value type") + return 0, false } // CheckUint64 coerces into an uint64 -func (j *Json) CheckUint64() (uint64, error) { +func (j *Json) CheckUint64() (uint64, bool) { switch j.data.(type) { case float32, float64: - return uint64(reflect.ValueOf(j.data).Float()), nil + return uint64(reflect.ValueOf(j.data).Float()), true case int, int8, int16, int32, int64: - return uint64(reflect.ValueOf(j.data).Int()), nil + return uint64(reflect.ValueOf(j.data).Int()), true case uint, uint8, uint16, uint32, uint64: - return reflect.ValueOf(j.data).Uint(), nil + return reflect.ValueOf(j.data).Uint(), true } - return 0, errors.New("invalid value type") + return 0, false } diff --git a/simplejson_go11.go b/simplejson_go11.go index 8adaf97..98fa9a1 100644 --- a/simplejson_go11.go +++ b/simplejson_go11.go @@ -5,7 +5,6 @@ package simplejson import ( "bytes" "encoding/json" - "errors" "io" "reflect" "strconv" @@ -28,62 +27,65 @@ func NewFromReader(r io.Reader) (*Json, error) { } // CheckFloat64 coerces into a float64 -func (j *Json) CheckFloat64() (float64, error) { +func (j *Json) CheckFloat64() (float64, bool) { switch j.data.(type) { case json.Number: - return j.data.(json.Number).Float64() + nr, err := j.data.(json.Number).Float64() + return nr, err == nil case float32, float64: - return reflect.ValueOf(j.data).Float(), nil + return reflect.ValueOf(j.data).Float(), true case int, int8, int16, int32, int64: - return float64(reflect.ValueOf(j.data).Int()), nil + return float64(reflect.ValueOf(j.data).Int()), true case uint, uint8, uint16, uint32, uint64: - return float64(reflect.ValueOf(j.data).Uint()), nil + return float64(reflect.ValueOf(j.data).Uint()), true } - return 0, errors.New("invalid value type") + return 0, false } // CheckInt coerces into an int -func (j *Json) CheckInt() (int, error) { +func (j *Json) CheckInt() (int, bool) { switch j.data.(type) { case json.Number: - i, err := j.data.(json.Number).Int64() - return int(i), err + nr, err := j.data.(json.Number).Int64() + return int(nr), err == nil case float32, float64: - return int(reflect.ValueOf(j.data).Float()), nil + return int(reflect.ValueOf(j.data).Float()), true case int, int8, int16, int32, int64: - return int(reflect.ValueOf(j.data).Int()), nil + return int(reflect.ValueOf(j.data).Int()), true case uint, uint8, uint16, uint32, uint64: - return int(reflect.ValueOf(j.data).Uint()), nil + return int(reflect.ValueOf(j.data).Uint()), true } - return 0, errors.New("invalid value type") + return 0, false } // CheckInt64 coerces into an int64 -func (j *Json) CheckInt64() (int64, error) { +func (j *Json) CheckInt64() (int64, bool) { switch j.data.(type) { case json.Number: - return j.data.(json.Number).Int64() + nr, err := j.data.(json.Number).Int64() + return nr, err == nil case float32, float64: - return int64(reflect.ValueOf(j.data).Float()), nil + return int64(reflect.ValueOf(j.data).Float()), true case int, int8, int16, int32, int64: - return reflect.ValueOf(j.data).Int(), nil + return reflect.ValueOf(j.data).Int(), true case uint, uint8, uint16, uint32, uint64: - return int64(reflect.ValueOf(j.data).Uint()), nil + return int64(reflect.ValueOf(j.data).Uint()), true } - return 0, errors.New("invalid value type") + return 0, false } // CheckUint64 coerces into an uint64 -func (j *Json) CheckUint64() (uint64, error) { +func (j *Json) CheckUint64() (uint64, bool) { switch j.data.(type) { case json.Number: - return strconv.ParseUint(j.data.(json.Number).String(), 10, 64) + nr, err := strconv.ParseUint(j.data.(json.Number).String(), 10, 64) + return nr, err == nil case float32, float64: - return uint64(reflect.ValueOf(j.data).Float()), nil + return uint64(reflect.ValueOf(j.data).Float()), true case int, int8, int16, int32, int64: - return uint64(reflect.ValueOf(j.data).Int()), nil + return uint64(reflect.ValueOf(j.data).Int()), true case uint, uint8, uint16, uint32, uint64: - return reflect.ValueOf(j.data).Uint(), nil + return reflect.ValueOf(j.data).Uint(), true } - return 0, errors.New("invalid value type") + return 0, false } diff --git a/simplejson_test.go b/simplejson_test.go index 0d79d68..4245846 100644 --- a/simplejson_test.go +++ b/simplejson_test.go @@ -87,13 +87,13 @@ func TestSimplejson(t *testing.T) { gpa2, _ := js.Get("test", "arraywithsubs", 1, "subkeythree").CheckInt() assert.Equal(t, 3, gpa2) - jm, err := js.Get("test").CheckJSONMap() - assert.Equal(t, err, nil) + jm, ok := js.Get("test").CheckJSONMap() + assert.Equal(t, ok, true) jmbool, _ := jm["bool"].CheckBool() assert.Equal(t, true, jmbool) - ja, err := js.Get("test", "string_array").CheckJSONArray() - assert.Equal(t, err, nil) + ja, ok := js.Get("test", "string_array").CheckJSONArray() + assert.Equal(t, ok, true) jastr, _ := ja[0].CheckString() assert.Equal(t, "asdf", jastr) @@ -152,8 +152,8 @@ func TestSet(t *testing.T) { js.Set("baz", "bing") - s, err := js.Get("baz").CheckString() - assert.Equal(t, nil, err) + s, ok := js.Get("baz").CheckString() + assert.Equal(t, true, ok) assert.Equal(t, "bing", s) } @@ -164,8 +164,8 @@ func TestReplace(t *testing.T) { err = js.UnmarshalJSON([]byte(`{"baz":"bing"}`)) assert.Equal(t, nil, err) - s, err := js.Get("baz").CheckString() - assert.Equal(t, nil, err) + s, ok := js.Get("baz").CheckString() + assert.Equal(t, true, ok) assert.Equal(t, "bing", s) } @@ -175,8 +175,8 @@ func TestSetPath(t *testing.T) { js.SetPath([]string{"foo", "bar"}, "baz") - s, err := js.Get("foo", "bar").CheckString() - assert.Equal(t, nil, err) + s, ok := js.Get("foo", "bar").CheckString() + assert.Equal(t, true, ok) assert.Equal(t, "baz", s) } @@ -189,8 +189,8 @@ func TestSetPathNoPath(t *testing.T) { js.SetPath([]string{}, map[string]interface{}{"foo": "bar"}) - s, err := js.Get("foo").CheckString() - assert.Equal(t, nil, err) + s, ok := js.Get("foo").CheckString() + assert.Equal(t, true, ok) assert.Equal(t, "bar", s) f = js.Get("some_number").Float64(99.0) @@ -226,8 +226,8 @@ func TestPathWillAugmentExisting(t *testing.T) { } for _, tc := range cases { - s, err := js.Get(tc.path...).CheckString() - assert.Equal(t, nil, err) + s, ok := js.Get(tc.path...).CheckString() + assert.Equal(t, true, ok) assert.Equal(t, tc.outcome, s) } } @@ -239,7 +239,7 @@ func TestPathWillOverwriteExisting(t *testing.T) { js.SetPath([]string{"this", "a", "foo"}, "bar") - s, err := js.Get("this", "a", "foo").CheckString() - assert.Equal(t, nil, err) + s, ok := js.Get("this", "a", "foo").CheckString() + assert.Equal(t, true, ok) assert.Equal(t, "bar", s) } From 246029bb680bf241a3102d8d86ebdb9e9c4220d6 Mon Sep 17 00:00:00 2001 From: AzuraMeta Date: Fri, 18 Jul 2014 18:19:51 +0300 Subject: [PATCH 13/16] Fixed a missed go1.0 bug --- simplejson_go10.go | 1 - 1 file changed, 1 deletion(-) diff --git a/simplejson_go10.go b/simplejson_go10.go index d0b6847..f70417a 100644 --- a/simplejson_go10.go +++ b/simplejson_go10.go @@ -4,7 +4,6 @@ package simplejson import ( "encoding/json" - "errors" "io" "reflect" ) From 880249d63473aede617b5868cb15c4400d831583 Mon Sep 17 00:00:00 2001 From: AzuraMeta Date: Mon, 21 Jul 2014 13:26:10 +0300 Subject: [PATCH 14/16] Renamed Json type to JSON --- simplejson.go | 112 ++++++++++++++++++++++----------------------- simplejson_go10.go | 16 +++---- simplejson_go11.go | 16 +++---- simplejson_test.go | 8 ++-- 4 files changed, 76 insertions(+), 76 deletions(-) diff --git a/simplejson.go b/simplejson.go index 0bc2f53..386ce69 100644 --- a/simplejson.go +++ b/simplejson.go @@ -10,14 +10,14 @@ func Version() string { return "0.5.0-alpha" } -type Json struct { +type JSON struct { data interface{} } -// NewJson returns a pointer to a new `Json` object +// NewJson returns a pointer to a new `JSON` object // after unmarshaling `body` bytes -func NewJson(body []byte) (*Json, error) { - j := new(Json) +func NewJson(body []byte) (*JSON, error) { + j := new(JSON) err := j.UnmarshalJSON(body) if err != nil { return nil, err @@ -25,36 +25,36 @@ func NewJson(body []byte) (*Json, error) { return j, nil } -// New returns a pointer to a new, empty `Json` object -func New() *Json { - return &Json{ +// New returns a pointer to a new, empty `JSON` object +func New() *JSON { + return &JSON{ data: make(map[string]interface{}), } } // Interface returns the underlying data -func (j *Json) Interface() interface{} { +func (j *JSON) Interface() interface{} { return j.data } // Encode returns its marshaled data as `[]byte` -func (j *Json) Encode() ([]byte, error) { +func (j *JSON) Encode() ([]byte, error) { return j.MarshalJSON() } // EncodePretty returns its marshaled data as `[]byte` with indentation -func (j *Json) EncodePretty() ([]byte, error) { +func (j *JSON) EncodePretty() ([]byte, error) { return json.MarshalIndent(&j.data, "", " ") } // Implements the json.Marshaler interface. -func (j *Json) MarshalJSON() ([]byte, error) { +func (j *JSON) MarshalJSON() ([]byte, error) { return json.Marshal(&j.data) } -// Set modifies `Json` map by `key` and `value` -// Useful for changing single key/value in a `Json` object easily. -func (j *Json) Set(key string, val interface{}) { +// Set modifies `JSON` map by `key` and `value` +// Useful for changing single key/value in a `JSON` object easily. +func (j *JSON) Set(key string, val interface{}) { m, ok := j.CheckMap() if !ok { return @@ -62,9 +62,9 @@ func (j *Json) Set(key string, val interface{}) { m[key] = val } -// SetPath modifies `Json`, recursively checking/creating map keys for the supplied path, +// SetPath modifies `JSON`, recursively checking/creating map keys for the supplied path, // and then finally writing in the value -func (j *Json) SetPath(branch []string, val interface{}) { +func (j *JSON) SetPath(branch []string, val interface{}) { if len(branch) == 0 { j.data = val return @@ -101,8 +101,8 @@ func (j *Json) SetPath(branch []string, val interface{}) { curr[branch[len(branch)-1]] = val } -// Del modifies `Json` map by deleting `key` if it is present. -func (j *Json) Del(key string) { +// Del modifies `JSON` map by deleting `key` if it is present. +func (j *JSON) Del(key string) { m, ok := j.CheckMap() if !ok { return @@ -110,51 +110,51 @@ func (j *Json) Del(key string) { delete(m, key) } -// getKey returns a pointer to a new `Json` object +// getKey returns a pointer to a new `JSON` object // for `key` in its `map` representation // and a bool identifying success or failure -func (j *Json) getKey(key string) (*Json, bool) { +func (j *JSON) getKey(key string) (*JSON, bool) { m, ok := j.CheckMap() if ok { if val, ok := m[key]; ok { - return &Json{val}, true + return &JSON{val}, true } } return nil, false } -// getIndex returns a pointer to a new `Json` object +// getIndex returns a pointer to a new `JSON` object // for `index` in its `array` representation // and a bool identifying success or failure -func (j *Json) getIndex(index int) (*Json, bool) { +func (j *JSON) getIndex(index int) (*JSON, bool) { a, ok := j.CheckArray() if ok { if len(a) > index { - return &Json{a[index]}, true + return &JSON{a[index]}, true } } return nil, false } // Get searches for the item as specified by the branch -// within a nested Json and returns a new Json pointer -// the pointer is always a valid Json, allowing for chained operations +// within a nested JSON and returns a new JSON pointer +// the pointer is always a valid JSON, allowing for chained operations // // newJs := js.Get("top_level", "entries", 3, "dict") -func (j *Json) Get(branch ...interface{}) *Json { +func (j *JSON) Get(branch ...interface{}) *JSON { jin, ok := j.CheckGet(branch...) if ok { return jin } - return &Json{nil} + return &JSON{nil} } // CheckGet is like Get, except it also returns a bool // indicating whenever the branch was found or not -// the Json pointer mai be nil +// the JSON pointer mai be nil // // newJs, ok := js.Get("top_level", "entries", 3, "dict") -func (j *Json) CheckGet(branch ...interface{}) (*Json, bool) { +func (j *JSON) CheckGet(branch ...interface{}) (*JSON, bool) { jin := j var ok bool for _, p := range branch { @@ -173,34 +173,34 @@ func (j *Json) CheckGet(branch ...interface{}) (*Json, bool) { return jin, true } -// CheckJSONMap returns a copy of a Json map, but with values as Jsons -func (j *Json) CheckJSONMap() (map[string]*Json, bool) { +// CheckJSONMap returns a copy of a JSON map, but with values as Jsons +func (j *JSON) CheckJSONMap() (map[string]*JSON, bool) { m, ok := j.CheckMap() if !ok { return nil, false } - jm := make(map[string]*Json) + jm := make(map[string]*JSON) for key, val := range m { - jm[key] = &Json{val} + jm[key] = &JSON{val} } return jm, true } -// CheckJSONArray returns a copy of an array, but with each value as a Json -func (j *Json) CheckJSONArray() ([]*Json, bool) { +// CheckJSONArray returns a copy of an array, but with each value as a JSON +func (j *JSON) CheckJSONArray() ([]*JSON, bool) { a, ok := j.CheckArray() if !ok { return nil, false } - ja := make([]*Json, len(a)) + ja := make([]*JSON, len(a)) for key, val := range a { - ja[key] = &Json{val} + ja[key] = &JSON{val} } return ja, true } // CheckMap type asserts to `map` -func (j *Json) CheckMap() (map[string]interface{}, bool) { +func (j *JSON) CheckMap() (map[string]interface{}, bool) { if m, ok := (j.data).(map[string]interface{}); ok { return m, true } @@ -208,7 +208,7 @@ func (j *Json) CheckMap() (map[string]interface{}, bool) { } // CheckArray type asserts to an `array` -func (j *Json) CheckArray() ([]interface{}, bool) { +func (j *JSON) CheckArray() ([]interface{}, bool) { if a, ok := (j.data).([]interface{}); ok { return a, true } @@ -216,7 +216,7 @@ func (j *Json) CheckArray() ([]interface{}, bool) { } // CheckBool type asserts to `bool` -func (j *Json) CheckBool() (bool, bool) { +func (j *JSON) CheckBool() (bool, bool) { if s, ok := (j.data).(bool); ok { return s, true } @@ -224,16 +224,16 @@ func (j *Json) CheckBool() (bool, bool) { } // CheckString type asserts to `string` -func (j *Json) CheckString() (string, bool) { +func (j *JSON) CheckString() (string, bool) { if s, ok := (j.data).(string); ok { return s, true } return "", false } -// JSONArray guarantees the return of a `[]*Json` (with optional default) -func (j *Json) JSONArray(args ...[]*Json) []*Json { - var def []*Json +// JSONArray guarantees the return of a `[]*JSON` (with optional default) +func (j *JSON) JSONArray(args ...[]*JSON) []*JSON { + var def []*JSON switch len(args) { case 0: @@ -251,9 +251,9 @@ func (j *Json) JSONArray(args ...[]*Json) []*Json { return def } -// JSONMap guarantees the return of a `map[string]*Json` (with optional default) -func (j *Json) JSONMap(args ...map[string]*Json) map[string]*Json { - var def map[string]*Json +// JSONMap guarantees the return of a `map[string]*JSON` (with optional default) +func (j *JSON) JSONMap(args ...map[string]*JSON) map[string]*JSON { + var def map[string]*JSON switch len(args) { case 0: @@ -277,7 +277,7 @@ func (j *Json) JSONMap(args ...map[string]*Json) map[string]*Json { // for i, v := range js.Get("results").Array() { // fmt.Println(i, v) // } -func (j *Json) Array(args ...[]interface{}) []interface{} { +func (j *JSON) Array(args ...[]interface{}) []interface{} { var def []interface{} switch len(args) { @@ -302,7 +302,7 @@ func (j *Json) Array(args ...[]interface{}) []interface{} { // for k, v := range js.Get("dictionary").Map() { // fmt.Println(k, v) // } -func (j *Json) Map(args ...map[string]interface{}) map[string]interface{} { +func (j *JSON) Map(args ...map[string]interface{}) map[string]interface{} { var def map[string]interface{} switch len(args) { @@ -325,7 +325,7 @@ func (j *Json) Map(args ...map[string]interface{}) map[string]interface{} { // // useful when you explicitly want a `string` in a single value return context: // myFunc(js.Get("param1").String(), js.Get("optional_param").String("my_default")) -func (j *Json) String(args ...string) string { +func (j *JSON) String(args ...string) string { var def string switch len(args) { @@ -348,7 +348,7 @@ func (j *Json) String(args ...string) string { // // useful when you explicitly want an `int` in a single value return context: // myFunc(js.Get("param1").Int(), js.Get("optional_param").Int(5150)) -func (j *Json) Int(args ...int) int { +func (j *JSON) Int(args ...int) int { var def int switch len(args) { @@ -371,7 +371,7 @@ func (j *Json) Int(args ...int) int { // // useful when you explicitly want a `float64` in a single value return context: // myFunc(js.Get("param1").Float64(), js.Get("optional_param").Float64(5.150)) -func (j *Json) Float64(args ...float64) float64 { +func (j *JSON) Float64(args ...float64) float64 { var def float64 switch len(args) { @@ -394,7 +394,7 @@ func (j *Json) Float64(args ...float64) float64 { // // useful when you explicitly want a `bool` in a single value return context: // myFunc(js.Get("param1").Bool(), js.Get("optional_param").Bool(true)) -func (j *Json) Bool(args ...bool) bool { +func (j *JSON) Bool(args ...bool) bool { var def bool switch len(args) { @@ -417,7 +417,7 @@ func (j *Json) Bool(args ...bool) bool { // // useful when you explicitly want an `int64` in a single value return context: // myFunc(js.Get("param1").Int64(), js.Get("optional_param").Int64(5150)) -func (j *Json) Int64(args ...int64) int64 { +func (j *JSON) Int64(args ...int64) int64 { var def int64 switch len(args) { @@ -440,7 +440,7 @@ func (j *Json) Int64(args ...int64) int64 { // // useful when you explicitly want an `uint64` in a single value return context: // myFunc(js.Get("param1").Uint64(), js.Get("optional_param").Uint64(5150)) -func (j *Json) Uint64(args ...uint64) uint64 { +func (j *JSON) Uint64(args ...uint64) uint64 { var def uint64 switch len(args) { diff --git a/simplejson_go10.go b/simplejson_go10.go index f70417a..0a24bbd 100644 --- a/simplejson_go10.go +++ b/simplejson_go10.go @@ -8,21 +8,21 @@ import ( "reflect" ) -// NewFromReader returns a *Json by decoding from an io.Reader -func NewFromReader(r io.Reader) (*Json, error) { - j := new(Json) +// NewFromReader returns a *JSON by decoding from an io.Reader +func NewFromReader(r io.Reader) (*JSON, error) { + j := new(JSON) dec := json.NewDecoder(r) err := dec.Decode(&j.data) return j, err } // Implements the json.Unmarshaler interface. -func (j *Json) UnmarshalJSON(p []byte) error { +func (j *JSON) UnmarshalJSON(p []byte) error { return json.Unmarshal(p, &j.data) } // CheckFloat64 coerces into a float64 -func (j *Json) CheckFloat64() (float64, bool) { +func (j *JSON) CheckFloat64() (float64, bool) { switch j.data.(type) { case float32, float64: return reflect.ValueOf(j.data).Float(), true @@ -35,7 +35,7 @@ func (j *Json) CheckFloat64() (float64, bool) { } // CheckInt coerces into an int -func (j *Json) CheckInt() (int, bool) { +func (j *JSON) CheckInt() (int, bool) { switch j.data.(type) { case float32, float64: return int(reflect.ValueOf(j.data).Float()), true @@ -48,7 +48,7 @@ func (j *Json) CheckInt() (int, bool) { } // CheckInt64 coerces into an int64 -func (j *Json) CheckInt64() (int64, bool) { +func (j *JSON) CheckInt64() (int64, bool) { switch j.data.(type) { case float32, float64: return int64(reflect.ValueOf(j.data).Float()), true @@ -61,7 +61,7 @@ func (j *Json) CheckInt64() (int64, bool) { } // CheckUint64 coerces into an uint64 -func (j *Json) CheckUint64() (uint64, bool) { +func (j *JSON) CheckUint64() (uint64, bool) { switch j.data.(type) { case float32, float64: return uint64(reflect.ValueOf(j.data).Float()), true diff --git a/simplejson_go11.go b/simplejson_go11.go index 98fa9a1..6498790 100644 --- a/simplejson_go11.go +++ b/simplejson_go11.go @@ -11,15 +11,15 @@ import ( ) // Implements the json.Unmarshaler interface. -func (j *Json) UnmarshalJSON(p []byte) error { +func (j *JSON) UnmarshalJSON(p []byte) error { dec := json.NewDecoder(bytes.NewBuffer(p)) dec.UseNumber() return dec.Decode(&j.data) } -// NewFromReader returns a *Json by decoding from an io.Reader -func NewFromReader(r io.Reader) (*Json, error) { - j := new(Json) +// NewFromReader returns a *JSON by decoding from an io.Reader +func NewFromReader(r io.Reader) (*JSON, error) { + j := new(JSON) dec := json.NewDecoder(r) dec.UseNumber() err := dec.Decode(&j.data) @@ -27,7 +27,7 @@ func NewFromReader(r io.Reader) (*Json, error) { } // CheckFloat64 coerces into a float64 -func (j *Json) CheckFloat64() (float64, bool) { +func (j *JSON) CheckFloat64() (float64, bool) { switch j.data.(type) { case json.Number: nr, err := j.data.(json.Number).Float64() @@ -43,7 +43,7 @@ func (j *Json) CheckFloat64() (float64, bool) { } // CheckInt coerces into an int -func (j *Json) CheckInt() (int, bool) { +func (j *JSON) CheckInt() (int, bool) { switch j.data.(type) { case json.Number: nr, err := j.data.(json.Number).Int64() @@ -59,7 +59,7 @@ func (j *Json) CheckInt() (int, bool) { } // CheckInt64 coerces into an int64 -func (j *Json) CheckInt64() (int64, bool) { +func (j *JSON) CheckInt64() (int64, bool) { switch j.data.(type) { case json.Number: nr, err := j.data.(json.Number).Int64() @@ -75,7 +75,7 @@ func (j *Json) CheckInt64() (int64, bool) { } // CheckUint64 coerces into an uint64 -func (j *Json) CheckUint64() (uint64, bool) { +func (j *JSON) CheckUint64() (uint64, bool) { switch j.data.(type) { case json.Number: nr, err := strconv.ParseUint(j.data.(json.Number).String(), 10, 64) diff --git a/simplejson_test.go b/simplejson_test.go index 4245846..33d8d9b 100644 --- a/simplejson_test.go +++ b/simplejson_test.go @@ -114,21 +114,21 @@ func TestSimplejson(t *testing.T) { js.Get("test", "sub_obj").Set("a", 3) assert.Equal(t, 3, js.Get("test", "sub_obj", "a").Int()) - jmm := js.Get("missing_map").JSONMap(map[string]*Json{"js1": js}) + jmm := js.Get("missing_map").JSONMap(map[string]*JSON{"js1": js}) assert.Equal(t, js, jmm["js1"]) - jma := js.Get("missing_array").JSONArray([]*Json{js}) + jma := js.Get("missing_array").JSONArray([]*JSON{js}) assert.Equal(t, js, jma[0]) } func TestStdlibInterfaces(t *testing.T) { val := new(struct { Name string `json:"name"` - Params *Json `json:"params"` + Params *JSON `json:"params"` }) val2 := new(struct { Name string `json:"name"` - Params *Json `json:"params"` + Params *JSON `json:"params"` }) raw := `{"name":"myobject","params":{"string":"simplejson"}}` From 2a47cd5b92895c3284540e770dc636c8fa9c5529 Mon Sep 17 00:00:00 2001 From: AzuraMeta Date: Mon, 21 Jul 2014 13:44:39 +0300 Subject: [PATCH 15/16] Renamed NewJson to NewJSON() --- simplejson.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simplejson.go b/simplejson.go index 386ce69..584b656 100644 --- a/simplejson.go +++ b/simplejson.go @@ -16,7 +16,7 @@ type JSON struct { // NewJson returns a pointer to a new `JSON` object // after unmarshaling `body` bytes -func NewJson(body []byte) (*JSON, error) { +func NewJSON(body []byte) (*JSON, error) { j := new(JSON) err := j.UnmarshalJSON(body) if err != nil { From 6c0cf6566026a8089b0f8dc12820f7c29e17e46e Mon Sep 17 00:00:00 2001 From: AzuraMeta Date: Mon, 21 Jul 2014 13:48:05 +0300 Subject: [PATCH 16/16] Forgot to update the tests :P --- simplejson_go10_test.go | 2 +- simplejson_go11_test.go | 2 +- simplejson_test.go | 14 +++++++------- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/simplejson_go10_test.go b/simplejson_go10_test.go index e925f8a..1d7b1da 100644 --- a/simplejson_go10_test.go +++ b/simplejson_go10_test.go @@ -49,7 +49,7 @@ func TestNewFromReader(t *testing.T) { } func TestSimplejsonGo10(t *testing.T) { - js, err := NewJson([]byte(`{ + js, err := NewJSON([]byte(`{ "test": { "array": [1, "2", 3], "arraywithsubs": [ diff --git a/simplejson_go11_test.go b/simplejson_go11_test.go index 93e4de2..97af515 100644 --- a/simplejson_go11_test.go +++ b/simplejson_go11_test.go @@ -55,7 +55,7 @@ func TestNewFromReader(t *testing.T) { } func TestSimplejsonGo11(t *testing.T) { - js, err := NewJson([]byte(`{ + js, err := NewJSON([]byte(`{ "test": { "array": [1, "2", 3], "arraywithsubs": [ diff --git a/simplejson_test.go b/simplejson_test.go index 33d8d9b..fc16ae1 100644 --- a/simplejson_test.go +++ b/simplejson_test.go @@ -11,7 +11,7 @@ func TestSimplejson(t *testing.T) { var ok bool var err error - js, err := NewJson([]byte(`{ + js, err := NewJSON([]byte(`{ "test": { "string_array": ["asdf", "ghjk", "zxcv"], "string_array_null": ["abc", null, "efg"], @@ -147,7 +147,7 @@ func TestStdlibInterfaces(t *testing.T) { } func TestSet(t *testing.T) { - js, err := NewJson([]byte(`{}`)) + js, err := NewJSON([]byte(`{}`)) assert.Equal(t, nil, err) js.Set("baz", "bing") @@ -158,7 +158,7 @@ func TestSet(t *testing.T) { } func TestReplace(t *testing.T) { - js, err := NewJson([]byte(`{}`)) + js, err := NewJSON([]byte(`{}`)) assert.Equal(t, nil, err) err = js.UnmarshalJSON([]byte(`{"baz":"bing"}`)) @@ -170,7 +170,7 @@ func TestReplace(t *testing.T) { } func TestSetPath(t *testing.T) { - js, err := NewJson([]byte(`{}`)) + js, err := NewJSON([]byte(`{}`)) assert.Equal(t, nil, err) js.SetPath([]string{"foo", "bar"}, "baz") @@ -181,7 +181,7 @@ func TestSetPath(t *testing.T) { } func TestSetPathNoPath(t *testing.T) { - js, err := NewJson([]byte(`{"some":"data","some_number":1.0,"some_bool":false}`)) + js, err := NewJSON([]byte(`{"some":"data","some_number":1.0,"some_bool":false}`)) assert.Equal(t, nil, err) f := js.Get("some_number").Float64(99.0) @@ -198,7 +198,7 @@ func TestSetPathNoPath(t *testing.T) { } func TestPathWillAugmentExisting(t *testing.T) { - js, err := NewJson([]byte(`{"this":{"a":"aa","b":"bb","c":"cc"}}`)) + js, err := NewJSON([]byte(`{"this":{"a":"aa","b":"bb","c":"cc"}}`)) assert.Equal(t, nil, err) js.SetPath([]string{"this", "d"}, "dd") @@ -234,7 +234,7 @@ func TestPathWillAugmentExisting(t *testing.T) { func TestPathWillOverwriteExisting(t *testing.T) { // notice how "a" is 0.1 - but then we'll try to set at path a, foo - js, err := NewJson([]byte(`{"this":{"a":0.1,"b":"bb","c":"cc"}}`)) + js, err := NewJSON([]byte(`{"this":{"a":0.1,"b":"bb","c":"cc"}}`)) assert.Equal(t, nil, err) js.SetPath([]string{"this", "a", "foo"}, "bar")