j boschiero
j boschiero

Reputation: 520

How do I turn an array of JSON objects into an array of structs with default values in Go?

I'm working on a Go API that can receive POSTs consisting of a JSON array of objects. The structure of the POST will look something like:

[
  {
    "name":"Las Vegas",
    "size":14
  },
  {
    "valid": false,
    "name":"Buffalo",
    "size":63
  }
]  

Let's say I have the following struct:

type Data {
    Valid    bool
    Name     string
    Size     float64
}

I want to create a bunch of Datas with Valid set to true anytime it's not actually specified in the JSON as false. If I were doing a single one I could use How to specify default values when parsing JSON in Go, but for doing an unknown number of them the only thing I've been able to come up with is something like:

var allMap []map[string]interface{}
var structs []Data
for _, item := range allMap {
  var data Data
  var v interface{}
  var ok bool
  if v, ok := item["value"]; ok {
    data.Valid = v
  } else {
    data.Valid = true
  }
  id v, ok := item["name"]; ok {
    data.Name = v
  }
  ...
  structs = append(structs, data)
}
return structs

Right now the struct I'm actually working with has 14 fields, some of them have values I want to assign defaults, others are fine to leave blank, but all of them have to be iterated through using this approach.

Is there a better way?

Upvotes: 3

Views: 415

Answers (2)

icza
icza

Reputation: 418137

You can use the json.RawMessage type to defer unmarshaling some JSON text value. If you use this type, then the JSON text will be stored in this without unmarshaling (so you can unmarshal this fragment later on as you wish).

So in your case if you try to unmarshal into a slice of such RawMessage, you can use the technique what you linked in your question, that is you can iterate over the slice of raw values (which are the JSON text for each Data), create a Data struct with values you want as defaults for missing values, and unmarshal a slice element into this prepared struct. That's all.

It looks like this:

allJson := []json.RawMessage{}
if err := json.Unmarshal(src, &allJson); err != nil {
    panic(err)
}

allData := make([]Data, len(allJson))
for i, v := range allJson {
    // Here create your Data with default values
    allData[i] = Data{Valid: true}
    if err := json.Unmarshal(v, &allData[i]); err != nil {
        panic(err)
    }
}

Try it on the Go Playground.

Notes / Variants

For efficiency (to avoid copying structs), you can also make the allData to be a slice of pointers in the above example, which would look like this:

allData := make([]*Data, len(allJson))
for i, v := range allJson {
    // Here create your Data with default values
    allData[i] = &Data{Valid: true}
    if err := json.Unmarshal(v, allData[i]); err != nil {
        panic(err)
    }
}

If you want to keep using non-pointers, for efficiency you can "prepare" your wished default values in the slice elements itself, which would look like this:

allData := make([]Data, len(allJson))
for i, v := range allJson {
    // Here set your default values in the slice elements
    // Only set those which defer from the zero values:
    allData[i].Valid = true
    if err := json.Unmarshal(v, &allData[i]); err != nil {
        panic(err)
    }
}

Upvotes: 3

hobbs
hobbs

Reputation: 240314

You can do a good trick by providing an UnmarshalJSON method on your type to make it transparent and work automatically even if your type is found within structs or slices.

func (d *Data) UnmarshalJSON(j []byte) error {
    type _Data Data // Dummy type to avoid infinite recursion in UnmarshalJSON
    tmp := _Data{ // Set defaults here
        Valid: true,
    }

    err := json.Unmarshal(j, &tmp)
    if err != nil {
        return err
    }

    *d = Data(tmp)
    return nil
}

The type _Data exists simply so that we can call json.Unmarshal(j, &tmp) and get the original un-overridden behavior, instead of calling the UnmarshalJSON method that we're already in the middle of. We can set default values on tmp using the trick that you already linked to. And then after the unmarshalling is done, we can just cast tmp to Data because after all Data and _Data are really the same type.

Given this method you can simply

var structs []Data
err := json.Unmarshal(input, &structs)

(or likewise with a json.Decoder) and have it work just the way you want.

Upvotes: 1

Related Questions