Skip to content

Commit 594a619

Browse files
authored
Merge pull request #30 from thedevsaddam/feature/out
Add Out method issue #29
2 parents f93554d + 63a178f commit 594a619

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

jsonq.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,18 @@ func (j *JSONQ) Count() int {
578578
return lnth
579579
}
580580

581+
// Out write the queried data to defined custom type
582+
func (j *JSONQ) Out(v interface{}) {
583+
data, err := json.Marshal(j.jsonContent)
584+
if err != nil {
585+
j.addError(err)
586+
return
587+
}
588+
if err := json.Unmarshal(data, &v); err != nil {
589+
j.addError(err)
590+
}
591+
}
592+
581593
// getFloatValFromArray returns a list of float64 values from array/map for aggregation
582594
func (j *JSONQ) getFloatValFromArray(arr []interface{}, property ...string) []float64 {
583595
ff := []float64{}

jsonq_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"math"
78
"reflect"
89
"strings"
910
"testing"
@@ -883,6 +884,54 @@ func TestJSONQ_Count_expecting_int_from_objects(t *testing.T) {
883884
assertJSON(t, out, expected, "Count expecting a int number of total item of an array of grouped objects")
884885
}
885886

887+
func TestJSONQ_Out_expecting_result(t *testing.T) {
888+
type item struct {
889+
ID int `json:"id"`
890+
Name string `json:"name"`
891+
Price int `json:"price"`
892+
}
893+
exptItm := item{
894+
ID: 1,
895+
Name: "MacBook Pro 13 inch retina",
896+
Price: 1350,
897+
}
898+
itm := item{}
899+
jq := New().JSONString(jsonStr).
900+
From("vendor.items.[0]")
901+
jq.Out(&itm)
902+
assertInterface(t, exptItm, itm, "failed to get Out result")
903+
}
904+
905+
func TestJSONQ_Out_expecting_decoding_error(t *testing.T) {
906+
type item struct {
907+
ID bool `json:"id"`
908+
Name string `json:"name"`
909+
Price int `json:"price"`
910+
}
911+
itm := item{}
912+
jq := New().JSONString(jsonStr).
913+
From("vendor.items.[0]")
914+
jq.Out(&itm)
915+
if jq.Error() == nil {
916+
t.Errorf("failed to get Out decoding error: %v", jq.Error())
917+
}
918+
}
919+
920+
func TestJSONQ_Out_expecting_encoding_error(t *testing.T) {
921+
type item struct {
922+
ID bool `json:"id"`
923+
Name string `json:"name"`
924+
Price int `json:"price"`
925+
}
926+
itm := item{}
927+
jq := New()
928+
jq.jsonContent = math.Inf(1)
929+
jq.Out(&itm)
930+
if jq.Error() == nil {
931+
t.Errorf("failed to get Out encoding error: %v", jq.Error())
932+
}
933+
}
934+
886935
func TestJSONQ_Sum_of_array_numeric_values(t *testing.T) {
887936
jq := New().JSONString(jsonStr).
888937
From("vendor.prices")

jsonq_testdata_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//
2-
31
package gojsonq
42

53
import (

0 commit comments

Comments
 (0)