github.com/viant/toolbox@v0.34.5/json_test.go (about)

     1  package toolbox_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/viant/toolbox"
    10  )
    11  
    12  func Test_IsCompleteJSON(t *testing.T) {
    13  	{
    14  		input := `{"a":1, "b":2}`
    15  		assert.True(t, toolbox.IsCompleteJSON(input))
    16  	}
    17  	{
    18  		input := `{"a":1, "b":2}
    19  	   {"a2":2, "b3":21}
    20  	   {"a3":3, "b4:22
    21  	   `
    22  		assert.False(t, toolbox.IsCompleteJSON(input))
    23  	}
    24  	{
    25  		input := `{"name":"abc"},{"id":"10}"`
    26  		assert.False(t, toolbox.IsCompleteJSON(input))
    27  	}
    28  	{
    29  		input := `"abc"`
    30  		assert.True(t, toolbox.IsCompleteJSON(input))
    31  	}
    32  
    33  }
    34  
    35  func Test_IsStructuredJSON(t *testing.T) {
    36  	{
    37  		input := `{"a":1, "b":2}`
    38  		assert.True(t, toolbox.IsStructuredJSON(input))
    39  	}
    40  	{
    41  		input := `{"a":1, "b":2}
    42  	   {"a2":2, "b3":21}
    43  	   {"a3":3, "b4:22
    44  	   `
    45  		assert.False(t, toolbox.IsStructuredJSON(input))
    46  	}
    47  	{
    48  		input := `{"name":"abc"},{"id":"10}"`
    49  		assert.False(t, toolbox.IsStructuredJSON(input))
    50  	}
    51  	{
    52  		input := `"abc""`
    53  		assert.False(t, toolbox.IsStructuredJSON(input))
    54  	}
    55  
    56  }
    57  
    58  func Test_IsNewDelimitedJSON(t *testing.T) {
    59  
    60  	{
    61  		input := `{"a":1, "b":2}`
    62  		assert.False(t, toolbox.IsNewLineDelimitedJSON(input))
    63  	}
    64  	{
    65  		input := `{"a":1, "b":2}
    66  {"a2":2, "b3":21}
    67  {"a3":3, "b4:22}
    68  `
    69  		assert.True(t, toolbox.IsNewLineDelimitedJSON(input))
    70  	}
    71  	{
    72  		input := `{"a":1, "b":2}
    73  {"a2":2, "b3":21
    74  {"a3":3, "b4:22}
    75  `
    76  		assert.False(t, toolbox.IsNewLineDelimitedJSON(input))
    77  	}
    78  
    79  	{
    80  		input := "{\"category\":\"Food\",\"country\":\"Poland\",\"expenditure\":\"6759.00\",\"id\":1,\"sub_category\":null,\"year\":2014}\n{\"category\":\"Housing\",\"country\":\"US\",\"expenditure\":\"17798.00\",\"id\":4,\"sub_category\":null,\"year\":2014}\n{\"category\":\"Food\",\"country\":\"Poland\",\"expenditure\":\"7023.00\",\"id\":2,\"sub_category\":null,\"year\":2015}\n{\"category\":\"Housing\",\"country\":\"US\",\"expenditure\":\"18409.00\",\"id\":5,\"sub_category\":null,\"year\":2015}\n{\"category\":\"Food\",\"country\":\"Poland\",\"expenditure\":\"7023.00\",\"id\":3,\"sub_category\":null,\"year\":2016}\n{\"category\":\"Housing\",\"country\":\"US\",\"expenditure\":\"18886.00\",\"id\":6,\"sub_category\":null,\"year\":2016}\n"
    81  		assert.True(t, toolbox.IsNewLineDelimitedJSON(input))
    82  	}
    83  
    84  }
    85  
    86  func Test_JSONToMap(t *testing.T) {
    87  	{
    88  		input := `{"a":1, "b":2}`
    89  		aMAp, err := toolbox.JSONToMap(input)
    90  		assert.Nil(t, err)
    91  		assert.True(t, len(aMAp) > 0)
    92  	}
    93  	{
    94  		input := `{"a":1, "b":2}`
    95  		aMAp, err := toolbox.JSONToMap([]byte(input))
    96  		assert.Nil(t, err)
    97  		assert.True(t, len(aMAp) > 0)
    98  	}
    99  	{
   100  		input := `{"a":1, "b":2}`
   101  		aMAp, err := toolbox.JSONToMap(strings.NewReader(input))
   102  		assert.Nil(t, err)
   103  		assert.True(t, len(aMAp) > 0)
   104  	}
   105  	{
   106  		//error case
   107  		_, err := toolbox.JSONToMap(1)
   108  		assert.NotNil(t, err)
   109  	}
   110  	{
   111  		//error case
   112  		input := `{"a":1, "b":2`
   113  		_, err := toolbox.JSONToMap(input)
   114  		assert.NotNil(t, err)
   115  	}
   116  
   117  }
   118  
   119  func Test_AsJSONText(t *testing.T) {
   120  	{
   121  		var soure = map[string]interface{}{
   122  			"k": 1,
   123  		}
   124  		text, err := toolbox.AsJSONText(soure)
   125  		assert.Nil(t, err)
   126  		assert.EqualValues(t, "{\"k\":1}\n", text)
   127  	}
   128  	{
   129  		type source struct {
   130  			K int
   131  		}
   132  		text, err := toolbox.AsJSONText(&source{K: 1})
   133  		assert.Nil(t, err)
   134  		assert.EqualValues(t, "{\"K\":1}\n", text)
   135  	}
   136  
   137  	{
   138  
   139  		text, err := toolbox.AsJSONText([]int{1, 3})
   140  		assert.Nil(t, err)
   141  		assert.EqualValues(t, "[1,3]\n", text)
   142  	}
   143  
   144  	{
   145  
   146  		_, err := toolbox.AsJSONText(1)
   147  		assert.NotNil(t, err)
   148  	}
   149  
   150  }
   151  
   152  func Test_JSONToInterface(t *testing.T) {
   153  	{
   154  		input := `{"a":1, "b":2}`
   155  		output, err := toolbox.JSONToInterface(input)
   156  		if assert.Nil(t, err) {
   157  			assert.NotNil(t, output)
   158  			assert.True(t, toolbox.IsMap(output))
   159  			aMap := toolbox.AsMap(output)
   160  			assert.EqualValues(t, 1, aMap["a"])
   161  			assert.EqualValues(t, 2, aMap["b"])
   162  		}
   163  	}
   164  	{
   165  		input := `[1,2]`
   166  		output, err := toolbox.JSONToInterface(input)
   167  		if assert.Nil(t, err) {
   168  			assert.NotNil(t, output)
   169  			assert.True(t, toolbox.IsSlice(output))
   170  			aSlice := toolbox.AsSlice(output)
   171  			assert.EqualValues(t, []interface{}{1.0, 2.0}, aSlice)
   172  		}
   173  	}
   174  }
   175  
   176  func TestAnyJSONType_Value(t *testing.T) {
   177  
   178  	var useCases = []struct {
   179  		description string
   180  		source      string
   181  		target      map[string]toolbox.AnyJSONType
   182  		key         string
   183  		expect      interface{}
   184  	}{
   185  
   186  		{
   187  			description: "string any type",
   188  			source:      `{"k":"abc"}`,
   189  			key:         "k",
   190  			expect:      "abc",
   191  		},
   192  
   193  		{
   194  			description: "numeric any type",
   195  			source:      `{"k":123}`,
   196  			key:         "k",
   197  			expect:      123,
   198  		},
   199  		{
   200  			description: "slice any type",
   201  			source:      `{"k":[1,2,3]}`,
   202  			key:         "k",
   203  			expect:      []interface{}{1.0, 2.0, 3.0},
   204  		},
   205  		{
   206  			description: "slice any type",
   207  			source:      `{"k":{"z":[1,2]}}`,
   208  			key:         "k",
   209  			expect: map[string]interface{}{
   210  				"z": []interface{}{float64(1), float64(2)},
   211  			},
   212  		},
   213  	}
   214  
   215  	for _, useCase := range useCases {
   216  		err := json.Unmarshal([]byte(useCase.source), &useCase.target)
   217  		if !assert.Nil(t, err, useCase.description) {
   218  			continue
   219  		}
   220  
   221  		actual, ok := useCase.target[useCase.key]
   222  		if !assert.True(t, ok, useCase.description) {
   223  			continue
   224  		}
   225  		actualValue, err := actual.Value()
   226  		if !assert.Nil(t, err, useCase.description) {
   227  			continue
   228  		}
   229  		assert.EqualValues(t, useCase.expect, actualValue, useCase.description)
   230  
   231  		_, err = json.Marshal(useCase.target)
   232  		assert.Nil(t, err)
   233  
   234  	}
   235  
   236  }