From b367c382df32de1d76dc748d8b1002dfce09ace1 Mon Sep 17 00:00:00 2001 From: Parshwa Shah Date: Thu, 6 Jun 2019 15:52:38 -0400 Subject: [PATCH 1/2] fix interface slice scenario When the JSON is of format [interface,interface,interface] gojson has trouble printing out the substructs. --- json-to-struct.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/json-to-struct.go b/json-to-struct.go index 811ee71..34e8c95 100644 --- a/json-to-struct.go +++ b/json-to-struct.go @@ -217,15 +217,9 @@ func Generate(input io.Reader, parser Parser, structName, pkgName string, tags [ case map[string]interface{}: result = iresult case []interface{}: - src := fmt.Sprintf("package %s\n\ntype %s %s\n", - pkgName, - structName, - typeForValue(iresult, structName, tags, subStructMap, convertFloats)) - formatted, err := format.Source([]byte(src)) - if err != nil { - err = fmt.Errorf("error formatting: %s, was formatting\n%s", err, src) - } - return formatted, err + result = map[string]interface{}{ + "temp" : iresult, + } default: return nil, fmt.Errorf("unexpected type: %T", iresult) } From c34b7a29bfe9f8c0883e28a5d15cfe3bedefd588 Mon Sep 17 00:00:00 2001 From: Parshwa Shah Date: Thu, 6 Jun 2019 16:00:01 -0400 Subject: [PATCH 2/2] Update json-to-struct.go --- json-to-struct.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/json-to-struct.go b/json-to-struct.go index 34e8c95..cef5d0d 100644 --- a/json-to-struct.go +++ b/json-to-struct.go @@ -217,9 +217,21 @@ func Generate(input io.Reader, parser Parser, structName, pkgName string, tags [ case map[string]interface{}: result = iresult case []interface{}: - result = map[string]interface{}{ - "temp" : iresult, - } + if len(iresult) == 1 { + src := fmt.Sprintf("package %s\n\ntype %s %s\n", + pkgName, + structName, + typeForValue(iresult, structName, tags, subStructMap, convertFloats)) + formatted, err := format.Source([]byte(src)) + if err != nil { + err = fmt.Errorf("error formatting: %s, was formatting\n%s", err, src) + } + return formatted, err + } else { + result = map[string]interface{}{ + "temp" : iresult, + } + } default: return nil, fmt.Errorf("unexpected type: %T", iresult) }