Tool to test invalid json->messagable

This commit is contained in:
Vaibhav Kamra 2023-12-20 21:52:12 -08:00
parent d6db70c239
commit 5f20dba3cd

View File

@ -0,0 +1,62 @@
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"github.com/alcionai/corso/src/pkg/services/m365/api"
)
func main() {
// Open the JSON file
jsonFile, err := os.Open("data.json")
if err != nil {
fmt.Println(err)
return
}
defer jsonFile.Close()
// Read the file contents
byteValue, _ := io.ReadAll(jsonFile)
// Declare an empty interface for holding the JSON data
var jsonData interface{}
if !json.Valid(byteValue) {
fmt.Println("INVALID JSON")
}
_, err = api.BytesToMessageable(byteValue)
if err != nil {
fmt.Println("Error converting to messagable", err)
}
// Unmarshal the byteValue into the jsonData interface
err = json.Unmarshal(byteValue, &jsonData)
if err != nil {
fmt.Println("Error parsing JSON:", err)
return
}
// Marshal the data back into JSON for pretty output
prettyJSON, err := json.MarshalIndent(jsonData, "", " ")
if err != nil {
fmt.Println("Error marshalling JSON:", err)
return
}
// Print the pretty JSON
fmt.Println(string(prettyJSON))
if !json.Valid(byteValue) {
fmt.Println("INVALID JSON")
}
_, err = api.BytesToMessageable(byteValue)
if err != nil {
fmt.Println("Error converting to messagable", err)
}
fmt.Println()
}