简体   繁体   中英

Unmarshal JSON to minimum type

I have a Json string that I want to unmarshal. This is working:

jsonString := []byte(`{"my_int": 3, "my_string": null}`)
var data map[string]interface{}
err := json.Unmarshal(jsonString, &data)
if err != nil {
    fmt.Println(err)
}
//avroJson := make(map[string]interface{})
for k, v := range data {
    fmt.Printf("%v, %T\n", k, v)
}

My issue is: the value of my_int which is 3 is returned as float64 .

My question is: how to parse a json string with the "minimum type" so that 3 will return int32 and not the maximum type 3 => float64 ?

Assumption: my Json is huge and only have primitive types and I want a minimum value that is really float64 to continue to show float64 .

Clarification: A "minimum type" means that if 3 can be considered both int32 and float64 the "minimum type" will be int32 , which is the exact type you'll get when running this: reflect.TypeOf(3).string()

Since you are unmarshaling into a map of interface{} , the following section of the golang json.Unmarshal documentation pertains:

To unmarshal JSON into an interface value, Unmarshal stores one of these in the interface value:
...
float64, for JSON numbers
string, for JSON strings
...

As such, to unmarshal your sample data into your desired types you should define a struct type which contains the desired field/type mappings, for example:

type MyType struct {
    MyInt    int     `json:"my_int"`
    MyString *string `json:"my_string"`
}

foo := MyType{}
jsonstr := `{"my_int": 3, "my_string": null}`

err := json.Unmarshal([]byte(jsonstr), &foo)
if err != nil {
    panic(err)
}
// foo => main.MyType{MyInt:3, MyString:(*string)(nil)}

Since you cannot describe your data in a struct then your options are to:

  1. Use a json.Decoder to convert the values to your desired types as they are parsed.

  2. Parse the document into a generic interface and post-process the value types.

Option #1 is the most flexible and can likely be implemented to be more performant than the other option since parsing and transformation could be performed in a single pass of the data.

Option #2 might be simpler but will require two passes over the data. Here is an example of what the post-processing step might look like:

func TransformValueTypes(o map[string]interface{}) {
  for k, v := range o {
    // Convert nil values to *string type.
    if v == interface{}(nil) {
      o[k] = (*string)(nil)
    }
    // Convert numbers to int32 if possible
    if x, isnumber := v.(float64); isnumber {
      if math.Floor(x) == x {
        if x >= math.MinInt32 && x <= math.MaxInt32 {
          o[k] = int32(x)
        }
        // Possibly check for other integer sizes here?
      }
      // Possibly check if float32 is possible here?
    }
    // Check for maps and slices here...
  }
}

So if you call TransformValueTypes(data) then your types will look like:

// my_int     -> 3     (int32)
// my_string  -> <nil> (*string)
// my_string2 -> "foo" (string)
// my_float   -> 1.23  (float64)

Of course, your transform function could also apply type transformation logic based on the key name.

Importantly, note that if your document might have additional structure not mentioned in your question (such as nested objects or arrays) then your transform function will need to account for them by more value type checking, recursive calls, and iteration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM