|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +// FormatJSONMixed handles two top-level JSON structures: |
| 10 | +// 1. A single-key object containing an array: {"key": [elements]} |
| 11 | +// 2. A pure array of objects: [elements] |
| 12 | +// It formats the output with compact, single-line array elements. |
| 13 | +func FormatJSONMixed(input interface{}) (string, error) { |
| 14 | + var rawJSON []byte |
| 15 | + var err error |
| 16 | + |
| 17 | + // 1. Ensure we have raw JSON bytes to work with. |
| 18 | + switch v := input.(type) { |
| 19 | + case []byte: |
| 20 | + rawJSON = v |
| 21 | + case string: |
| 22 | + rawJSON = []byte(v) |
| 23 | + default: |
| 24 | + rawJSON, err = json.Marshal(v) |
| 25 | + if err != nil { |
| 26 | + return "", fmt.Errorf("failed to marshal input: %v", err) |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + // 2. Unmarshal into a generic interface{} to check its type. |
| 31 | + var genericData interface{} |
| 32 | + if err := json.Unmarshal(rawJSON, &genericData); err != nil { |
| 33 | + return "", fmt.Errorf("failed to unmarshal JSON: %v", err) |
| 34 | + } |
| 35 | + |
| 36 | + var sb strings.Builder |
| 37 | + |
| 38 | + // Determine the array source based on the top-level structure |
| 39 | + switch v := genericData.(type) { |
| 40 | + case map[string]interface{}: |
| 41 | + // --- Case 1: Root is a single-key object --- |
| 42 | + |
| 43 | + // Validation check ensures only one key exists |
| 44 | + if len(v) != 1 { |
| 45 | + return "", fmt.Errorf("input JSON object must have exactly one root key, found %d", len(v)) |
| 46 | + } |
| 47 | + |
| 48 | + sb.WriteString("{ ") |
| 49 | + |
| 50 | + // Use a composite loop/assignment to get the single key/value pair |
| 51 | + // because Go maps are unordered and cannot be accessed by index or |
| 52 | + // a known key without knowing the key name first |
| 53 | + var key string |
| 54 | + var value interface{} |
| 55 | + for k, val := range v { |
| 56 | + key = k |
| 57 | + value = val |
| 58 | + break // geet the 1st (and only) pair, see earlier strong validation check |
| 59 | + } |
| 60 | + |
| 61 | + // Validate the value is an array (slice) |
| 62 | + elementArray, ok := value.([]interface{}) |
| 63 | + if !ok { |
| 64 | + return "", fmt.Errorf("value for key %q is not a JSON array", key) |
| 65 | + } |
| 66 | + |
| 67 | + keyBytes, _ := json.Marshal(key) |
| 68 | + // Write the indented key and array start |
| 69 | + //sb.WriteString(" ") // 2-space indent |
| 70 | + sb.Write(keyBytes) |
| 71 | + sb.WriteString(": [\n") |
| 72 | + |
| 73 | + // Format the elements |
| 74 | + if err := formatArrayElements(&sb, elementArray, 4); err != nil { |
| 75 | + return "", err |
| 76 | + } |
| 77 | + |
| 78 | + // Write the closing array and object |
| 79 | + sb.WriteString(" ]\n") |
| 80 | + sb.WriteString("}") |
| 81 | + |
| 82 | + case []interface{}: |
| 83 | + // --- Case 2: Root is a pure array --- |
| 84 | + |
| 85 | + sb.WriteString("[\n") |
| 86 | + |
| 87 | + // Format the elements |
| 88 | + if err := formatArrayElements(&sb, v, 2); err != nil { |
| 89 | + return "", err |
| 90 | + } |
| 91 | + |
| 92 | + // Write the closing array bracket |
| 93 | + sb.WriteString("]") |
| 94 | + |
| 95 | + default: |
| 96 | + return "", fmt.Errorf("unsupported root JSON structure. Must be an array object or an array") |
| 97 | + } |
| 98 | + |
| 99 | + return sb.String(), nil |
| 100 | +} |
| 101 | + |
| 102 | +// formatArrayElements contains the core logic for marshalling and indenting elements. |
| 103 | +func formatArrayElements(sb *strings.Builder, elements []interface{}, indentSpaces int) error { |
| 104 | + indent := strings.Repeat(" ", indentSpaces) |
| 105 | + numElements := len(elements) |
| 106 | + |
| 107 | + for i, element := range elements { |
| 108 | + // Marshal the single element (compact, one-line string) |
| 109 | + elemBytes, err := json.Marshal(element) |
| 110 | + if err != nil { |
| 111 | + return fmt.Errorf("failed to marshal element %d: %v", i, err) |
| 112 | + } |
| 113 | + |
| 114 | + // Write the indented element |
| 115 | + sb.WriteString(indent) |
| 116 | + sb.Write(elemBytes) |
| 117 | + |
| 118 | + // Add comma and newline |
| 119 | + if i < numElements-1 { |
| 120 | + sb.WriteString(",") |
| 121 | + } |
| 122 | + sb.WriteString("\n") |
| 123 | + } |
| 124 | + return nil |
| 125 | +} |
0 commit comments