Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistent payload json #36

Open
petervolvowinz opened this issue May 10, 2024 · 0 comments
Open

Consistent payload json #36

petervolvowinz opened this issue May 10, 2024 · 0 comments

Comments

@petervolvowinz
Copy link
Collaborator

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
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant