Skip to content

Commit

Permalink
Prevent escaping special characters in JSON output (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlange-42 authored Apr 18, 2024
1 parent b5da4f9 commit c734aac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Other

* Changed formatting functions to methods of type Solution (#13)
* Prevent escaping special characters in JSON output (#14)

## [[v0.2.0]](https://github.com/mlange-42/isso/compare/v0.1.0...v0.2.0)

Expand Down
28 changes: 16 additions & 12 deletions cmd/isso/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/mlange-42/isso"
"github.com/mlange-42/isso/fitness"
Expand Down Expand Up @@ -90,42 +91,45 @@ func run(file, format string, csvDelimiter string, pareto bool) (string, error)

fmt.Fprintf(os.Stderr, "Found %d solution(s)\n\n", len(solution))

output := ""
b := strings.Builder{}
switch format {
case "json":
jsData, err = json.MarshalIndent(&solution, "", " ")
enc := json.NewEncoder(&b)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
err = enc.Encode(&solution)
if err != nil {
return "", err
}
output = fmt.Sprintln(string(jsData))
b.WriteString(fmt.Sprintln(string(jsData)))

case "table":
for _, sol := range solution {
output += fmt.Sprintln(sol.ToTable())
output += fmt.Sprintf("(%d trips, %d samples)\n", sol.Fitness.Trips, sol.Fitness.Samples)
output += fmt.Sprintln("------------------------------------------------------------")
b.WriteString(fmt.Sprintln(sol.ToTable()))
b.WriteString(fmt.Sprintf("(%d trips, %d samples)\n", sol.Fitness.Trips, sol.Fitness.Samples))
b.WriteString(fmt.Sprintln("------------------------------------------------------------"))
}

case "csv":
for i, sol := range solution {
output += fmt.Sprint(sol.ToCSV(i, csvDelimiter))
b.WriteString(fmt.Sprint(sol.ToCSV(i, csvDelimiter)))
}

case "list":
for _, sol := range solution {
output += fmt.Sprintln(sol.ToList())
output += fmt.Sprintf("(%d trips, %d samples)\n", sol.Fitness.Trips, sol.Fitness.Samples)
output += fmt.Sprintln("------------------------------------------------------------")
b.WriteString(fmt.Sprintln(sol.ToList()))
b.WriteString(fmt.Sprintf("(%d trips, %d samples)\n", sol.Fitness.Trips, sol.Fitness.Samples))
b.WriteString(fmt.Sprintln("------------------------------------------------------------"))
}

case "fitness":
for _, sol := range solution {
output += fmt.Sprintf("(%d trips, %d samples)\n", sol.Fitness.Trips, sol.Fitness.Samples)
b.WriteString(fmt.Sprintf("(%d trips, %d samples)\n", sol.Fitness.Trips, sol.Fitness.Samples))
}

default:
return "", fmt.Errorf("unknown format '%s'", format)
}

return output, nil
return b.String(), nil
}

0 comments on commit c734aac

Please sign in to comment.