COVESA / vissr

Mozilla Public License 2.0
6 stars 5 forks source link

Consistent payload json #36

Open petervolvowinz opened 6 months ago

petervolvowinz commented 6 months ago

To facilitate client implementation and marshaling of the json we should consider to use the same data structure. e.g array even when only returning a single datapoint. That is we can rely on the json marshaling to do the translation for us and no need to check the type.

Ex in Golang:

var msg Message
err := json.Unmarshal([]byte(vssjson), &msg)
if err != nil {
    fmt.Println("Error:", err)
}

Now we need to do:

var msg Message
err := json.Unmarshal([]byte(vssjson), &msg)
if err != nil {
    fmt.Println("Error:", err)
}
switch dp := msg.Data.Dp.(type) {
case []interface{}:
    fmt.Println("Data Points:")
    val_string := ""
    for _, item := range dp {
       dataPoint := item.(map[string]interface{})
       value := dataPoint["value"].(string)
       ts := dataPoint["ts"].(string)
       fmt.Printf("  Value: %s, Timestamp: %s\n", value, ts)
       val_string = val_string + value + " "
    }
    return val_string
default:
    if dp != nil {
       fmt.Println("Single Data Point:")
       dataPoint := dp.(map[string]interface{})
       value := dataPoint["value"].(string)
       ts := dataPoint["ts"].(string)
       fmt.Printf("  Value: %s, Timestamp: %s\n", value, ts)
       return value
    }
}